@@ -7,7 +7,7 @@ pub struct FormattedDisplayList {
77
88impl From < DisplayList > for FormattedDisplayList {
99 fn from ( dl : DisplayList ) -> Self {
10- let max_lineno = dl. body . iter ( ) . fold ( 0 , |max, ref line| match line {
10+ let lineno_width = dl. body . iter ( ) . fold ( 0 , |max, ref line| match line {
1111 DisplayLine :: SourceLine { lineno, .. } => {
1212 let width = lineno. to_string ( ) . len ( ) ;
1313 if width > max {
@@ -18,9 +18,20 @@ impl From<DisplayList> for FormattedDisplayList {
1818 }
1919 _ => max,
2020 } ) ;
21+ let inline_marks_width = dl. body . iter ( ) . fold ( 0 , |max, ref line| match line {
22+ DisplayLine :: SourceLine { inline_marks, .. } => {
23+ let width = inline_marks. len ( ) ;
24+ if width > max {
25+ width + 1
26+ } else {
27+ max
28+ }
29+ }
30+ _ => max,
31+ } ) ;
2132 let body = dl. body
2233 . into_iter ( )
23- . map ( |line| FormattedDisplayLine :: format ( line, max_lineno ) )
34+ . map ( |line| FormattedDisplayLine :: format ( line, lineno_width , inline_marks_width ) )
2435 . collect ( ) ;
2536 FormattedDisplayList { body }
2637 }
@@ -39,6 +50,7 @@ impl fmt::Display for FormattedDisplayList {
3950 }
4051}
4152
53+ #[ derive( Debug ) ]
4254enum FormattedDisplayLine {
4355 RawLine ( String ) ,
4456 EmptySourceLine {
@@ -58,19 +70,19 @@ enum FormattedDisplayLine {
5870}
5971
6072impl FormattedDisplayLine {
61- fn format ( dl : DisplayLine , max_lineno : usize ) -> Self {
73+ fn format ( dl : DisplayLine , lineno_width : usize , inline_marks_width : usize ) -> Self {
6274 match dl {
6375 DisplayLine :: RawLine ( s) => FormattedDisplayLine :: RawLine ( s) ,
6476 DisplayLine :: EmptySourceLine => FormattedDisplayLine :: EmptySourceLine {
65- lineno : " " . repeat ( max_lineno ) ,
77+ lineno : " " . repeat ( lineno_width ) ,
6678 } ,
6779 DisplayLine :: SourceLine {
6880 lineno,
6981 inline_marks,
7082 content,
7183 } => FormattedDisplayLine :: SourceLine {
72- lineno : format ! ( "{: >width$}" , lineno, width = max_lineno ) ,
73- inline_marks : Self :: format_inline_marks ( & inline_marks) ,
84+ lineno : format ! ( "{: >width$}" , lineno, width = lineno_width ) ,
85+ inline_marks : Self :: format_inline_marks ( & inline_marks, inline_marks_width ) ,
7486 content,
7587 } ,
7688 DisplayLine :: AnnotationLine {
@@ -79,22 +91,23 @@ impl FormattedDisplayLine {
7991 label,
8092 annotation_type,
8193 } => FormattedDisplayLine :: AnnotationLine {
82- lineno : " " . repeat ( max_lineno ) ,
83- inline_marks : Self :: format_inline_marks ( & inline_marks) ,
94+ lineno : " " . repeat ( lineno_width ) ,
95+ inline_marks : Self :: format_inline_marks ( & inline_marks, inline_marks_width ) ,
8496 content : Self :: format_annotation_content ( range, label, annotation_type) ,
8597 } ,
8698 DisplayLine :: FoldLine => FormattedDisplayLine :: FoldLine ,
8799 }
88100 }
89101
90- fn format_inline_marks ( inline_marks : & [ DisplayMark ] ) -> String {
102+ fn format_inline_marks ( inline_marks : & [ DisplayMark ] , inline_marks_width : usize ) -> String {
91103 format ! (
92- "{}" ,
104+ "{: >width$ }" ,
93105 inline_marks
94106 . iter( )
95107 . map( |mark| format!( "{}" , mark) )
96108 . collect:: <Vec <String >>( )
97- . join( "" )
109+ . join( "" ) ,
110+ width = inline_marks_width
98111 )
99112 }
100113
@@ -103,17 +116,32 @@ impl FormattedDisplayLine {
103116 label : String ,
104117 annotation_type : DisplayAnnotationType ,
105118 ) -> String {
106- let underline_char = match annotation_type {
107- DisplayAnnotationType :: Error => "^" ,
108- DisplayAnnotationType :: Warning => "-" ,
109- } ;
110-
111- format ! (
112- "{}{} {}" ,
113- " " . repeat( range. 0 ) ,
114- underline_char. repeat( range. 1 - range. 0 ) ,
115- label
116- )
119+ match annotation_type {
120+ DisplayAnnotationType :: Error => format ! (
121+ "{}{} {}" ,
122+ " " . repeat( range. 0 ) ,
123+ "^" . repeat( range. 1 - range. 0 ) ,
124+ label
125+ ) ,
126+ DisplayAnnotationType :: Warning => format ! (
127+ "{}{} {}" ,
128+ " " . repeat( range. 0 ) ,
129+ "-" . repeat( range. 1 - range. 0 ) ,
130+ label
131+ ) ,
132+ DisplayAnnotationType :: MultilineStart => format ! (
133+ "{}{} {}" ,
134+ "_" . repeat( range. 0 ) ,
135+ "^" . repeat( range. 1 - range. 0 ) ,
136+ label
137+ ) ,
138+ DisplayAnnotationType :: MultilineEnd => format ! (
139+ "{}{} {}" ,
140+ "_" . repeat( range. 0 ) ,
141+ "^" . repeat( range. 1 - range. 0 ) ,
142+ label
143+ ) ,
144+ }
117145 }
118146}
119147
@@ -125,13 +153,13 @@ impl fmt::Display for FormattedDisplayLine {
125153 lineno,
126154 inline_marks,
127155 content,
128- } => write ! ( f, "{} | {} {}" , lineno, inline_marks, content) ,
156+ } => write ! ( f, "{} |{} {}" , lineno, inline_marks, content) ,
129157 FormattedDisplayLine :: RawLine ( body) => write ! ( f, "{}" , body) ,
130158 FormattedDisplayLine :: AnnotationLine {
131159 lineno,
132160 inline_marks,
133161 content,
134- } => write ! ( f, "{} | {}{}" , lineno, inline_marks, content) ,
162+ } => write ! ( f, "{} |{}{}" , lineno, inline_marks, content) ,
135163 FormattedDisplayLine :: FoldLine => write ! ( f, " ... |" ) ,
136164 }
137165 }
0 commit comments