Merge pull request #2858 from kalekseev/patch-flake8

Provide configuration option to run flake8 from project root.
This commit is contained in:
w0rp 2020-08-31 09:01:16 +01:00 committed by GitHub
commit 31942c99b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 12 deletions

View file

@ -4,7 +4,7 @@
call ale#Set('python_flake8_executable', 'flake8')
call ale#Set('python_flake8_options', '')
call ale#Set('python_flake8_use_global', get(g:, 'ale_use_global_executables', 0))
call ale#Set('python_flake8_change_directory', 1)
call ale#Set('python_flake8_change_directory', 'project')
call ale#Set('python_flake8_auto_pipenv', 0)
function! s:UsingModule(buffer) abort
@ -38,10 +38,34 @@ function! ale_linters#python#flake8#RunWithVersionCheck(buffer) abort
\)
endfunction
function! ale_linters#python#flake8#GetCdString(buffer) abort
let l:change_directory = ale#Var(a:buffer, 'python_flake8_change_directory')
" map legacy options to new ones
if l:change_directory is# 1
let l:change_directory = 'file'
elseif l:change_directory is# 0
let l:change_directory = 'off'
endif
if l:change_directory is# 'file'
return ale#path#BufferCdString(a:buffer)
elseif l:change_directory is# 'off'
return ''
endif
let l:project_root = ale#python#FindProjectRootIni(a:buffer)
if !empty(l:project_root)
return ale#path#CdString(l:project_root)
endif
return ale#path#BufferCdString(a:buffer)
endfunction
function! ale_linters#python#flake8#GetCommand(buffer, version) abort
let l:cd_string = ale#Var(a:buffer, 'python_flake8_change_directory')
\ ? ale#path#BufferCdString(a:buffer)
\ : ''
let l:cd_string = ale_linters#python#flake8#GetCdString(a:buffer)
let l:executable = ale_linters#python#flake8#GetExecutable(a:buffer)
let l:exec_args = l:executable =~? 'pipenv$'

View file

@ -169,13 +169,14 @@ flake8 *ale-python-flake8*
g:ale_python_flake8_change_directory *g:ale_python_flake8_change_directory*
*b:ale_python_flake8_change_directory*
Type: |Number|
Default: `1`
Type: |String|
Default: `project`
If set to `1`, ALE will switch to the directory the Python file being
checked with `flake8` is in before checking it. This helps `flake8` find
configuration files more easily. This option can be turned off if you want
to control the directory Python is executed from yourself.
If set to `project`, ALE will switch to the project root before checking file.
If set to `file`, ALE will switch to directory the Python file being
checked with `flake8` is in before checking it.
You can turn it off with `off` option if you want to control the directory
Python is executed from yourself.
g:ale_python_flake8_executable *g:ale_python_flake8_executable*

View file

@ -34,13 +34,32 @@ Execute(The flake8 callbacks should return the correct default values):
\]
Execute(The option for disabling changing directories should work):
let g:ale_python_flake8_change_directory = 0
let g:ale_python_flake8_change_directory = 'off'
AssertLinter 'flake8', [
\ ale#Escape('flake8') . ' --version',
\ ale#Escape('flake8') . ' --format=default --stdin-display-name %s -',
\]
Execute(The option for changing directory to project root should work):
silent execute 'file ' . fnameescape(g:dir . '/python_paths/namespace_package_tox/namespace/foo/bar.py')
AssertLinter 'flake8', [
\ ale#Escape('flake8') . ' --version',
\ ale#path#CdString(ale#python#FindProjectRootIni(bufnr('')))
\ . ale#Escape('flake8') . ' --format=default --stdin-display-name %s -',
\]
Execute(The option for changing directory to file dir should work):
let g:ale_python_flake8_change_directory = 'file'
silent execute 'file ' . fnameescape(g:dir . '/python_paths/namespace_package_tox/namespace/foo/bar.py')
AssertLinter 'flake8', [
\ ale#Escape('flake8') . ' --version',
\ ale#path#BufferCdString(bufnr(''))
\ . ale#Escape('flake8') . ' --format=default --stdin-display-name %s -',
\]
Execute(The flake8 command callback should let you set options):
let g:ale_python_flake8_options = '--some-option'
@ -163,5 +182,5 @@ Execute(Pipenv is detected when python_flake8_auto_pipenv is set):
call ale#test#SetFilename('../python_fixtures/pipenv/whatever.py')
AssertLinter 'pipenv',
\ ale#path#BufferCdString(bufnr(''))
\ ale#path#CdString(ale#python#FindProjectRootIni(bufnr('')))
\ . ale#Escape('pipenv') . ' run flake8 --format=default --stdin-display-name %s -'