diff options
author | Yining <zhang.yining@gmail.com> | 2022-12-22 11:53:01 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-22 09:53:01 +0900 |
commit | 1e398202b9a63fcd91808a3205d3422b79435fa0 (patch) | |
tree | a4c13b74b81c117dbd86558884fcf531ff1fdf68 /autoload | |
parent | 8bcbb2995822c4062d00cb41df713fd76092f04f (diff) | |
download | ale-1e398202b9a63fcd91808a3205d3422b79435fa0.zip |
fix: ruff not registered as fixer (#4393)
this commit fixes the issue reported at:
https://github.com/dense-analysis/ale/issues/4301#issuecomment-1359289391
it also had some code refactored and
tests added for ruff as fixer (missed in PR #4347).
Diffstat (limited to 'autoload')
-rw-r--r-- | autoload/ale/fix/registry.vim | 5 | ||||
-rw-r--r-- | autoload/ale/fixers/ruff.vim | 67 |
2 files changed, 56 insertions, 16 deletions
diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index b8d493f9..28e33926 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -580,6 +580,11 @@ let s:default_registry = { \ 'function': 'ale#fixers#raco_fmt#Fix', \ 'suggested_filetypes': ['racket'], \ 'description': 'Fix Racket files with raco fmt.', +\ }, +\ 'ruff': { +\ 'function': 'ale#fixers#ruff#Fix', +\ 'suggested_filetypes': ['python'], +\ 'description': 'Fix python files with ruff.', \ } \} diff --git a/autoload/ale/fixers/ruff.vim b/autoload/ale/fixers/ruff.vim index 92f9b75b..56bcf3df 100644 --- a/autoload/ale/fixers/ruff.vim +++ b/autoload/ale/fixers/ruff.vim @@ -1,6 +1,13 @@ " Author: Yining <zhang.yining@gmail.com> " Description: ruff as ALE fixer for python files +call ale#Set('python_ruff_executable', 'ruff') +call ale#Set('python_ruff_options', '') +call ale#Set('python_ruff_use_global', get(g:, 'ale_use_global_executables', 0)) +call ale#Set('python_ruff_change_directory', 1) +call ale#Set('python_ruff_auto_pipenv', 0) +call ale#Set('python_ruff_auto_poetry', 0) + function! ale#fixers#ruff#GetCwd(buffer) abort if ale#Var(a:buffer, 'python_ruff_change_directory') " Run from project root if found, else from buffer dir. @@ -9,7 +16,7 @@ function! ale#fixers#ruff#GetCwd(buffer) abort return !empty(l:project_root) ? l:project_root : '%s:h' endif - return '' + return '%s:h' endfunction function! ale#fixers#ruff#GetExecutable(buffer) abort @@ -26,29 +33,57 @@ function! ale#fixers#ruff#GetExecutable(buffer) abort return ale#python#FindExecutable(a:buffer, 'python_ruff', ['ruff']) endfunction -function! ale#fixers#ruff#GetCommand(buffer, version) abort - let l:executable = ale_linters#python#ruff#GetExecutable(a:buffer) +function! ale#fixers#ruff#GetCommand(buffer) abort + let l:executable = ale#fixers#ruff#GetExecutable(a:buffer) let l:exec_args = l:executable =~? 'pipenv\|poetry$' \ ? ' run ruff' \ : '' - " NOTE: ruff version `0.0.72` implement `--fix` with stdin return ale#Escape(l:executable) . l:exec_args - \ . ale#Pad(ale#Var(a:buffer, 'python_ruff_options')) - \ . ' --fix' - \ . (ale#semver#GTE(a:version, [0, 0, 72]) ? ' -' : ' %s') endfunction -function! ale#fixers#ruff#Fix(buffer) abort - let l:fix_cmd = {buffer -> ale#semver#RunWithVersionCheck( - \ buffer, - \ ale#fixers#ruff#GetExecutable(buffer), - \ '%e --version', - \ function('ale#fixers#ruff#GetCommand'), - \ )}(a:buffer) +function! ale#fixers#ruff#FixForVersion(buffer, version) abort + let l:executable = ale#fixers#ruff#GetExecutable(a:buffer) + let l:cmd = [ale#Escape(l:executable)] + + if l:executable =~? 'pipenv\|poetry$' + call extend(l:cmd, ['run', 'ruff']) + endif + + let l:options = ale#Var(a:buffer, 'python_ruff_options') + + if !empty(l:options) + call add(l:cmd, l:options) + endif + + " when --stdin-filename present, ruff will use it for proj root resolution + " https://github.com/charliermarsh/ruff/pull/1281 + let l:fname = expand('#' . a:buffer . '...') + call add(l:cmd, '--stdin-filename '.ale#Escape(ale#path#Simplify(l:fname))) + + call add(l:cmd, '--fix') + + " NOTE: ruff version `0.0.72` implements `--fix` with stdin + if ale#semver#GTE(a:version, [0, 0, 72]) + call add(l:cmd, '-') + else + call add(l:cmd, '%s') + endif return { - \ 'cwd': ale#fixers#ruff#GetCwd(a:buffer), - \ 'command': l:fix_cmd, + \ 'cwd': ale#fixers#ruff#GetCwd(a:buffer), + \ 'command': join(l:cmd, ' '), \} endfunction + +function! ale#fixers#ruff#Fix(buffer) abort + let l:executable = ale#fixers#ruff#GetExecutable(a:buffer) + let l:command = ale#fixers#ruff#GetCommand(a:buffer) . ale#Pad('--version') + + return ale#semver#RunWithVersionCheck( + \ a:buffer, + \ l:executable, + \ l:command, + \ function('ale#fixers#ruff#FixForVersion'), + \) +endfunction |