Skip to content

Commit f8642f4

Browse files
authored
Merge pull request #111 from epage/fold-rustc
fix(render): Fold like rustc
2 parents 65b6bef + 6e55ecc commit f8642f4

File tree

3 files changed

+59
-103
lines changed

3 files changed

+59
-103
lines changed

examples/expected_type.svg

+8-6
Loading

src/renderer/display_list.rs

+45-85
Original file line numberDiff line numberDiff line change
@@ -919,105 +919,65 @@ fn fold_prefix_suffix(mut snippet: snippet::Snippet<'_>) -> snippet::Snippet<'_>
919919
snippet
920920
}
921921

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

928926
let mut lines = vec![];
929-
let mut no_annotation_lines_counter = 0;
930-
for (idx, line) in body.iter().enumerate() {
931-
match line {
932-
DisplayLine::Source {
933-
line: DisplaySourceLine::Content { .. },
934-
annotations,
935-
..
936-
} => {
927+
let mut unhighlighed_lines = vec![];
928+
for line in body {
929+
match &line {
930+
DisplayLine::Source { annotations, .. } => {
937931
if annotations.is_empty() {
938-
no_annotation_lines_counter += 1;
939-
continue;
932+
unhighlighed_lines.push(line);
940933
} else {
941-
let fold_start = idx - no_annotation_lines_counter;
942-
if no_annotation_lines_counter >= 2 {
943-
let fold_end = idx;
944-
let pre_len = if no_annotation_lines_counter > 8 {
945-
4
946-
} else {
947-
0
948-
};
949-
let post_len = if no_annotation_lines_counter > 8 {
950-
2
951-
} else {
952-
1
953-
};
954-
for (i, _) in body
955-
.iter()
956-
.enumerate()
957-
.take(fold_start + pre_len)
958-
.skip(fold_start)
959-
{
960-
lines.push(Line::Source(i));
961-
}
962-
lines.push(Line::Fold(idx));
963-
for (i, _) in body
964-
.iter()
965-
.enumerate()
966-
.take(fold_end)
967-
.skip(fold_end + 1 - post_len)
968-
{
969-
lines.push(Line::Source(i));
934+
if lines.is_empty() {
935+
// Ignore leading unhighlighed lines
936+
unhighlighed_lines.clear();
937+
}
938+
match unhighlighed_lines.len() {
939+
0 => {}
940+
n if n <= INNER_UNFOLD_SIZE => {
941+
// Rather than render `...`, don't fold
942+
lines.append(&mut unhighlighed_lines);
970943
}
971-
} else {
972-
for (i, _) in body.iter().enumerate().take(idx).skip(fold_start) {
973-
lines.push(Line::Source(i));
944+
_ => {
945+
lines.extend(unhighlighed_lines.drain(..INNER_CONTEXT));
946+
let inline_marks = lines
947+
.last()
948+
.and_then(|line| {
949+
if let DisplayLine::Source {
950+
ref inline_marks, ..
951+
} = line
952+
{
953+
let mut inline_marks = inline_marks.clone();
954+
for mark in &mut inline_marks {
955+
mark.mark_type = DisplayMarkType::AnnotationThrough;
956+
}
957+
Some(inline_marks)
958+
} else {
959+
None
960+
}
961+
})
962+
.unwrap_or_default();
963+
lines.push(DisplayLine::Fold {
964+
inline_marks: inline_marks.clone(),
965+
});
966+
unhighlighed_lines
967+
.drain(..unhighlighed_lines.len().saturating_sub(INNER_CONTEXT));
968+
lines.append(&mut unhighlighed_lines);
974969
}
975970
}
976-
no_annotation_lines_counter = 0;
971+
lines.push(line);
977972
}
978973
}
979-
DisplayLine::Source { .. } => {
980-
no_annotation_lines_counter += 1;
981-
continue;
982-
}
983974
_ => {
984-
no_annotation_lines_counter += 1;
985-
}
986-
}
987-
lines.push(Line::Source(idx));
988-
}
989-
990-
let mut new_body = vec![];
991-
let mut removed = 0;
992-
for line in lines {
993-
match line {
994-
Line::Source(i) => {
995-
new_body.push(body.remove(i - removed));
996-
removed += 1;
997-
}
998-
Line::Fold(i) => {
999-
if let DisplayLine::Source {
1000-
line: DisplaySourceLine::Content { .. },
1001-
ref inline_marks,
1002-
ref annotations,
1003-
..
1004-
} = body.get(i - removed).unwrap()
1005-
{
1006-
if !annotations.is_empty() {
1007-
new_body.push(DisplayLine::Fold {
1008-
inline_marks: inline_marks.clone(),
1009-
});
1010-
} else {
1011-
unreachable!()
1012-
}
1013-
} else {
1014-
unreachable!()
1015-
}
975+
unhighlighed_lines.push(line);
1016976
}
1017977
}
1018978
}
1019979

1020-
new_body
980+
lines
1021981
}
1022982

1023983
fn format_body(

tests/fixtures/no-color/fold_ann_multiline.svg

+6-12
Loading

0 commit comments

Comments
 (0)