]> git.madduck.net Git - etc/vim.git/blob - autoload/ale/fixers/ruff_format.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Squashed '.vim/bundle/ale/' content from commit 22185c4c
[etc/vim.git] / autoload / ale / fixers / ruff_format.vim
1 " Author: Yining <zhang.yining@gmail.com>, Joseph Henrich <crimsonknave@gmail.com>
2 " Description: ruff formatter as ALE fixer for python files
3
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)
11
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)
16
17         return !empty(l:project_root) ? l:project_root : '%s:h'
18     endif
19
20     return '%s:h'
21 endfunction
22
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)
26         return 'pipenv'
27     endif
28
29     if (ale#Var(a:buffer, 'python_auto_poetry') || ale#Var(a:buffer, 'python_ruff_format_auto_poetry'))
30     \ && ale#python#PoetryPresent(a:buffer)
31         return 'poetry'
32     endif
33
34     if (ale#Var(a:buffer, 'python_auto_uv') || ale#Var(a:buffer, 'python_ruff_format_auto_uv'))
35     \ && ale#python#UvPresent(a:buffer)
36         return 'uv'
37     endif
38
39     return ale#python#FindExecutable(a:buffer, 'python_ruff_format', ['ruff'])
40 endfunction
41
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\)$'
45     \   ? ' run ruff'
46     \   : ''
47
48     return ale#Escape(l:executable) . l:exec_args
49 endfunction
50
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)]
54
55     if l:executable =~? '\(pipenv\|poetry\|uv\)$'
56         call extend(l:cmd, ['run', 'ruff'])
57     endif
58
59     let l:options = ale#Var(a:buffer, 'python_ruff_format_options')
60
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')
65
66     if !empty(l:options)
67         call add(l:cmd, l:options)
68     endif
69
70     call add(l:cmd, '--stdin-filename '.ale#Escape(ale#path#Simplify(l:fname)))
71
72     call add(l:cmd, '-')
73
74     return {
75     \   'cwd': ale#fixers#ruff_format#GetCwd(a:buffer),
76     \   'command': join(l:cmd, ' '),
77     \}
78 endfunction