robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f618cdf9c4
master
1" Default pre- and post-compiler actions and commands for SpotBugs 2" Maintainers: @konfekt and @zzzyxwvut 3" Last Change: 2024 Dec 08 4 5let s: save_cpo = & cpo 6set cpo & vim 7 8" Look for the setting of "g:spotbugs#state" in "ftplugin/java.vim". 9let s: state = get ( g:, 'spotbugs#state' , {}) 10let s: commands = get ( s: state , 'commands' , {}) 11let s: compiler = get ( s: state , 'compiler' , '' ) 12let s: readable = filereadable ( $ VIMRUNTIME . '/compiler/' . s: compiler . '.vim' ) 13 14if has_key ( s: commands , 'DefaultPreCompilerCommand' ) 15let g: SpotBugsPreCompilerCommand = s: commands . DefaultPreCompilerCommand 16else 17 18function ! s: DefaultPreCompilerCommand ( arguments ) abort 19execute 'make ' . a: arguments 20cc 21endfunction 22 23let g: SpotBugsPreCompilerCommand = function ( 's:DefaultPreCompilerCommand' ) 24endif 25 26if has_key ( s: commands , 'DefaultPreCompilerTestCommand' ) 27let g: SpotBugsPreCompilerTestCommand = s: commands . DefaultPreCompilerTestCommand 28else 29 30function ! s: DefaultPreCompilerTestCommand ( arguments ) abort 31execute 'make ' . a: arguments 32cc 33endfunction 34 35let g: SpotBugsPreCompilerTestCommand = function ( 's:DefaultPreCompilerTestCommand' ) 36endif 37 38if has_key ( s: commands , 'DefaultPostCompilerCommand' ) 39let g: SpotBugsPostCompilerCommand = s: commands . DefaultPostCompilerCommand 40else 41 42function ! s: DefaultPostCompilerCommand ( arguments ) abort 43execute 'make ' . a: arguments 44endfunction 45 46let g: SpotBugsPostCompilerCommand = function ( 's:DefaultPostCompilerCommand' ) 47endif 48 49if v: version > 900 || has ( 'nvim' ) 50 51function ! spotbugs#DeleteClassFiles () abort 52if ! exists ( 'b:spotbugs_class_files' ) 53return 54endif 55 56for pathname in b: spotbugs_class_files 57let classname = pathname =~# "^'.\\+\\.class'$" 58 \? eval ( pathname ) 59 \: pathname 60 61if classname =~# '\.class$' && filereadable ( classname ) 62" Since v9.0.0795. 63let octad = readblob ( classname , 0 , 8 ) 64 65" Test the magic number and the major version number (45 for v1.0). 66" Since v9.0.2027. 67if len ( octad ) == 8 && octad [ 0 : 3 ] == 0 zcafe.babe && 68" Nvim: no << operator 69"\ or((octad[6] << 8), octad[7]) >= 45 70 \or ((octad[6] * 256), octad[7]) >= 45 71echomsg printf ( 'Deleting %s: %d' , classname , delete ( classname )) 72endif 73endif 74endfor 75 76let b: spotbugs_class_files = [] 77endfunction 78 79else 80 81function ! s: DeleteClassFilesWithNewLineCodes ( classname ) abort 82" The distribution of "0a"s in class file versions 2560 and 2570: 83" 84" 0zca.fe.ba.be.00.00.0a.00 0zca.fe.ba.be.00.00.0a.0a 85" 0zca.fe.ba.be.00.0a.0a.00 0zca.fe.ba.be.00.0a.0a.0a 86" 0zca.fe.ba.be.0a.00.0a.00 0zca.fe.ba.be.0a.00.0a.0a 87" 0zca.fe.ba.be.0a.0a.0a.00 0zca.fe.ba.be.0a.0a.0a.0a 88let numbers = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] 89let offset = 0 90let lines = readfile ( a: classname , 'b' , 4 ) 91 92" Track NL byte counts to handle files of less than 8 bytes. 93let nl_cnt = len ( lines ) 94" Track non-NL byte counts for "0zca.fe.ba.be.0a.0a.0a.0a". 95let non_nl_cnt = 0 96 97for line in lines 98for idx in range ( strlen ( line )) 99" Remap NLs to Nuls. 100let numbers [ offset ] = ( line [ idx ] == "\n" ) ? 0 : char2nr ( line [ idx ]) % 256 101let non_nl_cnt += 1 102let offset += 1 103 104if offset > 7 105break 106endif 107endfor 108 109let nl_cnt -= 1 110 111if offset > 7 || ( nl_cnt < 1 && non_nl_cnt > 4 ) 112break 113endif 114 115" Reclaim NLs. 116let numbers [ offset ] = 10 117let offset += 1 118 119if offset > 7 120break 121endif 122endfor 123 124" Test the magic number and the major version number (45 for v1.0). 125if offset > 7 && numbers [ 0 ] == 0xca && numbers [ 1 ] == 0xfe && 126 \numbers [ 2 ] == 0xba && numbers [ 3 ] == 0xbe && 127 \( numbers [ 6 ] * 256 + numbers [ 7 ]) >= 45 128echomsg printf ( 'Deleting %s: %d' , a: classname , delete ( a: classname )) 129endif 130endfunction 131 132function ! spotbugs#DeleteClassFiles () abort 133if ! exists ( 'b:spotbugs_class_files' ) 134return 135endif 136 137let encoding = & encoding 138 139try 140set encoding = latin1 141 142for pathname in b: spotbugs_class_files 143let classname = pathname =~# "^'.\\+\\.class'$" 144 \? eval ( pathname ) 145 \: pathname 146 147if classname =~# '\.class$' && filereadable ( classname ) 148let line = get ( readfile ( classname , 'b' , 1 ), 0 , '' ) 149let length = strlen ( line ) 150 151" Test the magic number and the major version number (45 for v1.0). 152if length > 3 && line [ 0 : 3 ] == "\xca\xfe\xba\xbe" 153if length > 7 && (( line [ 6 ] == "\n" ? 0 : char2nr ( line [ 6 ]) % 256 ) * 256 + 154 \( line [ 7 ] == "\n" ? 0 : char2nr ( line [ 7 ]) % 256 )) >= 45 155echomsg printf ( 'Deleting %s: %d' , classname , delete ( classname )) 156else 157call s: DeleteClassFilesWithNewLineCodes ( classname ) 158endif 159endif 160endif 161endfor 162finally 163let & encoding = encoding 164endtry 165 166let b: spotbugs_class_files = [] 167endfunction 168 169endif 170 171function ! spotbugs#DefaultPostCompilerAction () abort 172" Since v7.4.191. 173call call ( g: SpotBugsPostCompilerCommand , [ '%:S' ]) 174endfunction 175 176if s: readable && s: compiler ==# 'maven' && executable ( 'mvn' ) 177 178function ! spotbugs#DefaultPreCompilerAction () abort 179call spotbugs#DeleteClassFiles () 180compiler maven 181call call ( g: SpotBugsPreCompilerCommand , [ 'compile' ]) 182endfunction 183 184function ! spotbugs#DefaultPreCompilerTestAction () abort 185call spotbugs#DeleteClassFiles () 186compiler maven 187call call ( g: SpotBugsPreCompilerTestCommand , [ 'test-compile' ]) 188endfunction 189 190function ! spotbugs#DefaultProperties () abort 191return { 192 \'PreCompilerAction' : 193 \function ( 'spotbugs#DefaultPreCompilerAction' ), 194 \'PreCompilerTestAction' : 195 \function ( 'spotbugs#DefaultPreCompilerTestAction' ), 196 \'PostCompilerAction' : 197 \function ( 'spotbugs#DefaultPostCompilerAction' ), 198 \'sourceDirPath' : [ 'src/main/java' ], 199 \'classDirPath' : [ 'target/classes' ], 200 \'testSourceDirPath' : [ 'src/test/java' ], 201 \'testClassDirPath' : [ 'target/test-classes' ], 202 \} 203endfunction 204 205unlet s: readable s: compiler 206elseif s: readable && s: compiler ==# 'ant' && executable ( 'ant' ) 207 208function ! spotbugs#DefaultPreCompilerAction () abort 209call spotbugs#DeleteClassFiles () 210compiler ant 211call call ( g: SpotBugsPreCompilerCommand , [ 'compile' ]) 212endfunction 213 214function ! spotbugs#DefaultPreCompilerTestAction () abort 215call spotbugs#DeleteClassFiles () 216compiler ant 217call call ( g: SpotBugsPreCompilerTestCommand , [ 'compile-test' ]) 218endfunction 219 220function ! spotbugs#DefaultProperties () abort 221return { 222 \'PreCompilerAction' : 223 \function ( 'spotbugs#DefaultPreCompilerAction' ), 224 \'PreCompilerTestAction' : 225 \function ( 'spotbugs#DefaultPreCompilerTestAction' ), 226 \'PostCompilerAction' : 227 \function ( 'spotbugs#DefaultPostCompilerAction' ), 228 \'sourceDirPath' : [ 'src' ], 229 \'classDirPath' : [ 'build/classes' ], 230 \'testSourceDirPath' : [ 'test' ], 231 \'testClassDirPath' : [ 'build/test/classes' ], 232 \} 233endfunction 234 235unlet s: readable s: compiler 236elseif s: readable && s: compiler ==# 'javac' && executable ( 'javac' ) 237let s: filename = tempname () 238 239function ! spotbugs#DefaultPreCompilerAction () abort 240call spotbugs#DeleteClassFiles () 241compiler javac 242 243if get ( b:, 'javac_makeprg_params' , get ( g:, 'javac_makeprg_params' , '' )) =~ '\s@\S' 244" Only read options and filenames from @options [@sources ...] and do 245" not update these files when filelists change. 246call call ( g: SpotBugsPreCompilerCommand , [ '' ]) 247else 248" Collect filenames so that Javac can figure out what to compile. 249let filelist = [] 250 251for arg_num in range ( argc ( -1 )) 252let arg_name = argv ( arg_num ) 253 254if arg_name =~# '\.java\=$' 255call add ( filelist , fnamemodify ( arg_name , ':p:S' )) 256endif 257endfor 258 259for buf_num in range ( 1 , bufnr ( '$' )) 260if ! buflisted ( buf_num ) 261continue 262endif 263 264let buf_name = bufname ( buf_num ) 265 266if buf_name =~# '\.java\=$' 267let buf_name = fnamemodify ( buf_name , ':p:S' ) 268 269if index ( filelist , buf_name ) < 0 270call add ( filelist , buf_name ) 271endif 272endif 273endfor 274 275noautocmd call writefile(filelist, s:filename) 276call call ( g: SpotBugsPreCompilerCommand , [ shellescape ( '@' . s: filename )]) 277endif 278endfunction 279 280function ! spotbugs#DefaultPreCompilerTestAction () abort 281call spotbugs#DefaultPreCompilerAction () 282endfunction 283 284function ! spotbugs#DefaultProperties () abort 285return { 286 \'PreCompilerAction' : 287 \function ( 'spotbugs#DefaultPreCompilerAction' ), 288 \'PostCompilerAction' : 289 \function ( 'spotbugs#DefaultPostCompilerAction' ), 290 \} 291endfunction 292 293unlet s: readable s: compiler g: SpotBugsPreCompilerTestCommand 294delfunction ! s:DefaultPreCompilerTestCommand 295else 296 297function ! spotbugs#DefaultPreCompilerAction () abort 298echomsg printf ( 'Not supported: "%s"' , s: compiler ) 299endfunction 300 301function ! spotbugs#DefaultPreCompilerTestAction () abort 302call spotbugs#DefaultPreCompilerAction () 303endfunction 304 305function ! spotbugs#DefaultProperties () abort 306return {} 307endfunction 308 309" XXX: Keep "s:compiler" around for "spotbugs#DefaultPreCompilerAction()", 310" "s:DefaultPostCompilerCommand" -- "spotbugs#DefaultPostCompilerAction()". 311unlet s: readable g: SpotBugsPreCompilerCommand g: SpotBugsPreCompilerTestCommand 312delfunction ! s:DefaultPreCompilerCommand 313delfunction ! s:DefaultPreCompilerTestCommand 314endif 315 316function ! s: DefineBufferAutocmd ( event , ...) abort 317if ! exists ( '#java_spotbugs#User' ) 318return 1 319endif 320 321for l: event in insert ( copy ( a: 000 ), a: event ) 322if l: event != 'User' 323execute printf ( 'silent! autocmd! java_spotbugs %s <buffer>' , l: event ) 324execute printf ( 'autocmd java_spotbugs %s <buffer> doautocmd User' , l: event ) 325endif 326endfor 327 328return 0 329endfunction 330 331function ! s: RemoveBufferAutocmd ( event , ...) abort 332if ! exists ( '#java_spotbugs' ) 333return 1 334endif 335 336for l: event in insert ( copy ( a: 000 ), a: event ) 337if l: event != 'User' 338execute printf ( 'silent! autocmd! java_spotbugs %s <buffer>' , l: event ) 339endif 340endfor 341 342return 0 343endfunction 344 345" Documented in ":help compiler-spotbugs". 346command ! -bar -nargs = + -complete = eventSpotBugsDefineBufferAutocmd 347 \ calls: DefineBufferAutocmd(<f-args>) 348command ! -bar -nargs=+ -complete=eventSpotBugsRemoveBufferAutocmd 349 \ calls: RemoveBufferAutocmd(<f-args>) 350 351let & cpo = s: save_cpo 352unlet s: commands s: state s: save_cpo 353 354" vim: set foldmethod=syntax shiftwidth=2 expandtab: