summaryrefslogtreecommitdiffstats
path: root/widget/src/text_editor.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2023-09-16 15:27:25 +0200
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2023-09-16 15:27:25 +0200
commitc6d0443627c22dcf1576303e5a426aa3622f1b7d (patch)
tree13d0b2a4d3378065709a7d81f6c54ea8edb10e77 /widget/src/text_editor.rs
parentf7fc13d98c52a9260b1ab55394a0c3d2693318ed (diff)
downloadiced-c6d0443627c22dcf1576303e5a426aa3622f1b7d.tar.gz
iced-c6d0443627c22dcf1576303e5a426aa3622f1b7d.tar.bz2
iced-c6d0443627c22dcf1576303e5a426aa3622f1b7d.zip
Implement methods to query the contents of a `TextEditor`
Diffstat (limited to 'widget/src/text_editor.rs')
-rw-r--r--widget/src/text_editor.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs
index 114d35ef..ec7a6d1d 100644
--- a/widget/src/text_editor.rs
+++ b/widget/src/text_editor.rs
@@ -100,6 +100,54 @@ where
internal.editor.perform(action);
internal.is_dirty = true;
}
+
+ pub fn line_count(&self) -> usize {
+ self.0.borrow().editor.line_count()
+ }
+
+ pub fn line(
+ &self,
+ index: usize,
+ ) -> Option<impl std::ops::Deref<Target = str> + '_> {
+ std::cell::Ref::filter_map(self.0.borrow(), |internal| {
+ internal.editor.line(index)
+ })
+ .ok()
+ }
+
+ pub fn lines(
+ &self,
+ ) -> impl Iterator<Item = impl std::ops::Deref<Target = str> + '_> {
+ struct Lines<'a, Renderer: text::Renderer> {
+ internal: std::cell::Ref<'a, Internal<Renderer>>,
+ current: usize,
+ }
+
+ impl<'a, Renderer: text::Renderer> Iterator for Lines<'a, Renderer> {
+ type Item = std::cell::Ref<'a, str>;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ let line = std::cell::Ref::filter_map(
+ std::cell::Ref::clone(&self.internal),
+ |internal| internal.editor.line(self.current),
+ )
+ .ok()?;
+
+ self.current += 1;
+
+ Some(line)
+ }
+ }
+
+ Lines {
+ internal: self.0.borrow(),
+ current: 0,
+ }
+ }
+
+ pub fn selection(&self) -> Option<String> {
+ self.0.borrow().editor.selection()
+ }
}
impl<Renderer> Default for Content<Renderer>