]> git.madduck.net Git - etc/vim.git/blob - .vim/bundle/ale/ale_linters/python/ruff.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:

Merge commit '294584081929424aec883f90c7d6515b3743358d' as '.vim/bundle/vim-lsp-ale'
[etc/vim.git] / .vim / bundle / ale / ale_linters / python / ruff.vim
1 " Author: Yining <zhang.yining@gmail.com>
2 " Description: ruff as linter for python files
3
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)
11
12 call ale#fix#registry#Add('ruff',
13 \   'ale#fixers#ruff#Fix',
14 \   ['python'],
15 \   'A python linter/fixer for Python written in Rust'
16 \)
17
18 function! ale_linters#python#ruff#GetExecutable(buffer) abort
19     if (ale#Var(a:buffer, 'python_auto_pipenv') || ale#Var(a:buffer, 'python_ruff_auto_pipenv'))
20     \ && ale#python#PipenvPresent(a:buffer)
21         return 'pipenv'
22     endif
23
24     if (ale#Var(a:buffer, 'python_auto_poetry') || ale#Var(a:buffer, 'python_ruff_auto_poetry'))
25     \ && ale#python#PoetryPresent(a:buffer)
26         return 'poetry'
27     endif
28
29     if (ale#Var(a:buffer, 'python_auto_uv') || ale#Var(a:buffer, 'python_ruff_auto_uv'))
30     \ && ale#python#UvPresent(a:buffer)
31         return 'uv'
32     endif
33
34     return ale#python#FindExecutable(a:buffer, 'python_ruff', ['ruff'])
35 endfunction
36
37 function! ale_linters#python#ruff#GetCwd(buffer) abort
38     if ale#Var(a:buffer, 'python_ruff_change_directory')
39         " Run from project root if found, else from buffer dir.
40         let l:project_root = ale#python#FindProjectRoot(a:buffer)
41
42         return !empty(l:project_root) ? l:project_root : '%s:h'
43     endif
44
45     return ''
46 endfunction
47
48 function! ale_linters#python#ruff#GetCommand(buffer, version) abort
49     let l:executable = ale_linters#python#ruff#GetExecutable(a:buffer)
50     let l:exec_args = l:executable =~? '\(pipenv\|poetry\|uv\)$'
51     \   ? ' run ruff'
52     \   : ''
53
54     " NOTE: ruff 0.3.0 deprecates `ruff <path>` in favor of `ruff check <path>`
55     let l:exec_args = l:exec_args
56     \   . (ale#semver#GTE(a:version, [0, 3, 0]) ? ' check' : '')
57
58     " NOTE: ruff version `0.0.69` supports linting input from stdin
59     " NOTE: ruff version `0.1.0` deprecates `--format text`
60     return ale#Escape(l:executable) . l:exec_args . ' -q'
61     \   . ' --no-fix'
62     \   . ale#Pad(ale#Var(a:buffer, 'python_ruff_options'))
63     \   . (ale#semver#GTE(a:version, [0, 1, 0]) ? ' --output-format json-lines' : ' --format json-lines')
64     \   . (ale#semver#GTE(a:version, [0, 0, 69]) ? ' --stdin-filename %s -' : ' %s')
65 endfunction
66
67 function! ale_linters#python#ruff#Handle(buffer, lines) abort
68     let l:output = []
69
70     " Read all lines of ruff output and parse use all the valid JSONL lines.
71     for l:line in a:lines
72         try
73             let l:item = json_decode(l:line)
74         catch
75             let l:item = v:null
76         endtry
77
78         if !empty(l:item)
79             call add(l:output, {
80             \   'lnum': l:item.location.row,
81             \   'col': l:item.location.column,
82             \   'end_lnum': l:item.end_location.row,
83             \   'end_col': l:item.end_location.column - 1,
84             \   'code': l:item.code,
85             \   'text': l:item.message,
86             \   'type': l:item.code =~? '\vE\d+' ? 'E' : 'W',
87             \})
88         endif
89     endfor
90
91     return l:output
92 endfunction
93
94 call ale#linter#Define('python', {
95 \   'name': 'ruff',
96 \   'executable': function('ale_linters#python#ruff#GetExecutable'),
97 \   'cwd': function('ale_linters#python#ruff#GetCwd'),
98 \   'command': {buffer -> ale#semver#RunWithVersionCheck(
99 \       buffer,
100 \       ale_linters#python#ruff#GetExecutable(buffer),
101 \       '%e --version',
102 \       function('ale_linters#python#ruff#GetCommand'),
103 \   )},
104 \   'callback': 'ale_linters#python#ruff#Handle',
105 \   'output_stream': 'both',
106 \   'read_buffer': 1,
107 \})