java.vim 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. " Vim filetype plugin file
  2. " Language: Java
  3. " Maintainer: Aliaksei Budavei <0x000c70 AT gmail DOT com>
  4. " Former Maintainer: Dan Sharp
  5. " Repository: https://github.com/zzzyxwvut/java-vim.git
  6. " Last Change: 2024 Nov 24
  7. " 2024 Jan 14 by Vim Project (browsefilter)
  8. " 2024 May 23 by Riley Bruins <ribru17@gmail.com> ('commentstring')
  9. " Make sure the continuation lines below do not cause problems in
  10. " compatibility mode.
  11. let s:save_cpo = &cpo
  12. set cpo-=C
  13. if (exists("g:java_ignore_javadoc") || exists("g:java_ignore_markdown")) &&
  14. \ exists("*javaformat#RemoveCommonMarkdownWhitespace")
  15. delfunction javaformat#RemoveCommonMarkdownWhitespace
  16. unlet! g:loaded_javaformat
  17. endif
  18. if exists("b:did_ftplugin")
  19. let &cpo = s:save_cpo
  20. unlet s:save_cpo
  21. finish
  22. endif
  23. let b:did_ftplugin = 1
  24. " For filename completion, prefer the .java extension over the .class
  25. " extension.
  26. set suffixes+=.class
  27. " Enable gf on import statements. Convert . in the package
  28. " name to / and append .java to the name, then search the path.
  29. setlocal includeexpr=substitute(v:fname,'\\.','/','g')
  30. setlocal suffixesadd=.java
  31. " Clean up in case this file is sourced again.
  32. unlet! s:zip_func_upgradable
  33. """" STRIVE TO REMAIN COMPATIBLE FOR AT LEAST VIM 7.0.
  34. " Documented in ":help ft-java-plugin".
  35. if exists("g:ftplugin_java_source_path") &&
  36. \ type(g:ftplugin_java_source_path) == type("")
  37. if filereadable(g:ftplugin_java_source_path)
  38. if exists("#zip") &&
  39. \ g:ftplugin_java_source_path =~# '.\.\%(jar\|zip\)$'
  40. if !exists("s:zip_files")
  41. let s:zip_files = {}
  42. endif
  43. let s:zip_files[bufnr('%')] = g:ftplugin_java_source_path
  44. let s:zip_files[0] = g:ftplugin_java_source_path
  45. let s:zip_func_upgradable = 1
  46. function! JavaFileTypeZipFile() abort
  47. let @/ = substitute(v:fname, '\.', '\\/', 'g') . '.java'
  48. return get(s:zip_files, bufnr('%'), s:zip_files[0])
  49. endfunction
  50. " E120 for "inex=s:JavaFileTypeZipFile()" before v8.2.3900.
  51. setlocal includeexpr=JavaFileTypeZipFile()
  52. setlocal suffixesadd<
  53. endif
  54. else
  55. let &l:path = g:ftplugin_java_source_path . ',' . &l:path
  56. endif
  57. endif
  58. " Set 'formatoptions' to break comment lines but not other lines,
  59. " and insert the comment leader when hitting <CR> or using "o".
  60. setlocal formatoptions-=t formatoptions+=croql
  61. " Set 'comments' to format Markdown Javadoc comments and dashed lists
  62. " in other multi-line comments (it behaves just like C).
  63. setlocal comments& comments^=:///,sO:*\ -,mO:*\ \ ,exO:*/
  64. setlocal commentstring=//\ %s
  65. " Change the :browse e filter to primarily show Java-related files.
  66. if (has("gui_win32") || has("gui_gtk")) && !exists("b:browsefilter")
  67. let b:browsefilter="Java Files (*.java)\t*.java\n" .
  68. \ "Properties Files (*.prop*)\t*.prop*\n" .
  69. \ "Manifest Files (*.mf)\t*.mf\n"
  70. if has("win32")
  71. let b:browsefilter .= "All Files (*.*)\t*\n"
  72. else
  73. let b:browsefilter .= "All Files (*)\t*\n"
  74. endif
  75. endif
  76. " The support for pre- and post-compiler actions for SpotBugs.
  77. if exists("g:spotbugs_properties") && has_key(g:spotbugs_properties, 'compiler')
  78. try
  79. let spotbugs#compiler = g:spotbugs_properties.compiler
  80. let g:spotbugs_properties = extend(
  81. \ spotbugs#DefaultProperties(),
  82. \ g:spotbugs_properties,
  83. \ 'force')
  84. catch
  85. echomsg v:errmsg
  86. finally
  87. call remove(g:spotbugs_properties, 'compiler')
  88. endtry
  89. endif
  90. if exists("g:spotbugs_properties") &&
  91. \ filereadable($VIMRUNTIME . '/compiler/spotbugs.vim')
  92. let s:request = 0
  93. if has_key(g:spotbugs_properties, 'PreCompilerAction')
  94. let s:dispatcher = 'call g:spotbugs_properties.PreCompilerAction() | '
  95. let s:request += 1
  96. endif
  97. if has_key(g:spotbugs_properties, 'PreCompilerTestAction')
  98. let s:dispatcher = 'call g:spotbugs_properties.PreCompilerTestAction() | '
  99. let s:request += 2
  100. endif
  101. if has_key(g:spotbugs_properties, 'PostCompilerAction')
  102. let s:request += 4
  103. endif
  104. if (s:request == 3 || s:request == 7) &&
  105. \ has_key(g:spotbugs_properties, 'sourceDirPath') &&
  106. \ has_key(g:spotbugs_properties, 'testSourceDirPath')
  107. function! s:DispatchAction(path_action_pairs) abort
  108. let name = expand('%:p')
  109. for [path, Action] in a:path_action_pairs
  110. if name =~# (path . '.\{-}\.java\=$')
  111. call Action()
  112. break
  113. endif
  114. endfor
  115. endfunction
  116. let s:dispatcher = printf('call s:DispatchAction(%s) | ',
  117. \ string([[g:spotbugs_properties.sourceDirPath,
  118. \ g:spotbugs_properties.PreCompilerAction],
  119. \ [g:spotbugs_properties.testSourceDirPath,
  120. \ g:spotbugs_properties.PreCompilerTestAction]]))
  121. endif
  122. if s:request
  123. if exists("b:spotbugs_syntax_once")
  124. let s:actions = [{'event': 'BufWritePost'}]
  125. else
  126. " XXX: Handle multiple FileType events when vimrc contains more
  127. " than one filetype setting for the language, e.g.:
  128. " :filetype plugin indent on
  129. " :autocmd BufRead,BufNewFile *.java setlocal filetype=java ...
  130. " XXX: DO NOT ADD b:spotbugs_syntax_once TO b:undo_ftplugin !
  131. let b:spotbugs_syntax_once = 1
  132. let s:actions = [{
  133. \ 'event': 'Syntax',
  134. \ 'once': 1,
  135. \ }, {
  136. \ 'event': 'BufWritePost',
  137. \ }]
  138. endif
  139. for s:idx in range(len(s:actions))
  140. if s:request == 7 || s:request == 6 || s:request == 5
  141. let s:actions[s:idx].cmd = s:dispatcher . 'compiler spotbugs | ' .
  142. \ 'call g:spotbugs_properties.PostCompilerAction()'
  143. elseif s:request == 4
  144. let s:actions[s:idx].cmd = 'compiler spotbugs | ' .
  145. \ 'call g:spotbugs_properties.PostCompilerAction()'
  146. elseif s:request == 3 || s:request == 2 || s:request == 1
  147. let s:actions[s:idx].cmd = s:dispatcher . 'compiler spotbugs'
  148. else
  149. let s:actions[s:idx].cmd = ''
  150. endif
  151. endfor
  152. if !exists("#java_spotbugs")
  153. augroup java_spotbugs
  154. augroup END
  155. endif
  156. " The events are defined in s:actions.
  157. silent! autocmd! java_spotbugs BufWritePost <buffer>
  158. silent! autocmd! java_spotbugs Syntax <buffer>
  159. for s:action in s:actions
  160. execute printf('autocmd java_spotbugs %s <buffer> %s',
  161. \ s:action.event,
  162. \ s:action.cmd . (has_key(s:action, 'once')
  163. \ ? printf(' | autocmd! java_spotbugs %s <buffer>',
  164. \ s:action.event)
  165. \ : ''))
  166. endfor
  167. unlet! s:action s:actions s:idx s:dispatcher
  168. endif
  169. unlet s:request
  170. endif
  171. function! JavaFileTypeCleanUp() abort
  172. setlocal suffixes< suffixesadd< formatoptions< comments< commentstring< path< includeexpr<
  173. unlet! b:browsefilter
  174. " The concatenated removals may be misparsed as a BufWritePost autocmd.
  175. silent! autocmd! java_spotbugs BufWritePost <buffer>
  176. silent! autocmd! java_spotbugs Syntax <buffer>
  177. endfunction
  178. " Undo the stuff we changed.
  179. let b:undo_ftplugin = 'call JavaFileTypeCleanUp() | delfunction JavaFileTypeCleanUp'
  180. " See ":help vim9-mix".
  181. if !has("vim9script")
  182. let &cpo = s:save_cpo
  183. unlet s:save_cpo
  184. finish
  185. endif
  186. if exists("s:zip_func_upgradable")
  187. delfunction! JavaFileTypeZipFile
  188. def! s:JavaFileTypeZipFile(): string
  189. @/ = substitute(v:fname, '\.', '\\/', 'g') .. '.java'
  190. return get(zip_files, bufnr('%'), zip_files[0])
  191. enddef
  192. setlocal includeexpr=s:JavaFileTypeZipFile()
  193. setlocal suffixesadd<
  194. endif
  195. if exists("*s:DispatchAction")
  196. def! s:DispatchAction(path_action_pairs: list<list<any>>)
  197. const name: string = expand('%:p')
  198. for [path: string, Action: func: any] in path_action_pairs
  199. if name =~# (path .. '.\{-}\.java\=$')
  200. Action()
  201. break
  202. endif
  203. endfor
  204. enddef
  205. endif
  206. " Restore the saved compatibility options.
  207. let &cpo = s:save_cpo
  208. unlet s:save_cpo
  209. " vim: fdm=syntax sw=4 ts=8 noet sta