1
+ //! The renderer for [`Snippet`]s
2
+ //!
3
+ //! # Example
4
+ //! ```
5
+ //! use annotate_snippets::renderer::Renderer;
6
+ //! use annotate_snippets::snippet::{Annotation, AnnotationType, Slice, Snippet};
7
+ //! let snippet = Snippet {
8
+ //! title: Some(Annotation {
9
+ //! label: Some("mismatched types"),
10
+ //! id: None,
11
+ //! annotation_type: AnnotationType::Error,
12
+ //! }),
13
+ //! footer: vec![],
14
+ //! slices: vec![
15
+ //! Slice {
16
+ //! source: "Foo",
17
+ //! line_start: 51,
18
+ //! origin: Some("src/format.rs"),
19
+ //! fold: false,
20
+ //! annotations: vec![],
21
+ //! },
22
+ //! Slice {
23
+ //! source: "Faa",
24
+ //! line_start: 129,
25
+ //! origin: Some("src/display.rs"),
26
+ //! fold: false,
27
+ //! annotations: vec![],
28
+ //! },
29
+ //! ],
30
+ //! };
31
+ //!
32
+ //! let renderer = Renderer::styled();
33
+ //! println!("{}", renderer.render(snippet));
34
+
1
35
pub mod margin;
2
36
3
37
use crate :: display_list:: DisplayList ;
@@ -6,6 +40,7 @@ use anstyle::{AnsiColor, Effects, Style};
6
40
use margin:: Margin ;
7
41
use std:: fmt:: Display ;
8
42
43
+ /// A renderer for [`Snippet`]s
9
44
#[ derive( Clone ) ]
10
45
pub struct Renderer {
11
46
anonymized_line_numbers : bool ,
@@ -14,6 +49,7 @@ pub struct Renderer {
14
49
}
15
50
16
51
impl Renderer {
52
+ /// Render a snippet into a `Display`able object
17
53
pub fn render < ' a > ( & ' a self , snippet : Snippet < ' a > ) -> impl Display + ' a {
18
54
DisplayList :: new (
19
55
snippet,
@@ -50,51 +86,90 @@ impl Renderer {
50
86
}
51
87
}
52
88
89
+ /// Anonymize line numbers
90
+ ///
91
+ /// This enables (or disables) line number anonymization. When enabled, line numbers are replaced
92
+ /// with `LL`.
93
+ ///
94
+ /// # Example
95
+ ///
96
+ /// ```text
97
+ /// --> $DIR/whitespace-trimming.rs:4:193
98
+ /// |
99
+ /// LL | ... let _: () = 42;
100
+ /// | ^^ expected (), found integer
101
+ /// |
102
+ /// ```
53
103
pub const fn anonymized_line_numbers ( mut self , anonymized_line_numbers : bool ) -> Self {
54
104
self . anonymized_line_numbers = anonymized_line_numbers;
55
105
self
56
106
}
57
107
108
+ /// Set the margin for the output
109
+ ///
110
+ /// This controls the various margins of the output.
111
+ ///
112
+ /// # Example
113
+ ///
114
+ /// ```text
115
+ /// error: expected type, found `22`
116
+ /// --> examples/footer.rs:29:25
117
+ /// |
118
+ /// 26 | ... annotations: vec![SourceAnnotation {
119
+ /// | ---------------- info: while parsing this struct
120
+ /// ...
121
+ /// 29 | ... range: <22, 25>,
122
+ /// | ^^
123
+ /// |
124
+ /// ```
58
125
pub const fn margin ( mut self , margin : Option < Margin > ) -> Self {
59
126
self . margin = margin;
60
127
self
61
128
}
62
129
130
+ /// Set the output style for `error`
63
131
pub const fn error ( mut self , style : Style ) -> Self {
64
132
self . stylesheet . error = style;
65
133
self
66
134
}
67
135
136
+ /// Set the output style for `warning`
68
137
pub const fn warning ( mut self , style : Style ) -> Self {
69
138
self . stylesheet . warning = style;
70
139
self
71
140
}
72
141
142
+ /// Set the output style for `info`
73
143
pub const fn info ( mut self , style : Style ) -> Self {
74
144
self . stylesheet . info = style;
75
145
self
76
146
}
77
147
148
+ /// Set the output style for `note`
78
149
pub const fn note ( mut self , style : Style ) -> Self {
79
150
self . stylesheet . note = style;
80
151
self
81
152
}
82
153
154
+ /// Set the output style for `help`
83
155
pub const fn help ( mut self , style : Style ) -> Self {
84
156
self . stylesheet . help = style;
85
157
self
86
158
}
87
159
160
+ /// Set the output style for line numbers
88
161
pub const fn line_no ( mut self , style : Style ) -> Self {
89
162
self . stylesheet . line_no = style;
90
163
self
91
164
}
92
165
166
+ /// Set the output style for emphasis
93
167
pub const fn emphasis ( mut self , style : Style ) -> Self {
94
168
self . stylesheet . emphasis = style;
95
169
self
96
170
}
97
171
172
+ /// Set the output style for none
98
173
pub const fn none ( mut self , style : Style ) -> Self {
99
174
self . stylesheet . none = style;
100
175
self
0 commit comments