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