Skip to content

Removing duplicate Slice origins and surrounding empty lines #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 37 additions & 22 deletions src/display_list/from_snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,16 @@ fn format_annotation(annotation: snippet::Annotation<'_>) -> Vec<DisplayLine<'_>
}

fn format_slice(
mut slice: snippet::Slice<'_>,
slice: snippet::Slice<'_>,
is_first: bool,
has_footer: bool,
) -> Vec<DisplayLine<'_>> {
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 {
Expand All @@ -122,6 +123,12 @@ fn format_slice(
result
}

#[inline]
// TODO: option_zip
fn zip_opt<A, B>(a: Option<A>, b: Option<B>) -> Option<(A, B)> {
a.and_then(|a| b.map(|b| (a, b)))
}

fn format_header<'a>(
origin: Option<&'a str>,
main_range: Option<usize>,
Expand All @@ -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 {
Expand All @@ -151,21 +158,22 @@ 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,
pos: None,
header_type: display_header,
}));
}

None
}

Expand Down Expand Up @@ -261,7 +269,11 @@ fn fold_body(mut body: Vec<DisplayLine<'_>>) -> Vec<DisplayLine<'_>> {
new_body
}

fn format_body(slice: snippet::Slice<'_>, has_footer: bool) -> Vec<DisplayLine<'_>> {
fn format_body(
slice: snippet::Slice<'_>,
need_empty_header: bool,
has_footer: bool,
) -> Vec<DisplayLine<'_>> {
let source_len = slice.source.chars().count();
if let Some(bigger) = slice.annotations.iter().find_map(|x| {
if source_len < x.range.1 {
Expand Down Expand Up @@ -445,14 +457,17 @@ fn format_body(slice: snippet::Slice<'_>, has_footer: bool) -> Vec<DisplayLine<'
body = fold_body(body);
}

body.insert(
0,
DisplayLine::Source {
lineno: None,
inline_marks: vec![],
line: DisplaySourceLine::Empty,
},
);
if need_empty_header {
body.insert(
0,
DisplayLine::Source {
lineno: None,
inline_marks: vec![],
line: DisplaySourceLine::Empty,
},
);
}

if has_footer {
body.push(DisplayLine::Source {
lineno: None,
Expand Down
28 changes: 28 additions & 0 deletions tests/fixtures/no-color/issue_9.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[title]
label = "expected one of `.`, `;`, `?`, or an operator, found `for`"
annotation_type = "Error"

[[slices]]
source = "let x = vec![1];"
line_start = 4
origin = "/code/rust/src/test/ui/annotate-snippet/suggestion.rs"
[[slices.annotations]]
label = "move occurs because `x` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait"
annotation_type = "Warning"
range = [4, 5]

[[slices]]
source = "let y = x;"
line_start = 7
[[slices.annotations]]
label = "value moved here"
annotation_type = "Warning"
range = [8, 9]

[[slices]]
source = "x;"
line_start = 9
[[slices.annotations]]
label = "value used here after move"
annotation_type = "Error"
range = [0, 1]
12 changes: 12 additions & 0 deletions tests/fixtures/no-color/issue_9.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error: expected one of `.`, `;`, `?`, or an operator, found `for`
--> /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<i32>`, which does not implement the `Copy` trait
|
7 | let y = x;
| - value moved here
|
9 | x;
| ^ value used here after move
|
2 changes: 2 additions & 0 deletions tests/snippet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down