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