Skip to content

Commit a6c4681

Browse files
author
Leonard Ehrenfried
committed
Improve implementation of sortInsideGroups
1 parent 427df92 commit a6c4681

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

plugin/scala.vim

+19-12
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,28 @@ endfunction
9090
function! s:sortInsideGroups()
9191
call cursor(1, 1)
9292

93-
while(1)
93+
let start = 1
94+
let end = 1
95+
96+
" repeat until we find no more matches
97+
while(start > 0 && end > 0)
9498
let pos = line(".")
95-
let start = search('^import') "find first line with import
96-
let end = search('^\import.*\n\(import\)\@!') "find first non-import line
97-
98-
" if the next match is above the current cursor position the search has
99-
" wrapped and we're done
100-
" also covered is the case when the import is the last line in the file
101-
" with no blank line after it
102-
if start < pos || end < start
103-
break
104-
end
99+
" find first line with import
100+
let start = search('^import', 'cW')
101+
" find next line which starts with an import, ends with a newline
102+
" and the next line is not an import
103+
" the 'c' flag accepts matches at the current position allowing single line groups
104+
let end = search('^\import.*\n\(import\)\@!', 'cW')
105105

106106
execute start','end'sort i'
107-
call cursor(end,0)
107+
108+
call cursor(end + 1, 0)
109+
110+
" stop if end is the last line in the file
111+
if line("$") == end
112+
break
113+
endif
114+
108115
endwhile
109116
endfunction
110117

0 commit comments

Comments
 (0)