diff --git a/src/display_list/from_snippet.rs b/src/display_list/from_snippet.rs index b3ba5a3..da0deea 100644 --- a/src/display_list/from_snippet.rs +++ b/src/display_list/from_snippet.rs @@ -104,15 +104,16 @@ fn format_annotation(annotation: snippet::Annotation<'_>) -> Vec } fn format_slice( - mut slice: snippet::Slice<'_>, + slice: snippet::Slice<'_>, is_first: bool, has_footer: bool, ) -> Vec> { let main_range = slice.annotations.get(0).map(|x| x.range.0); - let row = slice.line_start; - let origin = slice.origin.take(); - let mut body = format_body(slice, has_footer); - let header = format_header(origin, main_range, row, &body, is_first); + let origin = slice.origin; + let line_start = slice.line_start; + let need_empty_header = origin.is_some() || is_first; + let mut body = format_body(slice, need_empty_header, has_footer); + let header = format_header(origin, main_range, line_start, &body, is_first); let mut result = vec![]; if let Some(header) = header { @@ -122,6 +123,12 @@ fn format_slice( result } +#[inline] +// TODO: option_zip +fn zip_opt(a: Option, b: Option) -> Option<(A, B)> { + a.and_then(|a| b.map(|b| (a, b))) +} + fn format_header<'a>( origin: Option<&'a str>, main_range: Option, @@ -135,7 +142,7 @@ fn format_header<'a>( DisplayHeaderType::Continuation }; - if let Some(main_range) = main_range { + if let Some((main_range, path)) = zip_opt(main_range, origin) { let mut col = 1; for item in body { @@ -151,14 +158,14 @@ fn format_header<'a>( row += 1; } } - if let Some(path) = origin { - return Some(DisplayLine::Raw(DisplayRawLine::Origin { - path, - pos: Some((row, col)), - header_type: display_header, - })); - } + + return Some(DisplayLine::Raw(DisplayRawLine::Origin { + path, + pos: Some((row, col)), + header_type: display_header, + })); } + if let Some(path) = origin { return Some(DisplayLine::Raw(DisplayRawLine::Origin { path, @@ -166,6 +173,7 @@ fn format_header<'a>( header_type: display_header, })); } + None } @@ -261,7 +269,11 @@ fn fold_body(mut body: Vec>) -> Vec> { new_body } -fn format_body(slice: snippet::Slice<'_>, has_footer: bool) -> Vec> { +fn format_body( + slice: snippet::Slice<'_>, + need_empty_header: bool, + has_footer: bool, +) -> Vec> { let source_len = slice.source.chars().count(); if let Some(bigger) = slice.annotations.iter().find_map(|x| { if source_len < x.range.1 { @@ -445,14 +457,17 @@ fn format_body(slice: snippet::Slice<'_>, has_footer: bool) -> Vec /code/rust/src/test/ui/annotate-snippet/suggestion.rs:4:5 + | +4 | let x = vec![1]; + | - move occurs because `x` has type `std::vec::Vec`, which does not implement the `Copy` trait + | +7 | let y = x; + | - value moved here + | +9 | x; + | ^ value used here after move + | \ No newline at end of file diff --git a/tests/snippet/mod.rs b/tests/snippet/mod.rs index c334874..cc747a4 100644 --- a/tests/snippet/mod.rs +++ b/tests/snippet/mod.rs @@ -53,7 +53,9 @@ where #[derive(Deserialize)] #[serde(remote = "FormatOptions")] pub struct FormatOptionsDef { + #[serde(default)] pub color: bool, + #[serde(default)] pub anonymized_line_numbers: bool, }