A simple CPU rendered GUI IDE experience.
Diffstat (limited to 'src/text.rs')
| -rw-r--r-- | src/text.rs | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/src/text.rs b/src/text.rs index 293244f..2a06da0 100644 --- a/src/text.rs +++ b/src/text.rs @@ -8,6 +8,7 @@ use std::pin::pin; use std::sync::{Arc, LazyLock}; use std::vec::Vec; +use anyhow::anyhow; use atools::prelude::*; use diff_match_patch_rs::{DiffMatchPatch, Patches}; use dsb::Cell; @@ -308,16 +309,30 @@ impl TextArea { self.set_ho(); } - pub fn apply(&mut self, x: TextEdit) -> Result<(), ropey::Error> { + pub fn apply(&mut self, x: &TextEdit) -> Result<(), ropey::Error> { let begin = self.l_position(x.range.start)?; let end = self.l_position(x.range.end)?; self.rope.try_remove(begin..end)?; self.rope.try_insert(begin, &x.new_text)?; Ok(()) } + pub fn apply_snippet(&mut self, x: &TextEdit) -> anyhow::Result<()> { + let begin = self.l_position(x.range.start)?; + let end = self.l_position(x.range.end)?; + self.rope.try_remove(begin..end)?; + let (mut sni, tex) = + crate::sni::Snippet::parse(&x.new_text, begin) + .ok_or(anyhow!("failed to parse snippet"))?; + self.rope.try_insert(begin, &tex)?; + self.cursor = sni.next(); + Ok(()) + } pub fn cursor(&self) -> (usize, usize) { self.xy(self.cursor) } + pub fn visible(&self, x: usize) -> bool { + (self.vo..self.vo + self.r).contains(&self.rope.char_to_line(x)) + } pub fn x(&self, c: usize) -> usize { self.xy(c).0 } |