Skip to main content

TextEdit

Enum TextEdit 

Source
pub enum TextEdit {
    Insertion {
        at: Offset,
        text: String,
    },
    Deletion {
        start: Offset,
        end: Offset,
    },
}
Expand description

One text change: the text a buffer gains at a point, or the stretch it loses.

An edit acts on both halves of the indexing relation — on source text through Self::apply and on a Span through Span::shifted — and those two actions are designed to agree.

An insertion carries the text it inserts rather than a pair of boundaries, because the boundaries are recoverable (at and at + text.len()) while the text is not, and without the text an edit cannot act on a buffer at all.

Variants§

§

Insertion

Inserts text at one byte boundary.

Fields

§at: Offset

The insertion boundary.

§text: String

The inserted text.

§

Deletion

Deletes one half-open byte range.

Fields

§start: Offset

The first deleted byte.

§end: Offset

The boundary after the last deleted byte.

Implementations§

Source§

impl TextEdit

Source

pub fn insertion(at: Offset, text: impl Into<String>) -> Self

Creates an insertion of text landing at at.

Source

pub fn deletion(start: Offset, end: Offset) -> Self

Creates a deletion of the text occupying start..end.

§Panics

Panics when start is greater than end.

Examples found in repository?
examples/live_parser.rs (line 130)
97fn main() -> io::Result<()> {
98    let interactive = io::stdin().is_terminal() && io::stdout().is_terminal();
99    let _terminal = TerminalMode::raw();
100    let mut bytes = Vec::new();
101    let mut parser = NativeRealtimeParser::new("");
102    let mut stdout = io::stdout().lock();
103
104    if interactive {
105        stdout.write_all(render(&parser, true).as_bytes())?;
106        stdout.flush()?;
107    }
108
109    for byte in io::stdin().lock().bytes() {
110        let byte = byte?;
111        match byte {
112            3 | 4 => break,
113            8 | 127 => {
114                if let Ok(source) = std::str::from_utf8(&bytes)
115                    && let Some((index, _)) = source.char_indices().next_back()
116                {
117                    bytes.truncate(index);
118                }
119            }
120            b'\r' => bytes.push(b'\n'),
121            byte => bytes.push(byte),
122        }
123        let Ok(source) = std::str::from_utf8(&bytes) else {
124            continue;
125        };
126        if let Some(appended) = source.strip_prefix(parser.source()) {
127            parser.append(appended);
128        } else if parser.source().starts_with(source) {
129            parser
130                .apply_edit(&TextEdit::deletion(source.len(), parser.source().len()))
131                .expect("backspace always removes a valid source suffix");
132        } else {
133            parser = NativeRealtimeParser::new(source);
134        }
135        if interactive {
136            stdout.write_all(render(&parser, true).as_bytes())?;
137            stdout.flush()?;
138        }
139    }
140
141    if !interactive {
142        let source = String::from_utf8(bytes)
143            .map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;
144        debug_assert_eq!(source, parser.source());
145        stdout.write_all(render(&parser, false).as_bytes())?;
146    }
147    Ok(())
148}
Source

pub fn apply(&self, source: &str) -> Option<String>

Returns source with this edit applied, or None when the edit falls outside the buffer or across a character boundary.

Trait Implementations§

Source§

impl Clone for TextEdit

Source§

fn clone(&self) -> TextEdit

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TextEdit

Source§

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

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

impl Eq for TextEdit

Source§

impl PartialEq for TextEdit

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for TextEdit

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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, <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.