@@ -104,15 +104,16 @@ fn format_annotation(annotation: snippet::Annotation<'_>) -> Vec<DisplayLine<'_>
104
104
}
105
105
106
106
fn format_slice (
107
- mut slice : snippet:: Slice < ' _ > ,
107
+ slice : snippet:: Slice < ' _ > ,
108
108
is_first : bool ,
109
109
has_footer : bool ,
110
110
) -> Vec < DisplayLine < ' _ > > {
111
111
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) ;
116
117
let mut result = vec ! [ ] ;
117
118
118
119
if let Some ( header) = header {
@@ -122,6 +123,12 @@ fn format_slice(
122
123
result
123
124
}
124
125
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
+
125
132
fn format_header < ' a > (
126
133
origin : Option < & ' a str > ,
127
134
main_range : Option < usize > ,
@@ -135,7 +142,7 @@ fn format_header<'a>(
135
142
DisplayHeaderType :: Continuation
136
143
} ;
137
144
138
- if let Some ( main_range) = main_range {
145
+ if let Some ( ( main_range, path ) ) = zip_opt ( main_range, origin ) {
139
146
let mut col = 1 ;
140
147
141
148
for item in body {
@@ -151,21 +158,22 @@ fn format_header<'a>(
151
158
row += 1 ;
152
159
}
153
160
}
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
+ } ) ) ;
161
167
}
168
+
162
169
if let Some ( path) = origin {
163
170
return Some ( DisplayLine :: Raw ( DisplayRawLine :: Origin {
164
171
path,
165
172
pos : None ,
166
173
header_type : display_header,
167
174
} ) ) ;
168
175
}
176
+
169
177
None
170
178
}
171
179
@@ -261,7 +269,11 @@ fn fold_body(mut body: Vec<DisplayLine<'_>>) -> Vec<DisplayLine<'_>> {
261
269
new_body
262
270
}
263
271
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 < ' _ > > {
265
277
let source_len = slice. source . chars ( ) . count ( ) ;
266
278
if let Some ( bigger) = slice. annotations . iter ( ) . find_map ( |x| {
267
279
if source_len < x. range . 1 {
@@ -445,14 +457,17 @@ fn format_body(slice: snippet::Slice<'_>, has_footer: bool) -> Vec<DisplayLine<'
445
457
body = fold_body ( body) ;
446
458
}
447
459
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
+
456
471
if has_footer {
457
472
body. push ( DisplayLine :: Source {
458
473
lineno : None ,
0 commit comments