Skip to content

Commit

Permalink
add method to sort use statements
Browse files Browse the repository at this point in the history
Fixes #5
  • Loading branch information
greg0ire committed Feb 22, 2016
1 parent 36e3327 commit 2b7a7ce
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Leader>s <Esc>:call PhpSortUse()<CR>
autocmd FileType php noremap <Leader>s :call PhpSortUse()<CR>
## Installation:
### Using [pathogen](https://github.com/tpope/vim-pathogen)
Expand Down
10 changes: 10 additions & 0 deletions plugin/phpns.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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 <?php
if search('^use\_s\_[[:alnum:][:blank:]\\_]*;', 'be') > 0
execute "'{,'}-1sort"
else
throw "No use statements found."
endif
exe restorepos
endfunction
17 changes: 17 additions & 0 deletions tests/test-sort.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

Sort test 1

STARTTEST
:%d
a<?php

use Goo\Baz;
use Foo\Bar;

class Boo
{
}:call PhpSortUse()
ax:w! test.out
:qa!
ENDTEST

8 changes: 8 additions & 0 deletions tests/test-sort.ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use Foo\Bar;
use Goo\Baz;

class Boo
{
}x

0 comments on commit 2b7a7ce

Please sign in to comment.