BufferTracker

Struct BufferTracker 

Source
pub struct BufferTracker { /* private fields */ }
Expand description

A tracker for Changes that happen to a Buffer

Implementations§

Source§

impl BufferTracker

Source

pub fn update(&mut self)

Updates the inner Bytes and retrieves latest Moment

Source

pub fn request_parse(&self)

Request for the Buffer to call Parser::parse

This will prompt the Buffer to be updated and only update relevant Ranges, i.e., those that actually show up on area and that have been added by to the BufferTracker via add_range.

Source

pub fn add_range(&mut self, range: impl TextRange)

Adds a byte/Point Range to be updated on Parser::update

This Range will be merged with other Ranges on the list, and when a parse is requested, the intersections between the Ranges sent to this method and the Range of the Text shown on area will be updated.

For example, if you add the ranges 3..20 and 50..87, and the range shown on area is 17..61, then two calls to Parser::update will be made, one with the range 17..20 and one with the range 50..61. After that, the ranges 3..17 and 61..87 will remain on the list of Ranges to be updated

Source

pub fn add_ranges<R: TextRange>( &mut self, new_ranges: impl IntoIterator<Item = R>, )

Same as add_range, but add many Ranges at once

Source

pub fn track_changed_ranges(&mut self)

Automatically add every Change’s added range to update

This function turns on automatic Range addition for every Change that takes place. You can still add Ranges manually through add_range.

Source

pub fn track_changed_lines(&mut self)

Automatically add every changed line to the ranges to update

This function turns on automatic Range addition for every line that a Change’s added range belongs to. You can still add Ranges manually through add_range.

It’s probably the most common type of automatic range tracking that you’d want, since, for example, regex patterns are very convenient to separate line-wise, since most of them don’t tend to go over lines.

Additionally, this option will also make it so only whole lines are updated. For example, if a line is partially cutoff from the area, the range passed to Parser::update will include the parts of the line that are outside of the area. For the other tracking options, this won’t be the case.

Source

pub fn track_area(&mut self)

Always update the whole area

This function basically turns off Range tracking and assumes that you want to update the whole printed area.

One very common usecase of this is to do with parsing the Selections. You may want to, for example, do something for every selection that is visible on screen, maybe something like this:

use duat::prelude::*;

// When construction the SelectionLen, I turned on area tracking
struct SelectionLen {
    _tracker: BufferTracker,
    tagger: Tagger,
}

impl Parser for SelectionLen {
    fn update(&mut self, pa: &mut Pass, buffer: &Handle, on: Vec<Range<Point>>) {
        let mut parts = buffer.text_mut(pa).parts();
        // This is efficient even in very large `Text`s.
        parts.tags.remove(self.tagger, ..);

        for range in on {
            for (_, selection, is_main) in parts.selections.iter_within(range) {
                let range = selection.byte_range(&parts.bytes);
                let (start, end) = (range.start, range.end);
                let len = end - start;

                if len > 2 {
                    let nums = len.ilog10() as usize + 1;
                    let ghost = Ghost(if is_main {
                        txt!("[sel_len.main]{len}")
                    } else {
                        txt!("[sel_len]{len}")
                    });

                    parts.tags.insert(self.tagger, start, ghost);
                    parts.tags.insert(self.tagger, start..start + nums, Conceal);
                }
            }
        }
    }
}

This function will make it so, for every visible Selection, its length in bytes will be displayed within that selection with the "sel_len" Form.

§Note

This function is actually incorrect. It doesn’t take into account the possibility of intercepting newlines or the number being outside the printed area. The corrected version would be much too long for a simple example.

Source

pub fn turn_off_tracking(&mut self)

Disable the automatic Change tracking functionality

This makes it so no Ranges to update are added automatically whenever a Change takes place. Instead, they all must be added through add_range.

This is the default method of tracking Changes.

Source

pub fn bytes(&self) -> &Bytes

The Bytes, as they are since the last call to update

Do note that, because this tracks calls to update, this version of the Bytes may not reflect what the Bytes look like in the Buffer right now, as more Changes could have taken place since the last call.

Source

pub fn moment(&self) -> &Moment

The last Moment that was processed

This is not the last Moment sent to the Buffer, nor does it necessarily correspond to a Moment in the Text’s history. It’s just a collection of Changes that took place in between calls to Self::update.

Source

pub fn indent(&self, p: Point) -> usize

The indentation in the line at a Point

This value only takes ascii spaces and tabs into account, which may differ from the value from Text::indent, since that one calculates the indent through the Area, while this one only makes use of the Buffer’s PrintOpts.

Source

pub fn opts(&self) -> PrintOpts

The PrintOpts of the Buffer

Unlike the other parts of this struct, the PrintOpts will always be up to date with what it currently is in the Buffer

Trait Implementations§

Source§

impl Debug for BufferTracker

Source§

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

Formats the value using the given formatter. 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<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.