Skip to content

Commit cbf1271

Browse files
VonTumzesterer
authored andcommitted
Source::get_line_text no longer trims end whitespace
This was causing crashes using byte spans when byte spans start or end in whitespace after a line. When I write the code I did not expect get_line_text to trim whitespace. I edited uses of get_line_text to do the trimming separately. Technically this is a breaking change.
1 parent 4ca1b68 commit cbf1271

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

src/source.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,9 @@ impl<I: AsRef<str>> Source<I> {
212212
start..end
213213
}
214214

215-
/// Get the source text for a line, excluding trailing whitespace.
215+
/// Get the source text for a line, includes trailing whitespace and the newline
216216
pub fn get_line_text(&self, line: Line) -> Option<&'_ str> {
217-
self.text.as_ref().get(line.byte_span()).map(|text| text.trim_end())
217+
self.text.as_ref().get(line.byte_span())
218218
}
219219
}
220220

@@ -335,7 +335,7 @@ mod tests {
335335
assert_eq!(source_line.char_len, raw_line.chars().count());
336336
assert_eq!(
337337
source.get_line_text(source_line).unwrap(),
338-
raw_line.trim_end()
338+
raw_line
339339
);
340340
offset += source_line.char_len;
341341
}
@@ -398,7 +398,7 @@ mod tests {
398398
assert_eq!(source_line.char_len, raw_line.chars().count());
399399
assert_eq!(
400400
source.get_line_text(source_line).unwrap(),
401-
raw_line.trim_end()
401+
raw_line
402402
);
403403
offset += source_line.char_len;
404404
}

src/write.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ impl<S: Span> Report<'_, S> {
627627

628628
// Line
629629
if !is_ellipsis {
630-
for (col, c) in src.get_line_text(line).unwrap().chars().enumerate() {
630+
for (col, c) in src.get_line_text(line).unwrap().trim_end().chars().enumerate() {
631631
let color = if let Some(highlight) = get_highlight(col) {
632632
highlight.display_info.color
633633
} else {
@@ -665,7 +665,7 @@ impl<S: Span> Report<'_, S> {
665665
&margin_label,
666666
)?;
667667
// Lines alternate
668-
let mut chars = src.get_line_text(line).unwrap().chars();
668+
let mut chars = src.get_line_text(line).unwrap().trim_end().chars();
669669
for col in 0..arrow_len {
670670
let width =
671671
chars.next().map_or(1, |c| self.config.char_width(c, col).1);
@@ -719,7 +719,7 @@ impl<S: Span> Report<'_, S> {
719719
&margin_label,
720720
)?;
721721
// Lines
722-
let mut chars = src.get_line_text(line).unwrap().chars();
722+
let mut chars = src.get_line_text(line).unwrap().trim_end().chars();
723723
for col in 0..arrow_len {
724724
let width = chars.next().map_or(1, |c| self.config.char_width(c, col).1);
725725

@@ -1002,12 +1002,28 @@ mod tests {
10021002
,-[<unknown>:1:1]
10031003
|
10041004
1 | apple ==
1005-
| |
1006-
| `- Unexpected end of file
1005+
| |
1006+
| `- Unexpected end of file
10071007
---'
10081008
"###);
10091009
}
10101010

1011+
#[test]
1012+
fn byte_spans_never_crash() {
1013+
let source = "apple\np\n\nempty\n";
1014+
1015+
for i in 0..source.len() {
1016+
for j in i..source.len() {
1017+
let _ = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
1018+
.with_config(no_color_and_ascii().with_index_type(IndexType::Byte))
1019+
.with_message("Label")
1020+
.with_label(Label::new(i..j).with_message("Label"))
1021+
.finish()
1022+
.write_to_string(Source::from(source));
1023+
}
1024+
}
1025+
}
1026+
10111027
#[test]
10121028
fn multiline_label() {
10131029
let source = "apple\n==\norange";

0 commit comments

Comments
 (0)