robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f618cdf9c4
master
1" Vim compiler file 2" Compiler: Spotbugs (Java static checker; needs javac compiled classes) 3" Maintainers: @konfekt and @zzzyxwvut 4" Last Change: 2024 Dec 20 5 6if exists ( 'g:current_compiler' ) || bufname () !~# '\.java\=$' || wordcount (). chars < 9 7finish 8endif 9 10let s: cpo_save = & cpo 11set cpo & vim 12 13" Unfortunately Spotbugs does not output absolute paths, so you need to 14" pass the directory of the files being checked as `-sourcepath` parameter. 15" The regex, auxpath and glob try to include all dependent classes of the 16" current buffer. See https://github.com/spotbugs/spotbugs/issues/856 17 18" FIXME: When "search()" is used with the "e" flag, it makes no _further_ 19" progress after claiming an EOL match (i.e. "\_" or "\n", but not "$"). 20" XXX: Omit anonymous class declarations 21let s: keywords = '\C\<\%(\.\@1<!class\|@\=interface\|enum\|record\|package\)\%(\s\|$\)' 22let s: type_names = '\C\<\%(\.\@1<!class\|@\=interface\|enum\|record\)\s*\(\K\k*\)\>' 23" Capture ";" for counting a class file directory (see s:package_dir_heads below) 24let s: package_names = '\C\<package\s*\(\K\%(\k*\.\=\)\+;\)' 25let s: package = '' 26 27if has ( 'syntax' ) && exists ( 'g:syntax_on' ) && 28 \exists ( 'b:current_syntax' ) && b: current_syntax == 'java' && 29 \hlexists ( 'javaClassDecl' ) && hlexists ( 'javaExternal' ) 30 31function ! s: GetDeclaredTypeNames () abort 32if bufname () =~# '\<\%(module\|package\)-info\.java\=$' 33return [ expand ( '%:t:r' )] 34endif 35defer execute('silent! normal! g``') 36call cursor ( 1 , 1 ) 37let type_names = [] 38let lnum = search ( s: keywords , 'eW' ) 39while lnum > 0 40let name_attr = synIDattr ( synID ( lnum , ( col ( '.' ) - 1 ), 0 ), 'name' ) 41if name_attr ==# 'javaClassDecl' 42let tokens = matchlist ( getline ( lnum ) .. getline ( lnum + 1 ), s: type_names ) 43if ! empty ( tokens ) |call add ( type_names , tokens [ 1 ]) |endif 44elseif name_attr ==# 'javaExternal' 45let tokens = matchlist ( getline ( lnum ) .. getline ( lnum + 1 ), s: package_names ) 46if ! empty ( tokens ) |let s: package = tokens [ 1 ] |endif 47endif 48let lnum = search ( s: keywords , 'eW' ) 49endwhile 50return type_names 51endfunction 52 53else 54function ! s: GetDeclaredTypeNames () abort 55if bufname () =~# '\<\%(module\|package\)-info\.java\=$' 56return [ expand ( '%:t:r' )] 57endif 58" Undo the unsetting of &hls, see below 59if & hls 60defer execute('set hls') 61endif 62" Possibly restore the current values for registers '"' and "y", see below 63defer call('setreg', ['"', getreg('"'), getregtype('"')]) 64defer call('setreg', ['y', getreg('y'), getregtype('y')]) 65defer execute('silent bwipeout') 66" Copy buffer contents for modification 67silent %y y 68new 69" Apply ":help scratch-buffer" effects and match "$" in Java (generated) 70" type names (see s:type_names) 71setlocal iskeyword += $buftype = nofilebufhidden = hidenoswapfile nohls 720 put y 73" Discard text blocks and strings 74silent keeppatterns %s/\\\@<!"""\_.\{-}\\\@<!"""\|\\"//ge 75silent keeppatterns %s/".*"//ge 76" Discard comments 77silent keeppatterns %s/\/\/.\+$//ge 78silent keeppatterns %s/\/\*\_.\{-}\*\///ge 79call cursor ( 1 , 1 ) 80let type_names = [] 81let lnum = search ( s: keywords , 'eW' ) 82while lnum > 0 83let line = getline ( lnum ) 84if line =~# '\<package\>' 85let tokens = matchlist ( line .. getline ( lnum + 1 ), s: package_names ) 86if ! empty ( tokens ) |let s: package = tokens [ 1 ] |endif 87else 88let tokens = matchlist ( line .. getline ( lnum + 1 ), s: type_names ) 89if ! empty ( tokens ) |call add ( type_names , tokens [ 1 ]) |endif 90endif 91let lnum = search ( s: keywords , 'eW' ) 92endwhile 93return type_names 94endfunction 95endif 96 97if has ( 'win32' ) 98 99function ! s: GlobClassFiles ( src_type_name ) abort 100return glob ( a: src_type_name .. '$*.class' , 1 , 1 ) 101endfunction 102 103else 104function ! s: GlobClassFiles ( src_type_name ) abort 105return glob ( a: src_type_name .. '\$*.class' , 1 , 1 ) 106endfunction 107endif 108 109if exists ( 'b:spotbugs_properties' ) 110" Let "ftplugin/java.vim" merge global entries, if any, in buffer-local 111" entries 112 113function ! s: GetProperty ( name , default ) abort 114return get ( b: spotbugs_properties , a: name , a: default ) 115endfunction 116 117elseif exists ( 'g:spotbugs_properties' ) 118 119function ! s: GetProperty ( name , default ) abort 120return get ( g: spotbugs_properties , a: name , a: default ) 121endfunction 122 123else 124function ! s: GetProperty ( dummy , default ) abort 125return a: default 126endfunction 127endif 128 129if ( exists ( 'g:spotbugs_properties' ) || exists ( 'b:spotbugs_properties' )) && 130 \(( ! empty ( s: GetProperty ( 'sourceDirPath' , [])) && 131 \! empty ( s: GetProperty ( 'classDirPath' , []))) || 132 \( ! empty ( s: GetProperty ( 'testSourceDirPath' , [])) && 133 \! empty ( s: GetProperty ( 'testClassDirPath' , [])))) 134 135function ! s: CommonIdxsAndDirs () abort 136let src_dir_path = s: GetProperty ( 'sourceDirPath' , []) 137let bin_dir_path = s: GetProperty ( 'classDirPath' , []) 138let test_src_dir_path = s: GetProperty ( 'testSourceDirPath' , []) 139let test_bin_dir_path = s: GetProperty ( 'testClassDirPath' , []) 140let dir_cnt = min ([ len ( src_dir_path ), len ( bin_dir_path )]) 141let test_dir_cnt = min ([ len ( test_src_dir_path ), len ( test_bin_dir_path )]) 142" Do not break up path pairs with filtering! 143return [[ range ( dir_cnt ), 144 \src_dir_path [ 0 : dir_cnt - 1 ], 145 \bin_dir_path [ 0 : dir_cnt - 1 ]], 146 \[ range ( test_dir_cnt ), 147 \test_src_dir_path [ 0 : test_dir_cnt - 1 ], 148 \test_bin_dir_path [ 0 : test_dir_cnt - 1 ]]] 149endfunction 150 151let s: common_idxs_and_dirs = s: CommonIdxsAndDirs () 152delfunction s:CommonIdxsAndDirs 153 154function ! s: FindClassFiles ( src_type_name ) abort 155let class_files = [] 156" Match pairwise the components of source and class pathnames 157for [ idxs , src_dirs , bin_dirs ] in s: common_idxs_and_dirs 158" Do not use "fnamemodify(a:src_type_name, ':p:s?src?bin?')" because 159" only the rightmost "src" is looked for 160for idx in idxs 161let tail_idx = strridx ( a: src_type_name , src_dirs [ idx ]) 162" No such directory or no such inner type (i.e. without "$") 163if tail_idx < 0 |continue |endif 164" Substitute "bin_dirs[idx]" for the rightmost "src_dirs[idx]" 165let candidate_type_name = strpart ( a: src_type_name , 0 , tail_idx ) .. 166 \bin_dirs [ idx ] .. 167 \strpart ( a: src_type_name , ( tail_idx + strlen ( src_dirs [ idx ]))) 168for candidate in insert ( s: GlobClassFiles ( candidate_type_name ), 169 \candidate_type_name .. '.class' ) 170if filereadable ( candidate ) |call add ( class_files , shellescape ( candidate )) |endif 171endfor 172if ! empty ( class_files ) |break |endif 173endfor 174if ! empty ( class_files ) |break |endif 175endfor 176return class_files 177endfunction 178 179else 180function ! s: FindClassFiles ( src_type_name ) abort 181let class_files = [] 182for candidate in insert ( s: GlobClassFiles ( a: src_type_name ), 183 \a: src_type_name .. '.class' ) 184if filereadable ( candidate ) |call add ( class_files , shellescape ( candidate )) |endif 185endfor 186return class_files 187endfunction 188endif 189 190if exists ( 'g:spotbugs_alternative_path' ) && 191 \! empty ( get ( g: spotbugs_alternative_path , 'fromPath' , '' )) && 192 \! empty ( get ( g: spotbugs_alternative_path , 'toPath' , '' )) 193 194" See https://github.com/spotbugs/spotbugs/issues/909 195function ! s: ResolveAbsolutePathname () abort 196let pathname = expand ( '%:p' ) 197let head_idx = stridx ( pathname , g: spotbugs_alternative_path . toPath ) 198" No such file: a mismatched path request for a project 199if head_idx < 0 |return pathname |endif 200" Settle for failure with file readability tests _in s:FindClassFiles()_ 201return strpart ( pathname , 0 , head_idx ) .. 202 \g: spotbugs_alternative_path . fromPath .. 203 \strpart ( pathname , ( head_idx + strlen ( g: spotbugs_alternative_path . toPath ))) 204endfunction 205 206else 207function ! s: ResolveAbsolutePathname () abort 208return expand ( '%:p' ) 209endfunction 210endif 211 212function ! s: CollectClassFiles () abort 213" Possibly obtain a symlinked path for an unsupported directory name 214let pathname = s: ResolveAbsolutePathname () 215" Get a platform-independent pathname prefix, cf. "expand('%:p:h')..'/'" 216let tail_idx = strridx ( pathname , expand ( '%:t' )) 217let src_pathname = strpart ( pathname , 0 , tail_idx ) 218let all_class_files = [] 219" Get all type names in the current buffer and let the filename globbing 220" discover inner type names from arbitrary type names 221for type_name in s: GetDeclaredTypeNames () 222call extend ( all_class_files , s: FindClassFiles ( src_pathname .. type_name )) 223endfor 224return all_class_files 225endfunction 226 227" Expose class files for removal etc. 228let b: spotbugs_class_files = s: CollectClassFiles () 229let s: package_dir_heads = repeat ( ':h' , ( 1 + strlen ( substitute ( s: package , '[^.;]' , '' , 'g' )))) 230let s: package_root_dir = fnamemodify ( s: ResolveAbsolutePathname (), s: package_dir_heads .. ':S' ) 231let g: current_compiler = 'spotbugs' 232" CompilerSet makeprg=spotbugs 233let & l: makeprg = 'spotbugs' .. ( has ( 'win32' ) ? '.bat' : '' ) .. ' ' .. 234 \get ( b:, 'spotbugs_makeprg_params' , get ( g:, 'spotbugs_makeprg_params' , '-workHard -experimental' )) .. 235 \' -textui -emacs -auxclasspath ' .. s: package_root_dir .. ' -sourcepath ' .. s: package_root_dir .. ' ' .. 236 \join ( b: spotbugs_class_files , ' ' ) 237" Emacs expects doubled line numbers 238setlocal errorformat = %f:%l:%*[0-9]\ %m,%f:-%*[0-9]:-%*[0-9]\ %m 239 240" " This compiler is meant to be used for a single buffer only 241" exe 'CompilerSet makeprg='..escape(&l:makeprg, ' \|"') 242" exe 'CompilerSet errorformat='..escape(&l:errorformat, ' \|"') 243 244delfunction s:CollectClassFiles 245delfunction s:ResolveAbsolutePathname 246delfunction s:FindClassFiles 247delfunction s:GetProperty 248delfunction s:GlobClassFiles 249delfunction s:GetDeclaredTypeNames 250let & cpo = s: cpo_save 251unlet ! s: package_root_dir s: package_dir_heads s: common_idxs_and_dirs s: package 252unlet ! s: package_names s: type_names s: keywords s: cpo_save 253 254" vim: set foldmethod=syntax shiftwidth=2 expandtab: