Close #1476 - Make the javac executable configurable

This commit is contained in:
w0rp 2018-04-08 20:35:06 +01:00
parent 7cf3ddf6c4
commit 2f2dcb8444
No known key found for this signature in database
GPG Key ID: 0FC1ECAA8C81CD83
3 changed files with 38 additions and 9 deletions

View File

@ -3,8 +3,9 @@
let s:classpath_sep = has('unix') ? ':' : ';'
let g:ale_java_javac_options = get(g:, 'ale_java_javac_options', '')
let g:ale_java_javac_classpath = get(g:, 'ale_java_javac_classpath', '')
call ale#Set('java_javac_executable', 'javac')
call ale#Set('java_javac_options', '')
call ale#Set('java_javac_classpath', '')
function! ale_linters#java#javac#GetImportPaths(buffer) abort
let l:pom_path = ale#path#FindNearestFile(a:buffer, 'pom.xml')
@ -35,6 +36,10 @@ function! s:BuildClassPathOption(buffer, import_paths) abort
\ : ''
endfunction
function! ale_linters#java#javac#GetExecutable(buffer) abort
return ale#Var(a:buffer, 'java_javac_executable')
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 = ''
@ -72,11 +77,13 @@ function! ale_linters#java#javac#GetCommand(buffer, import_paths) abort
" Create .class files in a temporary directory, which we will delete later.
let l:class_file_directory = ale#engine#CreateDirectory(a:buffer)
let l:executable = ale_linters#java#javac#GetExecutable(a:buffer)
" Always run javac from the directory the file is in, so we can resolve
" relative paths correctly.
return ale#path#BufferCdString(a:buffer)
\ . 'javac -Xlint'
\ . ale#Escape(l:executable)
\ . ' -Xlint'
\ . ' ' . l:cp_option
\ . ' ' . l:sp_option
\ . ' -d ' . ale#Escape(l:class_file_directory)
@ -119,7 +126,7 @@ endfunction
call ale#linter#Define('java', {
\ 'name': 'javac',
\ 'executable': 'javac',
\ 'executable_callback': 'ale_linters#java#javac#GetExecutable',
\ 'command_chain': [
\ {'callback': 'ale_linters#java#javac#GetImportPaths', 'output_stream': 'stdout'},
\ {'callback': 'ale_linters#java#javac#GetCommand', 'output_stream': 'stderr'},

View File

@ -25,6 +25,14 @@ g:ale_java_javac_classpath *g:ale_java_javac_classpath*
This variable can be set to change the global classpath for Java.
g:ale_java_javac_executable *g:ale_java_javac_executable*
*b:ale_java_javac_executable*
Type: |String|
Default: `'javac'`
This variable can be set to change the executable path used for javac.
g:ale_java_javac_options *g:ale_java_javac_options*
*b:ale_java_javac_options*
Type: |String|

View File

@ -1,9 +1,11 @@
Before:
call ale#test#SetDirectory('/testplugin/test/command_callback')
Save g:ale_java_javac_executable
Save g:ale_java_javac_options
Save g:ale_java_javac_classpath
unlet! g:ale_java_javac_executable
unlet! g:ale_java_javac_options
unlet! g:ale_java_javac_classpath
@ -28,7 +30,8 @@ Before:
call ale#test#SetFilename('dummy.java')
let g:prefix = 'cd ' . ale#Escape(expand('%:p:h')) . ' && javac -Xlint'
let g:prefix = 'cd ' . ale#Escape(expand('%:p:h')) . ' && '
\ . ale#Escape('javac') . ' -Xlint'
After:
call ale#test#RestoreDirectory()
@ -57,6 +60,17 @@ Execute(The javac callback should use g:ale_java_javac_classpath correctly):
\ . ' -d TEMP %t',
\ GetCommand([])
Execute(The executable should be configurable):
let g:ale_java_javac_executable = 'foobar'
AssertEqual 'foobar', ale_linters#java#javac#GetExecutable(bufnr(''))
AssertEqual
\ 'cd ' . ale#Escape(expand('%:p:h')) . ' && '
\ . ale#Escape('foobar') . ' -Xlint'
\ . ' -d TEMP %t',
\ GetCommand([])
Execute(The javac callback should include discovered classpaths):
AssertEqual
\ g:prefix
@ -120,7 +134,7 @@ Execute(The javac callback should detect source directories):
call ale#engine#InitBufferInfo(bufnr(''))
AssertEqual
\ 'cd ' . ale#Escape(expand('%:p:h')) . ' && javac -Xlint'
\ 'cd ' . ale#Escape(expand('%:p:h')) . ' && ' . ale#Escape('javac') . ' -Xlint'
\ . ' -sourcepath ' . ale#Escape(
\ ale#path#Simplify(g:dir . '/java_paths/src/main/java/')
\ )
@ -133,7 +147,7 @@ Execute(The javac callback should combine detected source directories and classp
call ale#engine#InitBufferInfo(bufnr(''))
AssertEqual
\ 'cd ' . ale#Escape(expand('%:p:h')) . ' && javac -Xlint'
\ 'cd ' . ale#Escape(expand('%:p:h')) . ' && ' . ale#Escape('javac') . ' -Xlint'
\ . ' -cp ' . ale#Escape(join(['/foo/bar.jar', '/xyz/abc.jar'], g:cp_sep))
\ . ' -sourcepath ' . ale#Escape(
\ ale#path#Simplify(g:dir . '/java_paths/src/main/java/')
@ -164,7 +178,7 @@ Execute(The javac callback should include src/test/java for test paths):
call ale#engine#InitBufferInfo(bufnr(''))
AssertEqual
\ 'cd ' . ale#Escape(expand('%:p:h')) . ' && javac -Xlint'
\ 'cd ' . ale#Escape(expand('%:p:h')) . ' && ' . ale#Escape('javac') . ' -Xlint'
\ . ' -sourcepath ' . ale#Escape(join([
\ ale#path#Simplify(g:dir . '/java_paths/src/main/java/'),
\ ale#path#Simplify(g:dir . '/java_paths/src/test/java/'),
@ -178,7 +192,7 @@ Execute(The javac callback should include src/main/jaxb when available):
call ale#engine#InitBufferInfo(bufnr(''))
AssertEqual
\ 'cd ' . ale#Escape(expand('%:p:h')) . ' && javac -Xlint'
\ 'cd ' . ale#Escape(expand('%:p:h')) . ' && ' . ale#Escape('javac') . ' -Xlint'
\ . ' -sourcepath ' . ale#Escape(join([
\ ale#path#Simplify(g:dir . '/java_paths_with_jaxb/src/main/java/'),
\ ale#path#Simplify(g:dir . '/java_paths_with_jaxb/src/main/jaxb/'),