Fixes #361, fixes #417 Get classpaths from Maven, and automatically detect src/main/java paths

This commit is contained in:
w0rp 2017-04-25 22:32:48 +01:00
parent fe51866dfc
commit 45c2d6b580
3 changed files with 111 additions and 11 deletions

View File

@ -1,19 +1,53 @@
" Author: farenjihn <farenjihn@gmail.com>, w0rp <devw0rp@gmail.com> " Author: farenjihn <farenjihn@gmail.com>, w0rp <devw0rp@gmail.com>
" Description: Lints java files using javac " Description: Lints java files using javac
let s:classpath_sep = has('unix') ? ':' : ';'
let g:ale_java_javac_options = get(g:, 'ale_java_javac_options', '') let g:ale_java_javac_options = get(g:, 'ale_java_javac_options', '')
let g:ale_java_javac_classpath = get(g:, 'ale_java_javac_classpath', '') let g:ale_java_javac_classpath = get(g:, 'ale_java_javac_classpath', '')
function! ale_linters#java#javac#GetCommand(buffer) abort function! ale_linters#java#javac#GetImportPaths(buffer) abort
let l:cp_option = !empty(ale#Var(a:buffer, 'java_javac_classpath')) let l:pom_path = ale#path#FindNearestFile(a:buffer, 'pom.xml')
\ ? '-cp ' . ale#Var(a:buffer, 'java_javac_classpath')
if !empty(l:pom_path) && executable('mvn')
return ale#path#CdString(fnamemodify(l:pom_path, ':h'))
\ . 'mvn dependency:build-classpath'
endif
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 map(l:class_paths, 'fnameescape(v:val)')
call extend(
\ l:class_paths,
\ split(ale#Var(a:buffer, 'java_javac_classpath'), s:classpath_sep),
\)
return !empty(l:class_paths)
\ ? '-cp ' . join(l:class_paths, s:classpath_sep)
\ : '' \ : ''
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)
let l:sp_option = '-sourcepath ' . fnameescape(l:src_dir)
endif
" Create .class files in a temporary directory, which we will delete later. " Create .class files in a temporary directory, which we will delete later.
let l:class_file_directory = ale#engine#CreateDirectory(a:buffer) let l:class_file_directory = ale#engine#CreateDirectory(a:buffer)
return 'javac -Xlint' return 'javac -Xlint'
\ . l:cp_option \ . ' ' . l:cp_option
\ . ' ' . l:sp_option
\ . ' -d ' . fnameescape(l:class_file_directory) \ . ' -d ' . fnameescape(l:class_file_directory)
\ . ' ' . ale#Var(a:buffer, 'java_javac_options') \ . ' ' . ale#Var(a:buffer, 'java_javac_options')
\ . ' %t' \ . ' %t'
@ -41,8 +75,10 @@ endfunction
call ale#linter#Define('java', { call ale#linter#Define('java', {
\ 'name': 'javac', \ 'name': 'javac',
\ 'output_stream': 'stderr',
\ 'executable': 'javac', \ 'executable': 'javac',
\ 'command_callback': 'ale_linters#java#javac#GetCommand', \ 'command_chain': [
\ {'callback': 'ale_linters#java#javac#GetImportPaths', 'output_stream': 'stdout'},
\ {'callback': 'ale_linters#java#javac#GetCommand', 'output_stream': 'stderr'},
\ ],
\ 'callback': 'ale_linters#java#javac#Handle', \ 'callback': 'ale_linters#java#javac#Handle',
\}) \})

View File

