Skip to content

Commit 2b7a7ce

Browse files
committed
add method to sort use statements
Fixes #5
1 parent 36e3327 commit 2b7a7ce

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

README.markdown

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ Then, hitting `\e` in normal or insert mode will expand the class name to a full
4545
$this->getMock('RouterInterface<-- cursor here or on the name; hit \e now to expand the class name'
4646
```
4747
48+
### Sort existing use statements alphabetically
49+
50+
If you do not know how to organize use statements, (or anything else, for that
51+
matter), the alphabetical order might be a sensible choice, because it makes
52+
finding what you are looking for easier, and reduces the risk for conflicts :
53+
if everyone adds new things at the same line, conflicts are guaranteed.
54+
55+
This vim plugin defines a `PhpSortUse()` you may use in your mappings:
56+
57+
autocmd FileType php inoremap <Leader>s <Esc>:call PhpSortUse()<CR>
58+
autocmd FileType php noremap <Leader>s :call PhpSortUse()<CR>
59+
4860
## Installation:
4961
5062
### Using [pathogen](https://github.com/tpope/vim-pathogen)

plugin/phpns.vim

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,13 @@ function! s:saveCapture(capture)
156156
let s:capture = a:capture
157157
endfunction
158158

159+
function! PhpSortUse()
160+
let restorepos = line(".") . "normal!" . virtcol(".") . "|"
161+
" insert after last use or namespace or <?php
162+
if search('^use\_s\_[[:alnum:][:blank:]\\_]*;', 'be') > 0
163+
execute "'{,'}-1sort"
164+
else
165+
throw "No use statements found."
166+
endif
167+
exe restorepos
168+
endfunction

tests/test-sort.in

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
Sort test 1
3+
4+
STARTTEST
5+
:%d
6+
a<?php
7+
8+
use Goo\Baz;
9+
use Foo\Bar;
10+
11+
class Boo
12+
{
13+
}:call PhpSortUse()
14+
ax:w! test.out
15+
:qa!
16+
ENDTEST
17+

tests/test-sort.ok

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
use Foo\Bar;
4+
use Goo\Baz;
5+
6+
class Boo
7+
{
8+
}x

0 commit comments

Comments
 (0)