Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removes duplicate imports, and puts field-imports at the bottom. #153

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion plugin/scala.vim
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ endfunction
" 1. Java/Scala imports like java.util.UUID
" 2. Third party libraries
" 3. First party libraries (ie. your own stuff)
" 4. Field imports (MyObject._)
"
function! s:sortAcrossGroups()
let curr = 1
Expand All @@ -43,6 +44,7 @@ function! s:sortAcrossGroups()
let java_scala_imports = []
let first_party_imports = []
let third_party_imports = []
let field_imports = []

" loop over lines in buffer
while curr <= line('$')
Expand All @@ -56,6 +58,8 @@ function! s:sortAcrossGroups()

if line =~ '^import \(java\(x\)\?\|scala\)\.'
call add(java_scala_imports, line)
elseif line =~ '\v\C^import [A-Z]'
call add(field_imports, line)
elseif exists('g:scala_first_party_namespaces')
let regex = '^import '.g:scala_first_party_namespaces
if line =~ regex
Expand Down Expand Up @@ -86,6 +90,7 @@ function! s:sortAcrossGroups()
execute 'd'to_delete
endif

call s:sortAndPrint(field_imports)
call s:sortAndPrint(first_party_imports)
call s:sortAndPrint(third_party_imports)
call s:sortAndPrint(java_scala_imports)
Expand Down Expand Up @@ -132,7 +137,7 @@ endfunction

function! s:sortAndPrint(imports)
if len(a:imports) > 0
call sort(a:imports, "s:sortIgnoreCase")
call uniq(sort(a:imports, "s:sortIgnoreCase"))
call append(line("."), "")
call append(line("."), a:imports)
endif
Expand Down