2
2
use super :: * ;
3
3
use crate :: { formatter:: get_term_style, snippet} ;
4
4
5
- fn format_label ( label : Option < & str > , style : Option < DisplayTextStyle > ) -> Vec < DisplayTextFragment > {
5
+ fn format_label (
6
+ label : Option < & str > ,
7
+ style : Option < DisplayTextStyle > ,
8
+ ) -> Vec < DisplayTextFragment < ' _ > > {
6
9
let mut result = vec ! [ ] ;
7
10
if let Some ( label) = label {
8
11
for ( idx, element) in label. split ( "__" ) . enumerate ( ) {
@@ -17,15 +20,15 @@ fn format_label(label: Option<&str>, style: Option<DisplayTextStyle>) -> Vec<Dis
17
20
}
18
21
} ;
19
22
result. push ( DisplayTextFragment {
20
- content : element. to_string ( ) ,
23
+ content : element,
21
24
style : element_style,
22
25
} ) ;
23
26
}
24
27
}
25
28
result
26
29
}
27
30
28
- fn format_title ( annotation : snippet:: Annotation ) -> DisplayLine {
31
+ fn format_title ( annotation : snippet:: Annotation < ' _ > ) -> DisplayLine < ' _ > {
29
32
let label = annotation. label . unwrap_or_default ( ) ;
30
33
DisplayLine :: Raw ( DisplayRawLine :: Annotation {
31
34
annotation : Annotation {
@@ -38,7 +41,7 @@ fn format_title(annotation: snippet::Annotation) -> DisplayLine {
38
41
} )
39
42
}
40
43
41
- fn format_annotation ( annotation : snippet:: Annotation ) -> Vec < DisplayLine > {
44
+ fn format_annotation ( annotation : snippet:: Annotation < ' _ > ) -> Vec < DisplayLine < ' _ > > {
42
45
let mut result = vec ! [ ] ;
43
46
let label = annotation. label . unwrap_or_default ( ) ;
44
47
for ( i, line) in label. lines ( ) . enumerate ( ) {
@@ -55,7 +58,11 @@ fn format_annotation(annotation: snippet::Annotation) -> Vec<DisplayLine> {
55
58
result
56
59
}
57
60
58
- fn format_slice ( mut slice : snippet:: Slice , is_first : bool , has_footer : bool ) -> Vec < DisplayLine > {
61
+ fn format_slice (
62
+ mut slice : snippet:: Slice < ' _ > ,
63
+ is_first : bool ,
64
+ has_footer : bool ,
65
+ ) -> Vec < DisplayLine < ' _ > > {
59
66
let main_range = slice. annotations . get ( 0 ) . map ( |x| x. range . 0 ) ;
60
67
let row = slice. line_start ;
61
68
let origin = slice. origin . take ( ) ;
@@ -70,13 +77,13 @@ fn format_slice(mut slice: snippet::Slice, is_first: bool, has_footer: bool) ->
70
77
result
71
78
}
72
79
73
- fn format_header (
74
- origin : Option < String > ,
80
+ fn format_header < ' a > (
81
+ origin : Option < & ' a str > ,
75
82
main_range : Option < usize > ,
76
83
mut row : usize ,
77
- body : & [ DisplayLine ] ,
84
+ body : & [ DisplayLine < ' _ > ] ,
78
85
is_first : bool ,
79
- ) -> Option < DisplayLine > {
86
+ ) -> Option < DisplayLine < ' a > > {
80
87
let display_header = if is_first {
81
88
DisplayHeaderType :: Initial
82
89
} else {
@@ -117,17 +124,19 @@ fn format_header(
117
124
None
118
125
}
119
126
120
- fn fold_body ( body : & [ DisplayLine ] ) -> Vec < DisplayLine > {
121
- let mut new_body = vec ! [ ] ;
127
+ fn fold_body ( mut body : Vec < DisplayLine < ' _ > > ) -> Vec < DisplayLine < ' _ > > {
128
+ enum Line {
129
+ Fold ( usize ) ,
130
+ Source ( usize ) ,
131
+ } ;
122
132
133
+ let mut lines = vec ! [ ] ;
123
134
let mut no_annotation_lines_counter = 0 ;
124
- let mut idx = 0 ;
125
135
126
- while idx < body. len ( ) {
127
- match body [ idx ] {
136
+ for ( idx, line ) in body. iter ( ) . enumerate ( ) {
137
+ match line {
128
138
DisplayLine :: Source {
129
139
line : DisplaySourceLine :: Annotation { .. } ,
130
- ref inline_marks,
131
140
..
132
141
} => {
133
142
if no_annotation_lines_counter > 2 {
@@ -143,40 +152,71 @@ fn fold_body(body: &[DisplayLine]) -> Vec<DisplayLine> {
143
152
} else {
144
153
1
145
154
} ;
146
- for item in body. iter ( ) . take ( fold_start + pre_len) . skip ( fold_start) {
147
- new_body. push ( item. clone ( ) ) ;
155
+ for ( i, _) in body
156
+ . iter ( )
157
+ . enumerate ( )
158
+ . take ( fold_start + pre_len)
159
+ . skip ( fold_start)
160
+ {
161
+ lines. push ( Line :: Source ( i) ) ;
148
162
}
149
- new_body. push ( DisplayLine :: Fold {
150
- inline_marks : inline_marks. clone ( ) ,
151
- } ) ;
152
- for item in body. iter ( ) . take ( fold_end) . skip ( fold_end - post_len) {
153
- new_body. push ( item. clone ( ) ) ;
163
+ lines. push ( Line :: Fold ( idx) ) ;
164
+ for ( i, _) in body
165
+ . iter ( )
166
+ . enumerate ( )
167
+ . take ( fold_end)
168
+ . skip ( fold_end - post_len)
169
+ {
170
+ lines. push ( Line :: Source ( i) ) ;
154
171
}
155
172
} else {
156
173
let start = idx - no_annotation_lines_counter;
157
- for item in body. iter ( ) . take ( idx) . skip ( start) {
158
- new_body . push ( item . clone ( ) ) ;
174
+ for ( i , _ ) in body. iter ( ) . enumerate ( ) . take ( idx) . skip ( start) {
175
+ lines . push ( Line :: Source ( i ) ) ;
159
176
}
160
177
}
161
178
no_annotation_lines_counter = 0 ;
162
179
}
163
180
DisplayLine :: Source { .. } => {
164
181
no_annotation_lines_counter += 1 ;
165
- idx += 1 ;
166
182
continue ;
167
183
}
168
184
_ => {
169
185
no_annotation_lines_counter += 1 ;
170
186
}
171
187
}
172
- new_body. push ( body[ idx] . clone ( ) ) ;
173
- idx += 1 ;
188
+ lines. push ( Line :: Source ( idx) ) ;
189
+ }
190
+
191
+ let mut new_body = vec ! [ ] ;
192
+ let mut removed = 0 ;
193
+ for line in lines {
194
+ match line {
195
+ Line :: Source ( i) => {
196
+ new_body. push ( body. remove ( i - removed) ) ;
197
+ removed += 1 ;
198
+ }
199
+ Line :: Fold ( i) => {
200
+ if let DisplayLine :: Source {
201
+ line : DisplaySourceLine :: Annotation { .. } ,
202
+ ref inline_marks,
203
+ ..
204
+ } = body. get ( i - removed) . unwrap ( )
205
+ {
206
+ new_body. push ( DisplayLine :: Fold {
207
+ inline_marks : inline_marks. clone ( ) ,
208
+ } )
209
+ } else {
210
+ unreachable ! ( )
211
+ }
212
+ }
213
+ }
174
214
}
175
215
176
216
new_body
177
217
}
178
218
179
- fn format_body ( slice : snippet:: Slice , has_footer : bool ) -> Vec < DisplayLine > {
219
+ fn format_body ( slice : snippet:: Slice < ' _ > , has_footer : bool ) -> Vec < DisplayLine < ' _ > > {
180
220
let source_len = slice. source . chars ( ) . count ( ) ;
181
221
if let Some ( bigger) = slice. annotations . iter ( ) . find_map ( |x| {
182
222
if source_len < x. range . 1 {
@@ -205,7 +245,7 @@ fn format_body(slice: snippet::Slice, has_footer: bool) -> Vec<DisplayLine> {
205
245
lineno : Some ( current_line) ,
206
246
inline_marks : vec ! [ ] ,
207
247
line : DisplaySourceLine :: Content {
208
- text : line. to_string ( ) ,
248
+ text : line,
209
249
range : line_range,
210
250
} ,
211
251
} ) ;
@@ -361,7 +401,7 @@ fn format_body(slice: snippet::Slice, has_footer: bool) -> Vec<DisplayLine> {
361
401
}
362
402
363
403
if slice. fold {
364
- body = fold_body ( & body) ;
404
+ body = fold_body ( body) ;
365
405
}
366
406
367
407
body. insert (
@@ -388,16 +428,15 @@ fn format_body(slice: snippet::Slice, has_footer: bool) -> Vec<DisplayLine> {
388
428
body
389
429
}
390
430
391
- // TODO: From reference to DisplayList<'a>
392
- impl From < snippet:: Snippet > for DisplayList {
431
+ impl < ' a > From < snippet:: Snippet < ' a > > for DisplayList < ' a > {
393
432
fn from (
394
433
snippet:: Snippet {
395
434
title,
396
435
footer,
397
436
slices,
398
437
opt,
399
- } : snippet:: Snippet ,
400
- ) -> Self {
438
+ } : snippet:: Snippet < ' a > ,
439
+ ) -> DisplayList < ' a > {
401
440
let mut body = vec ! [ ] ;
402
441
if let Some ( annotation) = title {
403
442
body. push ( format_title ( annotation) ) ;
0 commit comments