11use display_list:: { DisplayAnnotationPart , DisplayAnnotationType , DisplayLine , DisplayMark ,
2- DisplayTextFragment } ;
2+ DisplayMarkType , DisplayTextFragment } ;
33use std:: fmt;
44
55pub trait DisplayListFormatting {
6- fn format_annotation_type ( annotation_type : & DisplayAnnotationType ) -> String ;
6+ fn format_annotation_type ( annotation_type : & DisplayAnnotationType ) -> String {
7+ match annotation_type {
8+ DisplayAnnotationType :: Error => "error" . to_string ( ) ,
9+ DisplayAnnotationType :: Warning => "warning" . to_string ( ) ,
10+ DisplayAnnotationType :: Info => "info" . to_string ( ) ,
11+ DisplayAnnotationType :: Note => "note" . to_string ( ) ,
12+ DisplayAnnotationType :: Help => "help" . to_string ( ) ,
13+ }
14+ }
715
8- fn format_inline_marks ( inline_marks : & [ DisplayMark ] , inline_marks_width : usize ) -> String ;
16+ fn get_inline_mark ( inline_mark : & DisplayMark ) -> String {
17+ let sigil = match inline_mark. mark_type {
18+ DisplayMarkType :: AnnotationThrough => '|' ,
19+ DisplayMarkType :: AnnotationStart => '/' ,
20+ } ;
21+ sigil. to_string ( )
22+ }
23+
24+ fn format_inline_mark ( inline_mark : & DisplayMark ) -> String {
25+ Self :: get_inline_mark ( inline_mark)
26+ }
27+
28+ fn format_inline_marks ( inline_marks : & [ DisplayMark ] , inline_marks_width : usize ) -> String {
29+ format ! (
30+ "{}{}" ,
31+ " " . repeat( inline_marks_width - inline_marks. len( ) ) ,
32+ inline_marks
33+ . iter( )
34+ . map( Self :: format_inline_mark)
35+ . collect:: <Vec <String >>( )
36+ . join( "" ) ,
37+ )
38+ }
39+
40+ fn get_source_annotation_marks (
41+ annotation_type : & DisplayAnnotationType ,
42+ annotation_part : & DisplayAnnotationPart ,
43+ ) -> ( char , char ) {
44+ let indent_char = match annotation_part {
45+ DisplayAnnotationPart :: Singleline => ' ' ,
46+ DisplayAnnotationPart :: MultilineStart => '_' ,
47+ DisplayAnnotationPart :: MultilineEnd => '_' ,
48+ } ;
49+ let mark = match annotation_type {
50+ DisplayAnnotationType :: Error => '^' ,
51+ DisplayAnnotationType :: Warning => '-' ,
52+ DisplayAnnotationType :: Info => '-' ,
53+ DisplayAnnotationType :: Note => '-' ,
54+ DisplayAnnotationType :: Help => '-' ,
55+ } ;
56+ return ( indent_char, mark) ;
57+ }
58+
59+ fn format_source_annotation_parts (
60+ annotation_type : & DisplayAnnotationType ,
61+ indent_char : char ,
62+ mark : char ,
63+ range : & ( usize , usize ) ,
64+ lineno_width : usize ,
65+ ) -> ( String , String , String ) ;
66+
67+ fn format_label_line ( _annotation_type : & DisplayAnnotationType , line : & str ) -> String {
68+ return line. to_string ( ) ;
69+ }
970
1071 fn format_source_annotation_lines (
1172 f : & mut fmt:: Formatter ,
@@ -15,7 +76,45 @@ pub trait DisplayListFormatting {
1576 label : & [ DisplayTextFragment ] ,
1677 annotation_type : & DisplayAnnotationType ,
1778 annotation_part : & DisplayAnnotationPart ,
18- ) -> fmt:: Result ;
79+ ) -> fmt:: Result {
80+ let ( indent_char, mark) =
81+ Self :: get_source_annotation_marks ( annotation_type, annotation_part) ;
82+ let ( lineno, indent, pointer) = Self :: format_source_annotation_parts (
83+ annotation_type,
84+ indent_char,
85+ mark,
86+ range,
87+ lineno_width,
88+ ) ;
89+ if let Some ( ( first, rest) ) = Self :: format_label ( label)
90+ . lines ( )
91+ . collect :: < Vec < & str > > ( )
92+ . split_first ( )
93+ {
94+ writeln ! (
95+ f,
96+ "{}{}{}{} {}" ,
97+ lineno,
98+ inline_marks,
99+ indent,
100+ pointer,
101+ Self :: format_label_line( annotation_type, * first) ,
102+ ) ?;
103+ for line in rest {
104+ writeln ! (
105+ f,
106+ "{}{}{} {}" ,
107+ lineno,
108+ inline_marks,
109+ " " . repeat( range. 1 ) ,
110+ Self :: format_label_line( annotation_type, * line) ,
111+ ) ?;
112+ }
113+ } else {
114+ writeln ! ( f, "{}{}{}{}" , lineno, inline_marks, indent, pointer, ) ?;
115+ }
116+ Ok ( ( ) )
117+ }
19118
20119 fn format_label ( label : & [ DisplayTextFragment ] ) -> String ;
21120
0 commit comments