robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #41093 from barrettruth/feat/zip-finish434792787b

master
13.8 KiB426 linesraw
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:		2025 May 08
7
8" Make sure the continuation lines below do not cause problems in
9" compatibility mode.
10let s:save_cpo = &cpo
11set cpo-=C
12
13if (exists("g:java_ignore_javadoc") || exists("g:java_ignore_markdown")) &&
14	\ exists("*javaformat#RemoveCommonMarkdownWhitespace")
15    delfunction javaformat#RemoveCommonMarkdownWhitespace
16    unlet! g:loaded_javaformat
17endif
18
19if exists("b:did_ftplugin")
20    let &cpo = s:save_cpo
21    unlet s:save_cpo
22    finish
23endif
24
25let b:did_ftplugin = 1
26
27" For filename completion, prefer the .java extension over the .class
28" extension.
29set suffixes+=.class
30
31" Set up "&define" and "&include".
32let s:peek = ''
33
34try
35    " Since v7.3.1037.
36    if 'ab' !~ 'a\@1<!b'
37	let s:peek = string(strlen('instanceof') + 8)
38    endif
39catch /\<E59:/
40endtry
41
42" Treat "s:common" as a non-backtracking unit to avoid matching constructor
43" declarations whose package-private headers are indistinguishable from method
44" invocation.  Note that "[@-]" must not and "$" may not be in "&l:iskeyword".
45let s:common = '\%(\%(\%(@\%(interface\)\@!\%(\K\k*\.\)*\K\k*\)\s\+\)*' .
46	\ '\%(p\%(rivate\|rotected\|ublic\)\s\+\)\=\)\@>'
47let s:types = '\%(\%(abstract\|final\|non-sealed\|s\%(ealed\|tatic\|trictfp\)\)\s\+\)*' .
48	\ '\%(class\|enum\|@\=interface\|record\)\s\+\ze\K\k*\>'
49let s:methods = '\%(\%(abstract\|default\|final\|native\|s\%(tatic\|trictfp\|ynchronized\)\)\s\+\)*' .
50	\ '\%(<.\{-1,}>\s\+\)\=\%(\K\k*\.\)*\K\k*\s*\%(<.\{-1,}>\%(\s\|\[\)\@=\)\=\s*\%(\[\]\s*\)*' .
51	\ '\s\+\ze\%(\<\%(assert\|case\|instanceof\|new\|return\|throw\|when\)\s\+\)\@' .
52	\ s:peek . '<!\K\k*\s*('
53let &l:define = printf('\C\m^\s*%s\%%(%s\|%s\)', s:common, s:types, s:methods)
54let &l:include = '\C\m^\s*import\s\+\ze\%(\K\k*\.\)\+\K\k*;'
55unlet s:methods s:types s:common s:peek
56
57" Enable gf on import statements.  Convert . in the package
58" name to / and append .java to the name, then search the path.
59setlocal includeexpr=substitute(v:fname,'\\.','/','g')
60setlocal suffixesadd=.java
61
62" Clean up in case this file is sourced again.
63unlet! s:zip_func_upgradable
64
65"""" STRIVE TO REMAIN COMPATIBLE FOR AT LEAST VIM 7.0.
66
67" Documented in ":help ft-java-plugin".
68if exists("g:ftplugin_java_source_path") &&
69		\ type(g:ftplugin_java_source_path) == type("")
70    if filereadable(g:ftplugin_java_source_path)
71	if (exists("#zip") || exists("#nvim.zip")) &&
72		    \ g:ftplugin_java_source_path =~# '.\.\%(jar\|zip\)$'
73	    if !exists("s:zip_files")
74		let s:zip_files = {}
75	    endif
76
77	    let s:zip_files[bufnr('%')] = g:ftplugin_java_source_path
78	    let s:zip_files[0] = g:ftplugin_java_source_path
79	    let s:zip_func_upgradable = 1
80
81	    function! JavaFileTypeZipFile() abort
82		let l:member = substitute(v:fname, '\.', '/', 'g') . '.java'
83		let l:archive = get(s:zip_files, bufnr('%'), s:zip_files[0])
84		" The builtin plugin joins the paths; zipPlugin.vim separates them with "::".
85		return exists('#nvim.zip')
86			    \ ? 'zip://' . l:archive . '/' . l:member
87			    \ : 'zipfile://' . l:archive . '::' . l:member
88	    endfunction
89
90	    " E120 for "inex=s:JavaFileTypeZipFile()" before v8.2.3900.
91	    setlocal includeexpr=JavaFileTypeZipFile()
92	    setlocal suffixesadd<
93	endif
94    else
95	let &l:path = g:ftplugin_java_source_path . ',' . &l:path
96    endif
97endif
98
99" Set 'formatoptions' to break comment lines but not other lines,
100" and insert the comment leader when hitting <CR> or using "o".
101setlocal formatoptions-=t formatoptions+=croql
102
103" Set 'comments' to format Markdown Javadoc comments and dashed lists
104" in other multi-line comments (it behaves just like C).
105setlocal comments& comments^=:///,sO:*\ -,mO:*\ \ ,exO:*/
106
107setlocal commentstring=//\ %s
108
109" Change the :browse e filter to primarily show Java-related files.
110if (has("gui_win32") || has("gui_gtk")) && !exists("b:browsefilter")
111    let  b:browsefilter="Java Files (*.java)\t*.java\n" .
112		\	"Properties Files (*.prop*)\t*.prop*\n" .
113		\	"Manifest Files (*.mf)\t*.mf\n"
114    if has("win32")
115	let b:browsefilter .= "All Files (*.*)\t*\n"
116    else
117	let b:browsefilter .= "All Files (*)\t*\n"
118    endif
119endif
120
121"""" Support pre- and post-compiler actions for SpotBugs.
122if (!empty(get(g:, 'spotbugs_properties', {})) ||
123	\ !empty(get(b:, 'spotbugs_properties', {}))) &&
124	\ filereadable($VIMRUNTIME . '/compiler/spotbugs.vim')
125
126    function! s:SpotBugsGetProperty(name, default) abort
127	return get(
128	    \ {s:spotbugs_properties_scope}spotbugs_properties,
129	    \ a:name,
130	    \ a:default)
131    endfunction
132
133    function! s:SpotBugsHasProperty(name) abort
134	return has_key(
135	    \ {s:spotbugs_properties_scope}spotbugs_properties,
136	    \ a:name)
137    endfunction
138
139    function! s:SpotBugsGetProperties() abort
140	return {s:spotbugs_properties_scope}spotbugs_properties
141    endfunction
142
143    " Work around ":bar"s and ":autocmd"s.
144    function! JavaFileTypeExecuteActionOnce(cleanup_cmd, action_cmd) abort
145	try
146	    execute a:cleanup_cmd
147	finally
148	    execute a:action_cmd
149	endtry
150    endfunction
151
152    if exists("b:spotbugs_properties")
153	let s:spotbugs_properties_scope = 'b:'
154
155	" Merge global entries, if any, in buffer-local entries, favouring
156	" defined buffer-local ones.
157	call extend(
158	    \ b:spotbugs_properties,
159	    \ get(g:, 'spotbugs_properties', {}),
160	    \ 'keep')
161    elseif exists("g:spotbugs_properties")
162	let s:spotbugs_properties_scope = 'g:'
163    endif
164
165    let s:commands = {}
166
167    for s:name in ['DefaultPreCompilerCommand',
168	    \ 'DefaultPreCompilerTestCommand',
169	    \ 'DefaultPostCompilerCommand']
170	if s:SpotBugsHasProperty(s:name)
171	    let s:commands[s:name] = remove(
172		\ s:SpotBugsGetProperties(),
173		\ s:name)
174	endif
175    endfor
176
177    if s:SpotBugsHasProperty('compiler')
178	" XXX: Postpone loading the script until all state, if any, has been
179	" collected.
180	if !empty(s:commands)
181	    let g:spotbugs#state = {
182		\ 'compiler': remove(s:SpotBugsGetProperties(), 'compiler'),
183		\ 'commands': copy(s:commands),
184	    \ }
185	else
186	    let g:spotbugs#state = {
187		\ 'compiler': remove(s:SpotBugsGetProperties(), 'compiler'),
188	    \ }
189	endif
190
191	" Merge default entries in global (or buffer-local) entries, favouring
192	" defined global (or buffer-local) ones.
193	call extend(
194	    \ {s:spotbugs_properties_scope}spotbugs_properties,
195	    \ spotbugs#DefaultProperties(),
196	    \ 'keep')
197    elseif !empty(s:commands)
198	" XXX: Postpone loading the script until all state, if any, has been
199	" collected.
200	let g:spotbugs#state = {'commands': copy(s:commands)}
201    endif
202
203    unlet s:commands s:name
204    let s:request = 0
205
206    if s:SpotBugsHasProperty('PostCompilerAction')
207	let s:request += 4
208    endif
209
210    if s:SpotBugsHasProperty('PreCompilerTestAction')
211	let s:dispatcher = printf('call call(%s, [])',
212	    \ string(s:SpotBugsGetProperties().PreCompilerTestAction))
213	let s:request += 2
214    endif
215
216    if s:SpotBugsHasProperty('PreCompilerAction')
217	let s:dispatcher = printf('call call(%s, [])',
218	    \ string(s:SpotBugsGetProperties().PreCompilerAction))
219	let s:request += 1
220    endif
221
222    " Adapt the tests for "s:FindClassFiles()" from "compiler/spotbugs.vim".
223    if (s:request == 3 || s:request == 7) &&
224	    \ (!empty(s:SpotBugsGetProperty('sourceDirPath', [])) &&
225		\ !empty(s:SpotBugsGetProperty('classDirPath', [])) &&
226	    \ !empty(s:SpotBugsGetProperty('testSourceDirPath', [])) &&
227		\ !empty(s:SpotBugsGetProperty('testClassDirPath', [])))
228	function! s:DispatchAction(paths_action_pairs) abort
229	    let name = expand('%:p')
230
231	    for [paths, Action] in a:paths_action_pairs
232		for path in paths
233		    if name =~# (path . '.\{-}\.java\=$')
234			call Action()
235			return
236		    endif
237		endfor
238	    endfor
239	endfunction
240
241	let s:dir_cnt = min([
242	    \ len(s:SpotBugsGetProperties().sourceDirPath),
243	    \ len(s:SpotBugsGetProperties().classDirPath)])
244	let s:test_dir_cnt = min([
245	    \ len(s:SpotBugsGetProperties().testSourceDirPath),
246	    \ len(s:SpotBugsGetProperties().testClassDirPath)])
247
248	" Do not break up path pairs with filtering!
249	let s:dispatcher = printf('call s:DispatchAction(%s)',
250	    \ string([[s:SpotBugsGetProperties().sourceDirPath[0 : s:dir_cnt - 1],
251			\ s:SpotBugsGetProperties().PreCompilerAction],
252		\ [s:SpotBugsGetProperties().testSourceDirPath[0 : s:test_dir_cnt - 1],
253			\ s:SpotBugsGetProperties().PreCompilerTestAction]]))
254	unlet s:test_dir_cnt s:dir_cnt
255    endif
256
257    if exists("s:dispatcher")
258	function! s:ExecuteActions(pre_action, post_action) abort
259	    try
260		execute a:pre_action
261	    catch /\<E42:/
262		execute a:post_action
263	    endtry
264	endfunction
265    endif
266
267    if s:request
268	if exists("b:spotbugs_syntax_once") || empty(join(getline(1, 8), ''))
269	    let s:actions = [{'event': 'User'}]
270	else
271	    " XXX: Handle multiple FileType events when vimrc contains more
272	    " than one filetype setting for the language, e.g.:
273	    "	:filetype plugin indent on
274	    "	:autocmd BufRead,BufNewFile *.java setlocal filetype=java ...
275	    " XXX: DO NOT ADD b:spotbugs_syntax_once TO b:undo_ftplugin !
276	    let b:spotbugs_syntax_once = 1
277	    let s:actions = [{
278		    \ 'event': 'Syntax',
279		    \ 'once': 1,
280		\ }, {
281		    \ 'event': 'User',
282		\ }]
283	endif
284
285	for s:idx in range(len(s:actions))
286	    if s:request == 7 || s:request == 6 || s:request == 5
287		let s:actions[s:idx].cmd = printf('call s:ExecuteActions(%s, %s)',
288		    \ string(s:dispatcher),
289		    \ string(printf('compiler spotbugs | call call(%s, [])',
290			\ string(s:SpotBugsGetProperties().PostCompilerAction))))
291	    elseif s:request == 4
292		let s:actions[s:idx].cmd = printf(
293		    \ 'compiler spotbugs | call call(%s, [])',
294		    \ string(s:SpotBugsGetProperties().PostCompilerAction))
295	    elseif s:request == 3 || s:request == 2 || s:request == 1
296		let s:actions[s:idx].cmd = printf('call s:ExecuteActions(%s, %s)',
297		    \ string(s:dispatcher),
298		    \ string('compiler spotbugs'))
299	    else
300		let s:actions[s:idx].cmd = ''
301	    endif
302	endfor
303
304	if !exists("#java_spotbugs")
305	    augroup java_spotbugs
306	    augroup END
307	endif
308
309	" The events are defined in s:actions.
310	silent! autocmd! java_spotbugs User <buffer>
311	silent! autocmd! java_spotbugs Syntax <buffer>
312
313	for s:action in s:actions
314	    if has_key(s:action, 'once')
315		execute printf('autocmd java_spotbugs %s <buffer> ' .
316			\ 'call JavaFileTypeExecuteActionOnce(%s, %s)',
317		    \ s:action.event,
318		    \ string(printf('autocmd! java_spotbugs %s <buffer>',
319			\ s:action.event)),
320		    \ string(s:action.cmd))
321	    else
322		execute printf('autocmd java_spotbugs %s <buffer> %s',
323		    \ s:action.event,
324		    \ s:action.cmd)
325	    endif
326	endfor
327
328	if s:SpotBugsHasProperty('PostCompilerActionExecutor') &&
329		\ (s:request == 7 || s:request == 6 ||
330		\ s:request == 5 || s:request == 4)
331	    let s:augroup = s:SpotBugsGetProperty(
332		\ 'augroupForPostCompilerAction',
333		\ 'java_spotbugs_post')
334	    let s:augroup = !empty(s:augroup) ? s:augroup : 'java_spotbugs_post'
335
336	    for s:candidate in ['java_spotbugs_post', s:augroup]
337		if !exists("#" . s:candidate)
338		    execute printf('augroup %s | augroup END', s:candidate)
339		endif
340	    endfor
341
342	    silent! autocmd! java_spotbugs_post User <buffer>
343
344	    " Define a User ":autocmd" to define a once-only ShellCmdPost
345	    " ":autocmd" that will invoke "PostCompilerActionExecutor" and let
346	    " it decide whether to proceed with ":compiler spotbugs" etc.; and
347	    " seek explicit synchronisation with ":doautocmd ShellCmdPost" by
348	    " omitting "nested" for "java_spotbugs_post" and "java_spotbugs".
349	    execute printf('autocmd java_spotbugs_post User <buffer> ' .
350				\ 'call JavaFileTypeExecuteActionOnce(%s, %s)',
351		\ string(printf('autocmd! %s ShellCmdPost <buffer>', s:augroup)),
352		\ string(printf('autocmd %s ShellCmdPost <buffer> ' .
353				\ 'call JavaFileTypeExecuteActionOnce(%s, %s)',
354		    \ s:augroup,
355		    \ string(printf('autocmd! %s ShellCmdPost <buffer>', s:augroup)),
356		    \ string(printf('call call(%s, [%s])',
357			\ string(s:SpotBugsGetProperties().PostCompilerActionExecutor),
358			\ string(printf('compiler spotbugs | call call(%s, [])',
359			    \ string(s:SpotBugsGetProperties().PostCompilerAction))))))))
360	endif
361
362	unlet! s:candidate s:augroup s:action s:actions s:idx s:dispatcher
363    endif
364
365    delfunction s:SpotBugsGetProperties
366    delfunction s:SpotBugsHasProperty
367    delfunction s:SpotBugsGetProperty
368    unlet! s:request s:spotbugs_properties_scope
369endif
370
371function! JavaFileTypeCleanUp() abort
372    setlocal suffixes< suffixesadd< formatoptions< comments< commentstring< path< includeexpr< include< define<
373    unlet! b:browsefilter
374
375    " The concatenated ":autocmd" removals may be misparsed as an ":autocmd".
376    " A _once-only_ ShellCmdPost ":autocmd" is always a call-site definition.
377    silent! autocmd! java_spotbugs User <buffer>
378    silent! autocmd! java_spotbugs Syntax <buffer>
379    silent! autocmd! java_spotbugs_post User <buffer>
380endfunction
381
382" Undo the stuff we changed.
383let b:undo_ftplugin = 'call JavaFileTypeCleanUp() | delfunction JavaFileTypeCleanUp'
384
385" See ":help vim9-mix".
386if !has("vim9script")
387    let &cpo = s:save_cpo
388    unlet s:save_cpo
389    finish
390endif
391
392if exists("s:zip_func_upgradable")
393    delfunction! JavaFileTypeZipFile
394
395    def! s:JavaFileTypeZipFile(): string
396	const member: string = substitute(v:fname, '\.', '/', 'g') .. '.java'
397	const archive: string = get(zip_files, bufnr('%'), zip_files[0])
398	# The builtin plugin joins the paths; zipPlugin.vim separates them with "::".
399	return exists('#nvim.zip')
400		? 'zip://' .. archive .. '/' .. member
401		: 'zipfile://' .. archive .. '::' .. member
402    enddef
403
404    setlocal includeexpr=s:JavaFileTypeZipFile()
405    setlocal suffixesadd<
406endif
407
408if exists("*s:DispatchAction")
409    def! s:DispatchAction(paths_action_pairs: list<list<any>>)
410	const name: string = expand('%:p')
411
412	for [paths: list<string>, Action: func: any] in paths_action_pairs
413	    for path in paths
414		if name =~# (path .. '.\{-}\.java\=$')
415		    Action()
416		    return
417		endif
418	    endfor
419	endfor
420    enddef
421endif
422
423" Restore the saved compatibility options.
424let &cpo = s:save_cpo
425unlet s:save_cpo
426" vim: fdm=syntax sw=4 ts=8 noet sta