diff options
Diffstat (limited to 'autoload/sonictemplate/lang/java.vim')
-rw-r--r-- | autoload/sonictemplate/lang/java.vim | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/autoload/sonictemplate/lang/java.vim b/autoload/sonictemplate/lang/java.vim index 59e5d69..eca4201 100644 --- a/autoload/sonictemplate/lang/java.vim +++ b/autoload/sonictemplate/lang/java.vim @@ -1,6 +1,31 @@ -function! sonictemplate#lang#java#util(name) +function! sonictemplate#lang#java#util(name) abort if a:name == 'package' - return 'package foo.bar;' + let fn = expand('%:p:h') + let fn = fn[len(s:project_root())+1:] + let fn = substitute(fn, '\\', '.', 'g') + let fn = substitute(fn, '^\(src\.main\.java\.\|src\.\)', '', '') + if fn != "" + return 'package ' . fn . ';' + endif endif return '' endfunction + +function! s:project_root() abort + let pwd = getcwd() + let elems = split(substitute(pwd, '\\', '/', 'g'), '/', 1) + let is_unc = len(elems) > 2 && (has('win32') || has('win64')) && elems[0:1] == ['', ''] + while len(elems) + let path = join(elems, '/') + for vcs in ['.rootdir', '.git', '.hg', '.svn', 'bzr', '_darcs', 'build.xml', '.project', 'build.gradle', 'pom.xml'] + if filereadable(path.'/'.vcs) || isdirectory(path.'/'.vcs) + return path + endif + endfor + let elems = elems[:-2] + if is_unc && len(elems) <= 3 + break + endif + endwhile + return pwd +endfunction |