Skip to content
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

fix(render): Fold like rustc #111

Merged
merged 1 commit into from
Mar 21, 2024
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
14 changes: 8 additions & 6 deletions examples/expected_type.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
130 changes: 45 additions & 85 deletions src/renderer/display_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -919,105 +919,65 @@ fn fold_prefix_suffix(mut snippet: snippet::Snippet<'_>) -> snippet::Snippet<'_>
snippet
}

fn fold_body(mut body: Vec<DisplayLine<'_>>) -> Vec<DisplayLine<'_>> {
enum Line {
Fold(usize),
Source(usize),
}
fn fold_body(body: Vec<DisplayLine<'_>>) -> Vec<DisplayLine<'_>> {
const INNER_CONTEXT: usize = 1;
const INNER_UNFOLD_SIZE: usize = INNER_CONTEXT * 2 + 1;

let mut lines = vec![];
let mut no_annotation_lines_counter = 0;
for (idx, line) in body.iter().enumerate() {
match line {
DisplayLine::Source {
line: DisplaySourceLine::Content { .. },
annotations,
..
} => {
let mut unhighlighed_lines = vec![];
for line in body {
match &line {
DisplayLine::Source { annotations, .. } => {
if annotations.is_empty() {
no_annotation_lines_counter += 1;
continue;
unhighlighed_lines.push(line);
} else {
let fold_start = idx - no_annotation_lines_counter;
if no_annotation_lines_counter >= 2 {
let fold_end = idx;
let pre_len = if no_annotation_lines_counter > 8 {
4
} else {
0
};
let post_len = if no_annotation_lines_counter > 8 {
2
} else {
1
};
for (i, _) in body
.iter()
.enumerate()
.take(fold_start + pre_len)
.skip(fold_start)
{
lines.push(Line::Source(i));
}
lines.push(Line::Fold(idx));
for (i, _) in body
.iter()
.enumerate()
.take(fold_end)
.skip(fold_end + 1 - post_len)
{
lines.push(Line::Source(i));
if lines.is_empty() {
// Ignore leading unhighlighed lines
unhighlighed_lines.clear();
}
match unhighlighed_lines.len() {
0 => {}
n if n <= INNER_UNFOLD_SIZE => {
// Rather than render `...`, don't fold
lines.append(&mut unhighlighed_lines);
}
} else {
for (i, _) in body.iter().enumerate().take(idx).skip(fold_start) {
lines.push(Line::Source(i));
_ => {
lines.extend(unhighlighed_lines.drain(..INNER_CONTEXT));
let inline_marks = lines
.last()
.and_then(|line| {
if let DisplayLine::Source {
ref inline_marks, ..
} = line
{
let mut inline_marks = inline_marks.clone();
for mark in &mut inline_marks {
mark.mark_type = DisplayMarkType::AnnotationThrough;
}
Some(inline_marks)
} else {
None
}
})
.unwrap_or_default();
lines.push(DisplayLine::Fold {
inline_marks: inline_marks.clone(),
});
unhighlighed_lines
.drain(..unhighlighed_lines.len().saturating_sub(INNER_CONTEXT));
lines.append(&mut unhighlighed_lines);
}
}
no_annotation_lines_counter = 0;
lines.push(line);
}
}
DisplayLine::Source { .. } => {
no_annotation_lines_counter += 1;
continue;
}
_ => {
no_annotation_lines_counter += 1;
}
}
lines.push(Line::Source(idx));
}

let mut new_body = vec![];
let mut removed = 0;
for line in lines {
match line {
Line::Source(i) => {
new_body.push(body.remove(i - removed));
removed += 1;
}
Line::Fold(i) => {
if let DisplayLine::Source {
line: DisplaySourceLine::Content { .. },
ref inline_marks,
ref annotations,
..
} = body.get(i - removed).unwrap()
{
if !annotations.is_empty() {
new_body.push(DisplayLine::Fold {
inline_marks: inline_marks.clone(),
});
} else {
unreachable!()
}
} else {
unreachable!()
}
unhighlighed_lines.push(line);
}
}
}

new_body
lines
}

fn format_body(
Expand Down
18 changes: 6 additions & 12 deletions tests/fixtures/no-color/fold_ann_multiline.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading