From cb5a919deb87f8fba748bed73b6f22ebe4e3390f Mon Sep 17 00:00:00 2001 From: Antoine Muller Date: Sat, 2 Oct 2021 22:53:57 +0200 Subject: [PATCH] fix(read_span): prevent multilines MietteSpanContents from skipping lines (#81) Fixes: https://github.com/zkat/miette/issues/76 --- src/source_impls.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/source_impls.rs b/src/source_impls.rs index 49ecc0e3..7419b4eb 100644 --- a/src/source_impls.rs +++ b/src/source_impls.rs @@ -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(()) + } }