Render tabs as 4 spaces in diagnostics (#4132)

This commit is contained in:
Micha Reiser 2023-05-02 15:14:02 +02:00 committed by GitHub
parent ac600bb3da
commit b14358fbfe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 229 additions and 176 deletions

View file

@ -8,7 +8,8 @@ use bitflags::bitflags;
use colored::Colorize;
use ruff_diagnostics::DiagnosticKind;
use ruff_python_ast::source_code::{OneIndexed, SourceLocation};
use ruff_text_size::TextRange;
use ruff_text_size::{TextRange, TextSize};
use std::borrow::Cow;
use std::fmt::{Display, Formatter};
use std::io::Write;
@ -172,6 +173,7 @@ impl Display for MessageCodeFrame<'_> {
};
let source_code = file.to_source_code();
let content_start_index = source_code.line_index(range.start());
let mut start_index = content_start_index.saturating_sub(2);
@ -200,26 +202,23 @@ impl Display for MessageCodeFrame<'_> {
let start_offset = source_code.line_start(start_index);
let end_offset = source_code.line_end(end_index);
let source_text = source_code.slice(TextRange::new(start_offset, end_offset));
let source = replace_whitespace(
source_code.slice(TextRange::new(start_offset, end_offset)),
range - start_offset,
);
let annotation_start_offset = range.start() - start_offset;
let annotation_end_offset = range.end() - start_offset;
let start_char = source_text[TextRange::up_to(annotation_start_offset)]
let start_char = source.text[TextRange::up_to(source.annotation_range.start())]
.chars()
.count();
let char_length = source_text
[TextRange::new(annotation_start_offset, annotation_end_offset)]
.chars()
.count();
let char_length = source.text[source.annotation_range].chars().count();
let label = kind.rule().noqa_code().to_string();
let snippet = Snippet {
title: None,
slices: vec![Slice {
source: source_text,
source: &source.text,
line_start: content_start_index.get(),
annotations: vec![SourceAnnotation {
label: &label,
@ -245,6 +244,60 @@ impl Display for MessageCodeFrame<'_> {
}
}
fn replace_whitespace(source: &str, annotation_range: TextRange) -> SourceCode {
static TAB_SIZE: TextSize = TextSize::new(4);
let mut result = String::new();
let mut last_end = 0;
let mut range = annotation_range;
let mut column = 0;
for (index, m) in source.match_indices(['\t', '\n', '\r']) {
match m {
"\t" => {
let tab_width = TAB_SIZE - TextSize::new(column % 4);
if index < usize::from(annotation_range.start()) {
range += tab_width - TextSize::new(1);
} else if index < usize::from(annotation_range.end()) {
range = range.add_end(tab_width - TextSize::new(1));
}
result.push_str(&source[last_end..index]);
for _ in 0..u32::from(tab_width) {
result.push(' ');
}
last_end = index + 1;
}
"\n" | "\r" => {
column = 0;
}
_ => unreachable!(),
}
}
// No tabs
if result.is_empty() {
SourceCode {
annotation_range,
text: Cow::Borrowed(source),
}
} else {
result.push_str(&source[last_end..]);
SourceCode {
annotation_range: range,
text: Cow::Owned(result),
}
}
}
struct SourceCode<'a> {
text: Cow<'a, str>,
annotation_range: TextRange,
}
#[cfg(test)]
mod tests {
use crate::message::tests::{capture_emitter_output, create_messages};

View file

@ -5,8 +5,8 @@ E101.py:11:1: E101 Indentation contains mixed spaces and tabs
|
11 | def func_mixed_start_with_tab():
12 | # E101
13 | print("mixed starts with tab")
| ^^ E101
13 | print("mixed starts with tab")
| ^^^^^^ E101
14 |
15 | def func_mixed_start_with_space():
|
@ -15,8 +15,8 @@ E101.py:15:1: E101 Indentation contains mixed spaces and tabs
|
15 | def func_mixed_start_with_space():
16 | # E101
17 | print("mixed starts with space")
| ^^^^^^^^ E101
17 | print("mixed starts with space")
| ^^^^^^^^^^^^^^^^^^^^ E101
18 |
19 | def xyz():
|
@ -25,8 +25,8 @@ E101.py:19:1: E101 Indentation contains mixed spaces and tabs
|
19 | def xyz():
20 | # E101
21 | print("xyz");
| ^^^ E101
21 | print("xyz");
| ^^^^^^^ E101
|

View file

@ -25,8 +25,8 @@ E11.py:42:1: E117 Over-indented
|
42 | #: E117 W191
43 | def start():
44 | print()
| E117
44 | print()
| ^^^^^^^^ E117
|

View file

@ -27,34 +27,34 @@ E20.py:6:15: E201 Whitespace after '('
8 | spam(ham[1], { eggs: 2})
| E201
9 | #: E201:1:6
10 | spam( ham[1], {eggs: 2})
10 | spam( ham[1], {eggs: 2})
|
E20.py:8:6: E201 Whitespace after '('
|
8 | spam(ham[1], { eggs: 2})
9 | #: E201:1:6
10 | spam( ham[1], {eggs: 2})
10 | spam( ham[1], {eggs: 2})
| E201
11 | #: E201:1:10
12 | spam(ham[ 1], {eggs: 2})
12 | spam(ham[ 1], {eggs: 2})
|
E20.py:10:10: E201 Whitespace after '('
|
10 | spam( ham[1], {eggs: 2})
10 | spam( ham[1], {eggs: 2})
11 | #: E201:1:10
12 | spam(ham[ 1], {eggs: 2})
12 | spam(ham[ 1], {eggs: 2})
| E201
13 | #: E201:1:15
14 | spam(ham[1], { eggs: 2})
14 | spam(ham[1], { eggs: 2})
|
E20.py:12:15: E201 Whitespace after '('
|
12 | spam(ham[ 1], {eggs: 2})
12 | spam(ham[ 1], {eggs: 2})
13 | #: E201:1:15
14 | spam(ham[1], { eggs: 2})
14 | spam(ham[1], { eggs: 2})
| E201
15 | #: Okay
16 | spam(ham[1], {eggs: 2})

View file

@ -27,34 +27,34 @@ E20.py:23:11: E202 Whitespace before ')'
25 | spam(ham[1 ], {eggs: 2})
| E202
26 | #: E202:1:23
27 | spam(ham[1], {eggs: 2} )
27 | spam(ham[1], {eggs: 2} )
|
E20.py:25:23: E202 Whitespace before ')'
|
25 | spam(ham[1 ], {eggs: 2})
26 | #: E202:1:23
27 | spam(ham[1], {eggs: 2} )
27 | spam(ham[1], {eggs: 2} )
| E202
28 | #: E202:1:22
29 | spam(ham[1], {eggs: 2 })
29 | spam(ham[1], {eggs: 2 })
|
E20.py:27:22: E202 Whitespace before ')'
|
27 | spam(ham[1], {eggs: 2} )
27 | spam(ham[1], {eggs: 2} )
28 | #: E202:1:22
29 | spam(ham[1], {eggs: 2 })
29 | spam(ham[1], {eggs: 2 })
| E202
30 | #: E202:1:11
31 | spam(ham[1 ], {eggs: 2})
31 | spam(ham[1 ], {eggs: 2})
|
E20.py:29:11: E202 Whitespace before ')'
|
29 | spam(ham[1], {eggs: 2 })
29 | spam(ham[1], {eggs: 2 })
30 | #: E202:1:11
31 | spam(ham[1 ], {eggs: 2})
31 | spam(ham[1 ], {eggs: 2})
| E202
32 | #: Okay
33 | spam(ham[1], {eggs: 2})

View file

@ -14,7 +14,7 @@ E20.py:55:10: E203 Whitespace before ',', ';', or ':'
|
55 | x, y = y, x
56 | #: E203:1:10
57 | if x == 4 :
57 | if x == 4 :
| E203
58 | print x, y
59 | x, y = y, x
@ -34,7 +34,7 @@ E20.py:63:15: E203 Whitespace before ',', ';', or ':'
|
63 | #: E203:2:15 E702:2:16
64 | if x == 4:
65 | print x, y ; x, y = y, x
65 | print x, y ; x, y = y, x
| E203
66 | #: E203:3:13
67 | if x == 4:
@ -54,7 +54,7 @@ E20.py:71:13: E203 Whitespace before ',', ';', or ':'
|
71 | if x == 4:
72 | print x, y
73 | x, y = y , x
73 | x, y = y , x
| E203
74 | #: Okay
75 | if x == 4:

View file

@ -5,7 +5,7 @@ E22.py:43:2: E223 Tab before operator
|
43 | #: E223
44 | foobart = 4
45 | a = 3 # aligned with tab
45 | a = 3 # aligned with tab
| E223
46 | #:
|

View file

@ -4,7 +4,7 @@ source: crates/ruff/src/rules/pycodestyle/mod.rs
E22.py:48:5: E224 Tab after operator
|
48 | #: E224
49 | a += 1
49 | a += 1
| E224
50 | b += 1000
51 | #:

View file

@ -28,12 +28,12 @@ E27.py:8:3: E271 Multiple spaces after keyword
10 | if 1:
| E271
11 | #: E273
12 | True and False
12 | True and False
|
E27.py:14:6: E271 Multiple spaces after keyword
|
14 | True and False
14 | True and False
15 | #: E271
16 | a and b
| E271

View file

@ -28,7 +28,7 @@ E27.py:24:5: E272 Multiple spaces before keyword
26 | this and False
| E272
27 | #: E273
28 | a and b
28 | a and b
|

View file

@ -5,17 +5,17 @@ E27.py:10:9: E273 Tab after keyword
|
10 | if 1:
11 | #: E273
12 | True and False
12 | True and False
| E273
13 | #: E273 E274
14 | True and False
14 | True and False
|
E27.py:12:5: E273 Tab after keyword
|
12 | True and False
12 | True and False
13 | #: E273 E274
14 | True and False
14 | True and False
| E273
15 | #: E271
16 | a and b
@ -23,10 +23,10 @@ E27.py:12:5: E273 Tab after keyword
E27.py:12:10: E273 Tab after keyword
|
12 | True and False
12 | True and False
13 | #: E273 E274
14 | True and False
| E273
14 | True and False
| E273
15 | #: E271
16 | a and b
|
@ -35,18 +35,18 @@ E27.py:26:6: E273 Tab after keyword
|
26 | this and False
27 | #: E273
28 | a and b
28 | a and b
| E273
29 | #: E274
30 | a and b
30 | a and b
|
E27.py:30:10: E273 Tab after keyword
|
30 | a and b
30 | a and b
31 | #: E273 E274
32 | this and False
| E273
32 | this and False
| E273
33 | #: Okay
34 | from u import (a, b)
|

View file

@ -3,20 +3,20 @@ source: crates/ruff/src/rules/pycodestyle/mod.rs
---
E27.py:28:3: E274 Tab before keyword
|
28 | a and b
28 | a and b
29 | #: E274
30 | a and b
| E274
30 | a and b
| E274
31 | #: E273 E274
32 | this and False
32 | this and False
|
E27.py:30:6: E274 Tab before keyword
|
30 | a and b
30 | a and b
31 | #: E273 E274
32 | this and False
| E274
32 | this and False
| E274
33 | #: Okay
34 | from u import (a, b)
|

View file

@ -5,8 +5,8 @@ W19.py:3:1: W191 Indentation contains tabs
|
3 | #: W191
4 | if False:
5 | print # indented with 1 tab
| W191
5 | print # indented with 1 tab
| ^^^^ W191
6 | #:
|
@ -14,8 +14,8 @@ W19.py:9:1: W191 Indentation contains tabs
|
9 | #: W191
10 | y = x == 2 \
11 | or x == 3
| W191
11 | or x == 3
| ^^^^ W191
12 | #: E101 W191 W504
13 | if (
|
@ -24,8 +24,8 @@ W19.py:16:1: W191 Indentation contains tabs
|
16 | ) or
17 | y == 4):
18 | pass
| W191
18 | pass
| ^^^^ W191
19 | #: E101 W191
20 | if x == 2 \
|
@ -34,8 +34,8 @@ W19.py:21:1: W191 Indentation contains tabs
|
21 | or y > 1 \
22 | or x == 3:
23 | pass
| W191
23 | pass
| ^^^^ W191
24 | #: E101 W191
25 | if x == 2 \
|
@ -44,8 +44,8 @@ W19.py:26:1: W191 Indentation contains tabs
|
26 | or y > 1 \
27 | or x == 3:
28 | pass
| W191
28 | pass
| ^^^^ W191
29 | #:
|
@ -53,8 +53,8 @@ W19.py:32:1: W191 Indentation contains tabs
|
32 | if (foo == bar and
33 | baz == bop):
34 | pass
| W191
34 | pass
| ^^^^ W191
35 | #: E101 W191 W504
36 | if (
|
@ -63,8 +63,8 @@ W19.py:38:1: W191 Indentation contains tabs
|
38 | baz == bop
39 | ):
40 | pass
| W191
40 | pass
| ^^^^ W191
41 | #:
|
@ -72,18 +72,18 @@ W19.py:44:1: W191 Indentation contains tabs
|
44 | if start[1] > end_col and not (
45 | over_indent == 4 and indent_next):
46 | return (0, "E121 continuation line over-"
| W191
47 | "indented for visual indent")
46 | return (0, "E121 continuation line over-"
| ^^^^ W191
47 | "indented for visual indent")
48 | #:
|
W19.py:45:1: W191 Indentation contains tabs
|
45 | over_indent == 4 and indent_next):
46 | return (0, "E121 continuation line over-"
47 | "indented for visual indent")
| ^^^^^^^^ W191
46 | return (0, "E121 continuation line over-"
47 | "indented for visual indent")
| ^^^^^^^^^^^^ W191
48 | #:
|
@ -91,8 +91,8 @@ W19.py:54:1: W191 Indentation contains tabs
|
54 | var_one, var_two, var_three,
55 | var_four):
56 | print(var_one)
| W191
56 | print(var_one)
| ^^^^ W191
57 | #: E101 W191 W504
58 | if ((row < 0 or self.moduleCount <= row or
|
@ -101,8 +101,8 @@ W19.py:58:1: W191 Indentation contains tabs
|
58 | if ((row < 0 or self.moduleCount <= row or
59 | col < 0 or self.moduleCount <= col)):
60 | raise Exception("%s,%s - %s" % (row, col, self.moduleCount))
| W191
60 | raise Exception("%s,%s - %s" % (row, col, self.moduleCount))
| ^^^^ W191
61 | #: E101 E101 E101 E101 W191 W191 W191 W191 W191 W191
62 | if bar:
|
@ -111,58 +111,58 @@ W19.py:61:1: W191 Indentation contains tabs
|
61 | #: E101 E101 E101 E101 W191 W191 W191 W191 W191 W191
62 | if bar:
63 | return (
| W191
64 | start, 'E121 lines starting with a '
65 | 'closing bracket should be indented '
63 | return (
| ^^^^ W191
64 | start, 'E121 lines starting with a '
65 | 'closing bracket should be indented '
|
W19.py:62:1: W191 Indentation contains tabs
|
62 | if bar:
63 | return (
64 | start, 'E121 lines starting with a '
| ^^^^ W191
65 | 'closing bracket should be indented '
66 | "to match that of the opening "
63 | return (
64 | start, 'E121 lines starting with a '
| ^^^^^^^^ W191
65 | 'closing bracket should be indented '
66 | "to match that of the opening "
|
W19.py:63:1: W191 Indentation contains tabs
|
63 | return (
64 | start, 'E121 lines starting with a '
65 | 'closing bracket should be indented '
| ^^^^ W191
66 | "to match that of the opening "
67 | "bracket's line"
63 | return (
64 | start, 'E121 lines starting with a '
65 | 'closing bracket should be indented '
| ^^^^^^^^ W191
66 | "to match that of the opening "
67 | "bracket's line"
|
W19.py:64:1: W191 Indentation contains tabs
|
64 | start, 'E121 lines starting with a '
65 | 'closing bracket should be indented '
66 | "to match that of the opening "
| ^^^^ W191
67 | "bracket's line"
68 | )
64 | start, 'E121 lines starting with a '
65 | 'closing bracket should be indented '
66 | "to match that of the opening "
| ^^^^^^^^ W191
67 | "bracket's line"
68 | )
|
W19.py:65:1: W191 Indentation contains tabs
|
65 | 'closing bracket should be indented '
66 | "to match that of the opening "
67 | "bracket's line"
| ^^^^ W191
68 | )
65 | 'closing bracket should be indented '
66 | "to match that of the opening "
67 | "bracket's line"
| ^^^^^^^^ W191
68 | )
69 | #
|
W19.py:66:1: W191 Indentation contains tabs
|
66 | "to match that of the opening "
67 | "bracket's line"
68 | )
| W191
66 | "to match that of the opening "
67 | "bracket's line"
68 | )
| ^^^^ W191
69 | #
70 | #: E101 W191 W504
|
@ -171,8 +171,8 @@ W19.py:73:1: W191 Indentation contains tabs
|
73 | foo.bar("bop")
74 | )):
75 | print "yes"
| W191
75 | print "yes"
| ^^^^ W191
76 | #: E101 W191 W504
77 | # also ok, but starting to look like LISP
|
@ -181,8 +181,8 @@ W19.py:78:1: W191 Indentation contains tabs
|
78 | if ((foo.bar("baz") and
79 | foo.bar("bop"))):
80 | print "yes"
| W191
80 | print "yes"
| ^^^^ W191
81 | #: E101 W191 W504
82 | if (a == 2 or
|
@ -191,8 +191,8 @@ W19.py:83:1: W191 Indentation contains tabs
|
83 | b == "abc def ghi"
84 | "jkl mno"):
85 | return True
| W191
85 | return True
| ^^^^ W191
86 | #: E101 W191 W504
87 | if (a == 2 or
|
@ -201,8 +201,8 @@ W19.py:88:1: W191 Indentation contains tabs
|
88 | b == """abc def ghi
89 | jkl mno"""):
90 | return True
| W191
90 | return True
| ^^^^ W191
91 | #: W191:2:1 W191:3:1 E101:3:2
92 | if length > options.max_line_length:
|
@ -211,35 +211,35 @@ W19.py:91:1: W191 Indentation contains tabs
|
91 | #: W191:2:1 W191:3:1 E101:3:2
92 | if length > options.max_line_length:
93 | return options.max_line_length, \
| W191
94 | "E501 line too long (%d characters)" % length
93 | return options.max_line_length, \
| ^^^^ W191
94 | "E501 line too long (%d characters)" % length
|
W19.py:92:1: W191 Indentation contains tabs
|
92 | if length > options.max_line_length:
93 | return options.max_line_length, \
94 | "E501 line too long (%d characters)" % length
| ^^^^ W191
93 | return options.max_line_length, \
94 | "E501 line too long (%d characters)" % length
| ^^^^^^^^ W191
|
W19.py:98:1: W191 Indentation contains tabs
|
98 | #: E101 W191 W191 W504
99 | if os.path.exists(os.path.join(path, PEP8_BIN)):
100 | cmd = ([os.path.join(path, PEP8_BIN)] +
| W191
101 | self._pep8_options(targetfile))
100 | cmd = ([os.path.join(path, PEP8_BIN)] +
| ^^^^ W191
101 | self._pep8_options(targetfile))
102 | #: W191 - okay
|
W19.py:99:1: W191 Indentation contains tabs
|
99 | if os.path.exists(os.path.join(path, PEP8_BIN)):
100 | cmd = ([os.path.join(path, PEP8_BIN)] +
101 | self._pep8_options(targetfile))
| ^^^^^^^ W191
100 | cmd = ([os.path.join(path, PEP8_BIN)] +
101 | self._pep8_options(targetfile))
| ^^^^^^^^^^^ W191
102 | #: W191 - okay
103 | '''
|
@ -248,36 +248,36 @@ W19.py:125:1: W191 Indentation contains tabs
|
125 | if foo is None and bar is "bop" and \
126 | blah == 'yeah':
127 | blah = 'yeahnah'
| W191
127 | blah = 'yeahnah'
| ^^^^ W191
|
W19.py:131:1: W191 Indentation contains tabs
|
131 | #: W191 W191 W191
132 | if True:
133 | foo(
| W191
134 | 1,
135 | 2)
133 | foo(
| ^^^^ W191
134 | 1,
135 | 2)
|
W19.py:132:1: W191 Indentation contains tabs
|
132 | if True:
133 | foo(
134 | 1,
| W191
135 | 2)
133 | foo(
134 | 1,
| ^^^^^^^^ W191
135 | 2)
136 | #: W191 W191 W191 W191 W191
|
W19.py:133:1: W191 Indentation contains tabs
|
133 | foo(
134 | 1,
135 | 2)
| W191
133 | foo(
134 | 1,
135 | 2)
| ^^^^^^^^ W191
136 | #: W191 W191 W191 W191 W191
137 | def test_keys(self):
|
@ -286,48 +286,48 @@ W19.py:136:1: W191 Indentation contains tabs
|
136 | #: W191 W191 W191 W191 W191
137 | def test_keys(self):
138 | """areas.json - All regions are accounted for."""
| W191
139 | expected = set([
140 | u'Norrbotten',
138 | """areas.json - All regions are accounted for."""
| ^^^^ W191
139 | expected = set([
140 | u'Norrbotten',
|
W19.py:137:1: W191 Indentation contains tabs
|
137 | def test_keys(self):
138 | """areas.json - All regions are accounted for."""
139 | expected = set([
| W191
140 | u'Norrbotten',
141 | u'V\xe4sterbotten',
138 | """areas.json - All regions are accounted for."""
139 | expected = set([
| ^^^^ W191
140 | u'Norrbotten',
141 | u'V\xe4sterbotten',
|
W19.py:138:1: W191 Indentation contains tabs
|
138 | """areas.json - All regions are accounted for."""
139 | expected = set([
140 | u'Norrbotten',
| W191
141 | u'V\xe4sterbotten',
142 | ])
138 | """areas.json - All regions are accounted for."""
139 | expected = set([
140 | u'Norrbotten',
| ^^^^^^^^ W191
141 | u'V\xe4sterbotten',
142 | ])
|
W19.py:139:1: W191 Indentation contains tabs
|
139 | expected = set([
140 | u'Norrbotten',
141 | u'V\xe4sterbotten',
| W191
142 | ])
139 | expected = set([
140 | u'Norrbotten',
141 | u'V\xe4sterbotten',
| ^^^^^^^^ W191
142 | ])
143 | #: W191
|
W19.py:140:1: W191 Indentation contains tabs
|
140 | u'Norrbotten',
141 | u'V\xe4sterbotten',
142 | ])
| W191
140 | u'Norrbotten',
141 | u'V\xe4sterbotten',
142 | ])
| ^^^^ W191
143 | #: W191
144 | x = [
|
@ -336,8 +336,8 @@ W19.py:143:1: W191 Indentation contains tabs
|
143 | #: W191
144 | x = [
145 | 'abc'
| W191
145 | 'abc'
| ^^^^ W191
146 | ]
147 | #: W191 - okay
|