From 1b1788c8ad1ece465a83395d21d96745bf79444d Mon Sep 17 00:00:00 2001 From: Jonathan Plasse <13716151+JonathanPlasse@users.noreply.github.com> Date: Mon, 8 May 2023 14:03:04 +0200 Subject: [PATCH] Fix replace_whitespace() tabulation to space (#4226) Co-authored-by: Micha Reiser --- crates/ruff/src/message/text.rs | 23 +++++++++++-------- ...les__pycodestyle__tests__E101_E101.py.snap | 8 +++---- ...ules__pycodestyle__tests__E201_E20.py.snap | 16 ++++++------- ...ules__pycodestyle__tests__E202_E20.py.snap | 16 ++++++------- ...ules__pycodestyle__tests__E203_E20.py.snap | 4 ++-- ...ules__pycodestyle__tests__E223_E22.py.snap | 2 +- ...ules__pycodestyle__tests__E271_E27.py.snap | 2 +- ...ules__pycodestyle__tests__E272_E27.py.snap | 2 +- ...ules__pycodestyle__tests__E273_E27.py.snap | 14 +++++------ ...ules__pycodestyle__tests__E274_E27.py.snap | 12 +++++----- 10 files changed, 51 insertions(+), 48 deletions(-) diff --git a/crates/ruff/src/message/text.rs b/crates/ruff/src/message/text.rs index 547cb64e77..dcfb6f62bc 100644 --- a/crates/ruff/src/message/text.rs +++ b/crates/ruff/src/message/text.rs @@ -245,36 +245,39 @@ impl Display for MessageCodeFrame<'_> { } 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 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); + for (index, c) in source.chars().enumerate() { + match c { + '\t' => { + let tab_width = TAB_SIZE - column % TAB_SIZE; + column += tab_width; 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()) { - 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]); - for _ in 0..u32::from(tab_width) { + for _ in 0..tab_width { result.push(' '); } last_end = index + 1; } - "\n" | "\r" => { + '\n' | '\r' => { column = 0; } - _ => unreachable!(), + _ => { + column += 1; + } } } diff --git a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E101_E101.py.snap b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E101_E101.py.snap index d1b7d11aee..1333b7fae3 100644 --- a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E101_E101.py.snap +++ b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E101_E101.py.snap @@ -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 | diff --git a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E201_E20.py.snap b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E201_E20.py.snap index 198479926f..2308d50890 100644 --- a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E201_E20.py.snap +++ b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E201_E20.py.snap @@ -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}) diff --git a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E202_E20.py.snap b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E202_E20.py.snap index 1215dbb53b..69edd156d1 100644 --- a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E202_E20.py.snap +++ b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E202_E20.py.snap @@ -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}) diff --git a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E203_E20.py.snap b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E203_E20.py.snap index b59e240bd7..de37bc5aaf 100644 --- a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E203_E20.py.snap +++ b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E203_E20.py.snap @@ -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: diff --git a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E223_E22.py.snap b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E223_E22.py.snap index 0e7405603f..0235b13891 100644 --- a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E223_E22.py.snap +++ b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E223_E22.py.snap @@ -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 | #: | diff --git a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E271_E27.py.snap b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E271_E27.py.snap index af6e1e52f0..d4e838c49f 100644 --- a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E271_E27.py.snap +++ b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E271_E27.py.snap @@ -33,7 +33,7 @@ E27.py:8:3: E271 Multiple spaces after keyword E27.py:14:6: E271 Multiple spaces after keyword | -14 | True and False +14 | True and False 15 | #: E271 16 | a and b | E271 diff --git a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E272_E27.py.snap b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E272_E27.py.snap index 1bc20e60e5..d295b49602 100644 --- a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E272_E27.py.snap +++ b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E272_E27.py.snap @@ -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 | diff --git a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E273_E27.py.snap b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E273_E27.py.snap index 87dce58363..ea55c2f8db 100644 --- a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E273_E27.py.snap +++ b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E273_E27.py.snap @@ -8,14 +8,14 @@ E27.py:10:9: E273 Tab after keyword 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 13 | #: E273 E274 -14 | True and False +14 | True and False | E273 15 | #: E271 16 | a and b @@ -25,7 +25,7 @@ E27.py:12:10: E273 Tab after keyword | 12 | True and False 13 | #: E273 E274 -14 | True and False +14 | True and False | E273 15 | #: E271 16 | a and b @@ -35,17 +35,17 @@ 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 +32 | this and False | E273 33 | #: Okay 34 | from u import (a, b) diff --git a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E274_E27.py.snap b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E274_E27.py.snap index 1150a45c69..f70b51e63d 100644 --- a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E274_E27.py.snap +++ b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E274_E27.py.snap @@ -3,19 +3,19 @@ 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 +32 | this and False | E274 33 | #: Okay 34 | from u import (a, b)