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§
Implementations§
Source§impl TextEdit
impl TextEdit
Sourcepub fn insertion(at: Offset, text: impl Into<String>) -> Self
pub fn insertion(at: Offset, text: impl Into<String>) -> Self
Creates an insertion of text landing at at.
Sourcepub fn deletion(start: Offset, end: Offset) -> Self
pub fn deletion(start: Offset, end: Offset) -> Self
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}Trait Implementations§
impl Eq for TextEdit
impl StructuralPartialEq for TextEdit
Auto Trait Implementations§
impl Freeze for TextEdit
impl RefUnwindSafe for TextEdit
impl Send for TextEdit
impl Sync for TextEdit
impl Unpin for TextEdit
impl UnsafeUnpin for TextEdit
impl UnwindSafe for TextEdit
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more