2017-02-11 17:02:38 -05:00
|
|
|
" Author: farenjihn <farenjihn@gmail.com>, w0rp <devw0rp@gmail.com>
|
2017-02-11 16:29:48 -05:00
|
|
|
" Description: Lints java files using javac
|
|
|
|
|
2017-04-25 17:32:48 -04:00
|
|
|
let s:classpath_sep = has('unix') ? ':' : ';'
|
|
|
|
|
2017-02-11 17:02:38 -05:00
|
|
|
let g:ale_java_javac_options = get(g:, 'ale_java_javac_options', '')
|
|
|
|
let g:ale_java_javac_classpath = get(g:, 'ale_java_javac_classpath', '')
|
2017-02-11 16:29:48 -05:00
|
|
|
|
2017-04-25 17:32:48 -04:00
|
|
|
function! ale_linters#java#javac#GetImportPaths(buffer) abort
|
|
|
|
let l:pom_path = ale#path#FindNearestFile(a:buffer, 'pom.xml')
|
|
|
|
|
|
|
|
if !empty(l:pom_path) && executable('mvn')
|
|
|
|
return ale#path#CdString(fnamemodify(l:pom_path, ':h'))
|
|
|
|
\ . 'mvn dependency:build-classpath'
|
|
|
|
endif
|
|
|
|
|
2017-08-29 18:39:23 -04:00
|
|
|
let l:classpath_command = ale#gradle#BuildClasspathCommand(a:buffer)
|
|
|
|
if !empty(l:classpath_command)
|
|
|
|
return l:classpath_command
|
|
|
|
endif
|
|
|
|
|
2017-04-25 17:32:48 -04:00
|
|
|
return ''
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:BuildClassPathOption(buffer, import_paths) abort
|
|
|
|
" Filter out lines like [INFO], etc.
|
|
|
|
let l:class_paths = filter(a:import_paths[:], 'v:val !~# ''[''')
|
|
|
|
call extend(
|
|
|
|
\ l:class_paths,
|
|
|
|
\ split(ale#Var(a:buffer, 'java_javac_classpath'), s:classpath_sep),
|
|
|
|
\)
|
|
|
|
|
|
|
|
return !empty(l:class_paths)
|
2017-05-12 04:20:16 -04:00
|
|
|
\ ? '-cp ' . ale#Escape(join(l:class_paths, s:classpath_sep))
|
2017-02-11 17:02:38 -05:00
|
|
|
\ : ''
|
2017-04-25 17:32:48 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! ale_linters#java#javac#GetCommand(buffer, import_paths) abort
|
|
|
|
let l:cp_option = s:BuildClassPathOption(a:buffer, a:import_paths)
|
|
|
|
let l:sp_option = ''
|
|
|
|
|
|
|
|
" Find the src directory, for files in this project.
|
|
|
|
let l:src_dir = ale#path#FindNearestDirectory(a:buffer, 'src/main/java')
|
|
|
|
|
|
|
|
if !empty(l:src_dir)
|
2017-05-12 04:20:16 -04:00
|
|
|
let l:sp_option = '-sourcepath ' . ale#Escape(l:src_dir)
|
2017-04-25 17:32:48 -04:00
|
|
|
endif
|
2017-02-11 17:02:38 -05:00
|
|
|
|
2017-03-31 15:14:53 -04:00
|
|
|
" Create .class files in a temporary directory, which we will delete later.
|
|
|
|
let l:class_file_directory = ale#engine#CreateDirectory(a:buffer)
|
|
|
|
|
2017-04-25 17:32:48 -04:00
|
|
|
return 'javac -Xlint'
|
|
|
|
\ . ' ' . l:cp_option
|
|
|
|
\ . ' ' . l:sp_option
|
2017-05-12 04:20:16 -04:00
|
|
|
\ . ' -d ' . ale#Escape(l:class_file_directory)
|
2017-04-15 20:24:08 -04:00
|
|
|
\ . ' ' . ale#Var(a:buffer, 'java_javac_options')
|
2017-02-11 17:02:38 -05:00
|
|
|
\ . ' %t'
|
|
|
|
endfunction
|
2017-02-11 16:29:48 -05:00
|
|
|
|
|
|
|
function! ale_linters#java#javac#Handle(buffer, lines) abort
|
|
|
|
" Look for lines like the following.
|
|
|
|
"
|
|
|
|
" Main.java:13: warning: [deprecation] donaught() in Testclass has been deprecated
|
|
|
|
" Main.java:16: error: ';' expected
|
|
|
|
|
2017-05-04 18:19:58 -04:00
|
|
|
let l:pattern = '\v^.*:(\d+): (.+):(.+)$'
|
2017-06-19 06:45:09 -04:00
|
|
|
let l:col_pattern = '\v^(\s*\^)$'
|
2017-05-04 18:34:52 -04:00
|
|
|
let l:symbol_pattern = '\v^ +symbol: *(class|method) +([^ ]+)'
|
2017-02-11 16:29:48 -05:00
|
|
|
let l:output = []
|
|
|
|
|
2017-06-19 06:45:09 -04:00
|
|
|
for l:match in ale#util#GetMatches(a:lines, [l:pattern, l:col_pattern, l:symbol_pattern])
|
|
|
|
if empty(l:match[2]) && empty(l:match[3])
|
|
|
|
let l:output[-1].col = len(l:match[1])
|
|
|
|
elseif empty(l:match[3])
|
2017-05-04 18:19:58 -04:00
|
|
|
" Add symbols to 'cannot find symbol' errors.
|
2017-08-08 03:39:13 -04:00
|
|
|
if l:output[-1].text is# 'error: cannot find symbol'
|
2017-05-04 18:19:58 -04:00
|
|
|
let l:output[-1].text .= ': ' . l:match[2]
|
|
|
|
endif
|
|
|
|
else
|
|
|
|
call add(l:output, {
|
|
|
|
\ 'lnum': l:match[1] + 0,
|
|
|
|
\ 'text': l:match[2] . ':' . l:match[3],
|
2017-08-08 03:39:13 -04:00
|
|
|
\ 'type': l:match[2] is# 'error' ? 'E' : 'W',
|
2017-05-04 18:19:58 -04:00
|
|
|
\})
|
|
|
|
endif
|
2017-02-11 16:29:48 -05:00
|
|
|
endfor
|
|
|
|
|
|
|
|
return l:output
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
call ale#linter#Define('java', {
|
|
|
|
\ 'name': 'javac',
|
|
|
|
\ 'executable': 'javac',
|
2017-04-25 17:32:48 -04:00
|
|
|
\ 'command_chain': [
|
|
|
|
\ {'callback': 'ale_linters#java#javac#GetImportPaths', 'output_stream': 'stdout'},
|
|
|
|
\ {'callback': 'ale_linters#java#javac#GetCommand', 'output_stream': 'stderr'},
|
|
|
|
\ ],
|
2017-02-11 16:29:48 -05:00
|
|
|
\ 'callback': 'ale_linters#java#javac#Handle',
|
|
|
|
\})
|