fix(read_span): prevent multilines MietteSpanContents from skipping lines (#81)

Fixes: https://github.com/zkat/miette/issues/76
This commit is contained in:
Antoine Muller 2021-10-02 22:53:57 +02:00 committed by Kat Marchán
parent 6fc4fb0b77
commit cb5a919deb
No known key found for this signature in database
GPG Key ID: AEB529C08A3C7E9E
1 changed files with 16 additions and 1 deletions

View File

@ -74,7 +74,7 @@ fn context_info<'a>(
if offset >= (span.offset() + span.len()).saturating_sub(1) {
let starting_offset = before_lines_starts.get(0).copied().unwrap_or_else(|| {
if line_count > 0 {
if context_lines_before == 0 {
span.offset()
} else {
0
@ -268,4 +268,19 @@ mod tests {
assert_eq!(&span, contents.span());
Ok(())
}
#[test]
fn multiline_with_context_line_start() -> Result<(), MietteError> {
let src = String::from("one\ntwo\n\nthree\nfour\nfive\n\nsix\nseven\n");
let contents = src.read_span(&(2, 0).into(), 2, 2)?;
assert_eq!(
"one\ntwo\n\n",
std::str::from_utf8(contents.data()).unwrap()
);
assert_eq!(0, contents.line());
assert_eq!(0, contents.column());
let span: SourceSpan = (0, 9).into();
assert_eq!(&span, contents.span());
Ok(())
}
}