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>, Joseph Henrich <crimsonknave@gmail.com>
2 " Description: ruff formatter as ALE fixer for python files
4 call ale#Set('python_ruff_format_executable', 'ruff')
5 call ale#Set('python_ruff_format_options', '')
6 call ale#Set('python_ruff_format_use_global', get(g:, 'ale_use_global_executables', 0))
7 call ale#Set('python_ruff_format_change_directory', 1)
8 call ale#Set('python_ruff_format_auto_pipenv', 0)
9 call ale#Set('python_ruff_format_auto_poetry', 0)
10 call ale#Set('python_ruff_format_auto_uv', 0)
12 function! ale#fixers#ruff_format#GetCwd(buffer) abort
13 if ale#Var(a:buffer, 'python_ruff_format_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_format#GetExecutable(buffer) abort
24 if (ale#Var(a:buffer, 'python_auto_pipenv') || ale#Var(a:buffer, 'python_ruff_format_auto_pipenv'))
25 \ && ale#python#PipenvPresent(a:buffer)
29 if (ale#Var(a:buffer, 'python_auto_poetry') || ale#Var(a:buffer, 'python_ruff_format_auto_poetry'))
30 \ && ale#python#PoetryPresent(a:buffer)
34 if (ale#Var(a:buffer, 'python_auto_uv') || ale#Var(a:buffer, 'python_ruff_format_auto_uv'))
35 \ && ale#python#UvPresent(a:buffer)
39 return ale#python#FindExecutable(a:buffer, 'python_ruff_format', ['ruff'])
42 function! ale#fixers#ruff_format#GetCommand(buffer) abort
43 let l:executable = ale#fixers#ruff_format#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_format#Fix(buffer) abort
52 let l:executable = ale#fixers#ruff_format#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 let l:options = ale#Var(a:buffer, 'python_ruff_format_options')
61 " when --stdin-filename present, ruff will use it for proj root resolution
62 " https://github.com/charliermarsh/ruff/pull/1281
63 let l:fname = expand('#' . a:buffer . '...')
64 call add(l:cmd, 'format')
67 call add(l:cmd, l:options)
70 call add(l:cmd, '--stdin-filename '.ale#Escape(ale#path#Simplify(l:fname)))
75 \ 'cwd': ale#fixers#ruff_format#GetCwd(a:buffer),
76 \ 'command': join(l:cmd, ' '),