From c5d3af04fced61a654029a07139faa365c4a8534 Mon Sep 17 00:00:00 2001 From: Jack Evans Date: Fri, 6 Apr 2018 10:08:25 +0100 Subject: [PATCH] Added support for Python black fixer (#1451) --- README.md | 2 +- autoload/ale/fix/registry.vim | 5 +++ autoload/ale/fixers/black.vim | 26 +++++++++++++ doc/ale-python.txt | 27 +++++++++++++ doc/ale.txt | 3 +- .../with_virtualenv/env/Scripts/black.exe | 0 .../with_virtualenv/env/bin/black | 0 test/fixers/test_black_fixer_callback.vader | 39 +++++++++++++++++++ 8 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 autoload/ale/fixers/black.vim create mode 100755 test/command_callback/python_paths/with_virtualenv/env/Scripts/black.exe create mode 100755 test/command_callback/python_paths/with_virtualenv/env/bin/black create mode 100644 test/fixers/test_black_fixer_callback.vader diff --git a/README.md b/README.md index 2669912..5f06d94 100644 --- a/README.md +++ b/README.md @@ -139,7 +139,7 @@ formatting. | proto | [protoc-gen-lint](https://github.com/ckaznocha/protoc-gen-lint) | | Pug | [pug-lint](https://github.com/pugjs/pug-lint) | | Puppet | [puppet](https://puppet.com), [puppet-lint](https://puppet-lint.com) | -| Python | [autopep8](https://github.com/hhatto/autopep8), [flake8](http://flake8.pycqa.org/en/latest/), [isort](https://github.com/timothycrosley/isort), [mypy](http://mypy-lang.org/), [prospector](http://github.com/landscapeio/prospector), [pycodestyle](https://github.com/PyCQA/pycodestyle), [pyls](https://github.com/palantir/python-language-server), [pylint](https://www.pylint.org/) !!, [yapf](https://github.com/google/yapf) | +| Python | [autopep8](https://github.com/hhatto/autopep8), [black](https://github.com/ambv/black), [flake8](http://flake8.pycqa.org/en/latest/), [isort](https://github.com/timothycrosley/isort), [mypy](http://mypy-lang.org/), [prospector](http://github.com/landscapeio/prospector), [pycodestyle](https://github.com/PyCQA/pycodestyle), [pyls](https://github.com/palantir/python-language-server), [pylint](https://www.pylint.org/) !!, [yapf](https://github.com/google/yapf) | | QML | [qmlfmt](https://github.com/jesperhh/qmlfmt), [qmllint](https://github.com/qt/qtdeclarative/tree/5.11/tools/qmllint) | | R | [lintr](https://github.com/jimhester/lintr) | | ReasonML | [merlin](https://github.com/the-lambda-church/merlin) see `:help ale-reasonml-ols` for configuration instructions, [ols](https://github.com/freebroccolo/ocaml-language-server), [refmt](https://github.com/reasonml/reason-cli) | diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 4c0703a..86a1090 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -17,6 +17,11 @@ let s:default_registry = { \ 'suggested_filetypes': ['python'], \ 'description': 'Fix PEP8 issues with autopep8.', \ }, +\ 'black': { +\ 'function': 'ale#fixers#black#Fix', +\ 'suggested_filetypes': ['python'], +\ 'description': 'Fix PEP8 issues with black.', +\ }, \ 'prettier_standard': { \ 'function': 'ale#fixers#prettier_standard#Fix', \ 'suggested_filetypes': ['javascript'], diff --git a/autoload/ale/fixers/black.vim b/autoload/ale/fixers/black.vim new file mode 100644 index 0000000..6f6d56f --- /dev/null +++ b/autoload/ale/fixers/black.vim @@ -0,0 +1,26 @@ +" Author: w0rp +" Description: Fixing Python files with black. +" +call ale#Set('python_black_executable', 'black') +call ale#Set('python_black_use_global', 0) +call ale#Set('python_black_options', '') + +function! ale#fixers#black#Fix(buffer) abort + let l:executable = ale#python#FindExecutable( + \ a:buffer, + \ 'python_black', + \ ['black'], + \) + + if !executable(l:executable) + return 0 + endif + + let l:options = ale#Var(a:buffer, 'python_black_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 4d55e75..dc7c8bb 100644 --- a/doc/ale-python.txt +++ b/doc/ale-python.txt @@ -29,6 +29,33 @@ g:ale_python_autopep8_use_global *g:ale_python_autopep8_use_global* See |ale-integrations-local-executables| +=============================================================================== +black *ale-python-black* + +g:ale_python_black_executable *g:ale_python_black_executable* + *b:ale_python_black_executable* + Type: |String| + Default: `'black'` + + See |ale-integrations-local-executables| + +autopep8 +g:ale_python_black_options *g:ale_python_black_options* + *b:ale_python_black_options* + Type: |String| + Default: `''` + + This variable can be set to pass extra options to black. + + +g:ale_python_black_use_global *g:ale_python_black_use_global* + *b:ale_python_black_use_global* + Type: |Number| + Default: `0` + + See |ale-integrations-local-executables| + + =============================================================================== flake8 *ale-python-flake8* diff --git a/doc/ale.txt b/doc/ale.txt index 4ba8e64..2e5c5e4 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -179,6 +179,7 @@ CONTENTS *ale-contents* puppetlint..........................|ale-puppet-puppetlint| python................................|ale-python-options| autopep8............................|ale-python-autopep8| + black...............................|ale-python-black| flake8..............................|ale-python-flake8| isort...............................|ale-python-isort| mypy................................|ale-python-mypy| @@ -369,7 +370,7 @@ Notes: * proto: `protoc-gen-lint` * Pug: `pug-lint` * Puppet: `puppet`, `puppet-lint` -* Python: `autopep8`, `flake8`, `isort`, `mypy`, `prospector`, `pycodestyle`, `pyls`, `pylint`!!, `yapf` +* Python: `autopep8`, `black`, `flake8`, `isort`, `mypy`, `prospector`, `pycodestyle`, `pyls`, `pylint`!!, `yapf` * QML: `qmlfmt`, `qmllint` * R: `lintr` * ReasonML: `merlin`, `ols`, `refmt` diff --git a/test/command_callback/python_paths/with_virtualenv/env/Scripts/black.exe b/test/command_callback/python_paths/with_virtualenv/env/Scripts/black.exe new file mode 100755 index 0000000..e69de29 diff --git a/test/command_callback/python_paths/with_virtualenv/env/bin/black b/test/command_callback/python_paths/with_virtualenv/env/bin/black new file mode 100755 index 0000000..e69de29 diff --git a/test/fixers/test_black_fixer_callback.vader b/test/fixers/test_black_fixer_callback.vader new file mode 100644 index 0000000..365b0fa --- /dev/null +++ b/test/fixers/test_black_fixer_callback.vader @@ -0,0 +1,39 @@ +Before: + Save g:ale_python_black_executable + Save g:ale_python_black_options + + " Use an invalid global executable, so we don't match it. + let g:ale_python_black_executable = 'xxxinvalid' + let g:ale_python_black_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 black callback should return the correct default values): + AssertEqual + \ 0, + \ ale#fixers#black#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 . '/black')) . ' -'}, + \ ale#fixers#black#Fix(bufnr('')) + +Execute(The black callback should include options): + let g:ale_python_black_options = '--some-option' + + 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 . '/black')) . ' --some-option -' }, + \ ale#fixers#black#Fix(bufnr(''))