Struct ModuleText

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

A view into the ScriptModule source code text and lexis.

Created by the ModuleRead::text function.

To fetch the raw text of the source code or a substring, use the ModuleText::substring function.

To print a snippet of the source code text with syntax highlighting and annotation messages to the terminal, use the ModuleText::snippet function. The Display implementation of this object also prints a highlighted snippet with default settings.

Implementations§

Source§

impl<'a> ModuleText<'a>

Source

pub fn snippet(&self) -> ScriptSnippet<'_>

Returns an annotated snippet that prints the module’s source code text with syntax highlighting and annotations for specific code fragments with string messages.

let module = ScriptModule::new(
    Package::meta(),
    r#"
    let foo = 10;
    let bar = foo + 20;
"#,
);

module.rename("my_module.adastra");

let handle = TriggerHandle::new();
let module_read = module.read(&handle, 1).unwrap();
let module_text = module_read.text();

let mut snippet = module_text.snippet();

snippet.annotate(
    Position::new(2, 9)..Position::new(2, 12),
    AnnotationPriority::Primary,
    "Annotation text.",
);

println!("{snippet}");

Outputs:

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

pub fn is_well_formed(&self) -> bool

Returns true if the script module does not have syntax errors.

Source

pub fn format(&self, config: ScriptFormatConfig) -> Option<String>

If the script module does not have syntax errors (i.e., the module is well-formed), returns the reformatted source code text according to the formatting rules.

See format_script_text for details.

Trait Implementations§

Source§

impl<'a> Debug for ModuleText<'a>

Source§

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

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

impl<'a> Display for ModuleText<'a>

Source§

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

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

impl<'a> Identifiable for ModuleText<'a>

Source§

fn id(&self) -> Id

Returns the globally unique identifier of the compilation unit to which this object belongs.
Source§

impl<'a> Lexis for ModuleText<'a>

Source§

type Lexis = Document<ScriptNode>

The target SourceCode delegation type.
Source§

fn lexis(&self) -> &Self::Lexis

This function fully exposes underlying SourceCode interface of this object.
Source§

impl<'a> ModuleTextResolver for ModuleText<'a>

Source§

fn resolve(&self, id: Id) -> Option<&ModuleText<'_>>

If the underlying compiler contains a script module with id, returns ModuleText of this module. Otherwise, returns None.

Auto Trait Implementations§

§

impl<'a> Freeze for ModuleText<'a>

§

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

§

impl<'a> !Send for ModuleText<'a>

§

impl<'a> Sync for ModuleText<'a>

§

impl<'a> Unpin for ModuleText<'a>

§

impl<'a> !UnwindSafe for ModuleText<'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<F> SourceCode for F
where F: Lexis,

Source§

type Token = <<F as Lexis>::Lexis as SourceCode>::Token

Specifies the type of the source code token and the lexical scanner of a programming language through the Token::scan function.
Source§

type Cursor<'code> = <<F as Lexis>::Lexis as SourceCode>::Cursor<'code> where F: 'code

Specifies the type of the token cursor that iterates through the streams of tokens of this source code type.
Source§

type CharIterator<'code> = <<F as Lexis>::Lexis as SourceCode>::CharIterator<'code> where F: 'code

Specifies the type of the iterator that iterates through the unicode characters of the source code text substrings.
Source§

fn chars(&self, span: impl ToSpan) -> <F as SourceCode>::CharIterator<'_>

Returns an iterator that iterates over the Unicode chars of the source code text substring in the specified span. Read more
Source§

fn has_chunk(&self, entry: &Entry) -> bool

Checks if the token referred to by the versioned index exists in this source code.
Source§

fn get_token(&self, entry: &Entry) -> Option<<F as SourceCode>::Token>

Returns a copy of the token referred to by the versioned index. Read more
Source§

fn get_site(&self, entry: &Entry) -> Option<usize>

Returns a start site of the token referred to by the versioned index. Read more
Source§

fn get_string(&self, entry: &Entry) -> Option<&str>

Returns a reference to the source code text substring covered by the token referred to by the versioned index. Read more
Source§

fn get_length(&self, entry: &Entry) -> Option<usize>

Returns the length of the source code text substring covered by the token referred to by the versioned index. Read more
Source§

fn cursor(&self, span: impl ToSpan) -> <F as SourceCode>::Cursor<'_>

Returns a cursor of the source code tokens stream in the specified span. Read more
Source§

fn length(&self) -> usize

Returns the total number of Unicode characters in this source code text.
Source§

fn tokens(&self) -> usize

Returns the total number of tokens in this source code.
Source§

fn lines(&self) -> &LineIndex

Provides access to the line index of this source code. Read more
Source§

fn chunks(&self, span: impl ToSpan) -> ChunkIter<'_, Self::Cursor<'_>>
where Self: Sized,

Returns an iterator of the source code tokens metadata in the specified span. Read more
Source§

fn substring(&self, span: impl ToSpan) -> Cow<'_, str>
where Self: Sized,

Returns a borrowed or an owned substring of the source code text in the specified span. Read more
Source§

fn end_site_ref(&self) -> SiteRef

Returns a SiteRef that points to the end of this source code.
Source§

fn is_empty(&self) -> bool

Returns true if the source code text is empty.
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.