add autoflake fixer (#3779)

* first attempt

* added autoflake executable

* added Windows executable for appveyor

* delete unused files

* corrected wrong sorting
This commit is contained in:
Nikolay Zakirov 2021-07-13 17:58:18 +05:00 committed by GitHub
parent d098124e59
commit 82a7e9f588
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 113 additions and 0 deletions

View File

@ -17,6 +17,11 @@ let s:default_registry = {
\ 'suggested_filetypes': ['python'],
\ 'description': 'Fix import issues with autoimport.',
\ },
\ 'autoflake': {
\ 'function': 'ale#fixers#autoflake#Fix',
\ 'suggested_filetypes': ['python'],
\ 'description': 'Fix flake issues with autoflake.',
\ },
\ 'autopep8': {
\ 'function': 'ale#fixers#autopep8#Fix',
\ 'suggested_filetypes': ['python'],

View File

@ -0,0 +1,28 @@
" Author: circld <circld1@gmail.com>
" Description: Fixing files with autoflake.
call ale#Set('python_autoflake_executable', 'autoflake')
call ale#Set('python_autoflake_use_global', get(g:, 'ale_use_global_executables', 0))
call ale#Set('python_autoflake_options', '')
function! ale#fixers#autoflake#Fix(buffer) abort
let l:executable = ale#python#FindExecutable(
\ a:buffer,
\ 'python_autoflake',
\ ['autoflake'],
\)
if !executable(l:executable)
return 0
endif
let l:options = ale#Var(a:buffer, 'python_autoflake_options')
return {
\ 'command': ale#Escape(l:executable)
\ . (!empty(l:options) ? ' ' . l:options : '')
\ . ' --in-place '
\ . ' %t',
\ 'read_temporary_file': 1,
\}
endfunction

View File

@ -43,6 +43,33 @@ ALE will look for configuration files with the following filenames. >
The first directory containing any of the files named above will be used.
===============================================================================
autoflake *ale-python-autoflake*
g:ale_python_autoflake_executable *g:ale_python_autoflake_executable*
*b:ale_python_autoflake_executable*
Type: |String|
Default: `'autoflake'`
See |ale-integrations-local-executables|
g:ale_python_autoflake_options *g:ale_python_autoflake_options*
*b:ale_python_autoflake_options*
Type: |String|
Default: `''`
This variable can be set to pass extra options to autoflake.
g:ale_python_autoflake_use_global *g:ale_python_autoflake_use_global*
*b:ale_python_autoflake_use_global*
Type: |Number|
Default: `get(g:, 'ale_use_global_executables', 0)`
See |ale-integrations-local-executables|
===============================================================================
autoimport *ale-python-autoimport*
@ -69,6 +96,7 @@ g:ale_python_autoimport_use_global *g:ale_python_autoimport_use_glob
See |ale-integrations-local-executables|
===============================================================================
autopep8 *ale-python-autopep8*

View File

@ -399,6 +399,7 @@ Notes:
* `purescript-language-server`
* `purty`
* Python
* `autoflake`!!
* `autoimport`
* `autopep8`
* `bandit`

View File

@ -2933,6 +2933,7 @@ documented in additional help files.
pyrex (cython)..........................|ale-pyrex-options|
cython................................|ale-pyrex-cython|
python..................................|ale-python-options|
autoflake.............................|ale-python-autoflake|
autoimport............................|ale-python-autoimport|
autopep8..............................|ale-python-autopep8|
bandit................................|ale-python-bandit|

View File

@ -408,6 +408,7 @@ formatting.
* [purescript-language-server](https://github.com/nwolverson/purescript-language-server)
* [purty](https://gitlab.com/joneshf/purty)
* Python
* [autoflake](https://github.com/myint/autoflake)
* [autoimport](https://lyz-code.github.io/autoimport/)
* [autopep8](https://github.com/hhatto/autopep8)
* [bandit](https://github.com/PyCQA/bandit) :warning:

View File

@ -0,0 +1,49 @@
Before:
Save g:ale_python_autoflake_executable
Save g:ale_python_autoflake_options
" Use an invalid global executable, so we don't match it.
let g:ale_python_autoflake_executable = 'xxxinvalid'
let g:ale_python_autoflake_options = ''
call ale#test#SetDirectory('/testplugin/test/fixers')
let g:dir = getcwd()
let b:bin_dir = has('win32') ? 'Scripts' : 'bin'
After:
Restore
unlet! b:bin_dir
call ale#test#RestoreDirectory()
Execute(The autoflake callback should return the correct default values):
AssertEqual
\ 0,
\ ale#fixers#autoflake#Fix(bufnr(''))
silent execute 'file ' . fnameescape(g:dir . '/../test-files/python/with_virtualenv/subdir/foo/bar.py')
AssertEqual
\ {
\ 'command': ale#Escape(ale#path#Simplify(g:dir . '/../test-files/python/with_virtualenv/env/' . b:bin_dir . '/autoflake'))
\ . ' --in-place '
\ . ' %t',
\ 'read_temporary_file': 1,
\ },
\ ale#fixers#autoflake#Fix(bufnr(''))
Execute(The autoflake callback should include options):
let g:ale_python_autoflake_options = '--some-option'
silent execute 'file ' . fnameescape(g:dir . '/../test-files/python/with_virtualenv/subdir/foo/bar.py')
AssertEqual
\ {
\ 'command': ale#Escape(ale#path#Simplify(g:dir . '/../test-files/python/with_virtualenv/env/' . b:bin_dir . '/autoflake'))
\ . ' --some-option'
\ . ' --in-place '
\ . ' %t',
\ 'read_temporary_file': 1,
\ },
\ ale#fixers#autoflake#Fix(bufnr(''))

View File