@ -2,6 +2,8 @@ Before:
runtime ale_linters/java/javac.vim runtime ale_linters/java/javac.vim
call ale#engine#InitBufferInfo(bufnr('')) call ale#engine#InitBufferInfo(bufnr(''))
silent! cd /testplugin/test/command_callback
After: After:
call ale#linter#Reset() call ale#linter#Reset()
" We need to clean up the buffer to remove the temporary directories created " We need to clean up the buffer to remove the temporary directories created
@ -11,7 +13,7 @@ After:
let g:ale_java_javac_classpath = '' let g:ale_java_javac_classpath = ''
Execute(The javac callback should return the correct default value): Execute(The javac callback should return the correct default value):
let b:command = ale_linters#java#javac#GetCommand(bufnr('')) let b:command = ale_linters#java#javac#GetCommand(bufnr(''), [])
Assert match(b:command, '\v^javac +-Xlint +-d +/tmp/[0-9a-zA-Z/]+ +\%t$') >= 0, Assert match(b:command, '\v^javac +-Xlint +-d +/tmp/[0-9a-zA-Z/]+ +\%t$') >= 0,
\ 'Invalid command string: ' . b:command \ 'Invalid command string: ' . b:command
@ -19,15 +21,77 @@ Execute(The javac callback should return the correct default value):
Execute(The javac callback should use g:ale_java_javac_classpath correctly): Execute(The javac callback should use g:ale_java_javac_classpath correctly):
let g:ale_java_javac_classpath = 'foo.jar' let g:ale_java_javac_classpath = 'foo.jar'
let b:command = ale_linters#java#javac#GetCommand(bufnr('')) let b:command = ale_linters#java#javac#GetCommand(bufnr(''), [])
Assert match(b:command, '\v^javac +-Xlint -cp foo\.jar +-d +/tmp/[0-9a-zA-Z/]+ +\%t$') >= 0, Assert match(b:command, '\v^javac +-Xlint +-cp +foo\.jar +-d +/tmp/[0-9a-zA-Z/]+ +\%t$') >= 0,
\ 'Invalid command string: ' . b:command
Execute(The javac callback should include discovered classpaths):
let b:command = ale_linters#java#javac#GetCommand(bufnr(''), [
\ '[DEBUG] Ignore this.',
\ '[INFO] Something we should ignore.',
\ '/foo/bar.jar',
\ '/xyz/abc.jar',
\])
Assert match(b:command, '\v^javac +-Xlint +-cp +/foo/bar\.jar:/xyz/abc\.jar +-d +/tmp/[0-9a-zA-Z/]+ +\%t$') >= 0,
\ 'Invalid command string: ' . b:command
Execute(The javac callback should combine discovered classpaths and manual ones):
let g:ale_java_javac_classpath = 'configured.jar'
let b:command = ale_linters#java#javac#GetCommand(bufnr(''), [
\ '[DEBUG] Ignore this.',
\ '[INFO] Something we should ignore.',
\ '/foo/bar.jar',
\ '/xyz/abc.jar',
\])
Assert match(b:command, '\v^javac +-Xlint +-cp +/foo/bar\.jar:/xyz/abc\.jar:configured\.jar +-d +/tmp/[0-9a-zA-Z/]+ +\%t$') >= 0,
\ 'Invalid command string: ' . b:command
let g:ale_java_javac_classpath = 'configured.jar:configured2.jar'
let b:command = ale_linters#java#javac#GetCommand(bufnr(''), [
\ '[DEBUG] Ignore this.',
\ '[INFO] Something we should ignore.',
\ '/foo/bar.jar',
\ '/xyz/abc.jar',
\])
Assert match(b:command, '\v^javac +-Xlint +-cp +/foo/bar\.jar:/xyz/abc\.jar:configured\.jar:configured2\.jar +-d +/tmp/[0-9a-zA-Z/]+ +\%t$') >= 0,
\ 'Invalid command string: ' . b:command
Execute(The javac callback should detect source directories):
call ale#cleanup#Buffer(bufnr(''))
:e! java_paths/src/main/java/com/something/dummy
call ale#engine#InitBufferInfo(bufnr(''))
let b:command = ale_linters#java#javac#GetCommand(bufnr(''), [])
Assert match(b:command, '\v^javac +-Xlint +-sourcepath /.*java_paths/src/main/java/ +-d +/tmp/[0-9a-zA-Z/]+ +\%t$') >= 0,
\ 'Invalid command string: ' . b:command
Execute(The javac callback should combine detected source directories and classpaths):
call ale#cleanup#Buffer(bufnr(''))
:e! java_paths/src/main/java/com/something/dummy
call ale#engine#InitBufferInfo(bufnr(''))
let b:command = ale_linters#java#javac#GetCommand(bufnr(''), [
\ '[DEBUG] Ignore this.',
\ '[INFO] Something we should ignore.',
\ '/foo/bar.jar',
\ '/xyz/abc.jar',
\])
Assert match(b:command, '\v^javac +-Xlint +-cp +/foo/bar\.jar:/xyz/abc\.jar +-sourcepath /.*java_paths/src/main/java/ +-d +/tmp/[0-9a-zA-Z/]+ +\%t$') >= 0,
\ 'Invalid command string: ' . b:command \ 'Invalid command string: ' . b:command
Execute(The javac callback should use g:ale_java_javac_options correctly): Execute(The javac callback should use g:ale_java_javac_options correctly):
let g:ale_java_javac_options = '--anything --else' let g:ale_java_javac_options = '--anything --else'
let b:command = ale_linters#java#javac#GetCommand(bufnr('')) let b:command = ale_linters#java#javac#GetCommand(bufnr(''), [])
Assert match(b:command, '\v^javac +-Xlint +-d +/tmp/[0-9a-zA-Z/]+ --anything --else +\%t$') >= 0, Assert match(b:command, '\v^javac +-Xlint +-d +/tmp/[0-9a-zA-Z/]+ --anything --else +\%t$') >= 0,
\ 'Invalid command string: ' . b:command \ 'Invalid command string: ' . b:command