robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
434792787b
master
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" ) 15delfunction javaformat#RemoveCommonMarkdownWhitespace 16unlet ! g: loaded_javaformat 17endif 18 19if exists ( "b:did_ftplugin" ) 20let & cpo = s: save_cpo 21unlet s: save_cpo 22finish 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. 36if 'ab' !~ 'a\@1<!b' 37let s: peek = string ( strlen ( 'instanceof' ) + 8 ) 38endif 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 ( "" ) 70if filereadable ( g: ftplugin_java_source_path ) 71if ( exists ( "#zip" ) || exists ( "#nvim.zip" )) && 72 \g: ftplugin_java_source_path =~# '.\.\%(jar\|zip\)$' 73if ! exists ( "s:zip_files" ) 74let s: zip_files = {} 75endif 76 77let s: zip_files [ bufnr ( '%' )] = g: ftplugin_java_source_path 78let s: zip_files [ 0 ] = g: ftplugin_java_source_path 79let s: zip_func_upgradable = 1 80 81function ! JavaFileTypeZipFile () abort 82let l: member = substitute ( v: fname , '\.' , '/' , 'g' ) . '.java' 83let l: archive = get ( s: zip_files , bufnr ( '%' ), s: zip_files [ 0 ]) 84" The builtin plugin joins the paths; zipPlugin.vim separates them with "::". 85return exists ( '#nvim.zip' ) 86 \? 'zip://' . l: archive . '/' . l: member 87 \: 'zipfile://' . l: archive . '::' . l: member 88endfunction 89 90" E120 for "inex=s:JavaFileTypeZipFile()" before v8.2.3900. 91setlocal includeexpr = JavaFileTypeZipFile () 92setlocal suffixesadd < 93endif 94else 95let & l: path = g: ftplugin_java_source_path . ',' . & l: path 96endif 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 -= tformatoptions += 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" ) 111let b: browsefilter = "Java Files (*.java)\t*.java\n" . 112 \"Properties Files (*.prop*)\t*.prop*\n" . 113 \"Manifest Files (*.mf)\t*.mf\n" 114if has ( "win32" ) 115let b: browsefilter .= "All Files (*.*)\t*\n" 116else 117let b: browsefilter .= "All Files (*)\t*\n" 118endif 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 126function ! s: SpotBugsGetProperty ( name , default ) abort 127return get ( 128 \{ s: spotbugs_properties_scope } spotbugs_properties , 129 \a: name , 130 \a: default ) 131endfunction 132 133function ! s: SpotBugsHasProperty ( name ) abort 134return has_key ( 135 \{ s: spotbugs_properties_scope } spotbugs_properties , 136 \a: name ) 137endfunction 138 139function ! s: SpotBugsGetProperties () abort 140return { s: spotbugs_properties_scope } spotbugs_properties 141endfunction 142 143" Work around ":bar"s and ":autocmd"s. 144function ! JavaFileTypeExecuteActionOnce ( cleanup_cmd , action_cmd ) abort 145try 146execute a: cleanup_cmd 147finally 148execute a: action_cmd 149endtry 150endfunction 151 152if exists ( "b:spotbugs_properties" ) 153let s: spotbugs_properties_scope = 'b:' 154 155" Merge global entries, if any, in buffer-local entries, favouring 156" defined buffer-local ones. 157call extend ( 158 \b: spotbugs_properties , 159 \get ( g:, 'spotbugs_properties' , {}), 160 \'keep' ) 161elseif exists ( "g:spotbugs_properties" ) 162let s: spotbugs_properties_scope = 'g:' 163endif 164 165let s: commands = {} 166 167for s: name in [ 'DefaultPreCompilerCommand' , 168 \'DefaultPreCompilerTestCommand' , 169 \'DefaultPostCompilerCommand' ] 170if s: SpotBugsHasProperty ( s: name ) 171let s: commands [ s: name ] = remove ( 172 \s: SpotBugsGetProperties (), 173 \s: name ) 174endif 175endfor 176 177if s: SpotBugsHasProperty ( 'compiler' ) 178" XXX: Postpone loading the script until all state, if any, has been 179" collected. 180if ! empty ( s: commands ) 181let g: spotbugs#state = { 182 \'compiler' : remove ( s: SpotBugsGetProperties (), 'compiler' ), 183 \'commands' : copy ( s: commands ), 184 \} 185else 186let g: spotbugs#state = { 187 \'compiler' : remove ( s: SpotBugsGetProperties (), 'compiler' ), 188 \} 189endif 190 191" Merge default entries in global (or buffer-local) entries, favouring 192" defined global (or buffer-local) ones. 193call extend ( 194 \{ s: spotbugs_properties_scope } spotbugs_properties , 195 \spotbugs#DefaultProperties (), 196 \'keep' ) 197elseif ! empty ( s: commands ) 198" XXX: Postpone loading the script until all state, if any, has been 199" collected. 200let g: spotbugs#state = { 'commands' : copy ( s: commands )} 201endif 202 203unlet s: commands s: name 204let s: request = 0 205 206if s: SpotBugsHasProperty ( 'PostCompilerAction' ) 207let s: request += 4 208endif 209 210if s: SpotBugsHasProperty ( 'PreCompilerTestAction' ) 211let s: dispatcher = printf ( 'call call(%s, [])' , 212 \string ( s: SpotBugsGetProperties (). PreCompilerTestAction )) 213let s: request += 2 214endif 215 216if s: SpotBugsHasProperty ( 'PreCompilerAction' ) 217let s: dispatcher = printf ( 'call call(%s, [])' , 218 \string ( s: SpotBugsGetProperties (). PreCompilerAction )) 219let s: request += 1 220endif 221 222" Adapt the tests for "s:FindClassFiles()" from "compiler/spotbugs.vim". 223if ( 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' , []))) 228function ! s: DispatchAction ( paths_action_pairs ) abort 229let name = expand ( '%:p' ) 230 231for [ paths , Action ] in a: paths_action_pairs 232for path in paths 233if name =~# ( path . '.\{-}\.java\=$' ) 234call Action () 235return 236endif 237endfor 238endfor 239endfunction 240 241let s: dir_cnt = min ([ 242 \len ( s: SpotBugsGetProperties (). sourceDirPath ), 243 \len ( s: SpotBugsGetProperties (). classDirPath )]) 244let 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! 249let 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 ]])) 254unlet s: test_dir_cnt s: dir_cnt 255endif 256 257if exists ( "s:dispatcher" ) 258function ! s: ExecuteActions ( pre_action , post_action ) abort 259try 260execute a: pre_action 261catch /\<E42:/ 262execute a: post_action 263endtry 264endfunction 265endif 266 267if s: request 268if exists ( "b:spotbugs_syntax_once" ) || empty ( join ( getline ( 1 , 8 ), '' )) 269let s: actions = [{ 'event' : 'User' }] 270else 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 ! 276let b: spotbugs_syntax_once = 1 277let s: actions = [{ 278 \'event' : 'Syntax' , 279 \'once' : 1 , 280 \}, { 281 \'event' : 'User' , 282 \}] 283endif 284 285for s: idx in range ( len ( s: actions )) 286if s: request == 7 || s: request == 6 || s: request == 5 287let 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 )))) 291elseif s: request == 4 292let s: actions [ s: idx ]. cmd = printf ( 293 \'compiler spotbugs | call call(%s, [])' , 294 \string ( s: SpotBugsGetProperties (). PostCompilerAction )) 295elseif s: request == 3 || s: request == 2 || s: request == 1 296let s: actions [ s: idx ]. cmd = printf ( 'call s:ExecuteActions(%s, %s)' , 297 \string ( s: dispatcher ), 298 \string ( 'compiler spotbugs' )) 299else 300let s: actions [ s: idx ]. cmd = '' 301endif 302endfor 303 304if ! exists ( "#java_spotbugs" ) 305augroup java_spotbugs 306augroup END 307endif 308 309" The events are defined in s:actions. 310silent ! autocmd ! java_spotbugs User <buffer> 311silent ! autocmd ! java_spotbugs Syntax <buffer> 312 313for s: action in s: actions 314if has_key ( s: action , 'once' ) 315execute 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 )) 321else 322execute printf ( 'autocmd java_spotbugs %s <buffer> %s' , 323 \s: action . event , 324 \s: action . cmd ) 325endif 326endfor 327 328if s: SpotBugsHasProperty ( 'PostCompilerActionExecutor' ) && 329 \( s: request == 7 || s: request == 6 || 330 \s: request == 5 || s: request == 4 ) 331let s: augroup = s: SpotBugsGetProperty ( 332 \'augroupForPostCompilerAction' , 333 \'java_spotbugs_post' ) 334let s: augroup = ! empty ( s: augroup ) ? s: augroup : 'java_spotbugs_post' 335 336for s: candidate in [ 'java_spotbugs_post' , s: augroup ] 337if ! exists ( "#" . s: candidate ) 338execute printf ( 'augroup %s | augroup END' , s: candidate ) 339endif 340endfor 341 342silent ! 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". 349execute 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 )))))))) 360endif 361 362unlet ! s: candidate s: augroup s: action s: actions s: idx s: dispatcher 363endif 364 365delfunction s:SpotBugsGetProperties 366delfunction s:SpotBugsHasProperty 367delfunction s:SpotBugsGetProperty 368unlet ! s: request s: spotbugs_properties_scope 369endif 370 371function ! JavaFileTypeCleanUp () abort 372setlocal suffixes < suffixesadd< formatoptions< comments< commentstring< path< includeexpr< include< define< 373unlet ! 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. 377silent ! autocmd ! java_spotbugs User <buffer> 378silent ! autocmd ! java_spotbugs Syntax <buffer> 379silent ! 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" ) 387let & cpo = s: save_cpo 388unlet s: save_cpo 389finish 390endif 391 392if exists ( "s:zip_func_upgradable" ) 393delfunction ! JavaFileTypeZipFile 394 395def ! s:JavaFileTypeZipFile(): string 396const member: string = substitute(v: fname , '\.', '/', 'g') .. '.java' 397const archive : string = get(zip_files, bufnr('%'), zip_files[0]) 398 #The builtin plugin joins the paths; zipPlugin.vim separates them with "::". 399return exists ( '#nvim.zip' ) 400 ?'zip://' .. archive .. '/' .. member 401: 'zipfile ://' .. archive ..'::' .. member 402enddef 403 404setlocal includeexpr = s :JavaFileTypeZipFile() 405setlocal suffixesadd < 406endif 407 408if exists ( "*s:DispatchAction" ) 409def ! s:DispatchAction(paths_action_pairs: list<list<any>>) 410const name : string = expand('%:p') 411 412for [ paths : list <string>, Action: func: any] in paths_action_pairs 413for path in paths 414if name =~# ( path .. '.\{-}\.java\=$' ) 415Action () 416return 417endif 418endfor 419endfor 420enddef 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