summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Evans <jack@evans.gb.net>2018-04-06 10:08:25 +0100
committerw0rp <w0rp@users.noreply.github.com>2018-04-06 11:08:25 +0200
commitc5d3af04fced61a654029a07139faa365c4a8534 (patch)
treef75aea22a44d8324c17cbf84eba60d234c53edfc
parent85a2a008266f69847a3c7a9d38c16b779636ab30 (diff)
downloadale-c5d3af04fced61a654029a07139faa365c4a8534.zip
Added support for Python black fixer (#1451)
-rw-r--r--README.md2
-rw-r--r--autoload/ale/fix/registry.vim5
-rw-r--r--autoload/ale/fixers/black.vim26
-rw-r--r--doc/ale-python.txt27
-rw-r--r--doc/ale.txt3
-rwxr-xr-xtest/command_callback/python_paths/with_virtualenv/env/Scripts/black.exe0
-rwxr-xr-xtest/command_callback/python_paths/with_virtualenv/env/bin/black0
-rw-r--r--test/fixers/test_black_fixer_callback.vader39
8 files changed, 100 insertions, 2 deletions
diff --git a/README.md b/README.md
index 26699126..5f06d945 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 4c0703a5..86a10908 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 00000000..6f6d56f3
--- /dev/null
+++ b/autoload/ale/fixers/black.vim
@@ -0,0 +1,26 @@
+" Author: w0rp <devw0rp@gmail.com>
+" 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 4d55e751..dc7c8bba 100644
--- a/doc/ale-python.txt
+++ b/doc/ale-python.txt
@@ -30,6 +30,33 @@ g:ale_python_autopep8_use_global *g:ale_python_autopep8_use_global*
===============================================================================
+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*
g:ale_python_flake8_executable *g:ale_python_flake8_executable*
diff --git a/doc/ale.txt b/doc/ale.txt
index 4ba8e649..2e5c5e47 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 00000000..e69de29b
--- /dev/null
+++ b/test/command_callback/python_paths/with_virtualenv/env/Scripts/black.exe
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 00000000..e69de29b
--- /dev/null
+++ b/test/command_callback/python_paths/with_virtualenv/env/bin/black
diff --git a/test/fixers/test_black_fixer_callback.vader b/test/fixers/test_black_fixer_callback.vader
new file mode 100644
index 00000000..365b0fa6
--- /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(''))