Skip to content

Commit 9a23ec1

Browse files
0xhtmlw0rp
andauthored
Ruff use json-lines output format (dense-analysis#4656)
* Ruff use json-lines output format * Fix Ruff: add -q to prevent non json output Using the json-lines output format allows for setting of the end_line, end_col and code field of the handle output. Additionally, the first letter of the code is used to determine the type field. Co-authored-by: w0rp <[email protected]>
1 parent ecc796b commit 9a23ec1

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

ale_linters/python/ruff.vim

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,25 @@ function! ale_linters#python#ruff#GetCommand(buffer, version) abort
4747

4848
" NOTE: ruff version `0.0.69` supports liniting input from stdin
4949
" NOTE: ruff version `0.1.0` deprecates `--format text`
50-
return ale#Escape(l:executable) . l:exec_args
50+
return ale#Escape(l:executable) . l:exec_args . ' -q'
5151
\ . ale#Pad(ale#Var(a:buffer, 'python_ruff_options'))
52-
\ . (ale#semver#GTE(a:version, [0, 1, 0]) ? ' --output-format text' : ' --format text')
52+
\ . (ale#semver#GTE(a:version, [0, 1, 0]) ? ' --output-format json-lines' : ' --format json-lines')
5353
\ . (ale#semver#GTE(a:version, [0, 0, 69]) ? ' --stdin-filename %s -' : ' %s')
5454
endfunction
5555

5656
function! ale_linters#python#ruff#Handle(buffer, lines) abort
57-
"Example: path/to/file.py:10:5: E999 SyntaxError: unexpected indent
58-
let l:pattern = '\v^[a-zA-Z]?:?[^:]+:(\d+):(\d+)?:? (.+)$'
5957
let l:output = []
6058

61-
for l:match in ale#util#GetMatches(a:lines, l:pattern)
59+
for l:line in a:lines
60+
let l:item = json_decode(l:line)
6261
call add(l:output, {
63-
\ 'lnum': l:match[1] + 0,
64-
\ 'col': l:match[2] + 0,
65-
\ 'text': l:match[3],
62+
\ 'lnum': l:item.location.row,
63+
\ 'col': l:item.location.column,
64+
\ 'end_lnum': l:item.end_location.row,
65+
\ 'end_col': l:item.end_location.column - 1,
66+
\ 'code': l:item.code,
67+
\ 'text': l:item.message,
68+
\ 'type': l:item.code =~? '\vE\d+' ? 'E' : 'W',
6669
\})
6770
endfor
6871

test/linter/test_ruff.vader

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ Before:
66
call ale#assert#SetUpLinterTest('python', 'ruff')
77

88
let b:bin_dir = has('win32') ? 'Scripts' : 'bin'
9-
let b:command_tail = ' --format text --stdin-filename %s -'
9+
let b:command_head = ale#Escape('ruff') . ' -q'
10+
let b:command_tail = ' --format json-lines --stdin-filename %s -'
1011

1112
GivenCommandOutput ['ruff 0.0.83']
1213

@@ -19,93 +20,93 @@ After:
1920

2021
Execute(The ruff callbacks should return the correct default values):
2122
AssertLinterCwd expand('%:p:h')
22-
AssertLinter 'ruff', ale#Escape('ruff') . b:command_tail
23+
AssertLinter 'ruff', b:command_head . b:command_tail
2324

2425
Execute(ruff should run with the file path of buffer in old versions):
2526
" version `0.0.69` supports liniting input from stdin
2627
GivenCommandOutput ['ruff 0.0.68']
2728

2829
AssertLinterCwd expand('%:p:h')
29-
AssertLinter 'ruff', ale#Escape('ruff') . b:command_tail[:-23] . ' %s'
30+
AssertLinter 'ruff', b:command_head . b:command_tail[:-23] . ' %s'
3031

3132
Execute(ruff should run with the --output-format flag in new versions):
3233
GivenCommandOutput ['ruff 0.1.0']
3334

3435
AssertLinterCwd expand('%:p:h')
35-
AssertLinter 'ruff', ale#Escape('ruff') . ' --output-format text --stdin-filename %s -'
36+
AssertLinter 'ruff', b:command_head . ' --output-format json-lines --stdin-filename %s -'
3637

3738
Execute(ruff should run with the stdin in new enough versions):
3839
GivenCommandOutput ['ruff 0.0.83']
3940

4041
AssertLinterCwd expand('%:p:h')
41-
AssertLinter 'ruff', ale#Escape('ruff') . b:command_tail[:-3] . ' -'
42-
" AssertLinter 'ruff', ale#Escape('ruff') . b:command_tail[:-3] . '--format text -'
42+
AssertLinter 'ruff', b:command_head . b:command_tail[:-3] . ' -'
43+
" AssertLinter 'ruff', b:command_head . b:command_tail[:-3] . '--format json-lines -'
4344

4445
Execute(The option for disabling changing directories should work):
4546
let g:ale_python_ruff_change_directory = 0
4647

4748
AssertLinterCwd ''
48-
AssertLinter 'ruff', ale#Escape('ruff') . b:command_tail
49+
AssertLinter 'ruff', b:command_head . b:command_tail
4950

5051
Execute(The ruff executable should be configurable, and escaped properly):
5152
let g:ale_python_ruff_executable = 'executable with spaces'
5253

53-
AssertLinter 'executable with spaces', ale#Escape('executable with spaces') . b:command_tail
54+
AssertLinter 'executable with spaces', ale#Escape('executable with spaces') . ' -q' . b:command_tail
5455

5556
Execute(The ruff command callback should let you set options):
5657
let g:ale_python_ruff_options = '--some-flag'
57-
AssertLinter 'ruff', ale#Escape('ruff') . ' --some-flag' . b:command_tail
58+
AssertLinter 'ruff', b:command_head . ' --some-flag' . b:command_tail
5859

5960
let g:ale_python_ruff_options = '--some-option value'
60-
AssertLinter 'ruff', ale#Escape('ruff') . ' --some-option value' . b:command_tail
61+
AssertLinter 'ruff', b:command_head . ' --some-option value' . b:command_tail
6162

6263
Execute(The ruff callbacks shouldn't detect virtualenv directories where they don't exist):
6364
call ale#test#SetFilename('../test-files/python/no_virtualenv/subdir/foo/bar.py')
6465

6566
AssertLinterCwd ale#path#Simplify(g:dir . '/../test-files/python/no_virtualenv/subdir')
66-
AssertLinter 'ruff', ale#Escape('ruff') . b:command_tail
67+
AssertLinter 'ruff', b:command_head . b:command_tail
6768

6869
Execute(The ruff callbacks should detect virtualenv directories):
6970
call ale#test#SetFilename('../test-files/python/with_virtualenv/subdir/foo/bar.py')
7071
let b:executable = ale#path#Simplify(
7172
\ g:dir . '/../test-files/python/with_virtualenv/env/' . b:bin_dir . '/ruff'
7273
\)
7374
AssertLinterCwd ale#path#Simplify(g:dir . '/../test-files/python/with_virtualenv/subdir')
74-
AssertLinter b:executable, ale#Escape(b:executable) . b:command_tail
75+
AssertLinter b:executable, ale#Escape(b:executable) . ' -q' . b:command_tail
7576

7677
Execute(You should able able to use the global ruff instead):
7778
call ale#test#SetFilename('../test-files/python/with_virtualenv/subdir/foo/bar.py')
7879
let g:ale_python_ruff_use_global = 1
7980

8081
AssertLinterCwd ale#path#Simplify(g:dir . '/../test-files/python/with_virtualenv/subdir')
81-
AssertLinter 'ruff', ale#Escape('ruff') . b:command_tail
82+
AssertLinter 'ruff', b:command_head . b:command_tail
8283

8384
Execute(Setting executable to 'pipenv' appends 'run ruff'):
8485
let g:ale_python_ruff_executable = 'path/to/pipenv'
8586
let g:ale_python_ruff_use_global = 1
8687

87-
AssertLinter 'path/to/pipenv', ale#Escape('path/to/pipenv') . ' run ruff'
88+
AssertLinter 'path/to/pipenv', ale#Escape('path/to/pipenv') . ' run ruff -q'
8889
\ . b:command_tail
8990

9091
Execute(Pipenv is detected when python_ruff_auto_pipenv is set):
9192
let g:ale_python_ruff_auto_pipenv = 1
9293
call ale#test#SetFilename('../test-files/python/pipenv/whatever.py')
9394

9495
AssertLinterCwd expand('%:p:h')
95-
AssertLinter 'pipenv', ale#Escape('pipenv') . ' run ruff'
96+
AssertLinter 'pipenv', ale#Escape('pipenv') . ' run ruff -q'
9697
\ . b:command_tail
9798

9899
Execute(Setting executable to 'poetry' appends 'run ruff'):
99100
let g:ale_python_ruff_executable = 'path/to/poetry'
100101
let g:ale_python_ruff_use_global = 1
101102

102-
AssertLinter 'path/to/poetry', ale#Escape('path/to/poetry') . ' run ruff'
103+
AssertLinter 'path/to/poetry', ale#Escape('path/to/poetry') . ' run ruff -q'
103104
\ . b:command_tail
104105

105106
Execute(poetry is detected when python_ruff_auto_poetry is set):
106107
let g:ale_python_ruff_auto_poetry = 1
107108
call ale#test#SetFilename('../test-files/python/poetry/whatever.py')
108109

109110
AssertLinterCwd expand('%:p:h')
110-
AssertLinter 'poetry', ale#Escape('poetry') . ' run ruff'
111+
AssertLinter 'poetry', ale#Escape('poetry') . ' run ruff -q'
111112
\ . b:command_tail

0 commit comments

Comments
 (0)