Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(javac): Add java_javac_sourcepath variable #3173

Merged
merged 5 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions ale_linters/java/javac.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ let s:classpath_sep = has('unix') ? ':' : ';'
call ale#Set('java_javac_executable', 'javac')
call ale#Set('java_javac_options', '')
call ale#Set('java_javac_classpath', '')
call ale#Set('java_javac_sourcepath', '')

function! ale_linters#java#javac#RunWithImportPaths(buffer) abort
let l:command = ''
Expand Down Expand Up @@ -40,10 +41,15 @@ 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),
\)
let l:cls_path = ale#Var(a:buffer, 'java_javac_classpath')

if !empty(l:cls_path) && type(l:cls_path) is v:t_string
call extend(l:class_paths, split(l:cls_path, s:classpath_sep))
endif

if !empty(l:cls_path) && type(l:cls_path) is v:t_list
call extend(l:class_paths, l:cls_path)
endif

return !empty(l:class_paths)
\ ? '-cp ' . ale#Escape(join(l:class_paths, s:classpath_sep))
Expand Down Expand Up @@ -79,6 +85,27 @@ function! ale_linters#java#javac#GetCommand(buffer, import_paths, meta) abort
endif
endif

let l:source_paths = []
let l:source_path = ale#Var(a:buffer, 'java_javac_sourcepath')

if !empty(l:source_path) && type(l:source_path) is v:t_string
let l:source_paths = split(l:source_path, s:classpath_sep)
endif

if !empty(l:source_path) && type(l:source_path) is v:t_list
let l:source_paths = l:source_path
endif

if !empty(l:source_paths)
for l:path in l:source_paths
let l:sp_path = ale#path#FindNearestDirectory(a:buffer, l:path)

if !empty(l:sp_path)
call add(l:sp_dirs, l:sp_path)
endif
endfor
endif

if !empty(l:sp_dirs)
let l:sp_option = '-sourcepath '
\ . ale#Escape(join(l:sp_dirs, s:classpath_sep))
Expand Down
26 changes: 25 additions & 1 deletion doc/ale-java.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ javac *ale-java-javac*

g:ale_java_javac_classpath *g:ale_java_javac_classpath*
*b:ale_java_javac_classpath*
Type: |String|
Type: |String| or |List|
Default: `''`

This variable can be set to change the global classpath for Java.
Expand All @@ -67,6 +67,30 @@ g:ale_java_javac_options *g:ale_java_javac_options*

This variable can be set to pass additional options to javac.

g:ale_java_javac_sourcepath *g:ale_java_javac_sourcepath*
*b:ale_java_javac_sourcepath*
Type: |String| or |List|
Default: `''`

This variable can set multiple source code paths, the source code path is a
relative path (relative to the project root directory).

Example:

String type:
Note that the unix system separator is a colon(`:`) window system
is a semicolon(`;`).
>
let g:ale_java_javac_sourcepath = 'build/gen/source/xx/main:build/gen/source'
<
List type:
>
let g:ale_java_javac_sourcepath = [
\ 'build/generated/source/querydsl/main',
\ 'target/generated-sources/source/querydsl/main'
\ ]
<


===============================================================================
google-java-format *ale-java-google-java-format*
Expand Down
Empty file.
141 changes: 140 additions & 1 deletion test/command_callback/test_javac_command_callback.vader
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,22 @@ After:
Execute(The javac callback should return the correct default value):
AssertLinter 'javac', g:prefix . ' -d ' . ale#Escape('TEMP_DIR') . ' %t'

Execute(The javac callback should use g:ale_java_javac_classpath correctly):
Execute(The javac callback should use string type g:ale_java_javac_classpath correctly):
let g:ale_java_javac_classpath = 'foo.jar'

AssertLinter 'javac',
\ g:prefix
\ . ' -cp ' . ale#Escape('foo.jar')
\ . ' -d ' . ale#Escape('TEMP_DIR') . ' %t'

Execute(The javac callback should use list type g:ale_java_javac_classpath correctly):
let g:ale_java_javac_classpath = ['foo.jar']

AssertLinter 'javac',
\ g:prefix
\ . ' -cp ' . ale#Escape('foo.jar')
\ . ' -d ' . ale#Escape('TEMP_DIR') . ' %t'

Execute(The executable should be configurable):
let g:ale_java_javac_executable = 'foobar'

Expand Down Expand Up @@ -108,6 +116,137 @@ Execute(The javac callback should combine discovered classpaths and manual ones)
\ . ' -d ' . ale#Escape('TEMP_DIR') . ' %t',
\ substitute(b:command, '%e', '\=ale#Escape(''javac'')', 'g')

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',
\], {})

