Skip to content

Commit

Permalink
Fix highlighting if leading/trailing whitespace contains multiple tabs.
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed May 4, 2023
1 parent 50a921e commit 18512b0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
42 changes: 27 additions & 15 deletions crates/ruff/src/rules/pycodestyle/rules/logical_lines/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,12 @@ impl Whitespace {
fn leading(content: &str) -> (Self, TextSize) {
let mut count = 0u32;
let mut len = TextSize::default();
let mut has_tabs = false;

for c in content.chars() {
if c == '\t' {
return (Self::Tab, len + c.text_len());
has_tabs = true;
len += c.text_len();
} else if matches!(c, '\n' | '\r') {
break;
} else if c.is_whitespace() {
Expand All @@ -375,20 +377,26 @@ impl Whitespace {
}
}

match count {
0 => (Whitespace::None, len),
1 => (Whitespace::Single, len),
_ => (Whitespace::Many, len),
if has_tabs {
(Whitespace::Tab, len)
} else {
match count {
0 => (Whitespace::None, len),
1 => (Whitespace::Single, len),
_ => (Whitespace::Many, len),
}
}
}

fn trailing(content: &str) -> (Self, TextSize) {
let mut len = TextSize::default();
let mut count = 0usize;
let mut has_tabs = false;

for c in content.chars().rev() {
if c == '\t' {
return (Self::Tab, len + c.text_len());
has_tabs = true;
len += c.text_len();
} else if matches!(c, '\n' | '\r') {
// Indent
return (Self::None, TextSize::default());
Expand All @@ -400,15 +408,19 @@ impl Whitespace {
}
}

match count {
0 => (Self::None, TextSize::default()),
1 => (Self::Single, len),
_ => {
if len == content.text_len() {
// All whitespace up to the start of the line -> Indent
(Self::None, TextSize::default())
} else {
(Self::Many, len)
if has_tabs {
(Self::Tab, len)
} else {
match count {
0 => (Self::None, TextSize::default()),
1 => (Self::Single, len),
_ => {
if len == content.text_len() {
// All whitespace up to the start of the line -> Indent
(Self::None, TextSize::default())
} else {
(Self::Many, len)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ E27.py:10:9: E273 Tab after keyword
10 | if 1:
11 | #: E273
12 | True and False
| ^^^^ E273
| ^^^^^^^^ E273
13 | #: E273 E274
14 | True and False
|
Expand All @@ -16,7 +16,7 @@ E27.py:12:5: E273 Tab after keyword
12 | True and False
13 | #: E273 E274
14 | True and False
| ^^^^ E273
| ^^^^^^^^ E273
15 | #: E271
16 | a and b
|
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
---
source: crates/ruff/src/rules/pycodestyle/mod.rs
---
E27.py:28:3: E274 Tab before keyword
E27.py:28:2: E274 Tab before keyword
|
28 | a and b
29 | #: E274
30 | a and b
| ^^^^ E274
| ^^^^^^^^ E274
31 | #: E273 E274
32 | this and False
|

E27.py:30:6: E274 Tab before keyword
E27.py:30:5: E274 Tab before keyword
|
30 | a and b
31 | #: E273 E274
32 | this and False
| ^^^^ E274
| ^^^^^^^^ E274
33 | #: Okay
34 | from u import (a, b)
|
Expand Down

0 comments on commit 18512b0

Please sign in to comment.