diff options
author | pmacosta <pmacosta@users.noreply.github.com> | 2018-12-06 13:27:03 -0500 |
---|---|---|
committer | Bjorn Neergaard <bjorn@neersighted.com> | 2018-12-06 11:27:03 -0700 |
commit | fdd37acc1f381230fc2ed87303e98c7a4daafaad (patch) | |
tree | f7e139d3f7c3d7c3b683efa1fd8f0171cd647f75 /ale_linters/python | |
parent | 2760cf7018893b42c4ae5c750a7f1d38669aa821 (diff) | |
download | ale-fdd37acc1f381230fc2ed87303e98c7a4daafaad.zip |
Add support for pydocstyle linter (#2085)
The linter can correctly parse pydocstyle output with any of the following
command-line options enabled: --explain, --source, --debug, and/or
--verbose
Diffstat (limited to 'ale_linters/python')
-rw-r--r-- | ale_linters/python/pydocstyle.vim | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/ale_linters/python/pydocstyle.vim b/ale_linters/python/pydocstyle.vim new file mode 100644 index 00000000..ebf92bf1 --- /dev/null +++ b/ale_linters/python/pydocstyle.vim @@ -0,0 +1,74 @@ +" Author: Pablo Acosta <pmasdev@gmail.com> +" Description: pydocstyle for python files + +call ale#Set('python_pydocstyle_executable', 'pydocstyle') +call ale#Set('python_pydocstyle_options', '') +call ale#Set('python_pydocstyle_use_global', get(g:, 'ale_use_global_executables', 0)) +call ale#Set('python_pydocstyle_auto_pipenv', 0) + +function! ale_linters#python#pydocstyle#GetExecutable(buffer) abort + if (ale#Var(a:buffer, 'python_auto_pipenv') || ale#Var(a:buffer, 'python_pydocstyle_auto_pipenv')) + \ && ale#python#PipenvPresent(a:buffer) + return 'pipenv' + endif + + return ale#python#FindExecutable(a:buffer, 'python_pydocstyle', ['pydocstyle']) +endfunction + +function! ale_linters#python#pydocstyle#GetCommand(buffer) abort + let l:dir = fnamemodify(bufname(a:buffer), ':p:h') + let l:executable = ale_linters#python#pydocstyle#GetExecutable(a:buffer) + + let l:exec_args = l:executable =~? 'pipenv$' + \ ? ' run pydocstyle' + \ : '' + + return ale#path#CdString(l:dir) + \ . ale#Escape(l:executable) . l:exec_args + \ . ' ' . ale#Var(a:buffer, 'python_pydocstyle_options') + \ . ' ' . ale#Escape(fnamemodify(bufname(a:buffer), ':p:t')) +endfunction + +function! ale_linters#python#pydocstyle#Handle(buffer, lines) abort + " Matches patterns like the following: + " mydir/myfile.py:33 in public function `myfunction`: + " DXXX: Error description + let l:fname = ale#Escape(fnamemodify(bufname(a:buffer), ':p:t')) + let l:line1_pattern = '\v^' . l:fname . ':\s*(\d+)\s+.*$' + let l:line2_pattern = '\v^.*([a-zA-Z]\d+):\s*(.*)$' + let l:output = [] + + let l:num_lines = len(a:lines) + let l:index = 0 + + while l:index < l:num_lines + let l:lnum = matchlist(a:lines[l:index], l:line1_pattern) + + if !empty(l:lnum) && (l:index + 1 < l:num_lines) + let l:desc = matchlist(a:lines[l:index + 1], l:line2_pattern) + + if !empty(l:desc) + call add(l:output, { + \ 'lnum': l:lnum[1] + 0, + \ 'col': 1, + \ 'type': 'W', + \ 'text': l:desc[2], + \ 'code': l:desc[1], + \}) + endif + + let l:index = l:index + 2 + else + let l:index = l:index + 1 + endif + endwhile + + return l:output +endfunction + +call ale#linter#Define('python', { +\ 'name': 'pydocstyle', +\ 'executable_callback': 'ale_linters#python#pydocstyle#GetExecutable', +\ 'command_callback': 'ale_linters#python#pydocstyle#GetCommand', +\ 'callback': 'ale_linters#python#pydocstyle#Handle', +\}) |