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 {
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;
}
}
}

View File

@ -16,7 +16,7 @@ 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
| ^^^^^^^^^^^^^^^ E101
18 |
19 | def xyz():
|
@ -26,7 +26,7 @@ E101.py:19:1: E101 Indentation contains mixed spaces and tabs
19 | def xyz():
20 | # E101
21 | print("xyz");
| ^^^^^^^ E101
| ^^^^ E101
|