11pub mod styles;
22
33use super :: Renderer as RendererTrait ;
4- use crate :: DisplayList ;
4+ use crate :: annotation:: AnnotationType ;
5+ use crate :: display_list:: annotation:: Annotation ;
56use crate :: display_list:: line:: DisplayLine ;
7+ use crate :: display_list:: line:: DisplayMark ;
8+ use crate :: display_list:: line:: DisplayMarkType ;
69use crate :: display_list:: line:: DisplayRawLine ;
710use crate :: display_list:: line:: DisplaySourceLine ;
8- use crate :: display_list:: annotation:: Annotation ;
11+ use crate :: DisplayList ;
12+ use std:: cmp;
913use std:: io:: Write ;
1014use std:: marker:: PhantomData ;
1115use styles:: Style as StyleTrait ;
12- use std:: cmp;
1316
1417fn digits ( n : usize ) ->usize {
1518let mut n = n;
@@ -22,14 +25,12 @@ fn digits(n: usize) -> usize {
2225}
2326
2427pub struct Renderer < S : StyleTrait > {
25- style : PhantomData < S >
28+ style : PhantomData < S > ,
2629}
2730
2831impl < S : StyleTrait > Renderer < S > {
2932pub fn new ( ) ->Self {
30- Renderer {
31- style : PhantomData
32- }
33+ Renderer { style : PhantomData }
3334}
3435
3536pub fn fmt ( & self , w : & mut impl Write , dl : & DisplayList ) -> std:: io:: Result < ( ) > {
@@ -54,7 +55,13 @@ impl<S: StyleTrait> Renderer<S> {
5455Ok ( ( ) )
5556}
5657
57- fn fmt_line ( & self , w : & mut impl Write , line : & DisplayLine , lineno_max : Option < usize > , inline_marks_width : usize ) -> std:: io:: Result < ( ) > {
58+ fn fmt_line (
59+ & self ,
60+ w : & mut impl Write ,
61+ line : & DisplayLine ,
62+ lineno_max : Option < usize > ,
63+ inline_marks_width : usize ,
64+ ) -> std:: io:: Result < ( ) > {
5865let lineno_max = lineno_max. unwrap_or ( 1 ) ;
5966match line{
6067DisplayLine :: Source {
@@ -70,42 +77,46 @@ impl<S: StyleTrait> Renderer<S> {
7077write ! ( w, " | " ) ?;
7178write ! ( w, "{:>1$}" , "" , inline_marks_width - inline_marks. len( ) ) ?;
7279for markin inline_marks{
73- write ! ( w , "{}" , mark) ?;
80+ self . fmt_display_mark ( w , mark) ?;
7481}
7582self . fmt_source_line ( w, line) ?;
76- //line.fmt_with_style(w, style)?;
7783write ! ( w, "\n " )
78- } ,
79- DisplayLine :: Raw ( l) =>{
80- self . fmt_raw_line ( w, l, lineno_max)
81- } ,
84+ }
85+ DisplayLine :: Raw ( l) =>self . fmt_raw_line ( w, l, lineno_max) ,
8286}
8387}
8488
85- fn fmt_source_line ( & self , w : & mut impl std:: io:: Write , line : & DisplaySourceLine ) -> std:: io:: Result < ( ) > {
86- match line{
87- DisplaySourceLine :: Content { text} =>{
88- write ! ( w, " {}" , text)
89- }
90- DisplaySourceLine :: Annotation {
91- annotation,
92- range : ( start, end) ,
93- } =>{
94- let indent =if start ==& 0 { 0 } else { start +1 } ;
95- write ! ( w, "{:>1$}" , "" , indent) ?;
96- if start ==& 0 {
97- write ! ( w, "{:_>1$}" , "^" , end - start +1 ) ?;
98- } else {
99- write ! ( w, "{:->1$}" , "" , end - start) ?;
100- }
101- write ! ( w, " " ) ?;
102- self . fmt_annotation ( w, annotation)
103- }
104- DisplaySourceLine :: Empty =>Ok ( ( ) ) ,
105- }
89+ fn fmt_source_line (
90+ & self ,
91+ w : & mut impl std:: io:: Write ,
92+ line : & DisplaySourceLine ,
93+ ) -> std:: io:: Result < ( ) > {
94+ match line{
95+ DisplaySourceLine :: Content { text} =>write ! ( w, " {}" , text) ,
96+ DisplaySourceLine :: Annotation {
97+ annotation,
98+ range : ( start, end) ,
99+ } =>{
100+ let indent =if start ==& 0 { 0 } else { start +1 } ;
101+ write ! ( w, "{:>1$}" , "" , indent) ?;
102+ if start ==& 0 {
103+ write ! ( w, "{:_>1$}" , "^" , end - start +1 ) ?;
104+ } else {
105+ write ! ( w, "{:->1$}" , "" , end - start) ?;
106+ }
107+ write ! ( w, " " ) ?;
108+ self . fmt_annotation ( w, annotation)
109+ }
110+ DisplaySourceLine :: Empty =>Ok ( ( ) ) ,
111+ }
106112}
107113
108- fn fmt_raw_line ( & self , w : & mut impl std:: io:: Write , line : & DisplayRawLine , lineno_max : usize ) -> std:: io:: Result < ( ) > {
114+ fn fmt_raw_line (
115+ & self ,
116+ w : & mut impl std:: io:: Write ,
117+ line : & DisplayRawLine ,
118+ lineno_max : usize ,
119+ ) -> std:: io:: Result < ( ) > {
109120match line{
110121DisplayRawLine :: Origin { path, pos} =>{
111122S :: fmt ( w, format_args ! ( "{:>1$}" , "" , lineno_max) ) ?;
@@ -114,9 +125,9 @@ impl<S: StyleTrait> Renderer<S> {
114125S :: fmt ( w, format_args ! ( ":{}" , line) ) ?;
115126}
116127write ! ( w, "\n " )
117- } ,
128+ }
118129DisplayRawLine :: Annotation { annotation, ..} =>{
119- S :: fmt ( w, format_args ! ( "{}" , annotation. annotation_type ) ) ?;
130+ self . fmt_annotation ( w, annotation) ?;
120131if let Some ( id) = annotation. id {
121132write ! ( w, "[{}]" , id) ?;
122133}
@@ -125,9 +136,30 @@ impl<S: StyleTrait> Renderer<S> {
125136}
126137}
127138
128-
129- fn fmt_annotation ( & self , w : & mut impl std:: io:: Write , annotation : & Annotation ) -> std:: io:: Result < ( ) > {
130- write ! ( w, "{}" , annotation. label)
139+ fn fmt_annotation (
140+ & self ,
141+ w : & mut impl std:: io:: Write ,
142+ annotation : & Annotation ,
143+ ) -> std:: io:: Result < ( ) > {
144+ match annotation. annotation_type {
145+ AnnotationType :: None =>Ok ( ( ) ) ,
146+ AnnotationType :: Error =>write ! ( w, "error" ) ,
147+ AnnotationType :: Warning =>write ! ( w, "warning" ) ,
148+ AnnotationType :: Info =>write ! ( w, "info" ) ,
149+ AnnotationType :: Note =>write ! ( w, "note" ) ,
150+ AnnotationType :: Help =>write ! ( w, "help" ) ,
151+ }
152+ }
153+
154+ fn fmt_display_mark (
155+ & self ,
156+ w : & mut impl std:: io:: Write ,
157+ display_mark : & DisplayMark ,
158+ ) -> std:: io:: Result < ( ) > {
159+ match display_mark. mark_type {
160+ DisplayMarkType :: AnnotationStart =>write ! ( w, "/" ) ,
161+ DisplayMarkType :: AnnotationThrough =>write ! ( w, "|" ) ,
162+ }
131163}
132164}
133165