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