1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
"============================================================================
"File:        javac.vim
"Description: Syntax checking plugin for syntastic.vim
"Maintainer:  Jochen Keil <jochen.keil at gmail dot com>
"             Dmitry Geurkov <d.geurkov at gmail dot com>
"License:     This program is free software. It comes without any warranty,
"             to the extent permitted by applicable law. You can redistribute
"             it and/or modify it under the terms of the Do What The Fuck You
"             Want To Public License, Version 2, as published by Sam Hocevar.
"             See http://sam.zoy.org/wtfpl/COPYING for more details.
"============================================================================

if exists('g:loaded_syntastic_java_javac_checker')
    finish
endif
let g:loaded_syntastic_java_javac_checker = 1
let g:syntastic_java_javac_maven_pom_tags = ['build', 'properties']
let g:syntastic_java_javac_maven_pom_properties = {}
let s:has_maven = 0

let s:save_cpo = &cpo
set cpo&vim

" Checker options {{{1

if !exists('g:syntastic_java_javac_executable')
    let g:syntastic_java_javac_executable = 'javac'
endif

if !exists('g:syntastic_java_maven_executable')
    let g:syntastic_java_maven_executable = 'mvn'
endif

if !exists('g:syntastic_java_javac_options')
    let g:syntastic_java_javac_options = '-Xlint'
endif

if !exists('g:syntastic_java_maven_options')
    let g:syntastic_java_maven_options = ''
endif

if !exists('g:syntastic_java_javac_classpath')
    let g:syntastic_java_javac_classpath = ''
endif

if !exists('g:syntastic_java_javac_delete_output')
    let g:syntastic_java_javac_delete_output = 1
endif

if !exists('g:syntastic_java_javac_autoload_maven_classpath')
    let g:syntastic_java_javac_autoload_maven_classpath = 1
endif

if !exists('g:syntastic_java_javac_config_file_enabled')
    let g:syntastic_java_javac_config_file_enabled = 0
endif

if !exists('g:syntastic_java_javac_config_file')
    let g:syntastic_java_javac_config_file = '.syntastic_javac_config'
endif

if !exists('g:syntastic_java_javac_custom_classpath_command')
    let g:syntastic_java_javac_custom_classpath_command = ''
endif

if !exists('g:syntastic_java_javac_maven_pom_ftime')
    let g:syntastic_java_javac_maven_pom_ftime = {}
endif

if !exists('g:syntastic_java_javac_maven_pom_classpath')
    let g:syntastic_java_javac_maven_pom_classpath = {}
endif

" }}}1

" Constants {{{1

let s:_FILE_SHORTCUTS = {
        \ '%FILE_PATH%':  '%:p',
        \ '%FILE_NAME%':  '%:t',
        \ '%FILE_DIR%':   '%:p:h',
    \ }
lockvar! s:_FILE_SHORTCUTS

" }}}1

" Commands {{{1

command! SyntasticJavacEditClasspath call s:EditClasspath()
command! SyntasticJavacEditConfig    call s:EditConfig()

" }}}1

function! SyntaxCheckers_java_javac_IsAvailable() dict " {{{1
    let s:has_maven = executable(expand(g:syntastic_java_maven_executable, 1))
    return executable(expand(g:syntastic_java_javac_executable, 1))
endfunction " }}}1

