summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLyz <lyz@riseup.net>2020-10-23 18:53:38 +0200
committerLyz <lyz@riseup.net>2020-10-23 18:53:38 +0200
commit513e6ee972ea4ee57b28b8b8c10e0b89bb674f25 (patch)
tree0ea0ac99a370b4de35b7f1d82bc0739e0f756b83
parent557a1ed5da70cb443a8650766f4e8ea95e8c0da3 (diff)
downloadale-513e6ee972ea4ee57b28b8b8c10e0b89bb674f25.zip
feat: add autoimport fixer
-rw-r--r--autoload/ale/fix/registry.vim5
-rw-r--r--autoload/ale/fixers/autoimport.vim25
-rw-r--r--doc/ale-python.txt26
-rw-r--r--doc/ale-supported-languages-and-tools.txt1
-rw-r--r--doc/ale.txt1
-rw-r--r--supported-tools.md1
-rwxr-xr-xtest/command_callback/python_paths/with_virtualenv/env/Scripts/autoimport.exe0
-rwxr-xr-xtest/command_callback/python_paths/with_virtualenv/env/bin/autoimport0
-rw-r--r--test/fixers/test_autoimport_fixer_callback.vader50
9 files changed, 109 insertions, 0 deletions
diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim
index d71668f2..ed9c4370 100644
--- a/autoload/ale/fix/registry.vim
+++ b/autoload/ale/fix/registry.vim
@@ -12,6 +12,11 @@ let s:default_registry = {
\ 'suggested_filetypes': ['help'],
\ 'description': 'Align help tags to the right margin',
\ },
+\ 'autoimport': {
+\ 'function': 'ale#fixers#autoimport#Fix',
+\ 'suggested_filetypes': ['python'],
+\ 'description': 'Fix import issues with autoimport.',
+\ },
\ 'autopep8': {
\ 'function': 'ale#fixers#autopep8#Fix',
\ 'suggested_filetypes': ['python'],
diff --git a/autoload/ale/fixers/autoimport.vim b/autoload/ale/fixers/autoimport.vim
new file mode 100644
index 00000000..37a52db8
--- /dev/null
+++ b/autoload/ale/fixers/autoimport.vim
@@ -0,0 +1,25 @@
+" Author: lyz-code
+" Description: Fixing Python imports with autoimport.
+
+call ale#Set('python_autoimport_executable', 'autoimport')
+call ale#Set('python_autoimport_options', '')
+call ale#Set('python_autoimport_use_global', get(g:, 'ale_use_global_executables', 0))
+
+function! ale#fixers#autoimport#Fix(buffer) abort
+ let l:options = ale#Var(a:buffer, 'python_autoimport_options')
+
+ let l:executable = ale#python#FindExecutable(
+ \ a:buffer,
+ \ 'python_autoimport',
+ \ ['autoimport'],
+ \)
+
+ if !executable(l:executable)
+ return 0
+ endif
+
+ return {
+ \ 'command': ale#path#BufferCdString(a:buffer)
+ \ . ale#Escape(l:executable) . (!empty(l:options) ? ' ' . l:options : '') . ' -',
+ \}
+endfunction
diff --git a/doc/ale-python.txt b/doc/ale-python.txt
index 6b1a6d33..c6a207ac 100644
--- a/doc/ale-python.txt
+++ b/doc/ale-python.txt
@@ -42,6 +42,32 @@ The first directory containing any of the files named above will be used.
===============================================================================
+autoimport *ale-python-autoimport*
+
+g:ale_python_autoimport_executable *g:ale_python_autoimport_executable*
+ *b:ale_python_autoimport_executable*
+ Type: |String|
+ Default: `'autoimport'`
+
+ See |ale-integrations-local-executables|
+
+
+g:ale_python_autoimport_options *g:ale_python_autoimport_options*
+ *b:ale_python_autoimport_options*
+ Type: |String|
+ Default: `''`
+
+ This variable can be set to pass extra options to autoimport.
+
+
+g:ale_python_autoimport_use_global *g:ale_python_autoimport_use_global*
+ *b:ale_python_autoimport_use_global*
+ Type: |Number|
+ Default: `get(g:, 'ale_use_global_executables', 0)`
+
+ See |ale-integrations-local-executables|
+
+===============================================================================
autopep8 *ale-python-autopep8*
g:ale_python_autopep8_executable *g:ale_python_autopep8_executable*
diff --git a/doc/ale-supported-languages-and-tools.txt b/doc/ale-supported-languages-and-tools.txt
index 4f3afd85..62eed26f 100644
--- a/doc/ale-supported-languages-and-tools.txt
+++ b/doc/ale-supported-languages-and-tools.txt
@@ -361,6 +361,7 @@ Notes:
* `purescript-language-server`
* `purty`
* Python
+ * `autoimport`
* `autopep8`
* `bandit`
* `black`
diff --git a/doc/ale.txt b/doc/ale.txt
index eb8f0275..204bd0e2 100644
--- a/doc/ale.txt
+++ b/doc/ale.txt
@@ -2777,6 +2777,7 @@ documented in additional help files.
pyrex (cython)..........................|ale-pyrex-options|
cython................................|ale-pyrex-cython|
python..................................|ale-python-options|
+ autoimport............................|ale-python-autoimport|
autopep8..............................|ale-python-autopep8|
bandit................................|ale-python-bandit|
black.................................|ale-python-black|
diff --git a/supported-tools.md b/supported-tools.md
index 49460892..6325e906 100644
--- a/supported-tools.md
+++ b/supported-tools.md
@@ -370,6 +370,7 @@ formatting.
* [purescript-language-server](https://github.com/nwolverson/purescript-language-server)
* [purty](https://gitlab.com/joneshf/purty)
* Python
+ * [autoimport](https://lyz-code.github.io/autoimport/)
* [autopep8](https://github.com/hhatto/autopep8)
* [bandit](https://github.com/PyCQA/bandit) :warning:
* [black](https://github.com/ambv/black)
diff --git a/test/command_callback/python_paths/with_virtualenv/env/Scripts/autoimport.exe b/test/command_callback/python_paths/with_virtualenv/env/Scripts/autoimport.exe
new file mode 100755
index 00000000..e69de29b
--- /dev/null
+++ b/test/command_callback/python_paths/with_virtualenv/env/Scripts/autoimport.exe
diff --git a/test/command_callback/python_paths/with_virtualenv/env/bin/autoimport b/test/command_callback/python_paths/with_virtualenv/env/bin/autoimport
new file mode 100755
index 00000000..e69de29b
--- /dev/null
+++ b/test/command_callback/python_paths/with_virtualenv/env/bin/autoimport
diff --git a/test/fixers/test_autoimport_fixer_callback.vader b/test/fixers/test_autoimport_fixer_callback.vader
new file mode 100644
index 00000000..6952cbb8
--- /dev/null
+++ b/test/fixers/test_autoimport_fixer_callback.vader
@@ -0,0 +1,50 @@
+Before:
+ Save g:ale_python_autoimport_executable
+ Save g:ale_python_autoimport_options
+
+ " Use an invalid global executable, so we don't match it.
+ let g:ale_python_autoimport_executable = 'xxxinvalid'
+ let g:ale_python_autoimport_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 autoimport callback should return the correct default values):
+ AssertEqual
+ \ 0,
+ \ ale#fixers#autoimport#Fix(bufnr(''))
+
+ silent execute 'file ' . fnameescape(g:dir . '/python_paths/with_virtualenv/subdir/foo/bar.py')
+ AssertEqual
+ \ {
+ \ 'command': ale#path#BufferCdString(bufnr(''))
+ \ . ale#Escape(ale#path#Simplify(g:dir . '/python_paths/with_virtualenv/env/' . b:bin_dir . '/autoimport')) . ' -',
+ \ },
+ \ ale#fixers#autoimport#Fix(bufnr(''))
+
+Execute(The autoimport callback should respect custom options):
+ let g:ale_python_autoimport_options = '--multi-line=3 --trailing-comma'
+
+ AssertEqual
+ \ 0,
+ \ ale#fixers#autoimport#Fix(bufnr(''))
+
+ silent execute 'file ' . fnameescape(g:dir . '/python_paths/with_virtualenv/subdir/foo/bar.py')
+ AssertEqual
+ \ {
+ \ 'command': ale#path#BufferCdString(bufnr(''))
+ \ . ale#Escape(ale#path#Simplify(g:dir . '/python_paths/with_virtualenv/env/' . b:bin_dir . '/autoimport'))
+ \ . ' --multi-line=3 --trailing-comma -',
+ \ },
+ \ ale#fixers#autoimport#Fix(bufnr(''))