Skip to content

Commit 6765dda

Browse files
committed
fix: Better account for unicode chars when trimming
1 parent 06f12ea commit 6765dda

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

src/renderer/mod.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -967,21 +967,7 @@ impl Renderer {
967967

968968
let line_offset = buffer.num_lines();
969969

970-
// Left trim
971-
let left = margin.left(str_width(&source_string));
972-
973-
// FIXME: This looks fishy. See #132860.
974-
// Account for unicode characters of width !=0 that were removed.
975-
let mut taken = 0;
976-
source_string.chars().for_each(|ch| {
977-
let next = char_width(ch);
978-
if taken + next <= left {
979-
taken += next;
980-
}
981-
});
982-
983-
let left = taken;
984-
self.draw_line(
970+
let left = self.draw_line(
985971
buffer,
986972
&source_string,
987973
line_info.line_index,
@@ -2036,12 +2022,12 @@ impl Renderer {
20362022
code_offset: usize,
20372023
max_line_num_len: usize,
20382024
margin: Margin,
2039-
) {
2025+
) -> usize {
20402026
// Tabs are assumed to have been replaced by spaces in calling code.
20412027
debug_assert!(!source_string.contains('\t'));
20422028
let line_len = str_width(source_string);
20432029
// Create the source line we will highlight.
2044-
let left = margin.left(line_len);
2030+
let mut left = margin.left(line_len);
20452031
let right = margin.right(line_len);
20462032
// FIXME: The following code looks fishy. See #132860.
20472033
// On long lines, we strip the source line, accounting for unicode.
@@ -2074,10 +2060,15 @@ impl Renderer {
20742060
break;
20752061
}
20762062
}
2063+
2064+
if width_taken > padding {
2065+
left -= width_taken - padding;
2066+
}
2067+
20772068
buffer.puts(
20782069
line_offset,
20792070
code_offset,
2080-
&format!("{placeholder:>width_taken$}"),
2071+
placeholder,
20812072
ElementStyle::LineNumber,
20822073
);
20832074
(width_taken, bytes_taken)
@@ -2121,6 +2112,8 @@ impl Renderer {
21212112
);
21222113

21232114
self.draw_col_separator_no_space(buffer, line_offset, width_offset - 2);
2115+
2116+
left
21242117
}
21252118

21262119
fn draw_range(

tests/formatter.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,8 +2178,8 @@ fn unicode_cut_handling2() {
21782178
let expected_ascii = str![[r#"
21792179
error: expected item, found `?`
21802180
|
2181-
1 | ...的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。*/?
2182-
| ^ expected item
2181+
1 | ... 的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。*/?
2182+
| ^ expected item
21832183
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
21842184
"#]];
21852185

@@ -2189,8 +2189,8 @@ error: expected item, found `?`
21892189
let expected_unicode = str![[r#"
21902190
error: expected item, found `?`
21912191
2192-
1 │ 宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。*/?
2193-
│ ━ expected item
2192+
1 │ 宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。*/?
2193+
━ expected item
21942194
╰ note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
21952195
"#]];
21962196
let renderer_unicode = renderer_ascii.theme(OutputTheme::Unicode);
@@ -2215,8 +2215,8 @@ fn unicode_cut_handling3() {
22152215
let expected_ascii = str![[r#"
22162216
error: expected item, found `?`
22172217
|
2218-
1 | ...。这是宽的。这是宽的。这是宽的...
2219-
| ^^ expected item
2218+
1 | ... 。这是宽的。这是宽的。这是宽的...
2219+
| ^^ expected item
22202220
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
22212221
"#]];
22222222

@@ -2226,8 +2226,8 @@ error: expected item, found `?`
22262226
let expected_unicode = str![[r#"
22272227
error: expected item, found `?`
22282228
2229-
1 │ 的。这是宽的。这是宽的。这是宽的。…
2230-
│ ━━ expected item
2229+
1 │ 的。这是宽的。这是宽的。这是宽的。…
2230+
━━ expected item
22312231
╰ note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
22322232
"#]];
22332233
let renderer_unicode = renderer_ascii.theme(OutputTheme::Unicode);

0 commit comments

Comments
 (0)