BufferUpdated

Struct BufferUpdated 

Source
pub struct BufferUpdated(/* private fields */);
Expand description

Hookable: Triggers when a Buffer updates

This is triggered after a batch of writing calls to the Buffer, once per frame. This can happen after typing a key, calling a command, triggering hooks, or any other action with access to a Pass, which could be used to write to the Buffer.

Think of this is as a “last pass” on the Buffer, right before printing, where it can be adjusted given the modifications to it, like Changes and such.

As an example, here’s a hook that will highlight every non ascii character:

use duat::{
    prelude::*,
    text::{Bytes, Tags},
};

static TRACKER: BufferTracker = BufferTracker::new();

fn setup() {
    let tagger = Tagger::new();
    let tag = form::id_of!("non_ascii_char").to_tag(50);

    let highlight_non_ascii = move |tags: &mut Tags, bytes: &Bytes, range| {
        for (b, char) in bytes.strs(range).unwrap().char_indices() {
            if !char.is_ascii() {
                tags.insert(tagger, b..b + char.len_utf8(), tag);
            }
        }
    };

    hook::add::<BufferOpened>(move |pa, handle| {
        TRACKER.register_buffer(handle.write(pa));

        let mut parts = handle.text_parts(pa);
        let range = Point::default()..parts.bytes.len();
        highlight_non_ascii(&mut parts.tags, parts.bytes, range);
    });

    hook::add::<BufferUpdated>(move |pa, handle| {
        let mut parts = TRACKER.parts(handle.write(pa)).unwrap();

        for change in parts.changes {
            parts.tags.remove(tagger, change.added_range());
            highlight_non_ascii(&mut parts.tags, parts.bytes, change.added_range())
        }
    });
}

The BufferTracker will keep track of each registered Buffer, telling you about every new Change that took place since the last call to BufferTracker::parts. The BufferTracker::parts function works much like Text::parts, by separating the Bytes, Tags and Selections, letting you modify the tags, without permitting further edits to the Text.

This is a nice way to automatically keep track of the changes, and it will work even if the function isn’t called frequently.

§Arguments

Trait Implementations§

Source§

impl Hookable for BufferUpdated

Source§

type Input<'h> = &'h Handle

The arguments that are passed to each hook.
Source§

fn get_input<'h>(&'h mut self, _: &mut Pass) -> Self::Input<'h>

How to get the arguments from the Hookable Read more
Source§

impl PartialEq<Handle> for BufferUpdated

Source§

fn eq(&self, other: &Handle) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

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<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
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, 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.