Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " Vim filetype plugin file |
Doug Kearns | 93197fd | 2024-01-14 20:59:02 +0100 | [diff] [blame] | 2 | " Language: Java |
Aliaksei Budavei | 4052474 | 2024-04-14 19:57:00 +0300 | [diff] [blame] | 3 | " Maintainer: Aliaksei Budavei <0x000c70 AT gmail DOT com> |
| 4 | " Former Maintainer: Dan Sharp |
| 5 | " Repository: https://github.com/zzzyxwvut/java-vim.git |
Konfekt | 65311c6 | 2024-11-28 21:06:09 +0100 | [diff] [blame] | 6 | " Last Change: 2024 Nov 24 |
Doug Kearns | 93197fd | 2024-01-14 20:59:02 +0100 | [diff] [blame] | 7 | " 2024 Jan 14 by Vim Project (browsefilter) |
Riley Bruins | 0a08306 | 2024-06-03 20:40:45 +0200 | [diff] [blame] | 8 | " 2024 May 23 by Riley Bruins <ribru17@gmail.com> ('commentstring') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10 | " Make sure the continuation lines below do not cause problems in |
| 11 | " compatibility mode. |
| 12 | let s:save_cpo = &cpo |
| 13 | set cpo-=C |
| 14 | |
Aliaksei Budavei | 85f054a | 2024-09-30 19:40:04 +0200 | [diff] [blame] | 15 | if (exists("g:java_ignore_javadoc") || exists("g:java_ignore_markdown")) && |
| 16 | \ exists("*javaformat#RemoveCommonMarkdownWhitespace") |
| 17 | delfunction javaformat#RemoveCommonMarkdownWhitespace |
| 18 | unlet! g:loaded_javaformat |
| 19 | endif |
| 20 | |
| 21 | if exists("b:did_ftplugin") |
| 22 | let &cpo = s:save_cpo |
| 23 | unlet s:save_cpo |
| 24 | finish |
| 25 | endif |
| 26 | |
| 27 | let b:did_ftplugin = 1 |
| 28 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 29 | " For filename completion, prefer the .java extension over the .class |
| 30 | " extension. |
| 31 | set 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. |
| 35 | setlocal includeexpr=substitute(v:fname,'\\.','/','g') |
| 36 | setlocal suffixesadd=.java |
Aliaksei Budavei | 36e667a | 2024-04-18 23:01:52 +0200 | [diff] [blame] | 37 | |
| 38 | " Clean up in case this file is sourced again. |
| 39 | unlet! s:zip_func_upgradable |
| 40 | |
Aliaksei Budavei | 85f054a | 2024-09-30 19:40:04 +0200 | [diff] [blame] | 41 | """" STRIVE TO REMAIN COMPATIBLE FOR AT LEAST VIM 7.0. |
| 42 | |
Aliaksei Budavei | 36e667a | 2024-04-18 23:01:52 +0200 | [diff] [blame] | 43 | " Documented in ":help ft-java-plugin". |
| 44 | if 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 69 | endif |
| 70 | |
| 71 | " Set 'formatoptions' to break comment lines but not other lines, |
| 72 | " and insert the comment leader when hitting <CR> or using "o". |
| 73 | setlocal formatoptions-=t formatoptions+=croql |
| 74 | |
Aliaksei Budavei | 85f054a | 2024-09-30 19:40:04 +0200 | [diff] [blame] | 75 | " Set 'comments' to format Markdown Javadoc comments and dashed lists |
| 76 | " in other multi-line comments (it behaves just like C). |
| 77 | setlocal comments& comments^=:///,sO:*\ -,mO:*\ \ ,exO:*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 78 | |
Riley Bruins | 0a08306 | 2024-06-03 20:40:45 +0200 | [diff] [blame] | 79 | setlocal commentstring=//\ %s |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 80 | |
| 81 | " Change the :browse e filter to primarily show Java-related files. |
Doug Kearns | 93197fd | 2024-01-14 20:59:02 +0100 | [diff] [blame] | 82 | if (has("gui_win32") || has("gui_gtk")) && !exists("b:browsefilter") |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 83 | let b:browsefilter="Java Files (*.java)\t*.java\n" . |
| 84 | \ "Properties Files (*.prop*)\t*.prop*\n" . |
Doug Kearns | 93197fd | 2024-01-14 20:59:02 +0100 | [diff] [blame] | 85 | \ "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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 91 | endif |
| 92 | |
Konfekt | 65311c6 | 2024-11-28 21:06:09 +0100 | [diff] [blame] | 93 | " The support for pre- and post-compiler actions for SpotBugs. |
| 94 | if 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 |
| 106 | endif |
| 107 | |
| 108 | if 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 |
| 201 | endif |
| 202 | |
| 203 | function! 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> |
| 210 | endfunction |
| 211 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 212 | " Undo the stuff we changed. |
Konfekt | 65311c6 | 2024-11-28 21:06:09 +0100 | [diff] [blame] | 213 | let b:undo_ftplugin = 'call JavaFileTypeCleanUp() | delfunction JavaFileTypeCleanUp' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 214 | |
Aliaksei Budavei | 36e667a | 2024-04-18 23:01:52 +0200 | [diff] [blame] | 215 | " See ":help vim9-mix". |
| 216 | if !has("vim9script") |
| 217 | let &cpo = s:save_cpo |
| 218 | unlet s:save_cpo |
| 219 | finish |
| 220 | endif |
| 221 | |
| 222 | if 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< |
| 232 | endif |
| 233 | |
Konfekt | 65311c6 | 2024-11-28 21:06:09 +0100 | [diff] [blame] | 234 | if 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 |
| 245 | endif |
| 246 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 247 | " Restore the saved compatibility options. |
| 248 | let &cpo = s:save_cpo |
Bram Moolenaar | 84f7235 | 2012-03-11 15:57:40 +0100 | [diff] [blame] | 249 | unlet s:save_cpo |
Aliaksei Budavei | 85f054a | 2024-09-30 19:40:04 +0200 | [diff] [blame] | 250 | " vim: fdm=syntax sw=4 ts=8 noet sta |