function! SyntaxCheckers_java_javac_GetLocList() dict " {{{1
    let javac_opts = g:syntastic_java_javac_options

    let output_dir = ''
    if g:syntastic_java_javac_delete_output
        let output_dir = syntastic#util#tmpdir()
        let javac_opts .= ' -d ' . syntastic#util#shescape(output_dir)
    endif

    " load classpath from config file
    if g:syntastic_java_javac_config_file_enabled
        call s:LoadConfigFile()
    endif


    " add classpathes to javac_classpath {{{2
    let javac_classpath = ''

    for path in split(g:syntastic_java_javac_classpath, s:ClassSep())
        if path !=# ''
            try
                let ps = glob(path, 1, 1)
            catch
                let ps = split(glob(path, 1), "\n")
            endtry
            if type(ps) == type([])
                for p in ps
                    let javac_classpath = s:AddToClasspath(javac_classpath, p)
                endfor
            else
                let javac_classpath = s:AddToClasspath(javac_classpath, ps)
            endif
        endif
    endfor

    if s:has_maven && g:syntastic_java_javac_autoload_maven_classpath
        if !g:syntastic_java_javac_delete_output
            let javac_opts .= ' -d ' . syntastic#util#shescape(s:MavenOutputDirectory())
        endif
        let javac_classpath = s:AddToClasspath(javac_classpath, s:GetMavenClasspath())
    endif
    " }}}2

    " load custom classpath {{{2
    if g:syntastic_java_javac_custom_classpath_command !=# ''
        " Pre-process the classpath command string a little.
        let classpath_command = g:syntastic_java_javac_custom_classpath_command
        for [key, val] in items(s:_FILE_SHORTCUTS)
            let classpath_command = substitute(classpath_command, '\V' . key, syntastic#util#shexpand(val), 'g')
        endfor
        let lines = syntastic#util#system(classpath_command)
        if syntastic#util#isRunningWindows() || has('win32unix')
            let lines = substitute(lines, "\r\n", "\n", 'g')
        endif
        for l in split(lines, "\n")
            let javac_classpath = s:AddToClasspath(javac_classpath, l)
        endfor
    endif

    if javac_classpath !=# ''
        let javac_opts .= ' -cp ' . syntastic#util#shexpand(javac_classpath)
    endif
    " }}}2

    let fname = expand('%:p:h', 1) . syntastic#util#Slash() . expand ('%:t', 1)

    if has('win32unix')
        let fname = syntastic#util#CygwinPath(fname)
    endif

    let makeprg = self.makeprgBuild({
        \ 'args': javac_opts,
        \ 'fname': syntastic#util#shescape(fname) })

    " unashamedly stolen from *errorformat-javac* (quickfix.txt) and modified to include error types
    let errorformat =
        \ '%E%f:%l: error: %m,'.
        \ '%W%f:%l: warning: %m,'.
        \ '%E%f:%l: %m,'.
        \ '%Z%p^,'.
        \ '%-G%.%#'

    if output_dir !=# ''
        silent! call mkdir(output_dir, 'p')
    endif
    let errors = SyntasticMake({
        \ 'makeprg': makeprg,
        \ 'errorformat': errorformat,
        \ 'postprocess': ['cygwinRemoveCR'] })

    if output_dir !=# ''
        call syntastic#util#rmrf(output_dir)
    endif
    return errors

endfunction " }}}1

" Utilities {{{1

function! s:RemoveCarriageReturn(line) " {{{2
    return substitute(a:line, "\r", '', 'g')
endfunction " }}}2

function! s:ClassSep() " {{{2
    return (syntastic#util#isRunningWindows() || has('win32unix')) ? ';' : ':'
endfunction " }}}2

function! s:AddToClasspath(classpath, path) " {{{2
    if a:path ==# ''
        return a:classpath
    endif
    return (a:classpath !=# '') ? a:classpath . s:ClassSep() . a:path : a:path
endfunction " }}}2

function! s:SplitClasspath(classpath) " {{{2
    return split(a:classpath, s:ClassSep())
endfunction " }}}2

function! s:LoadConfigFile() " {{{2
    if filereadable(expand(g:syntastic_java_javac_config_file, 1))
        execute 'source ' . fnameescape(expand(g:syntastic_java_javac_config_file, 1))
    endif
endfunction " }}}2

function! s:SaveClasspath() " {{{2
    " build classpath from lines
    let path = ''
    let lines = getline(1, line('$'))
    for l in lines
        let path = s:AddToClasspath(path, l)
    endfor
    " save classpath to config file
    if g:syntastic_java_javac_config_file_enabled
        if filereadable(expand(g:syntastic_java_javac_config_file, 1))
            " load lines from config file
            let lines = readfile(expand(g:syntastic_java_javac_config_file, 1))
            " strip g:syntastic_java_javac_classpath options from config file lines
            let i = 0
            while i < len(lines)
                if match(lines[i], 'g:syntastic_java_javac_classpath') != -1
                    call remove(lines, i)
                else
                    let i += 1
                endif
            endwhile
        else
            let lines = []
        endif
        " add new g:syntastic_java_javac_classpath option to config
        call add(lines, 'let g:syntastic_java_javac_classpath = ' . string(path))
        " save config file lines
        call writefile(lines, expand(g:syntastic_java_javac_config_file, 1))
    endif
    " set new classpath
    let g:syntastic_java_javac_classpath = path
    let &modified = 0
endfunction " }}}2

function! s:EditClasspath() " {{{2
    let command = 'syntastic javac classpath'
    let winnr = bufwinnr('^' . command . '$')
    if winnr < 0
        let path = []
        let pathlines = split(g:syntastic_java_javac_classpath, "\n")
        for p in pathlines
            call extend(path, s:SplitClasspath(p))
        endfor
        execute (len(path) + 5) . 'sp ' . fnameescape(command)

        augroup syntastic
            autocmd BufWriteCmd <buffer> call s:SaveClasspath() | bwipeout
        augroup END

        setlocal buftype=acwrite bufhidden=wipe nobuflisted noswapfile nowrap number
        for p in path
            call append(line('$') - 1, p)
        endfor
        let &modified = 0
    else
        execute winnr . 'wincmd w'
    endif
endfunction " }}}2

function! s:SaveConfig() " {{{2
    " get lines
    let lines = getline(1, line('$'))
    if g:syntastic_java_javac_config_file_enabled
        " save config file lines
        call writefile(lines, expand(g:syntastic_java_javac_config_file, 1))
    endif
    let &modified = 0
endfunction " }}}2

function! s:EditConfig() " {{{2
    if !g:syntastic_java_javac_config_file_enabled
        return
    endif

    let command = 'syntastic javac config'
    let winnr = bufwinnr('^' . command . '$')
    if winnr < 0
        let lines = []
        if filereadable(expand(g:syntastic_java_javac_config_file, 1))
            let lines = readfile(expand(g:syntastic_java_javac_config_file, 1))
        endif
        execute (len(lines) + 5) . 'sp ' . fnameescape(command)

        augroup syntastic
            autocmd BufWriteCmd <buffer> call s:SaveConfig() | bwipeout
        augroup END

        setlocal ft=vim buftype=acwrite bufhidden=wipe nobuflisted noswapfile nowrap number
        for l in lines
            call append(line('$') - 1, l)
        endfor
        let &modified = 0
    else
        execute winnr . 'wincmd w'
    endif
endfunction " }}}2

function! s:GetMavenProperties() " {{{2
    let mvn_properties = {}
    let pom = syntastic#util#findFileInParent('pom.xml', expand('%:p:h', 1))
    if s:has_maven && filereadable(pom)
        if !has_key(g:syntastic_java_javac_maven_pom_properties, pom)
            let mvn_cmd = syntastic#util#shexpand(g:syntastic_java_maven_executable) .
                \ ' -f ' . syntastic#util#shescape(pom) .
                \ ' ' . g:syntastic_java_maven_options
            let mvn_is_managed_tag = 1
            let mvn_settings_output = split(syntastic#util#system(mvn_cmd . ' help:effective-pom'), "\n")
            let current_path = 'project'
            for line in mvn_settings_output
                let matches = matchlist(line, '\m^\s*<\([a-zA-Z0-9\-\.]\+\)>\s*$')
                if mvn_is_managed_tag && !empty(matches)
                    let mvn_is_managed_tag = index(g:syntastic_java_javac_maven_pom_tags, matches[1]) >= 0
                    let current_path .= '.' . matches[1]
                else
                    let matches = matchlist(line, '\m^\s*</\([a-zA-Z0-9\-\.]\+\)>\s*$')
                    if !empty(matches)
                        let mvn_is_managed_tag = index(g:syntastic_java_javac_maven_pom_tags, matches[1]) < 0
                        let current_path  = substitute(current_path, '\m\.' . matches[1] . '$', '', '')
                    else
                        let matches = matchlist(line, '\m^\s*<\([a-zA-Z0-9\-\.]\+\)>\(.\+\)</[a-zA-Z0-9\-\.]\+>\s*$')
                        if mvn_is_managed_tag && !empty(matches)
                            let mvn_properties[current_path . '.' . matches[1]] = matches[2]
                        endif
                    endif
                endif
            endfor
            let g:syntastic_java_javac_maven_pom_properties[pom] = mvn_properties
        endif
        return g:syntastic_java_javac_maven_pom_properties[pom]
    endif
    return mvn_properties
endfunction " }}}2

function! s:GetMavenClasspath() " {{{2
    let pom = syntastic#util#findFileInParent('pom.xml', expand('%:p:h', 1))
    if s:has_maven && filereadable(pom)
        if !has_key(g:syntastic_java_javac_maven_pom_ftime, pom) || g:syntastic_java_javac_maven_pom_ftime[pom] != getftime(pom)
            let mvn_cmd = syntastic#util#shexpand(g:syntastic_java_maven_executable) .
                \ ' -f ' . syntastic#util#shescape(pom) .
                \ ' ' . g:syntastic_java_maven_options
            let mvn_classpath_output = split(syntastic#util#system(mvn_cmd . ' dependency:build-classpath -DincludeScope=test'), "\n")
            let mvn_classpath = ''
            let class_path_next = 0

            for line in mvn_classpath_output
                if class_path_next == 1
                    let mvn_classpath = s:RemoveCarriageReturn(line)
                    break
                endif
                if stridx(line, 'Dependencies classpath:') >= 0
                    let class_path_next = 1
                endif
            endfor

            let mvn_properties = s:GetMavenProperties()

            let sep = syntastic#util#Slash()
            let output_dir = get(mvn_properties, 'project.build.outputDirectory', join(['target', 'classes'], sep))
            let mvn_classpath = s:AddToClasspath(mvn_classpath, output_dir)

            let test_output_dir = get(mvn_properties, 'project.build.testOutputDirectory', join(['target', 'test-classes'], sep))
            let mvn_classpath = s:AddToClasspath(mvn_classpath, test_output_dir)

            let g:syntastic_java_javac_maven_pom_ftime[pom] = getftime(pom)
            let g:syntastic_java_javac_maven_pom_classpath[pom] = mvn_classpath
        endif
        return g:syntastic_java_javac_maven_pom_classpath[pom]
    endif
    return ''
endfunction " }}}2

function! s:MavenOutputDirectory() " {{{2
    let pom = syntastic#util#findFileInParent('pom.xml', expand('%:p:h', 1))
    if s:has_maven && filereadable(pom)
        let mvn_properties = s:GetMavenProperties()
        let output_dir = get(mvn_properties, 'project.properties.build.dir', getcwd())

        let sep = syntastic#util#Slash()
        let src_main_dir = get(mvn_properties, 'project.build.sourceDirectory', join(['src', 'main', 'java'], sep))
        let src_test_dir = get(mvn_properties, 'project.build.testsourceDirectory', join(['src', 'test', 'java'], sep))
        if stridx(expand('%:p:h', 1), src_main_dir) >= 0
            let output_dir = get(mvn_properties, 'project.build.outputDirectory', join ([output_dir, 'target', 'classes'], sep))
        endif
        if stridx(expand('%:p:h', 1), src_test_dir) >= 0
            let output_dir = get(mvn_properties, 'project.build.testOutputDirectory', join([output_dir, 'target', 'test-classes'], sep))
        endif

        if has('win32unix')
            let output_dir = syntastic#util#CygwinPath(output_dir)
        endif
        return output_dir
    endif
    return '.'
endfunction " }}}2

" }}}1

call g:SyntasticRegistry.CreateAndRegisterChecker({
    \ 'filetype': 'java',
    \ 'name': 'javac'})

let &cpo = s:save_cpo
unlet s:save_cpo

" vim: set sw=4 sts=4 et fdm=marker: