diff options
author | Yining <zhang.yining@gmail.com> | 2023-01-27 12:24:18 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-27 10:24:18 +0900 |
commit | b6f6d84c25169744f43db6d0ee296ee44eb78d91 (patch) | |
tree | 7df3bc78af81f76a7ed2813f90ccae96a8d0e16b /autoload | |
parent | 26c79014f52b0306ec619666bc73766f22621edb (diff) | |
download | ale-b6f6d84c25169744f43db6d0ee296ee44eb78d91.zip |
add: pycln as a python linter and fixer (#4415)
this commit adds pycln as a Python linter and fixer, together with some
tests and documentation. It addresses #4340
pycln repo: https://github.com/hadialqattan/pycln
Diffstat (limited to 'autoload')
-rw-r--r-- | autoload/ale/fix/registry.vim | 5 | ||||
-rw-r--r-- | autoload/ale/fixers/pycln.vim | 90 |
2 files changed, 95 insertions, 0 deletions
diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 28e33926..6657fc3c 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -585,6 +585,11 @@ let s:default_registry = { \ 'function': 'ale#fixers#ruff#Fix', \ 'suggested_filetypes': ['python'], \ 'description': 'Fix python files with ruff.', +\ }, +\ 'pycln': { +\ 'function': 'ale#fixers#pycln#Fix', +\ 'suggested_filetypes': ['python'], +\ 'description': 'remove unused python import statements', \ } \} diff --git a/autoload/ale/fixers/pycln.vim b/autoload/ale/fixers/pycln.vim new file mode 100644 index 00000000..1f61d083 --- /dev/null +++ b/autoload/ale/fixers/pycln.vim @@ -0,0 +1,90 @@ +" Author: Yining <zhang.yining@gmail.com> +" Description: pycln as ALE fixer for python files + +call ale#Set('python_pycln_executable', 'pycln') +call ale#Set('python_pycln_options', '') +call ale#Set('python_pycln_use_global', get(g:, 'ale_use_global_executables', 0)) +call ale#Set('python_pycln_change_directory', 1) +call ale#Set('python_pycln_auto_pipenv', 0) +call ale#Set('python_pycln_auto_poetry', 0) +call ale#Set('python_pycln_config_file', '') + +function! ale#fixers#pycln#GetCwd(buffer) abort + if ale#Var(a:buffer, 'python_pycln_change_directory') + " Run from project root if found, else from buffer dir. + let l:project_root = ale#python#FindProjectRoot(a:buffer) + + return !empty(l:project_root) ? l:project_root : '%s:h' + endif + + return '%s:h' +endfunction + +function! ale#fixers#pycln#GetExecutable(buffer) abort + if (ale#Var(a:buffer, 'python_auto_pipenv') || ale#Var(a:buffer, 'python_pycln_auto_pipenv')) + \ && ale#python#PipenvPresent(a:buffer) + return 'pipenv' + endif + + if (ale#Var(a:buffer, 'python_auto_poetry') || ale#Var(a:buffer, 'python_pycln_auto_poetry')) + \ && ale#python#PoetryPresent(a:buffer) + return 'poetry' + endif + + return ale#python#FindExecutable(a:buffer, 'python_pycln', ['pycln']) +endfunction + +function! ale#fixers#pycln#GetCommand(buffer) abort + let l:executable = ale#fixers#pycln#GetExecutable(a:buffer) + let l:exec_args = l:executable =~? 'pipenv\|poetry$' + \ ? ' run pycln' + \ : '' + + return ale#Escape(l:executable) . l:exec_args +endfunction + +function! ale#fixers#pycln#FixForVersion(buffer, version) abort + let l:executable = ale#fixers#pycln#GetExecutable(a:buffer) + let l:cmd = [ale#Escape(l:executable)] + + if l:executable =~? 'pipenv\|poetry$' + call extend(l:cmd, ['run', 'pycln']) + endif + + let l:options = ale#Var(a:buffer, 'python_pycln_options') + + if !empty(l:options) + call add(l:cmd, l:options) + endif + + let l:config_file = ale#Var(a:buffer, 'python_pycln_config_file') + let l:config_file = l:options !~# '\v(^| )--config ' && !empty(l:config_file) + \ ? ale#Escape(ale#path#Simplify(l:config_file)) + \ : '' + + if !empty(l:config_file) + call add(l:cmd, '--config ' . l:config_file) + endif + + call add(l:cmd, '--silence') + + " NOTE: pycln version `1.3.0` support reading from stdin + call add(l:cmd, ale#semver#GTE(a:version, [1, 3, 0]) ? '-' : '%s') + + return { + \ 'cwd': ale#fixers#pycln#GetCwd(a:buffer), + \ 'command': join(l:cmd, ' '), + \} +endfunction + +function! ale#fixers#pycln#Fix(buffer) abort + let l:executable = ale#fixers#pycln#GetExecutable(a:buffer) + let l:command = ale#fixers#pycln#GetCommand(a:buffer) . ale#Pad('--version') + + return ale#semver#RunWithVersionCheck( + \ a:buffer, + \ l:executable, + \ l:command, + \ function('ale#fixers#pycln#FixForVersion'), + \) +endfunction |