Skip to content

Commit 2815fc3

Browse files
authored
Merge pull request #566 from ratmice/zero_len_span
Fix minimum length check of zero length span
2 parents a883a57 + 4d69278 commit 2815fc3

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

nimbleparse/src/diagnostics.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,12 @@ impl<'a> SpannedDiagnosticFormatter<'a> {
123123
)
124124
));
125125
// Add underline upto the end of line.
126-
out.push_str(&(underline_c.to_string()).repeat(UnicodeWidthStr::width(
127-
&self.src[underline_span.start()..underline_span.end().max(1)],
128-
)));
126+
out.push_str(
127+
&(underline_c.to_string()).repeat(
128+
UnicodeWidthStr::width(&self.src[underline_span.start()..underline_span.end()])
129+
.max(1),
130+
),
131+
);
129132

130133
if source_lines.peek().is_none() {
131134
// If we're at the end print the message.
@@ -650,4 +653,25 @@ mod test {
650653
^^ ^^ Not crabs"
651654
);
652655
}
656+
657+
#[test]
658+
fn underline_zero_length() {
659+
let s = r#"Error is between the quotes "" there"#;
660+
let test_path = PathBuf::from("test");
661+
let formatter = SpannedDiagnosticFormatter::new(&s, &test_path);
662+
let mut out = String::from("\n");
663+
let pos = s.find("\"").unwrap() + 1;
664+
out.push_str(&formatter.prefixed_underline_span_with_text(
665+
"",
666+
Span::new(pos, pos),
667+
"Here".to_string(),
668+
'^',
669+
));
670+
assert_eq!(
671+
out,
672+
r#"
673+
1| Error is between the quotes "" there
674+
^ Here"#
675+
);
676+
}
653677
}

0 commit comments

Comments
 (0)