Skip to content
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
55 changes: 49 additions & 6 deletions src/renderer/display_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,17 +696,19 @@ impl<'a> Iterator for CursorLines<'a> {
}

fn format_message(
snippet::Message {
message: snippet::Message<'_>,
term_width: usize,
anonymized_line_numbers: bool,
primary: bool,
) -> Vec<DisplaySet<'_>> {
let snippet::Message {
level,
id,
title,
footer,
snippets,
}: snippet::Message<'_>,
term_width: usize,
anonymized_line_numbers: bool,
primary: bool,
) -> Vec<DisplaySet<'_>> {
} = message;

let mut sets = vec![];
let body = if !snippets.is_empty() || primary {
vec![format_title(level, id, title)]
Expand All @@ -715,6 +717,7 @@ fn format_message(
};

for (idx, snippet) in snippets.into_iter().enumerate() {
let snippet = fold_prefix_suffix(snippet);
sets.push(format_snippet(
snippet,
idx == 0,
Expand Down Expand Up @@ -876,6 +879,46 @@ fn format_header<'a>(
None
}

fn fold_prefix_suffix(mut snippet: snippet::Snippet<'_>) -> snippet::Snippet<'_> {
if !snippet.fold {
return snippet;
}

let ann_start = snippet
.annotations
.iter()
.map(|ann| ann.range.start)
.min()
.unwrap_or(0);
if let Some(before_new_start) = snippet.source[0..ann_start].rfind('\n') {
let new_start = before_new_start + 1;

let line_offset = snippet.source[..new_start].lines().count();
snippet.line_start += line_offset;

snippet.source = &snippet.source[new_start..];

for ann in &mut snippet.annotations {
let range_start = ann.range.start - new_start;
let range_end = ann.range.end - new_start;
ann.range = range_start..range_end;
}
}

let ann_end = snippet
.annotations
.iter()
.map(|ann| ann.range.end)
.max()
.unwrap_or(snippet.source.len());
if let Some(end_offset) = snippet.source[ann_end..].find('\n') {
let new_end = ann_end + end_offset;
snippet.source = &snippet.source[..new_end];
}

snippet
}

fn fold_body(mut body: Vec<DisplayLine<'_>>) -> Vec<DisplayLine<'_>> {
enum Line {
Fold(usize),
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions tests/fixtures/no-color/fold_leading.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions tests/fixtures/no-color/fold_leading.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[message]
level = "Error"
id = "E0308"
title = "invalid type: integer `20`, expected a bool"

[[message.snippets]]
source = """
[workspace]

[package]
name = "hello"
version = "1.0.0"
license = "MIT"
rust-version = "1.70"
edition = "2021"

[lints]
workspace = 20
"""
line_start = 1
origin = "Cargo.toml"
fold = true
[[message.snippets.annotations]]
label = ""
level = "Error"
range = [132, 134]
33 changes: 33 additions & 0 deletions tests/fixtures/no-color/fold_trailing.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions tests/fixtures/no-color/fold_trailing.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[message]
level = "Error"
id = "E0308"
title = "invalid type: integer `20`, expected a lints table"

[[message.snippets]]
source = """
lints = 20

[workspace]

[package]
name = "hello"
version = "1.0.0"
license = "MIT"
rust-version = "1.70"
edition = "2021"
"""
line_start = 1
origin = "Cargo.toml"
fold = true
[[message.snippets.annotations]]
label = ""
level = "Error"
range = [8, 10]
1 change: 0 additions & 1 deletion tests/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ fn test_i_29() {
error: oops
--> <current file>:2:8
|
1 | First line
2 | Second oops line
| ^^^^ oops
|"#]]
Expand Down