1- use snippet:: { AnnotationType , Slice , Snippet , Annotation } ;
1+ use snippet:: { Annotation , AnnotationType , Slice , Snippet } ;
22
33pub struct DisplayList {
44 pub body : Vec < DisplayLine > ,
55}
66
77#[ derive( Debug , Clone , PartialEq ) ]
88pub enum DisplayLine {
9- AlignedAnnotation {
10- label : String ,
11- annotation_type : DisplayAnnotationType ,
12- } ,
139 Annotation {
14- label : String ,
10+ label : Vec < DisplayTextFragment > ,
1511 id : Option < String > ,
12+ aligned : bool ,
1613 annotation_type : DisplayAnnotationType ,
1714 } ,
1815 Origin {
@@ -30,7 +27,7 @@ pub enum DisplayLine {
3027 SourceAnnotation {
3128 inline_marks : Vec < DisplayMark > ,
3229 range : ( usize , usize ) ,
33- label : Option < String > ,
30+ label : Vec < DisplayTextFragment > ,
3431 annotation_type : DisplayAnnotationType ,
3532 annotation_part : DisplayAnnotationPart ,
3633 } ,
@@ -39,6 +36,18 @@ pub enum DisplayLine {
3936 } ,
4037}
4138
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+
4251#[ derive( Debug , Clone , PartialEq ) ]
4352pub enum DisplayAnnotationPart {
4453 Singleline ,
@@ -68,25 +77,52 @@ pub enum DisplayHeaderType {
6877
6978// Formatting
7079
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+
71104fn format_title ( annotation : & Annotation ) -> DisplayLine {
72105 let label = annotation. label . clone ( ) . unwrap_or ( "" . to_string ( ) ) ;
73106 DisplayLine :: Annotation {
74107 annotation_type : DisplayAnnotationType :: from ( annotation. annotation_type ) ,
75108 id : annotation. id . clone ( ) ,
76- label,
109+ aligned : false ,
110+ label : format_label ( Some ( & label) , Some ( DisplayTextStyle :: Emphasis ) ) ,
77111 }
78112}
79113
80114fn format_annotation ( annotation : & Annotation ) -> DisplayLine {
81115 let label = annotation. label . clone ( ) . unwrap_or ( "" . to_string ( ) ) ;
82- DisplayLine :: AlignedAnnotation {
116+ DisplayLine :: Annotation {
83117 annotation_type : DisplayAnnotationType :: from ( annotation. annotation_type ) ,
84- label,
118+ aligned : true ,
119+ id : None ,
120+ label : format_label ( Some ( & label) , None ) ,
85121 }
86122}
87123
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 ) ;
90126 let mut result = vec ! [ ] ;
91127
92128 let header = format_header ( slice, & body, is_first) ;
@@ -185,7 +221,7 @@ fn fold_body(body: &[DisplayLine]) -> Vec<DisplayLine> {
185221 return new_body;
186222}
187223
188- fn format_body ( slice : & Slice ) -> Vec < DisplayLine > {
224+ fn format_body ( slice : & Slice , has_footer : bool ) -> Vec < DisplayLine > {
189225 let mut body = vec ! [ ] ;
190226
191227 let mut current_line = slice. line_start ;
@@ -221,7 +257,7 @@ fn format_body(slice: &Slice) -> Vec<DisplayLine> {
221257 DisplayLine :: SourceAnnotation {
222258 inline_marks : vec ! [ ] ,
223259 range,
224- label : Some ( annotation. label . clone ( ) ) ,
260+ label : format_label ( Some ( & annotation. label ) , None ) ,
225261 annotation_type : DisplayAnnotationType :: from (
226262 annotation. annotation_type ,
227263 ) ,
@@ -247,7 +283,7 @@ fn format_body(slice: &Slice) -> Vec<DisplayLine> {
247283 DisplayLine :: SourceAnnotation {
248284 inline_marks : vec ! [ ] ,
249285 range,
250- label : None ,
286+ label : vec ! [ ] ,
251287 annotation_type : DisplayAnnotationType :: from (
252288 annotation. annotation_type ,
253289 ) ,
@@ -282,7 +318,7 @@ fn format_body(slice: &Slice) -> Vec<DisplayLine> {
282318 DisplayLine :: SourceAnnotation {
283319 inline_marks : vec ! [ DisplayMark :: AnnotationThrough ] ,
284320 range,
285- label : Some ( annotation. label . clone ( ) ) ,
321+ label : format_label ( Some ( & annotation. label ) , None ) ,
286322 annotation_type : DisplayAnnotationType :: from (
287323 annotation. annotation_type ,
288324 ) ,
@@ -302,7 +338,9 @@ fn format_body(slice: &Slice) -> Vec<DisplayLine> {
302338 }
303339
304340 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 ( ) {
306344 body. push ( DisplayLine :: EmptySource ) ;
307345 }
308346 body
@@ -317,7 +355,11 @@ impl From<Snippet> for DisplayList {
317355
318356 let mut slice_idx = 0 ;
319357 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+ ) ) ;
321363 slice_idx += 1 ;
322364 }
323365 if let Some ( annotation) = snippet. footer {
0 commit comments