Skip to content

Commit 66409d3

Browse files
Zollerboy1zesterer
authored andcommitted
Fix incorrect column number when using byte indexes
1 parent 3f6832a commit 66409d3

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

src/write.rs

+35-2
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,17 @@ impl<S: Span> Report<'_, S> {
247247
} else {
248248
labels[0].char_span.start
249249
};
250-
let (line_no, col_no) = src
251-
.get_offset_line(location)
250+
let line_and_col = match self.config.index_type {
251+
IndexType::Char => src.get_offset_line(location),
252+
IndexType::Byte => src.get_byte_line(location).map(|(line_obj, idx, col)| {
253+
let line_text = src.get_line_text(line_obj).unwrap();
254+
255+
let col = line_text[..col.min(line_text.len())].chars().count();
256+
257+
(line_obj, idx, col)
258+
}),
259+
};
260+
let (line_no, col_no) = line_and_col
252261
.map(|(_, idx, col)| (format!("{}", idx + 1), format!("{}", col + 1)))
253262
.unwrap_or_else(|| ('?'.to_string(), '?'.to_string()));
254263
let line_ref = format!(":{}:{}", line_no, col_no);
@@ -1020,6 +1029,30 @@ mod tests {
10201029
"###);
10211030
}
10221031

1032+
#[test]
1033+
fn byte_column() {
1034+
let source = "äpplë == örängë;";
1035+
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 11)
1036+
.with_config(no_color_and_ascii().with_index_type(IndexType::Byte))
1037+
.with_message("can't compare äpplës with örängës")
1038+
.with_label(Label::new(0..7).with_message("This is an äpplë"))
1039+
.with_label(Label::new(11..20).with_message("This is an örängë"))
1040+
.finish()
1041+
.write_to_string(Source::from(source));
1042+
// TODO: it would be nice if these lines didn't cross
1043+
assert_snapshot!(msg, @r###"
1044+
Error: can't compare äpplës with örängës
1045+
,-[<unknown>:1:10]
1046+
|
1047+
1 | äpplë == örängë;
1048+
| ^^|^^ ^^^|^^
1049+
| `-------------- This is an äpplë
1050+
| |
1051+
| `---- This is an örängë
1052+
---'
1053+
"###);
1054+
}
1055+
10231056
#[test]
10241057
fn label_at_end_of_long_line() {
10251058
let source = format!("{}orange", "apple == ".repeat(100));

0 commit comments

Comments
 (0)