Skip to content

Instantly share code, notes, and snippets.

@kamichidu
Created August 5, 2014 11:59
Show Gist options
  • Select an option

  • Save kamichidu/28810e4fb6f4263c83e5 to your computer and use it in GitHub Desktop.

Select an option

Save kamichidu/28810e4fb6f4263c83e5 to your computer and use it in GitHub Desktop.
" The MIT License (MIT)
"
" Copyright (c) 2014 kamichidu
"
" Permission is hereby granted, free of charge, to any person obtaining a copy
" of this software and associated documentation files (the "Software"), to deal
" in the Software without restriction, including without limitation the rights
" to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
" copies of the Software, and to permit persons to whom the Software is
" furnished to do so, subject to the following conditions:
"
" The above copyright notice and this permission notice shall be included in
" all copies or substantial portions of the Software.
"
" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
" IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
" FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
" AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
" LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
" OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
" THE SOFTWARE.
let s:save_cpo= &cpo
set cpo&vim
let s:L= javaimport#Data_List()
let s:prototype= {}
function! s:prototype.sort()
let l:save_pos= getpos('.')
try
" gather already existed import statements
let l:region= javaimport#region_of_import_statements()
if l:region ==# [0, 0]
return
endif
let l:classes= javaimport#imported_classes()
call sort(l:classes)
" separate each statements on defferent top-level domain
let l:statements= []
let l:last_domain= matchstr(l:classes[0], '^\w\+')
for l:class in l:classes
let l:domain= matchstr(l:class, '^\w\+')
if l:domain !=# l:last_domain
call add(l:statements, '')
endif
call add(l:statements, printf('import %s;', l:class))
let l:last_domain= l:domain
endfor
execute l:region[0] . ',' . l:region[1] . 'delete _'
call append(l:region[0] - 1, l:statements)
finally
call setpos('.', l:save_pos)
endtry
endfunction
function! s:prototype.add(classes)
let classes= (type(a:classes) == type([])) ? a:classes : [a:classes]
let save_pos= getpos('.')
try
let [slnum, elnum]= self.region()
let imports= s:L.uniq(self.imported_classes() + classes)
call append(slnum, map(imports, 'printf("import %s;", v:val)'))
call self.sort()
finally
let [bufnum, lnum, col, off]= save_pos
call setpos('.', [bufnum, lnum + len(delta), col, off])
endtry
endfunction
function! s:prototype.remove(classes)
let classes= (type(a:classes) == type([])) ? a:classes : [a:classes]
let imports= self.imported_classes()
let imports= filter(imports, '!s:L.has(classes, v:val)')
let [slnum, elnum]= self.region()
execute printf('%d,%ddelete _', slnum, elnum)
call self.add(imports)
endfunction
function! s:prototype.region()
let save_pos= getpos('.')
try
call setpos('.', [0, 1, 1, 0])
let slnum= search('\C^\s*\<import\>', 'cn')
call setpos('.', [0, line('$'), 1, 0])
let elnum= search('\C^\s*\<import\>', 'cnb')
return [slnum, elnum]
finally
call setpos('.', save_pos)
endtry
endfunction
function! s:prototype.imported_classes()
let save_pos= getpos('.')
try
let [slnum, elnum]= self.region()
if [slnum, elnum] ==# [0, 0]
return []
endif
let classes= getline(slnum, elnum)
let classes= filter(classes, 'v:val =~# ''\C^\s*import''')
let classes= filter(classes, 'v:val !~# ''\C^\s*import\s\+\<static\>''')
let classes= map(classes, 'matchstr(v:val, ''\C^\s*import\s\+\zs[^;]\+\ze;'')')
let classes= map(classes, 'substitute(v:val, ''\s\+'', "", "g")')
return s:L.uniq(classes)
finally
call setpos('.', save_pos)
endtry
endfunction
function! javaimport#import_manager#new()
return deepcopy(s:prototype)
endfunction
let &cpo= s:save_cpo
unlet s:save_cpo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment