summaryrefslogtreecommitdiff
path: root/ale_linters/java/javac.vim
diff options
context:
space:
mode:
Diffstat (limited to 'ale_linters/java/javac.vim')
-rw-r--r--ale_linters/java/javac.vim50
1 files changed, 43 insertions, 7 deletions
diff --git a/ale_linters/java/javac.vim b/ale_linters/java/javac.vim
index 186d3834..5815c658 100644
--- a/ale_linters/java/javac.vim
+++ b/ale_linters/java/javac.vim
@@ -1,19 +1,53 @@
" Author: farenjihn <farenjihn@gmail.com>, w0rp <devw0rp@gmail.com>
" 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_classpath = get(g:, 'ale_java_javac_classpath', '')
-function! ale_linters#java#javac#GetCommand(buffer) abort
- let l:cp_option = !empty(ale#Var(a:buffer, 'java_javac_classpath'))
- \ ? '-cp ' . ale#Var(a:buffer, 'java_javac_classpath')
+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
+
+ 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.
let l:class_file_directory = ale#engine#CreateDirectory(a:buffer)
- return 'javac -Xlint '
- \ . l:cp_option
+ return 'javac -Xlint'
+ \ . ' ' . l:cp_option
+ \ . ' ' . l:sp_option
\ . ' -d ' . fnameescape(l:class_file_directory)
\ . ' ' . ale#Var(a:buffer, 'java_javac_options')
\ . ' %t'
@@ -41,8 +75,10 @@ endfunction
call ale#linter#Define('java', {
\ 'name': 'javac',
-\ 'output_stream': 'stderr',
\ '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',
\})