All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
1 " Author: Yining <zhang.yining@gmail.com>
2 " Description: ruff as ALE fixer for python files
4 call ale#Set('python_ruff_executable', 'ruff')
5 call ale#Set('python_ruff_options', '')
6 call ale#Set('python_ruff_use_global', get(g:, 'ale_use_global_executables', 0))
7 call ale#Set('python_ruff_change_directory', 1)
8 call ale#Set('python_ruff_auto_pipenv', 0)
9 call ale#Set('python_ruff_auto_poetry', 0)
10 call ale#Set('python_ruff_auto_uv', 0)
12 function! ale#fixers#ruff#GetCwd(buffer) abort
13 if ale#Var(a:buffer, 'python_ruff_change_directory')
14 " Run from project root if found, else from buffer dir.
15 let l:project_root = ale#python#FindProjectRoot(a:buffer)
17 return !empty(l:project_root) ? l:project_root : '%s:h'
23 function! ale#fixers#ruff#GetExecutable(buffer) abort
24 if (ale#Var(a:buffer, 'python_auto_pipenv') || ale#Var(a:buffer, 'python_ruff_auto_pipenv'))
25 \ && ale#python#PipenvPresent(a:buffer)
29 if (ale#Var(a:buffer, 'python_auto_poetry') || ale#Var(a:buffer, 'python_ruff_auto_poetry'))
30 \ && ale#python#PoetryPresent(a:buffer)
34 if (ale#Var(a:buffer, 'python_auto_uv') || ale#Var(a:buffer, 'python_ruff_auto_uv'))
35 \ && ale#python#UvPresent(a:buffer)
39 return ale#python#FindExecutable(a:buffer, 'python_ruff', ['ruff'])
42 function! ale#fixers#ruff#GetCommand(buffer) abort
43 let l:executable = ale#fixers#ruff#GetExecutable(a:buffer)
44 let l:exec_args = l:executable =~? '\(pipenv\|poetry\|uv\)$'
48 return ale#Escape(l:executable) . l:exec_args
51 function! ale#fixers#ruff#FixForVersion(buffer, version) abort
52 let l:executable = ale#fixers#ruff#GetExecutable(a:buffer)
53 let l:cmd = [ale#Escape(l:executable)]
55 if l:executable =~? '\(pipenv\|poetry\|uv\)$'
56 call extend(l:cmd, ['run', 'ruff'])
59 " NOTE: ruff 0.5.0 removes `ruff <path>` in favor of `ruff check <path>`
60 if ale#semver#GTE(a:version, [0, 5, 0])
61 call extend(l:cmd, ['check'])
64 let l:options = ale#Var(a:buffer, 'python_ruff_options')
67 call add(l:cmd, l:options)
70 " when --stdin-filename present, ruff will use it for proj root resolution
71 " https://github.com/charliermarsh/ruff/pull/1281
72 let l:fname = expand('#' . a:buffer . '...')
73 call add(l:cmd, '--stdin-filename '.ale#Escape(ale#path#Simplify(l:fname)))
75 call add(l:cmd, '--fix')
77 " NOTE: ruff version `0.0.72` implements `--fix` with stdin
78 if ale#semver#GTE(a:version, [0, 0, 72])
85 \ 'cwd': ale#fixers#ruff#GetCwd(a:buffer),
86 \ 'command': join(l:cmd, ' '),
90 function! ale#fixers#ruff#Fix(buffer) abort
91 let l:executable = ale#fixers#ruff#GetExecutable(a:buffer)
92 let l:command = ale#fixers#ruff#GetCommand(a:buffer) . ale#Pad('--version')
94 return ale#semver#RunWithVersionCheck(
98 \ function('ale#fixers#ruff#FixForVersion'),