Fix replace_whitespace() tabulation to space (#4226)

Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
Jonathan Plasse 2023-05-08 14:03:04 +02:00 committed by GitHub
parent 4d5a339d9e
commit 1b1788c8ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 51 additions and 48 deletions

View File

@ -245,36 +245,39 @@ impl Display for MessageCodeFrame<'_> {
} }
fn replace_whitespace(source: &str, annotation_range: TextRange) -> SourceCode { fn replace_whitespace(source: &str, annotation_range: TextRange) -> SourceCode {
static TAB_SIZE: TextSize = TextSize::new(4); static TAB_SIZE: u32 = 4; // TODO(jonathan): use `pycodestyle.tab-size`
let mut result = String::new(); let mut result = String::new();
let mut last_end = 0; let mut last_end = 0;
let mut range = annotation_range; let mut range = annotation_range;
let mut column = 0; let mut column = 0;
for (index, m) in source.match_indices(['\t', '\n', '\r']) { for (index, c) in source.chars().enumerate() {
match m { match c {
"\t" => { '\t' => {
let tab_width = TAB_SIZE - TextSize::new(column % 4); let tab_width = TAB_SIZE - column % TAB_SIZE;
column += tab_width;
if index < usize::from(annotation_range.start()) { if index < usize::from(annotation_range.start()) {
range += tab_width - TextSize::new(1); range += TextSize::new(tab_width - 1);
} else if index < usize::from(annotation_range.end()) { } else if index < usize::from(annotation_range.end()) {
range = range.add_end(tab_width - TextSize::new(1)); range = range.add_end(TextSize::new(tab_width - 1));
} }
result.push_str(&source[last_end..index]); result.push_str(&source[last_end..index]);
for _ in 0..u32::from(tab_width) { for _ in 0..tab_width {
result.push(' '); result.push(' ');
} }
last_end = index + 1; last_end = index + 1;
} }
"\n" | "\r" => { '\n' | '\r' => {
column = 0; column = 0;
} }
_ => unreachable!(), _ => {
column += 1;
}
} }
} }

View File

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

View File

@ -27,34 +27,34 @@ E20.py:6:15: E201 Whitespace after '('
8 | spam(ham[1], { eggs: 2}) 8 | spam(ham[1], { eggs: 2})
| E201 | E201
9 | #: E201:1:6 9 | #: E201:1:6
10 | spam( ham[1], {eggs: 2}) 10 | spam( ham[1], {eggs: 2})
| |
E20.py:8:6: E201 Whitespace after '(' E20.py:8:6: E201 Whitespace after '('
| |
8 | spam(ham[1], { eggs: 2}) 8 | spam(ham[1], { eggs: 2})
9 | #: E201:1:6 9 | #: E201:1:6
10 | spam( ham[1], {eggs: 2}) 10 | spam( ham[1], {eggs: 2})
| E201 | E201
11 | #: E201:1:10 11 | #: E201:1:10
12 | spam(ham[ 1], {eggs: 2}) 12 | spam(ham[ 1], {eggs: 2})
| |
E20.py:10:10: E201 Whitespace after '(' E20.py:10:10: E201 Whitespace after '('
| |
10 | spam( ham[1], {eggs: 2}) 10 | spam( ham[1], {eggs: 2})
11 | #: E201:1:10 11 | #: E201:1:10
12 | spam(ham[ 1], {eggs: 2}) 12 | spam(ham[ 1], {eggs: 2})
| E201 | E201
13 | #: E201:1:15 13 | #: E201:1:15
14 | spam(ham[1], { eggs: 2}) 14 | spam(ham[1], { eggs: 2})
| |
E20.py:12:15: E201 Whitespace after '(' E20.py:12:15: E201 Whitespace after '('
| |
12 | spam(ham[ 1], {eggs: 2}) 12 | spam(ham[ 1], {eggs: 2})
13 | #: E201:1:15 13 | #: E201:1:15
14 | spam(ham[1], { eggs: 2}) 14 | spam(ham[1], { eggs: 2})
| E201 | E201
15 | #: Okay 15 | #: Okay
16 | spam(ham[1], {eggs: 2}) 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}) 25 | spam(ham[1 ], {eggs: 2})
| E202 | E202
26 | #: E202:1:23 26 | #: E202:1:23
27 | spam(ham[1], {eggs: 2} ) 27 | spam(ham[1], {eggs: 2} )
| |
E20.py:25:23: E202 Whitespace before ')' E20.py:25:23: E202 Whitespace before ')'
| |
25 | spam(ham[1 ], {eggs: 2}) 25 | spam(ham[1 ], {eggs: 2})
26 | #: E202:1:23 26 | #: E202:1:23
27 | spam(ham[1], {eggs: 2} ) 27 | spam(ham[1], {eggs: 2} )
| E202 | E202
28 | #: E202:1:22 28 | #: E202:1:22
29 | spam(ham[1], {eggs: 2 }) 29 | spam(ham[1], {eggs: 2 })
| |
E20.py:27:22: E202 Whitespace before ')' E20.py:27:22: E202 Whitespace before ')'
| |
27 | spam(ham[1], {eggs: 2} ) 27 | spam(ham[1], {eggs: 2} )
28 | #: E202:1:22 28 | #: E202:1:22
29 | spam(ham[1], {eggs: 2 }) 29 | spam(ham[1], {eggs: 2 })
| E202 | E202
30 | #: E202:1:11 30 | #: E202:1:11
31 | spam(ham[1 ], {eggs: 2}) 31 | spam(ham[1 ], {eggs: 2})
| |
E20.py:29:11: E202 Whitespace before ')' E20.py:29:11: E202 Whitespace before ')'
| |
29 | spam(ham[1], {eggs: 2 }) 29 | spam(ham[1], {eggs: 2 })
30 | #: E202:1:11 30 | #: E202:1:11
31 | spam(ham[1 ], {eggs: 2}) 31 | spam(ham[1 ], {eggs: 2})
| E202 | E202
32 | #: Okay 32 | #: Okay
33 | spam(ham[1], {eggs: 2}) 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 55 | x, y = y, x
56 | #: E203:1:10 56 | #: E203:1:10
57 | if x == 4 : 57 | if x == 4 :
| E203 | E203
58 | print x, y 58 | print x, y
59 | x, y = y, x 59 | x, y = y, x
@ -34,7 +34,7 @@ E20.py:63:15: E203 Whitespace before ',', ';', or ':'
| |
63 | #: E203:2:15 E702:2:16 63 | #: E203:2:15 E702:2:16
64 | if x == 4: 64 | if x == 4:
65 | print x, y ; x, y = y, x 65 | print x, y ; x, y = y, x
| E203 | E203
66 | #: E203:3:13 66 | #: E203:3:13
67 | if x == 4: 67 | if x == 4:

View File

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

View File

@ -33,7 +33,7 @@ E27.py:8:3: E271 Multiple spaces after keyword
E27.py:14:6: E271 Multiple spaces after keyword E27.py:14:6: E271 Multiple spaces after keyword
| |
14 | True and False 14 | True and False
15 | #: E271 15 | #: E271
16 | a and b 16 | a and b
| E271 | E271

View File

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

View File

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

View File

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