diff --git a/crates/ruff_annotate_snippets/src/renderer/display_list.rs b/crates/ruff_annotate_snippets/src/renderer/display_list.rs index 91e031b878..3037cb8943 100644 --- a/crates/ruff_annotate_snippets/src/renderer/display_list.rs +++ b/crates/ruff_annotate_snippets/src/renderer/display_list.rs @@ -352,7 +352,7 @@ impl DisplaySet<'_> { // FIXME: `unicode_width` sometimes disagrees with terminals on how wide a `char` // is. For now, just accept that sometimes the code line will be longer than // desired. - let next = unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1); + let next = char_width(ch).unwrap_or(1); if taken + next > right - left { was_cut_right = true; break; @@ -377,7 +377,7 @@ impl DisplaySet<'_> { let left: usize = text .chars() .take(left) - .map(|ch| unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1)) + .map(|ch| char_width(ch).unwrap_or(1)) .sum(); let mut annotations = annotations.clone(); @@ -1393,6 +1393,7 @@ fn format_body<'m>( let line_length: usize = line.len(); let line_range = (current_index, current_index + line_length); let end_line_size = end_line.len(); + body.push(DisplayLine::Source { lineno: Some(current_line), inline_marks: vec![], @@ -1452,12 +1453,12 @@ fn format_body<'m>( let annotation_start_col = line [0..(start - line_start_index).min(line_length)] .chars() - .map(|c| unicode_width::UnicodeWidthChar::width(c).unwrap_or(0)) + .map(|c| char_width(c).unwrap_or(0)) .sum::(); let mut annotation_end_col = line [0..(end - line_start_index).min(line_length)] .chars() - .map(|c| unicode_width::UnicodeWidthChar::width(c).unwrap_or(0)) + .map(|c| char_width(c).unwrap_or(0)) .sum::(); if annotation_start_col == annotation_end_col { // At least highlight something @@ -1499,7 +1500,7 @@ fn format_body<'m>( let annotation_start_col = line [0..(start - line_start_index).min(line_length)] .chars() - .map(|c| unicode_width::UnicodeWidthChar::width(c).unwrap_or(0)) + .map(|c| char_width(c).unwrap_or(0)) .sum::(); let annotation_end_col = annotation_start_col + 1; @@ -1558,7 +1559,7 @@ fn format_body<'m>( { let end_mark = line[0..(end - line_start_index).min(line_length)] .chars() - .map(|c| unicode_width::UnicodeWidthChar::width(c).unwrap_or(0)) + .map(|c| char_width(c).unwrap_or(0)) .sum::() .saturating_sub(1); // If the annotation ends on a line-end character, we @@ -1754,3 +1755,11 @@ fn format_inline_marks( } Ok(()) } + +fn char_width(c: char) -> Option { + if c == '\t' { + Some(4) + } else { + unicode_width::UnicodeWidthChar::width(c) + } +} diff --git a/crates/ruff_annotate_snippets/tests/fixtures/color/regression_leading_tab_long_line.svg b/crates/ruff_annotate_snippets/tests/fixtures/color/regression_leading_tab_long_line.svg index e5a290c006..228ec94551 100644 --- a/crates/ruff_annotate_snippets/tests/fixtures/color/regression_leading_tab_long_line.svg +++ b/crates/ruff_annotate_snippets/tests/fixtures/color/regression_leading_tab_long_line.svg @@ -27,7 +27,7 @@ 4 | ... s_data['d_dails'] = bb['contacted'][hostip]['ansible_facts']['ansible_devices']['vda']['vendor'] + " " + bb['contacted'][hostip... - ^ | unresolved reference + | ^^^^^^ | diff --git a/crates/ruff_annotate_snippets/tests/fixtures/color/strip_line_non_ws.svg b/crates/ruff_annotate_snippets/tests/fixtures/color/strip_line_non_ws.svg index e4f8a856b6..67e2cfd9b2 100644 --- a/crates/ruff_annotate_snippets/tests/fixtures/color/strip_line_non_ws.svg +++ b/crates/ruff_annotate_snippets/tests/fixtures/color/strip_line_non_ws.svg @@ -21,7 +21,7 @@ error[E0308]: mismatched types - --> $DIR/non-whitespace-trimming.rs:4:242 + --> $DIR/non-whitespace-trimming.rs:4:238 | diff --git a/crates/ruff_annotate_snippets/tests/fixtures/color/strip_line_non_ws.toml b/crates/ruff_annotate_snippets/tests/fixtures/color/strip_line_non_ws.toml index c6573ff40a..bfe9f317f2 100644 --- a/crates/ruff_annotate_snippets/tests/fixtures/color/strip_line_non_ws.toml +++ b/crates/ruff_annotate_snippets/tests/fixtures/color/strip_line_non_ws.toml @@ -13,12 +13,12 @@ origin = "$DIR/non-whitespace-trimming.rs" [[message.snippets.annotations]] label = "expected `()`, found integer" level = "Error" -range = [241, 243] +range = [237, 239] [[message.snippets.annotations]] label = "expected due to this" level = "Error" -range = [236, 238] +range = [232, 234] [renderer]