Add support for reorder-python-imports fixer

isort is great, but I've come to prefer reorder-python-imports. The tool
has a focus on smaller diffs than isort. reorder-python-imports is also
a little smarter than isort which is nice.
This commit is contained in:
Jake Kaufman 2019-06-05 23:18:33 -04:00
parent 7b78f2b846
commit 56641e0230
9 changed files with 112 additions and 0 deletions

View File

@ -310,6 +310,11 @@ let s:default_registry = {
\ 'suggested_filetypes': ['sql'],
\ 'description': 'A PostgreSQL SQL syntax beautifier',
\ },
\ 'reorder-python-imports': {
\ 'function': 'ale#fixers#reorder_python_imports#Fix',
\ 'suggested_filetypes': ['python'],
\ 'description': 'Sort Python imports with reorder-python-imports.',
\ },
\}
" Reset the function registry to the default entries.

View File

@ -0,0 +1,25 @@
" Author: jake <me@jake.computer>
" Description: Fixing Python imports with reorder-python-imports.
call ale#Set('python_reorder_python_imports_executable', 'reorder-python-imports')
call ale#Set('python_reorder_python_imports_options', '')
call ale#Set('python_reorder_python_imports_use_global', get(g:, 'ale_use_global_executables', 0))
function! ale#fixers#reorder_python_imports#Fix(buffer) abort
let l:executable = ale#python#FindExecutable(
\ a:buffer,
\ 'python_reorder_python_imports',
\ ['reorder-python-imports'],
\)
if !executable(l:executable)
return 0
endif
let l:options = ale#Var(a:buffer, 'python_reorder_python_imports_options')
return {
\ 'command': ale#Escape(l:executable)
\ . (!empty(l:options) ? ' ' . l:options : '') . ' -',
\}
endfunction

View File

@ -672,6 +672,36 @@ g:ale_python_pyre_auto_pipenv *g:ale_python_pyre_auto_pipenv*
if true. This is overridden by a manually-set executable.
===============================================================================
reorder-python-imports *ale-python-reorder_python_imports*
g:ale_python_reorder_python_imports_executable
*g:ale_python_reorder_python_imports_executable*
*b:ale_python_reorder_python_imports_executable*
Type: |String|
Default: `'reorder-python-imports'`
See |ale-integrations-local-executables|
g:ale_python_reorder_python_imports_options
*g:ale_python_reorder_python_imports_options*
*b:ale_python_reorder_python_imports_options*
Type: |String|
Default: `''`
This variable can be set to pass extra options to reorder-python-imports.
g:ale_python_reorder_python_imports_use_global
*g:ale_python_reorder_python_imports_use_global*
*b:ale_python_reorder_python_imports_use_global*
Type: |Number|
Default: `get(g:, 'ale_use_global_executables', 0)`
See |ale-integrations-local-executables|
===============================================================================
vulture *ale-python-vulture*

View File

@ -353,6 +353,7 @@ Notes:
* `pylint`!!
* `pyls`
* `pyre`
* `reorder-python-imports`
* `vulture`!!
* `yapf`
* QML

View File

@ -2184,6 +2184,7 @@ documented in additional help files.
pylint................................|ale-python-pylint|
pyls..................................|ale-python-pyls|
pyre..................................|ale-python-pyre|
reorder-python-imports................|ale-python-reorder_python_imports|
vulture...............................|ale-python-vulture|
yapf..................................|ale-python-yapf|
qml.....................................|ale-qml-options|

View File

@ -362,6 +362,7 @@ formatting.
* [pylint](https://www.pylint.org/) :floppy_disk:
* [pyls](https://github.com/palantir/python-language-server) :warning:
* [pyre](https://github.com/facebook/pyre-check) :warning:
* [reorder-python-imports](https://github.com/asottile/reorder_python_imports)
* [vulture](https://github.com/jendrikseipp/vulture) :warning: :floppy_disk:
* [yapf](https://github.com/google/yapf)
* QML

View File

@ -0,0 +1,49 @@
Before:
Save g:ale_python_reorder_python_imports_executable
Save g:ale_python_reorder_python_imports_options
" Use an invalid global executable, so we don't match it.
let g:ale_python_reorder_python_imports_executable = 'xxxinvalid'
let g:ale_python_reorder_python_imports_options = ''
call ale#test#SetDirectory('/testplugin/test/fixers')
silent cd ..
silent cd command_callback
let g:dir = getcwd()
let b:bin_dir = has('win32') ? 'Scripts' : 'bin'
After:
Restore
unlet! b:bin_dir
call ale#test#RestoreDirectory()
Execute(The reorder_python_imports callback should return the correct default values):
AssertEqual
\ 0,
\ ale#fixers#reorder_python_imports#Fix(bufnr(''))
silent execute 'file ' . fnameescape(g:dir . '/python_paths/with_virtualenv/subdir/foo/bar.py')
AssertEqual
\ {
\ 'command': ale#Escape(ale#path#Simplify(g:dir . '/python_paths/with_virtualenv/env/'
\ . b:bin_dir . '/reorder-python-imports')) . ' -',
\ },
\ ale#fixers#reorder_python_imports#Fix(bufnr(''))
Execute(The reorder_python_imports callback should respect custom options):
let g:ale_python_reorder_python_imports_options = '--py3-plus'
AssertEqual
\ 0,
\ ale#fixers#reorder_python_imports#Fix(bufnr(''))
silent execute 'file ' . fnameescape(g:dir . '/python_paths/with_virtualenv/subdir/foo/bar.py')
AssertEqual
\ {
\ 'command': ale#Escape(ale#path#Simplify(g:dir . '/python_paths/with_virtualenv/env/'
\ . b:bin_dir . '/reorder-python-imports')) . ' --py3-plus -',
\ },
\ ale#fixers#reorder_python_imports#Fix(bufnr(''))