diff --git a/README.markdown b/README.markdown index 40863b6..29ecc38 100644 --- a/README.markdown +++ b/README.markdown @@ -45,6 +45,18 @@ Then, hitting `\e` in normal or insert mode will expand the class name to a full $this->getMock('RouterInterface<-- cursor here or on the name; hit \e now to expand the class name' ``` +### Sort existing use statements alphabetically + +If you do not know how to organize use statements, (or anything else, for that +matter), the alphabetical order might be a sensible choice, because it makes +finding what you are looking for easier, and reduces the risk for conflicts : +if everyone adds new things at the same line, conflicts are guaranteed. + +This vim plugin defines a `PhpSortUse()` you may use in your mappings: + + autocmd FileType php inoremap s :call PhpSortUse() + autocmd FileType php noremap s :call PhpSortUse() + ## Installation: ### Using [pathogen](https://github.com/tpope/vim-pathogen) diff --git a/plugin/phpns.vim b/plugin/phpns.vim index de0ecd3..b2cb4ac 100644 --- a/plugin/phpns.vim +++ b/plugin/phpns.vim @@ -156,3 +156,13 @@ function! s:saveCapture(capture) let s:capture = a:capture endfunction +function! PhpSortUse() + let restorepos = line(".") . "normal!" . virtcol(".") . "|" + " insert after last use or namespace or 0 + execute "'{,'}-1sort" + else + throw "No use statements found." + endif + exe restorepos +endfunction diff --git a/tests/test-sort.in b/tests/test-sort.in new file mode 100644 index 0000000..4355c62 --- /dev/null +++ b/tests/test-sort.in @@ -0,0 +1,17 @@ + +Sort test 1 + +STARTTEST +:%d +a