@@ -919,105 +919,65 @@ fn fold_prefix_suffix(mut snippet: snippet::Snippet<'_>) -> snippet::Snippet<'_>
919
919
snippet
920
920
}
921
921
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 ;
927
925
928
926
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, .. } => {
937
931
if annotations. is_empty ( ) {
938
- no_annotation_lines_counter += 1 ;
939
- continue ;
932
+ unhighlighed_lines. push ( line) ;
940
933
} 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) ;
970
943
}
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) ;
974
969
}
975
970
}
976
- no_annotation_lines_counter = 0 ;
971
+ lines . push ( line ) ;
977
972
}
978
973
}
979
- DisplayLine :: Source { .. } => {
980
- no_annotation_lines_counter += 1 ;
981
- continue ;
982
- }
983
974
_ => {
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) ;
1016
976
}
1017
977
}
1018
978
}
1019
979
1020
- new_body
980
+ lines
1021
981
}
1022
982
1023
983
fn format_body (
0 commit comments