Skip to main content

ScriptHost

Struct ScriptHost 

Source
pub struct ScriptHost {
    pub errors: Vec<String>,
    /* private fields */
}
Expand description

Loads TextWrite editor scripts and runs their commands.

Mirrors fast_text_plugin::PluginHost, but backed by the TextWrite interpreter instead of Rhai.

Fields§

§errors: Vec<String>

Errors encountered while loading or running scripts.

Implementations§

Source§

impl ScriptHost

Source

pub fn new() -> Self

Creates a host with the editor API installed but no scripts loaded.

Source

pub fn set_module_dir(&mut self, dir: impl Into<PathBuf>)

Sets the directory that relative imports in loaded scripts resolve against (normally the plugin directory).

Source

pub fn add_script(&mut self, name: &str, source: &str)

Adds a script by name, re-running all loaded scripts.

Source

pub fn load_dir(&mut self, dir: &Path)

Loads every .tw file in a directory, sorted by name.

Source

pub fn commands(&self) -> Vec<CommandInfo>

Every registered command, sorted by title.

Source

pub fn command_titles(&self) -> Vec<String>

Registered command titles, sorted.

Source

pub fn keybindings(&self) -> Vec<(KeySpec, String)>

All registered keybindings.

Source

pub fn themes(&self) -> Vec<(String, Vec<(String, u32)>)>

Every theme defined by a loaded script, as (name, colors).

Source

pub fn theme_names(&self) -> Vec<String>

The names of every theme defined by a loaded script.

Source

pub fn lookup_key(&self, key: &KeySpec) -> Option<String>

Finds the command title bound to a key, if any.

Source

pub fn has_command(&self, title: &str) -> bool

True when a command with this title is registered.

Source

pub fn run_command( &mut self, title: &str, ctx: &EditorContext, ) -> Vec<EditorAction>

Runs a command by title, returning the actions it requested.

Source

pub fn eval(&mut self, source: &str, ctx: &EditorContext) -> Result<Value>

Evaluates a snippet of TextWrite against the current editor snapshot.

Actions requested by the snippet are available afterwards via take_actions.

Source

pub fn take_actions(&self) -> Vec<EditorAction>

Takes the actions requested by the most recent eval.

Source

pub fn has_hook(&self, name: &str) -> bool

True when a loaded script defines the lifecycle hook name.

Source

pub fn run_hook(&mut self, name: &str, ctx: &EditorContext) -> Vec<EditorAction>

Runs a lifecycle hook (on_buffer_open, on_buffer_save, …) against an editor snapshot, returning the actions it queued.

A missing hook is not an error; a failing hook is recorded in ScriptHost::errors and produces a message action.

Source

pub fn compile_file(&mut self, path: &str) -> Result<()>

Compiles a .tw file: parses it and resolves (recursively) every module it imports, without running anything. The .tw extension is optional.

Source

pub fn run_file( &mut self, path: &str, ctx: &EditorContext, ) -> Result<Vec<EditorAction>>

Compiles and runs a .tw file against a snapshot of the editor state, returning the actions it queued. The .tw extension is optional.

Trait Implementations§

Source§

impl Default for ScriptHost

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.