]> git.madduck.net Git - etc/vim.git/blob - ale_linters/java/checkstyle.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Squashed '.vim/bundle/ale/' content from commit 22185c4c
[etc/vim.git] / ale_linters / java / checkstyle.vim
1 " Author: Devon Meunier <devon.meunier@gmail.com>
2 " Description: checkstyle for Java files
3
4 call ale#Set('java_checkstyle_executable', 'checkstyle')
5 call ale#Set('java_checkstyle_config', '/google_checks.xml')
6 call ale#Set('java_checkstyle_options', '')
7
8 function! ale_linters#java#checkstyle#Handle(buffer, lines) abort
9     let l:output = []
10
11     " modern checkstyle versions
12     let l:pattern = '\v\[(WARN|ERROR)\] [a-zA-Z]?:?[^:]+:(\d+):(\d+)?:? (.*) \[(.+)\]'
13
14     for l:match in ale#util#GetMatches(a:lines, l:pattern)
15         call add(l:output, {
16         \   'type': l:match[1] is? 'WARN' ? 'W' : 'E',
17         \   'sub_type': 'style',
18         \   'lnum': l:match[2] + 0,
19         \   'col': l:match[3] + 0,
20         \   'text': l:match[4],
21         \   'code': l:match[5],
22         \})
23     endfor
24
25     if !empty(l:output)
26         return l:output
27     endif
28
29     " old checkstyle versions
30     let l:pattern = '\v(.+):(\d+): ([^:]+): (.+)$'
31
32     for l:match in ale#util#GetMatches(a:lines, l:pattern)
33         call add(l:output, {
34         \   'type': l:match[3] is? 'warning' ? 'W' : 'E',
35         \   'sub_type': 'style',
36         \   'lnum': l:match[2] + 0,
37         \   'text': l:match[4],
38         \})
39     endfor
40
41     return l:output
42 endfunction
43
44 function! s:GetConfig(buffer, config) abort
45     if ale#path#IsAbsolute(a:config)
46         return a:config
47     endif
48
49     let s:file = ale#path#FindNearestFile(a:buffer, a:config)
50
51     return !empty(s:file) ? s:file : a:config
52 endfunction
53
54 function! ale_linters#java#checkstyle#GetCommand(buffer) abort
55     let l:options = ale#Var(a:buffer, 'java_checkstyle_options')
56     let l:config_option = ale#Var(a:buffer, 'java_checkstyle_config')
57     let l:config = l:options !~# '\v(^| )-c ' && !empty(l:config_option)
58     \   ? s:GetConfig(a:buffer, l:config_option)
59     \   : ''
60
61     return '%e'
62     \ . ale#Pad(l:options)
63     \ . (!empty(l:config) ? ' -c ' . ale#Escape(l:config) : '')
64     \ . ' %s'
65 endfunction
66
67 call ale#linter#Define('java', {
68 \   'name': 'checkstyle',
69 \   'executable': {b -> ale#Var(b, 'java_checkstyle_executable')},
70 \   'command': function('ale_linters#java#checkstyle#GetCommand'),
71 \   'callback': 'ale_linters#java#checkstyle#Handle',
72 \   'lint_file': 1,
73 \})