Struct ScriptSnippet

Source
pub struct ScriptSnippet<'a> { /* private fields */ }
Expand description

A drawing object that renders the source code text of a ScriptModule with syntax highlighting and annotated source code ranges.

The intended use of this object is printing script source code to the terminal.

   ╭──╢ ‹doctest›.‹my_module.adastra› ╟────────────────────────────────────────╮
 1 │                                                                           │
 2 │     let foo = 10;                                                         │
   │         ╰╴ Annotation text.                                               │
 3 │     let bar = foo + 20;                                                   │
 4 │                                                                           │
   ╰───────────────────────────────────────────────────────────────────────────╯

There are several crate API functions that create this object, such as ModuleText::snippet and ModuleDiagnostics::highlight.

The Display implementation of this object performs the actual snippet rendering. For example, you can print the snippet to the terminal using the println macro: println!("{my_snippet}")

Implementations§

Source§

impl<'a> ScriptSnippet<'a>

Source

pub fn set_config(&mut self, config: ScriptSnippetConfig) -> &mut Self

Sets the configuration for snippet drawing features.

See ScriptSnippetConfig for details.

Source

pub fn set_caption(&mut self, caption: impl AsRef<str>) -> &mut Self

Sets the caption of the printed content.

The caption string will be printed in the header of the snippet. By default, the caption is an empty string, and in this case, the renderer does not include a custom caption in the header.

The caption parameter must be a single-line string. Any additional caption lines (separated by the \n character) will be ignored.

Source

pub fn set_summary(&mut self, summary: impl AsRef<str>) -> &mut Self

Sets the footer summary text of the printed content.

The summary string will be printed below the source code. By default, the summary is an empty string, and in this case, the renderer does not print any footer text.

Unlike the caption and annotation text, the summary text can have multiple lines.

Source

pub fn annotate( &mut self, span: impl ToSpan, priority: AnnotationPriority, message: impl AsRef<str>, ) -> &mut Self

Adds an annotation to the source code.

The span argument specifies the source code range intended for annotation. You can use a 10..20 absolute Unicode character range, the line-column range Position::new(10, 3)..Position::new(12, 4), or the [ScriptOrigin] instance. The span argument must represent a valid value (e.g., 20..10 is not a valid range because the upper bound is less than the lower bound). Otherwise, the annotation will be silently ignored.

The priority argument specifies the annotation priority. The snippet interface supports the following priority types:

The message argument specifies the text that should label the spanned range. The message should be a single-line string. Any additional message lines (separated by the \n character) will be ignored.

You can leave the message as an empty string. In this case, the renderer will not label the spanned text.

Note that if the ScriptSnippet does not have any annotations, the object will render the entire source code. Otherwise, the renderer will output only the annotated lines plus a few lines of surrounding context.

Trait Implementations§

Source§

impl<'a> Display for ScriptSnippet<'a>

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S: AsRef<str>> From<S> for ScriptSnippet<'static>

Source§

fn from(string: S) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'a> Freeze for ScriptSnippet<'a>

§

impl<'a> !RefUnwindSafe for ScriptSnippet<'a>

§

impl<'a> Send for ScriptSnippet<'a>

§

impl<'a> Sync for ScriptSnippet<'a>

§

impl<'a> Unpin for ScriptSnippet<'a>

§

impl<'a> !UnwindSafe for ScriptSnippet<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToCompactString for T
where T: Display,

Source§

fn to_compact_string(&self) -> CompactString

Converts the given value to a CompactString. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.