From 56641e02301475cfee9ddcaf547066898f073048 Mon Sep 17 00:00:00 2001 From: Jake Kaufman Date: Wed, 5 Jun 2019 23:18:33 -0400 Subject: 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. --- autoload/ale/fix/registry.vim | 5 +++ autoload/ale/fixers/reorder_python_imports.vim | 25 +++++++++++ doc/ale-python.txt | 30 +++++++++++++ doc/ale-supported-languages-and-tools.txt | 1 + doc/ale.txt | 1 + supported-tools.md | 1 + .../env/Scripts/reorder-python-imports.exe | 0 .../with_virtualenv/env/bin/reorder-python-imports | 0 ...est_reorder_python_imports_fixer_callback.vader | 49 ++++++++++++++++++++++ 9 files changed, 112 insertions(+) create mode 100644 autoload/ale/fixers/reorder_python_imports.vim create mode 100755 test/command_callback/python_paths/with_virtualenv/env/Scripts/reorder-python-imports.exe create mode 100755 test/command_callback/python_paths/with_virtualenv/env/bin/reorder-python-imports create mode 100644 test/fixers/test_reorder_python_imports_fixer_callback.vader diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 925181ca..f7bd3af6 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -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. diff --git a/autoload/ale/fixers/reorder_python_imports.vim b/autoload/ale/fixers/reorder_python_imports.vim new file mode 100644 index 00000000..42a0a6e2 --- /dev/null +++ b/autoload/ale/fixers/reorder_python_imports.vim @@ -0,0 +1,25 @@ +" Author: jake +" 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 diff --git a/doc/ale-python.txt b/doc/ale-python.txt index 43cdf648..9d5846d2 100644 --- a/doc/ale-python.txt +++ b/doc/ale-python.txt @@ -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* diff --git a/doc/ale-supported-languages-and-tools.txt b/doc/ale-supported-languages-and-tools.txt index 9487829e..4d851201 100644 --- a/doc/ale-supported-languages-and-tools.txt +++ b/doc/ale-supported-languages-and-tools.txt @@ -353,6 +353,7 @@ Notes: * `pylint`!! * `pyls` * `pyre` + * `reorder-python-imports` * `vulture`!! * `yapf` * QML diff --git a/doc/ale.txt b/doc/ale.txt index f0820be6..23cbacf5 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -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| diff --git a/supported-tools.md b/supported-tools.md index a34a3f90..7d21de8b 100644 --- a/supported-tools.md +++ b/supported-tools.md @@ -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 diff --git a/test/command_callback/python_paths/with_virtualenv/env/Scripts/reorder-python-imports.exe b/test/command_callback/python_paths/with_virtualenv/env/Scripts/reorder-python-imports.exe new file mode 100755 index 00000000..e69de29b diff --git a/test/command_callback/python_paths/with_virtualenv/env/bin/reorder-python-imports b/test/command_callback/python_paths/with_virtualenv/env/bin/reorder-python-imports new file mode 100755 index 00000000..e69de29b diff --git a/test/fixers/test_reorder_python_imports_fixer_callback.vader b/test/fixers/test_reorder_python_imports_fixer_callback.vader new file mode 100644 index 00000000..30b64eb8 --- /dev/null +++ b/test/fixers/test_reorder_python_imports_fixer_callback.vader @@ -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('')) -- cgit v1.2.3