From f6b0a28cbacba36954cec02bffaee9f126610d69 Mon Sep 17 00:00:00 2001 From: w0rp Date: Wed, 14 Jun 2017 16:20:30 +0100 Subject: Split up the flake8 and ansible-lint handlers --- ale_linters/ansible/ansible-lint.vim | 9 ------ ale_linters/ansible/ansible_lint.vim | 48 ++++++++++++++++++++++++++++ ale_linters/python/flake8.vim | 61 +++++++++++++++++++++++++++++++++++- 3 files changed, 108 insertions(+), 10 deletions(-) delete mode 100644 ale_linters/ansible/ansible-lint.vim create mode 100644 ale_linters/ansible/ansible_lint.vim (limited to 'ale_linters') diff --git a/ale_linters/ansible/ansible-lint.vim b/ale_linters/ansible/ansible-lint.vim deleted file mode 100644 index 7f641b6c..00000000 --- a/ale_linters/ansible/ansible-lint.vim +++ /dev/null @@ -1,9 +0,0 @@ -" Author: Bjorn Neergaard -" Description: ansible-lint for ansible-yaml files - -call ale#linter#Define('ansible', { -\ 'name': 'ansible', -\ 'executable': 'ansible', -\ 'command': 'ansible-lint -p %t', -\ 'callback': 'ale#handlers#python#HandlePEP8Format', -\}) diff --git a/ale_linters/ansible/ansible_lint.vim b/ale_linters/ansible/ansible_lint.vim new file mode 100644 index 00000000..3efd95fd --- /dev/null +++ b/ale_linters/ansible/ansible_lint.vim @@ -0,0 +1,48 @@ +" Author: Bjorn Neergaard +" Description: ansible-lint for ansible-yaml files + +function! ale_linters#ansible#ansible_lint#Handle(buffer, lines) abort + for l:line in a:lines[:10] + if match(l:line, '^Traceback') >= 0 + return [{ + \ 'lnum': 1, + \ 'text': 'An exception was thrown. See :ALEDetail', + \ 'detail': join(a:lines, "\n"), + \}] + endif + endfor + + " Matches patterns line the following: + " + " test.yml:35: [EANSIBLE0002] Trailing whitespace + let l:pattern = '\v^[a-zA-Z]?:?[^:]+:(\d+):?(\d+)?: \[?([[:alnum:]]+)\]? (.*)$' + let l:output = [] + + for l:match in ale#util#GetMatches(a:lines, l:pattern) + let l:code = l:match[3] + + if (l:code ==# 'EANSIBLE002') + \ && !ale#Var(a:buffer, 'warn_about_trailing_whitespace') + " Skip warnings for trailing whitespace if the option is off. + continue + endif + + let l:item = { + \ 'lnum': l:match[1] + 0, + \ 'col': l:match[2] + 0, + \ 'text': l:code . ': ' . l:match[4], + \ 'type': l:code[:0] ==# 'E' ? 'E' : 'W', + \} + + call add(l:output, l:item) + endfor + + return l:output +endfunction + +call ale#linter#Define('ansible', { +\ 'name': 'ansible', +\ 'executable': 'ansible', +\ 'command': 'ansible-lint -p %t', +\ 'callback': 'ale_linters#ansible#ansible_lint#Handle', +\}) diff --git a/ale_linters/python/flake8.vim b/ale_linters/python/flake8.vim index 253c7101..7af02d4e 100644 --- a/ale_linters/python/flake8.vim +++ b/ale_linters/python/flake8.vim @@ -94,6 +94,65 @@ function! ale_linters#python#flake8#GetCommand(buffer, version_output) abort \ . l:display_name_args . ' -' endfunction +let s:end_col_pattern_map = { +\ 'F405': '\(.\+\) may be undefined', +\ 'F821': 'undefined name ''\([^'']\+\)''', +\ 'F999': '^''\([^'']\+\)''', +\ 'F841': 'local variable ''\([^'']\+\)''', +\} + +function! ale_linters#python#flake8#Handle(buffer, lines) abort + for l:line in a:lines[:10] + if match(l:line, '^Traceback') >= 0 + return [{ + \ 'lnum': 1, + \ 'text': 'An exception was thrown. See :ALEDetail', + \ 'detail': join(a:lines, "\n"), + \}] + endif + endfor + + " Matches patterns line the following: + " + " Matches patterns line the following: + " + " stdin:6:6: E111 indentation is not a multiple of four + " test.yml:35: [EANSIBLE0002] Trailing whitespace + let l:pattern = '\v^[a-zA-Z]?:?[^:]+:(\d+):?(\d+)?: ([[:alnum:]]+) (.*)$' + let l:output = [] + + for l:match in ale#util#GetMatches(a:lines, l:pattern) + let l:code = l:match[3] + + if (l:code ==# 'W291' || l:code ==# 'W293') + \ && !ale#Var(a:buffer, 'warn_about_trailing_whitespace') + " Skip warnings for trailing whitespace if the option is off. + continue + endif + + let l:item = { + \ 'lnum': l:match[1] + 0, + \ 'col': l:match[2] + 0, + \ 'text': l:code . ': ' . l:match[4], + \ 'type': l:code[:0] ==# 'E' ? 'E' : 'W', + \} + + let l:end_col_pattern = get(s:end_col_pattern_map, l:code, '') + + if !empty(l:end_col_pattern) + let l:end_col_match = matchlist(l:match[4], l:end_col_pattern) + + if !empty(l:end_col_match) + let l:item.end_col = l:item.col + len(l:end_col_match[1]) - 1 + endif + endif + + call add(l:output, l:item) + endfor + + return l:output +endfunction + call ale#linter#Define('python', { \ 'name': 'flake8', \ 'executable_callback': 'ale_linters#python#flake8#GetExecutable', @@ -101,5 +160,5 @@ call ale#linter#Define('python', { \ {'callback': 'ale_linters#python#flake8#VersionCheck'}, \ {'callback': 'ale_linters#python#flake8#GetCommand', 'output_stream': 'both'}, \ ], -\ 'callback': 'ale#handlers#python#HandlePEP8Format', +\ 'callback': 'ale_linters#python#flake8#Handle', \}) -- cgit v1.2.3