diff options
author | Valentin Finini <farenjihn@gmail.com> | 2017-02-11 22:29:48 +0100 |
---|---|---|
committer | w0rp <w0rp@users.noreply.github.com> | 2017-02-11 21:29:48 +0000 |
commit | 8c4846b68a36a97dfb7760bdee6a0fb7a673ab68 (patch) | |
tree | efdab2f4df75052d68c33b4350b271f6671429b9 | |
parent | 3551bde012207ee7f22b2f36247b53014afe4a55 (diff) | |
download | ale-8c4846b68a36a97dfb7760bdee6a0fb7a673ab68.zip |
Added support for javac (with eclipse classpath support for now) (#141)
* A try at javac support for ALE
* Small cleanup: moved '/tmp/java_ale' string into script var
* Fixed Travis-CI build failing on autocmd not being in augroup and stupid omission
* One more fix for Travis-CI
* For some reason, expandtab was not set
* Indentation and removal of header guard.
Used examples from ale_linters/c/gcc.vim and
ale_linters/javascript/eslint.vim for the indentation of string concat blocks.
-rw-r--r-- | ale_linters/java/javac.vim | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/ale_linters/java/javac.vim b/ale_linters/java/javac.vim new file mode 100644 index 00000000..80986314 --- /dev/null +++ b/ale_linters/java/javac.vim @@ -0,0 +1,102 @@ +" Author: farenjihn <farenjihn@gmail.com> +" Description: Lints java files using javac + +let g:loaded_ale_linters_java_javac = 1 +let g:ale_java_javac_classpath = '' + +let s:eclipse_classpath = '' +let s:tmppath = '/tmp/java_ale/' + +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 + + let l:pattern = '^.*\:\(\d\+\):\ \(.*\):\(.*\)$' + let l:output = [] + + for l:line in a:lines + let l:match = matchlist(l:line, l:pattern) + + if len(l:match) == 0 + continue + endif + + call add(l:output, { + \ 'bufnr': a:buffer, + \ 'lnum': l:match[1] + 0, + \ 'vcol': 0, + \ 'col': 1, + \ 'text': l:match[2] . ':' . l:match[3], + \ 'type': l:match[2] ==# 'error' ? 'E' : 'W', + \ 'nr': -1, + \}) + endfor + + return l:output +endfunction + +function! ale_linters#java#javac#ParseEclipseClasspath() + let l:eclipse_classpath = '' + +python << EOF + + import xml.etree.ElementTree as ET, vim + tree = ET.parse(".classpath") + root = tree.getroot() + + classpath = '' + + for child in root: + classpath += child.get("path") + classpath += ':' + + vim.command("let l:eclipse_classpath = '%s'" % classpath); + +# Cannot indent this EOF token as per :h python +EOF + + return l:eclipse_classpath +endfunction + +function! ale_linters#java#javac#CheckEclipseClasspath() + " Eclipse .classpath parsing through python + if file_readable('.classpath') + let s:eclipse_classpath = ale_linters#java#javac#ParseEclipseClasspath() + endif +endfunction + +function! ale_linters#java#javac#GetCommand(buffer) + let l:path = s:tmppath . expand('%:p:h') + let l:file = expand('%:t') + let l:buf = getline(1, '$') + + " Javac cannot compile files from stdin so we move a copy into /tmp instead + call mkdir(l:path, 'p') + call writefile(l:buf, l:path . '/' . l:file) + + return 'javac ' + \ . '-Xlint ' + \ . '-cp ' . s:eclipse_classpath . g:ale_java_javac_classpath . ':. ' + \ . g:ale_java_javac_options + \ . ' ' . l:path . '/' . l:file +endfunction + +function! ale_linters#java#javac#CleanupTmp() + call delete(s:tmppath, 'rf') +endfunction + +augroup java_ale_au + autocmd! BufEnter *.java call ale_linters#java#javac#CheckEclipseClasspath() + autocmd! BufLeave *.java call ale_linters#java#javac#CleanupTmp() +augroup END + +call ale_linters#java#javac#CheckEclipseClasspath() +call ale#linter#Define('java', { +\ 'name': 'javac', +\ 'output_stream': 'stderr', +\ 'executable': 'javac', +\ 'command_callback': 'ale_linters#java#javac#GetCommand', +\ 'callback': 'ale_linters#java#javac#Handle', +\}) |