javac.vim 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. "============================================================================
  2. "File: javac.vim
  3. "Description: Syntax checking plugin for syntastic.vim
  4. "Maintainer: Jochen Keil <jochen.keil at gmail dot com>
  5. " Dmitry Geurkov <d.geurkov at gmail dot com>
  6. "License: This program is free software. It comes without any warranty,
  7. " to the extent permitted by applicable law. You can redistribute
  8. " it and/or modify it under the terms of the Do What The Fuck You
  9. " Want To Public License, Version 2, as published by Sam Hocevar.
  10. " See http://sam.zoy.org/wtfpl/COPYING for more details.
  11. "============================================================================
  12. if exists('g:loaded_syntastic_java_javac_checker')
  13. finish
  14. endif
  15. let g:loaded_syntastic_java_javac_checker = 1
  16. let g:syntastic_java_javac_maven_pom_tags = ['build', 'properties']
  17. let g:syntastic_java_javac_maven_pom_properties = {}
  18. let s:has_maven = 0
  19. let s:save_cpo = &cpo
  20. set cpo&vim
  21. " Checker options {{{1
  22. if !exists('g:syntastic_java_javac_executable')
  23. let g:syntastic_java_javac_executable = 'javac'
  24. endif
  25. if !exists('g:syntastic_java_maven_executable')
  26. let g:syntastic_java_maven_executable = 'mvn'
  27. endif
  28. if !exists('g:syntastic_java_javac_options')
  29. let g:syntastic_java_javac_options = '-Xlint'
  30. endif
  31. if !exists('g:syntastic_java_maven_options')
  32. let g:syntastic_java_maven_options = ''
  33. endif
  34. if !exists('g:syntastic_java_javac_classpath')
  35. let g:syntastic_java_javac_classpath = ''
  36. endif
  37. if !exists('g:syntastic_java_javac_delete_output')
  38. let g:syntastic_java_javac_delete_output = 1
  39. endif
  40. if !exists('g:syntastic_java_javac_autoload_maven_classpath')
  41. let g:syntastic_java_javac_autoload_maven_classpath = 1
  42. endif
  43. if !exists('g:syntastic_java_javac_config_file_enabled')
  44. let g:syntastic_java_javac_config_file_enabled = 0
  45. endif
  46. if !exists('g:syntastic_java_javac_config_file')
  47. let g:syntastic_java_javac_config_file = '.syntastic_javac_config'
  48. endif
  49. if !exists('g:syntastic_java_javac_custom_classpath_command')
  50. let g:syntastic_java_javac_custom_classpath_command = ''
  51. endif
  52. if !exists('g:syntastic_java_javac_maven_pom_ftime')
  53. let g:syntastic_java_javac_maven_pom_ftime = {}
  54. endif
  55. if !exists('g:syntastic_java_javac_maven_pom_classpath')
  56. let g:syntastic_java_javac_maven_pom_classpath = {}
  57. endif
  58. " }}}1
  59. " Constants {{{1
  60. let s:_FILE_SHORTCUTS = {
  61. \ '%FILE_PATH%': '%:p',
  62. \ '%FILE_NAME%': '%:t',
  63. \ '%FILE_DIR%': '%:p:h',
  64. \ }
  65. lockvar! s:_FILE_SHORTCUTS
  66. " }}}1
  67. " Commands {{{1
  68. command! SyntasticJavacEditClasspath call s:EditClasspath()
  69. command! SyntasticJavacEditConfig call s:EditConfig()
  70. " }}}1
  71. function! SyntaxCheckers_java_javac_IsAvailable() dict " {{{1
  72. let s:has_maven = executable(expand(g:syntastic_java_maven_executable, 1))
  73. return executable(expand(g:syntastic_java_javac_executable, 1))
  74. endfunction " }}}1
  75. function! SyntaxCheckers_java_javac_GetLocList() dict " {{{1
  76. let javac_opts = g:syntastic_java_javac_options
  77. let output_dir = ''
  78. if g:syntastic_java_javac_delete_output
  79. let output_dir = syntastic#util#tmpdir()
  80. let javac_opts .= ' -d ' . syntastic#util#shescape(output_dir)
  81. endif
  82. " load classpath from config file
  83. if g:syntastic_java_javac_config_file_enabled
  84. call s:LoadConfigFile()
  85. endif
  86. " add classpathes to javac_classpath {{{2
  87. let javac_classpath = ''
  88. for path in split(g:syntastic_java_javac_classpath, s:ClassSep())
  89. if path !=# ''
  90. try
  91. let ps = glob(path, 1, 1)
  92. catch
  93. let ps = split(glob(path, 1), "\n")
  94. endtry
  95. if type(ps) == type([])
  96. for p in ps
  97. let javac_classpath = s:AddToClasspath(javac_classpath, p)
  98. endfor
  99. else
  100. let javac_classpath = s:AddToClasspath(javac_classpath, ps)
  101. endif
  102. endif
  103. endfor
  104. if s:has_maven && g:syntastic_java_javac_autoload_maven_classpath
  105. if !g:syntastic_java_javac_delete_output
  106. let javac_opts .= ' -d ' . syntastic#util#shescape(s:MavenOutputDirectory())
  107. endif
  108. let javac_classpath = s:AddToClasspath(javac_classpath, s:GetMavenClasspath())
  109. endif
  110. " }}}2
  111. " load custom classpath {{{2
  112. if g:syntastic_java_javac_custom_classpath_command !=# ''
  113. " Pre-process the classpath command string a little.
  114. let classpath_command = g:syntastic_java_javac_custom_classpath_command
  115. for [key, val] in items(s:_FILE_SHORTCUTS)
  116. let classpath_command = substitute(classpath_command, '\V' . key, syntastic#util#shexpand(val), 'g')
  117. endfor
  118. let lines = syntastic#util#system(classpath_command)
  119. if syntastic#util#isRunningWindows() || has('win32unix')
  120. let lines = substitute(lines, "\r\n", "\n", 'g')
  121. endif
  122. for l in split(lines, "\n")
  123. let javac_classpath = s:AddToClasspath(javac_classpath, l)
  124. endfor
  125. endif
  126. if javac_classpath !=# ''
  127. let javac_opts .= ' -cp ' . syntastic#util#shexpand(javac_classpath)
  128. endif
  129. " }}}2
  130. let fname = expand('%:p:h', 1) . syntastic#util#Slash() . expand ('%:t', 1)
  131. if has('win32unix')
  132. let fname = syntastic#util#CygwinPath(fname)
  133. endif
  134. let makeprg = self.makeprgBuild({
  135. \ 'args': javac_opts,
  136. \ 'fname': syntastic#util#shescape(fname) })
  137. " unashamedly stolen from *errorformat-javac* (quickfix.txt) and modified to include error types
  138. let errorformat =
  139. \ '%E%f:%l: error: %m,'.
  140. \ '%W%f:%l: warning: %m,'.
  141. \ '%E%f:%l: %m,'.
  142. \ '%Z%p^,'.
  143. \ '%-G%.%#'
  144. if output_dir !=# ''
  145. silent! call mkdir(output_dir, 'p')
  146. endif
  147. let errors = SyntasticMake({
  148. \ 'makeprg': makeprg,
  149. \ 'errorformat': errorformat,
  150. \ 'postprocess': ['cygwinRemoveCR'] })
  151. if output_dir !=# ''
  152. call syntastic#util#rmrf(output_dir)
  153. endif
  154. return errors
  155. endfunction " }}}1
  156. " Utilities {{{1
  157. function! s:RemoveCarriageReturn(line) " {{{2
  158. return substitute(a:line, "\r", '', 'g')
  159. endfunction " }}}2
  160. function! s:ClassSep() " {{{2
  161. return (syntastic#util#isRunningWindows() || has('win32unix')) ? ';' : ':'
  162. endfunction " }}}2
  163. function! s:AddToClasspath(classpath, path) " {{{2
  164. if a:path ==# ''
  165. return a:classpath
  166. endif
  167. return (a:classpath !=# '') ? a:classpath . s:ClassSep() . a:path : a:path
  168. endfunction " }}}2
  169. function! s:SplitClasspath(classpath) " {{{2
  170. return split(a:classpath, s:ClassSep())
  171. endfunction " }}}2
  172. function! s:LoadConfigFile() " {{{2
  173. if filereadable(expand(g:syntastic_java_javac_config_file, 1))
  174. execute 'source ' . fnameescape(expand(g:syntastic_java_javac_config_file, 1))
  175. endif
  176. endfunction " }}}2
  177. function! s:SaveClasspath() " {{{2
  178. " build classpath from lines
  179. let path = ''
  180. let lines = getline(1, line('$'))
  181. for l in lines
  182. let path = s:AddToClasspath(path, l)
  183. endfor
  184. " save classpath to config file
  185. if g:syntastic_java_javac_config_file_enabled
  186. if filereadable(expand(g:syntastic_java_javac_config_file, 1))
  187. " load lines from config file
  188. let lines = readfile(expand(g:syntastic_java_javac_config_file, 1))
  189. " strip g:syntastic_java_javac_classpath options from config file lines
  190. let i = 0
  191. while i < len(lines)
  192. if match(lines[i], 'g:syntastic_java_javac_classpath') != -1
  193. call remove(lines, i)
  194. else
  195. let i += 1
  196. endif
  197. endwhile
  198. else
  199. let lines = []
  200. endif
  201. " add new g:syntastic_java_javac_classpath option to config
  202. call add(lines, 'let g:syntastic_java_javac_classpath = ' . string(path))
  203. " save config file lines
  204. call writefile(lines, expand(g:syntastic_java_javac_config_file, 1))
  205. endif
  206. " set new classpath
  207. let g:syntastic_java_javac_classpath = path
  208. let &modified = 0
  209. endfunction " }}}2
  210. function! s:EditClasspath() " {{{2
  211. let command = 'syntastic javac classpath'
  212. let winnr = bufwinnr('^' . command . '$')
  213. if winnr < 0
  214. let path = []
  215. let pathlines = split(g:syntastic_java_javac_classpath, "\n")
  216. for p in pathlines
  217. call extend(path, s:SplitClasspath(p))
  218. endfor
  219. execute (len(path) + 5) . 'sp ' . fnameescape(command)
  220. augroup syntastic
  221. autocmd BufWriteCmd <buffer> call s:SaveClasspath() | bwipeout
  222. augroup END
  223. setlocal buftype=acwrite bufhidden=wipe nobuflisted noswapfile nowrap number
  224. for p in path
  225. call append(line('$') - 1, p)
  226. endfor
  227. let &modified = 0
  228. else
  229. execute winnr . 'wincmd w'
  230. endif
  231. endfunction " }}}2
  232. function! s:SaveConfig() " {{{2
  233. " get lines
  234. let lines = getline(1, line('$'))
  235. if g:syntastic_java_javac_config_file_enabled
  236. " save config file lines
  237. call writefile(lines, expand(g:syntastic_java_javac_config_file, 1))
  238. endif
  239. let &modified = 0
  240. endfunction " }}}2
  241. function! s:EditConfig() " {{{2
  242. if !g:syntastic_java_javac_config_file_enabled
  243. return
  244. endif
  245. let command = 'syntastic javac config'
  246. let winnr = bufwinnr('^' . command . '$')
  247. if winnr < 0
  248. let lines = []
  249. if filereadable(expand(g:syntastic_java_javac_config_file, 1))
  250. let lines = readfile(expand(g:syntastic_java_javac_config_file, 1))
  251. endif
  252. execute (len(lines) + 5) . 'sp ' . fnameescape(command)
  253. augroup syntastic
  254. autocmd BufWriteCmd <buffer> call s:SaveConfig() | bwipeout
  255. augroup END
  256. setlocal ft=vim buftype=acwrite bufhidden=wipe nobuflisted noswapfile nowrap number
  257. for l in lines
  258. call append(line('$') - 1, l)
  259. endfor
  260. let &modified = 0
  261. else
  262. execute winnr . 'wincmd w'
  263. endif
  264. endfunction " }}}2
  265. function! s:GetMavenProperties() " {{{2
  266. let mvn_properties = {}
  267. let pom = syntastic#util#findFileInParent('pom.xml', expand('%:p:h', 1))
  268. if s:has_maven && filereadable(pom)
  269. if !has_key(g:syntastic_java_javac_maven_pom_properties, pom)
  270. let mvn_cmd = syntastic#util#shexpand(g:syntastic_java_maven_executable) .
  271. \ ' -f ' . syntastic#util#shescape(pom) .
  272. \ ' ' . g:syntastic_java_maven_options
  273. let mvn_is_managed_tag = 1
  274. let mvn_settings_output = split(syntastic#util#system(mvn_cmd . ' help:effective-pom'), "\n")
  275. let current_path = 'project'
  276. for line in mvn_settings_output
  277. let matches = matchlist(line, '\m^\s*<\([a-zA-Z0-9\-\.]\+\)>\s*$')
  278. if mvn_is_managed_tag && !empty(matches)
  279. let mvn_is_managed_tag = index(g:syntastic_java_javac_maven_pom_tags, matches[1]) >= 0
  280. let current_path .= '.' . matches[1]
  281. else
  282. let matches = matchlist(line, '\m^\s*</\([a-zA-Z0-9\-\.]\+\)>\s*$')
  283. if !empty(matches)
  284. let mvn_is_managed_tag = index(g:syntastic_java_javac_maven_pom_tags, matches[1]) < 0
  285. let current_path = substitute(current_path, '\m\.' . matches[1] . '$', '', '')
  286. else
  287. let matches = matchlist(line, '\m^\s*<\([a-zA-Z0-9\-\.]\+\)>\(.\+\)</[a-zA-Z0-9\-\.]\+>\s*$')
  288. if mvn_is_managed_tag && !empty(matches)
  289. let mvn_properties[current_path . '.' . matches[1]] = matches[2]
  290. endif
  291. endif
  292. endif
  293. endfor
  294. let g:syntastic_java_javac_maven_pom_properties[pom] = mvn_properties
  295. endif
  296. return g:syntastic_java_javac_maven_pom_properties[pom]
  297. endif
  298. return mvn_properties
  299. endfunction " }}}2
  300. function! s:GetMavenClasspath() " {{{2
  301. let pom = syntastic#util#findFileInParent('pom.xml', expand('%:p:h', 1))
  302. if s:has_maven && filereadable(pom)
  303. if !has_key(g:syntastic_java_javac_maven_pom_ftime, pom) || g:syntastic_java_javac_maven_pom_ftime[pom] != getftime(pom)
  304. let mvn_cmd = syntastic#util#shexpand(g:syntastic_java_maven_executable) .
  305. \ ' -f ' . syntastic#util#shescape(pom) .
  306. \ ' ' . g:syntastic_java_maven_options
  307. let mvn_classpath_output = split(syntastic#util#system(mvn_cmd . ' dependency:build-classpath -DincludeScope=test'), "\n")
  308. let mvn_classpath = ''
  309. let class_path_next = 0
  310. for line in mvn_classpath_output
  311. if class_path_next == 1
  312. let mvn_classpath = s:RemoveCarriageReturn(line)
  313. break
  314. endif
  315. if stridx(line, 'Dependencies classpath:') >= 0
  316. let class_path_next = 1
  317. endif
  318. endfor
  319. let mvn_properties = s:GetMavenProperties()
  320. let sep = syntastic#util#Slash()
  321. let output_dir = get(mvn_properties, 'project.build.outputDirectory', join(['target', 'classes'], sep))
  322. let mvn_classpath = s:AddToClasspath(mvn_classpath, output_dir)
  323. let test_output_dir = get(mvn_properties, 'project.build.testOutputDirectory', join(['target', 'test-classes'], sep))
  324. let mvn_classpath = s:AddToClasspath(mvn_classpath, test_output_dir)
  325. let g:syntastic_java_javac_maven_pom_ftime[pom] = getftime(pom)
  326. let g:syntastic_java_javac_maven_pom_classpath[pom] = mvn_classpath
  327. endif
  328. return g:syntastic_java_javac_maven_pom_classpath[pom]
  329. endif
  330. return ''
  331. endfunction " }}}2
  332. function! s:MavenOutputDirectory() " {{{2
  333. let pom = syntastic#util#findFileInParent('pom.xml', expand('%:p:h', 1))
  334. if s:has_maven && filereadable(pom)
  335. let mvn_properties = s:GetMavenProperties()
  336. let output_dir = get(mvn_properties, 'project.properties.build.dir', getcwd())
  337. let sep = syntastic#util#Slash()
  338. let src_main_dir = get(mvn_properties, 'project.build.sourceDirectory', join(['src', 'main', 'java'], sep))
  339. let src_test_dir = get(mvn_properties, 'project.build.testsourceDirectory', join(['src', 'test', 'java'], sep))
  340. if stridx(expand('%:p:h', 1), src_main_dir) >= 0
  341. let output_dir = get(mvn_properties, 'project.build.outputDirectory', join ([output_dir, 'target', 'classes'], sep))
  342. endif
  343. if stridx(expand('%:p:h', 1), src_test_dir) >= 0
  344. let output_dir = get(mvn_properties, 'project.build.testOutputDirectory', join([output_dir, 'target', 'test-classes'], sep))
  345. endif
  346. if has('win32unix')
  347. let output_dir = syntastic#util#CygwinPath(output_dir)
  348. endif
  349. return output_dir
  350. endif
  351. return '.'
  352. endfunction " }}}2
  353. " }}}1
  354. call g:SyntasticRegistry.CreateAndRegisterChecker({
  355. \ 'filetype': 'java',
  356. \ 'name': 'javac'})
  357. let &cpo = s:save_cpo
  358. unlet s:save_cpo
  359. " vim: set sw=4 sts=4 et fdm=marker: