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+
135mod margin;
236pub ( crate ) mod stylesheet;
337
@@ -8,6 +42,7 @@ pub use margin::Margin;
842use std:: fmt:: Display ;
943use stylesheet:: Stylesheet ;
1044
45+ /// A renderer for [`Snippet`]s
1146#[ derive( Clone ) ]
1247pub struct Renderer {
1348 anonymized_line_numbers : bool ,
@@ -42,56 +77,96 @@ impl Renderer {
4277 }
4378 }
4479
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+ /// ```
4594 pub const fn anonymized_line_numbers ( mut self , anonymized_line_numbers : bool ) -> Self {
4695 self . anonymized_line_numbers = anonymized_line_numbers;
4796 self
4897 }
4998
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+ /// ```
50116 pub const fn margin ( mut self , margin : Option < Margin > ) -> Self {
51117 self . margin = margin;
52118 self
53119 }
54120
121+ /// Set the output style for `error`
55122 pub const fn error ( mut self , style : Style ) -> Self {
56123 self . stylesheet . error = style;
57124 self
58125 }
59126
127+ /// Set the output style for `warning`
60128 pub const fn warning ( mut self , style : Style ) -> Self {
61129 self . stylesheet . warning = style;
62130 self
63131 }
64132
133+ /// Set the output style for `info`
65134 pub const fn info ( mut self , style : Style ) -> Self {
66135 self . stylesheet . info = style;
67136 self
68137 }
69138
139+ /// Set the output style for `note`
70140 pub const fn note ( mut self , style : Style ) -> Self {
71141 self . stylesheet . note = style;
72142 self
73143 }
74144
145+ /// Set the output style for `help`
75146 pub const fn help ( mut self , style : Style ) -> Self {
76147 self . stylesheet . help = style;
77148 self
78149 }
79150
151+ /// Set the output style for line numbers
80152 pub const fn line_no ( mut self , style : Style ) -> Self {
81153 self . stylesheet . line_no = style;
82154 self
83155 }
84156
157+ /// Set the output style for emphasis
85158 pub const fn emphasis ( mut self , style : Style ) -> Self {
86159 self . stylesheet . emphasis = style;
87160 self
88161 }
89162
163+ /// Set the output style for none
90164 pub const fn none ( mut self , style : Style ) -> Self {
91165 self . stylesheet . none = style;
92166 self
93167 }
94168
169+ /// Render a snippet into a `Display`able object
95170 pub fn render < ' a > ( & ' a self , snippet : Snippet < ' a > ) -> impl Display + ' a {
96171 DisplayList :: new (
97172 snippet,
0 commit comments