@@ -104,15 +104,16 @@ fn format_annotation(annotation: snippet::Annotation<'_>) -> Vec<DisplayLine<'_>
104104}
105105
106106fn format_slice (
107- mut slice : snippet:: Slice < ' _ > ,
107+ slice : snippet:: Slice < ' _ > ,
108108 is_first : bool ,
109109 has_footer : bool ,
110110) -> Vec < DisplayLine < ' _ > > {
111111 let main_range = slice. annotations . get ( 0 ) . map ( |x| x. range . 0 ) ;
112- let row = slice. line_start ;
113- let origin = slice. origin . take ( ) ;
114- let mut body = format_body ( slice, has_footer) ;
115- let header = format_header ( origin, main_range, row, & body, is_first) ;
112+ let origin = slice. origin ;
113+ let line_start = slice. line_start ;
114+ let need_empty_header = origin. is_some ( ) || is_first;
115+ let mut body = format_body ( slice, need_empty_header, has_footer) ;
116+ let header = format_header ( origin, main_range, line_start, & body, is_first) ;
116117 let mut result = vec ! [ ] ;
117118
118119 if let Some ( header) = header {
@@ -122,6 +123,12 @@ fn format_slice(
122123 result
123124}
124125
126+ #[ inline]
127+ // TODO: option_zip
128+ fn zip_opt < A , B > ( a : Option < A > , b : Option < B > ) -> Option < ( A , B ) > {
129+ a. and_then ( |a| b. map ( |b| ( a, b) ) )
130+ }
131+
125132fn format_header < ' a > (
126133 origin : Option < & ' a str > ,
127134 main_range : Option < usize > ,
@@ -135,7 +142,7 @@ fn format_header<'a>(
135142 DisplayHeaderType :: Continuation
136143 } ;
137144
138- if let Some ( main_range) = main_range {
145+ if let Some ( ( main_range, path ) ) = zip_opt ( main_range, origin ) {
139146 let mut col = 1 ;
140147
141148 for item in body {
@@ -151,21 +158,22 @@ fn format_header<'a>(
151158 row += 1 ;
152159 }
153160 }
154- if let Some ( path) = origin {
155- return Some ( DisplayLine :: Raw ( DisplayRawLine :: Origin {
156- path,
157- pos : Some ( ( row, col) ) ,
158- header_type : display_header,
159- } ) ) ;
160- }
161+
162+ return Some ( DisplayLine :: Raw ( DisplayRawLine :: Origin {
163+ path,
164+ pos : Some ( ( row, col) ) ,
165+ header_type : display_header,
166+ } ) ) ;
161167 }
168+
162169 if let Some ( path) = origin {
163170 return Some ( DisplayLine :: Raw ( DisplayRawLine :: Origin {
164171 path,
165172 pos : None ,
166173 header_type : display_header,
167174 } ) ) ;
168175 }
176+
169177 None
170178}
171179
@@ -261,7 +269,11 @@ fn fold_body(mut body: Vec<DisplayLine<'_>>) -> Vec<DisplayLine<'_>> {
261269 new_body
262270}
263271
264- fn format_body ( slice : snippet:: Slice < ' _ > , has_footer : bool ) -> Vec < DisplayLine < ' _ > > {
272+ fn format_body (
273+ slice : snippet:: Slice < ' _ > ,
274+ need_empty_header : bool ,
275+ has_footer : bool ,
276+ ) -> Vec < DisplayLine < ' _ > > {
265277 let source_len = slice. source . chars ( ) . count ( ) ;
266278 if let Some ( bigger) = slice. annotations . iter ( ) . find_map ( |x| {
267279 if source_len < x. range . 1 {
@@ -445,14 +457,17 @@ fn format_body(slice: snippet::Slice<'_>, has_footer: bool) -> Vec<DisplayLine<'
445457 body = fold_body ( body) ;
446458 }
447459
448- body. insert (
449- 0 ,
450- DisplayLine :: Source {
451- lineno : None ,
452- inline_marks : vec ! [ ] ,
453- line : DisplaySourceLine :: Empty ,
454- } ,
455- ) ;
460+ if need_empty_header {
461+ body. insert (
462+ 0 ,
463+ DisplayLine :: Source {
464+ lineno : None ,
465+ inline_marks : vec ! [ ] ,
466+ line : DisplaySourceLine :: Empty ,
467+ } ,
468+ ) ;
469+ }
470+
456471 if has_footer {
457472 body. push ( DisplayLine :: Source {
458473 lineno : None ,
0 commit comments