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::{Strs, Tags},
};
fn setup() {
let ns = Ns::new();
let tag = form::id_of!("non_ascii_char").to_tag(50);
let hl_non_ascii = move |tags: &mut Tags, strs: &Strs, range: Range<Point>| {
for (b, char) in strs[range.clone()].char_indices() {
let b = b + range.start.byte();
if !char.is_ascii() {
tags.insert(ns, b..b + char.len_utf8(), tag);
}
}
};
hook::add::<BufferOpened>(move |pa, buffer| {
let _ = buffer.read(pa).moment_for(ns);
let mut parts = buffer.text_parts(pa);
let range = Point::default()..parts.strs.end_point();
hl_non_ascii(&mut parts.tags, parts.strs, range);
});
hook::add::<BufferUpdated>(move |pa, buffer| {
let moment = buffer.read(pa).moment_for(ns);
let mut parts = buffer.text_parts(pa);
for change in moment.iter() {
parts.tags.remove(ns, change.added_range());
hl_non_ascii(&mut parts.tags, parts.strs, change.added_range())
}
});
}You can see here that I called Buffer::moment_for. This
function will give, for a given namespace, a list of all
Changes that have taken place since the last call of that
function with the same namespace. This is very useful for tracking
alterations to the Buffer and acting on them accordingly.
In this case, I’m just taking the added range of changes and adding tags to every non ascii character in them.
Note that the moment_for function will work anywhere (that you
have a Pass, this is just a common situation where you’d use
it.