1
- use snippet:: { AnnotationType , Slice , Snippet , Annotation } ;
1
+ use snippet:: { Annotation , AnnotationType , Slice , Snippet } ;
2
2
3
3
pub struct DisplayList {
4
4
pub body : Vec < DisplayLine > ,
5
5
}
6
6
7
7
#[ derive( Debug , Clone , PartialEq ) ]
8
8
pub enum DisplayLine {
9
- AlignedAnnotation {
10
- label : String ,
11
- annotation_type : DisplayAnnotationType ,
12
- } ,
13
9
Annotation {
14
- label : String ,
10
+ label : Vec < DisplayTextFragment > ,
15
11
id : Option < String > ,
12
+ aligned : bool ,
16
13
annotation_type : DisplayAnnotationType ,
17
14
} ,
18
15
Origin {
@@ -30,7 +27,7 @@ pub enum DisplayLine {
30
27
SourceAnnotation {
31
28
inline_marks : Vec < DisplayMark > ,
32
29
range : ( usize , usize ) ,
33
- label : Option < String > ,
30
+ label : Vec < DisplayTextFragment > ,
34
31
annotation_type : DisplayAnnotationType ,
35
32
annotation_part : DisplayAnnotationPart ,
36
33
} ,
@@ -39,6 +36,18 @@ pub enum DisplayLine {
39
36
} ,
40
37
}
41
38
39
+ #[ derive( Debug , Clone , PartialEq ) ]
40
+ pub struct DisplayTextFragment {
41
+ pub content : String ,
42
+ pub style : DisplayTextStyle ,
43
+ }
44
+
45
+ #[ derive( Debug , Clone , Copy , PartialEq ) ]
46
+ pub enum DisplayTextStyle {
47
+ Regular ,
48
+ Emphasis ,
49
+ }
50
+
42
51
#[ derive( Debug , Clone , PartialEq ) ]
43
52
pub enum DisplayAnnotationPart {
44
53
Singleline ,
@@ -68,25 +77,52 @@ pub enum DisplayHeaderType {
68
77
69
78
// Formatting
70
79
80
+ fn format_label ( label : Option < & str > , style : Option < DisplayTextStyle > ) -> Vec < DisplayTextFragment > {
81
+ let mut result = vec ! [ ] ;
82
+ if let Some ( label) = label {
83
+ let elements: Vec < & str > = label. split ( "__" ) . collect ( ) ;
84
+ let mut idx = 0 ;
85
+ for element in elements {
86
+ let element_style = match style {
87
+ Some ( s) => s,
88
+ None => if idx % 2 == 0 {
89
+ DisplayTextStyle :: Regular
90
+ } else {
91
+ DisplayTextStyle :: Emphasis
92
+ } ,
93
+ } ;
94
+ result. push ( DisplayTextFragment {
95
+ content : element. to_string ( ) ,
96
+ style : element_style,
97
+ } ) ;
98
+ idx += 1 ;
99
+ }
100
+ }
101
+ return result;
102
+ }
103
+
71
104
fn format_title ( annotation : & Annotation ) -> DisplayLine {
72
105
let label = annotation. label . clone ( ) . unwrap_or ( "" . to_string ( ) ) ;
73
106
DisplayLine :: Annotation {
74
107
annotation_type : DisplayAnnotationType :: from ( annotation. annotation_type ) ,
75
108
id : annotation. id . clone ( ) ,
76
- label,
109
+ aligned : false ,
110
+ label : format_label ( Some ( & label) , Some ( DisplayTextStyle :: Emphasis ) ) ,
77
111
}
78
112
}
79
113
80
114
fn format_annotation ( annotation : & Annotation ) -> DisplayLine {
81
115
let label = annotation. label . clone ( ) . unwrap_or ( "" . to_string ( ) ) ;
82
- DisplayLine :: AlignedAnnotation {
116
+ DisplayLine :: Annotation {
83
117
annotation_type : DisplayAnnotationType :: from ( annotation. annotation_type ) ,
84
- label,
118
+ aligned : true ,
119
+ id : None ,
120
+ label : format_label ( Some ( & label) , None ) ,
85
121
}
86
122
}
87
123
88
- fn format_slice ( slice : & Slice , is_first : bool ) -> Vec < DisplayLine > {
89
- let mut body = format_body ( slice) ;
124
+ fn format_slice ( slice : & Slice , is_first : bool , has_footer : bool ) -> Vec < DisplayLine > {
125
+ let mut body = format_body ( slice, has_footer ) ;
90
126
let mut result = vec ! [ ] ;
91
127
92
128
let header = format_header ( slice, & body, is_first) ;
@@ -185,7 +221,7 @@ fn fold_body(body: &[DisplayLine]) -> Vec<DisplayLine> {
185
221
return new_body;
186
222
}
187
223
188
- fn format_body ( slice : & Slice ) -> Vec < DisplayLine > {
224
+ fn format_body ( slice : & Slice , has_footer : bool ) -> Vec < DisplayLine > {
189
225
let mut body = vec ! [ ] ;
190
226
191
227
let mut current_line = slice. line_start ;
@@ -221,7 +257,7 @@ fn format_body(slice: &Slice) -> Vec<DisplayLine> {
221
257
DisplayLine :: SourceAnnotation {
222
258
inline_marks : vec ! [ ] ,
223
259
range,
224
- label : Some ( annotation. label . clone ( ) ) ,
260
+ label : format_label ( Some ( & annotation. label ) , None ) ,
225
261
annotation_type : DisplayAnnotationType :: from (
226
262
annotation. annotation_type ,
227
263
) ,
@@ -247,7 +283,7 @@ fn format_body(slice: &Slice) -> Vec<DisplayLine> {
247
283
DisplayLine :: SourceAnnotation {
248
284
inline_marks : vec ! [ ] ,
249
285
range,
250
- label : None ,
286
+ label : vec ! [ ] ,
251
287
annotation_type : DisplayAnnotationType :: from (
252
288
annotation. annotation_type ,
253
289
) ,
@@ -282,7 +318,7 @@ fn format_body(slice: &Slice) -> Vec<DisplayLine> {
282
318
DisplayLine :: SourceAnnotation {
283
319
inline_marks : vec ! [ DisplayMark :: AnnotationThrough ] ,
284
320
range,
285
- label : Some ( annotation. label . clone ( ) ) ,
321
+ label : format_label ( Some ( & annotation. label ) , None ) ,
286
322
annotation_type : DisplayAnnotationType :: from (
287
323
annotation. annotation_type ,
288
324
) ,
@@ -302,7 +338,9 @@ fn format_body(slice: &Slice) -> Vec<DisplayLine> {
302
338
}
303
339
304
340
body. insert ( 0 , DisplayLine :: EmptySource ) ;
305
- if let Some ( DisplayLine :: Source { .. } ) = body. last ( ) {
341
+ if has_footer {
342
+ body. push ( DisplayLine :: EmptySource ) ;
343
+ } else if let Some ( DisplayLine :: Source { .. } ) = body. last ( ) {
306
344
body. push ( DisplayLine :: EmptySource ) ;
307
345
}
308
346
body
@@ -317,7 +355,11 @@ impl From<Snippet> for DisplayList {
317
355
318
356
let mut slice_idx = 0 ;
319
357
for slice in snippet. slices {
320
- body. append ( & mut format_slice ( & slice, slice_idx == 0 ) ) ;
358
+ body. append ( & mut format_slice (
359
+ & slice,
360
+ slice_idx == 0 ,
361
+ snippet. footer . is_some ( ) ,
362
+ ) ) ;
321
363
slice_idx += 1 ;
322
364
}
323
365
if let Some ( annotation) = snippet. footer {
0 commit comments