AssertEqual
\ g:prefix
\ . ' -cp '
\ . ale#Escape(join(
\ [
\ '/foo/bar.jar',
\ '/xyz/abc.jar',
\ 'configured.jar',
\ ],
\ g:cp_sep
\ ))
\ . ' -d ' . ale#Escape('TEMP_DIR') . ' %t',
\ substitute(b:command, '%e', '\=ale#Escape(''javac'')', 'g')

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',
\], {})

AssertEqual
\ g:prefix
\ . ' -cp '
\ . ale#Escape(join(
\ [
\ '/foo/bar.jar',
\ '/xyz/abc.jar',
\ 'configured.jar',
\ 'configured2.jar',
\ ],
\ g:cp_sep
\ ))
\ . ' -d ' . ale#Escape('TEMP_DIR') . ' %t',
\ substitute(b:command, '%e', '\=ale#Escape(''javac'')', 'g')

Execute(The javac callback should use string type g:ale_java_javac_sourcepath correctly):
let g:ale_java_javac_sourcepath = 'java_paths/build/gen/main'

AssertLinter 'javac',
\ g:prefix
\ . ' -sourcepath ' . ale#Escape(
\ ale#path#Simplify(g:dir . '/java_paths/build/gen/main/')
\ )
\ . ' -d ' . ale#Escape('TEMP_DIR') . ' %t'

Execute(The javac callback should use list type g:ale_java_javac_sourcepath correctly):
let g:ale_java_javac_sourcepath = ['java_paths/build/gen/main']

AssertLinter 'javac',
\ g:prefix
\ . ' -sourcepath ' . ale#Escape(
\ ale#path#Simplify(g:dir . '/java_paths/build/gen/main/')
\ )
\ . ' -d ' . ale#Escape('TEMP_DIR') . ' %t'

Execute(The javac callback shouldn't add -sourcepath when g:ale_java_javac_sourcepath variable path doesn't exist):
let g:ale_java_javac_sourcepath = 'java_paths/build/gen3/main'

AssertLinter 'javac',
\ g:prefix
\ . ' -d ' . ale#Escape('TEMP_DIR') . ' %t'

Execute(The javac callback should combine discovered sourcepath and manual ones):
call ale#engine#Cleanup(bufnr(''))
call ale#test#SetFilename('java_paths/src/main/java/com/something/dummy.java')
call ale#engine#InitBufferInfo(bufnr(''))

let g:ale_java_javac_sourcepath = 'java_paths/build/gen/main'
let b:command = ale_linters#java#javac#GetCommand(bufnr(''), [], {})

AssertEqual
\ ale#path#CdString(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/build/gen/main/'),
\ ], g:cp_sep))
\ . ' -d ' . ale#Escape('TEMP_DIR') . ' %t',
\ substitute(b:command, '%e', '\=ale#Escape(''javac'')', 'g')

let g:ale_java_javac_sourcepath = 'java_paths/build/gen/main'
\ . g:cp_sep . 'java_paths/build/gen2/main'
let b:command = ale_linters#java#javac#GetCommand(bufnr(''), [], {})

AssertEqual
\ ale#path#CdString(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/build/gen/main/'),
\ ale#path#Simplify(g:dir . '/java_paths/build/gen2/main/')
\ ], g:cp_sep))
\ . ' -d ' . ale#Escape('TEMP_DIR') . ' %t',
\ substitute(b:command, '%e', '\=ale#Escape(''javac'')', 'g')

let g:ale_java_javac_sourcepath = ['java_paths/build/gen/main']
let b:command = ale_linters#java#javac#GetCommand(bufnr(''), [], {})

AssertEqual
\ ale#path#CdString(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/build/gen/main/')
\ ], g:cp_sep))
\ . ' -d ' . ale#Escape('TEMP_DIR') . ' %t',
\ substitute(b:command, '%e', '\=ale#Escape(''javac'')', 'g')

let g:ale_java_javac_sourcepath = [
\ 'java_paths/build/gen/main',
\ 'java_paths/build/gen2/main'
\ ]
let b:command = ale_linters#java#javac#GetCommand(bufnr(''), [], {})

AssertEqual
\ ale#path#CdString(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/build/gen/main/'),
\ ale#path#Simplify(g:dir . '/java_paths/build/gen2/main/')
\ ], g:cp_sep))
\ . ' -d ' . ale#Escape('TEMP_DIR') . ' %t',
\ substitute(b:command, '%e', '\=ale#Escape(''javac'')', 'g')

Execute(The javac callback should detect source directories):
call ale#engine#Cleanup(bufnr(''))
noautocmd e! java_paths/src/main/java/com/something/dummy
Expand Down