#289 Only use the --stdin-display-name flag if the flake8 version supports it

This commit is contained in:
w0rp 2017-02-06 11:12:21 +00:00
parent a9a76241ac
commit a3b7056cad
3 changed files with 100 additions and 3 deletions

View File

@ -7,18 +7,70 @@ let g:ale_python_flake8_executable =
let g:ale_python_flake8_args =
\ get(g:, 'ale_python_flake8_args', '')
" A map from Python executable paths to semver strings parsed for those
" executables, so we don't have to look up the version number constantly.
let s:version_cache = {}
function! ale_linters#python#flake8#GetExecutable(buffer) abort
return g:ale_python_flake8_executable
endfunction
function! ale_linters#python#flake8#GetCommand(buffer) abort
function! ale_linters#python#flake8#VersionCheck(buffer) abort
let l:executable = ale_linters#python#flake8#GetExecutable(a:buffer)
" If we have previously stored the version number in a cache, then
" don't look it up again.
if has_key(s:version_cache, l:executable)
" Returning an empty string skips this command.
return ''
endif
return ale_linters#python#flake8#GetExecutable(a:buffer) . ' --version'
endfunction
" Get the flake8 version from the output, or the cache.
function! s:GetVersion(buffer, version_output) abort
let l:executable = ale_linters#python#flake8#GetExecutable(a:buffer)
let l:version = []
" Get the version from the cache.
if has_key(s:version_cache, l:executable)
return s:version_cache[l:executable]
endif
if !empty(a:version_output)
" Parse the version string, and store it in the cache.
let l:version = ale#semver#Parse(a:version_output[0])
let s:version_cache[l:executable] = l:version
endif
return l:version
endfunction
" flake8 versions 3 and up support the --stdin-display-name argument.
function! s:SupportsDisplayName(version) abort
return !empty(a:version) && ale#semver#GreaterOrEqual(a:version, [3, 0, 0])
endfunction
function! ale_linters#python#flake8#GetCommand(buffer, version_output) abort
let l:version = s:GetVersion(a:buffer, a:version_output)
" Only include the --stdin-display-name argument if we can parse the
" flake8 version, and it is recent enough to support it.
let l:display_name_args = s:SupportsDisplayName(l:version)
\ ? '--stdin-display-name %s'
\ : ''
return ale_linters#python#flake8#GetExecutable(a:buffer)
\ . ' ' . g:ale_python_flake8_args . ' --stdin-display-name %s -'
\ . ' ' . g:ale_python_flake8_args . ' ' . l:display_name_args . ' -'
endfunction
call ale#linter#Define('python', {
\ 'name': 'flake8',
\ 'executable_callback': 'ale_linters#python#flake8#GetExecutable',
\ 'command_callback': 'ale_linters#python#flake8#GetCommand',
\ 'command_chain': [
\ {'callback': 'ale_linters#python#flake8#VersionCheck'},
\ {'callback': 'ale_linters#python#flake8#GetCommand'},
\ ],
\ 'callback': 'ale#handlers#HandlePEP8Format',
\})

29
autoload/ale/semver.vim Normal file
View File

@ -0,0 +1,29 @@
" Given some text, parse a semantic versioning string from the text
" into a triple of integeers [major, minor, patch].
"
" If no match can be performed, then an empty List will be returned instead.
function! ale#semver#Parse(text) abort
let l:match = matchlist(a:text, '^ *\(\d\+\)\.\(\d\+\)\.\(\d\+\)')
if empty(l:match)
return []
endif
return [l:match[1] + 0, l:match[2] + 0, l:match[3] + 0]
endfunction
" Given two triples of integers [major, minor, patch], compare the triples
" and return 1 if the lhs is greater than or equal to the rhs.
function! ale#semver#GreaterOrEqual(lhs, rhs) abort
if a:lhs[0] > a:rhs[0]
return 1
elseif a:lhs[0] == a:rhs[0]
if a:lhs[1] > a:rhs[1]
return 1
elseif a:lhs[1] == a:rhs[1]
return a:lhs[2] >= a:rhs[2]
endif
endif
return 0
endfunction

View File

@ -0,0 +1,16 @@
Execute(ParseSemver should return the correct results):
" We should be able to parse the semver string from flake8
AssertEqual [3, 0, 4], ale#semver#Parse('3.0.4 (mccabe: 0.5.2, pyflakes: 1.2.3, pycodestyle: 2.0.0) CPython 2.7.12 on Linux')
Execute(GreaterOrEqual should compare triples correctly):
Assert ale#semver#GreaterOrEqual([3, 0, 4], [3, 0, 0])
Assert ale#semver#GreaterOrEqual([3, 0, 0], [3, 0, 0])
Assert ale#semver#GreaterOrEqual([3, 0, 0], [2, 0, 0])
Assert ale#semver#GreaterOrEqual([3, 1, 0], [3, 1, 0])
Assert ale#semver#GreaterOrEqual([3, 2, 0], [3, 1, 0])
Assert ale#semver#GreaterOrEqual([3, 2, 2], [3, 1, 6])
Assert ale#semver#GreaterOrEqual([3, 2, 5], [3, 2, 5])
Assert ale#semver#GreaterOrEqual([3, 2, 6], [3, 2, 5])
Assert !ale#semver#GreaterOrEqual([2, 9, 1], [3, 0, 0])
Assert !ale#semver#GreaterOrEqual([3, 2, 3], [3, 3, 3])
Assert !ale#semver#GreaterOrEqual([3, 3, 2], [3, 3, 3])