pub struct Annotations<'a, M> { /* private fields */ }Expand description
A collection of annotations attached to a Snippet.
§Spans
Each annotation is a Range<usize> whose indices are
positions in the snippet’s source unit sequence (as defined by the concrete
Snippet implementation).
Spans are half-open: start is inclusive, end is exclusive. Zero-length
spans (start == end) are allowed and render as a single caret pointing at
that position.
§Labels
A label is stored as a list of (String, M) fragments. This allows
attaching different metadata to different parts of the label (for example,
to highlight a keyword inside the label). If you do not need per-fragment
metadata, you can use a single-element vector.
Empty label fragments are allowed; the renderer will simply output nothing for them (just the carets or connector lines of the annotation).
§Example
See the crate-level documentation.
Implementations§
Source§impl<'a, M> Annotations<'a, M>
impl<'a, M> Annotations<'a, M>
Sourcepub fn new(snippet: &'a Snippet, main_style: &'a MainStyle<M>) -> Self
pub fn new(snippet: &'a Snippet, main_style: &'a MainStyle<M>) -> Self
Creates a new annotation collection for snippet.
Sourcepub fn add_annotation(
&mut self,
span: Range<usize>,
style: &'a AnnotStyle<M>,
label: Vec<(String, M)>,
)
pub fn add_annotation( &mut self, span: Range<usize>, style: &'a AnnotStyle<M>, label: Vec<(String, M)>, )
Adds an annotation span with the given style and label.
span is a half-open range (start inclusive, end exclusive)
expressed in the snippet’s source units (see Annotations docs).
label is a list of text fragments with associated metadata; these
fragments are concatenated when rendered.
pub fn max_line_no_width(&self) -> usize
Sourcepub fn render<O: Output<M>>(
&self,
max_line_no_width: usize,
max_fill_after_first: usize,
max_fill_before_last: usize,
out: O,
) -> Result<(), O::Error>
pub fn render<O: Output<M>>( &self, max_line_no_width: usize, max_fill_after_first: usize, max_fill_before_last: usize, out: O, ) -> Result<(), O::Error>
Renders the snippet with the annotations.
If no annotations have been added, this outputs nothing.
max_line_no_width should be at least
self.max_line_no_width(), but it can be
greater to align vertically the margin of multiple snippets.
max_fill_after_first and max_fill_before_last control how many
unannotated lines are rendered when there is a gap between two
annotated lines:
- If the gap size is greater than
max_fill_after_first + max_fill_before_last, the renderer outputs only the firstmax_fill_after_firstlines after the previous annotated line, then a dotted separator line, then the lastmax_fill_before_lastlines before the next annotated line. - Otherwise, all lines in the gap are rendered.