aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYasuhiro Matsumoto <mattn.jp@gmail.com>2015-10-02 19:57:37 +0900
committerYasuhiro Matsumoto <mattn.jp@gmail.com>2015-10-02 19:57:37 +0900
commit9f9c5f04477b6060ba5a7d71cd21f1c54806f151 (patch)
tree4668c6aa029778c9c65617809282d05dee990546
parent89f67a9bacbcdcccd631dc4c0deab45c1c402eb7 (diff)
downloadvim-sonictemplate-9f9c5f04477b6060ba5a7d71cd21f1c54806f151.tar.gz
vim-sonictemplate-9f9c5f04477b6060ba5a7d71cd21f1c54806f151.tar.bz2
vim-sonictemplate-9f9c5f04477b6060ba5a7d71cd21f1c54806f151.zip
package name generator
-rw-r--r--autoload/sonictemplate/lang/java.vim29
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