summaryrefslogtreecommitdiffstats
path: root/graphics
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 /graphics
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 'graphics')
-rw-r--r--graphics/src/text/editor.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/graphics/src/text/editor.rs b/graphics/src/text/editor.rs
index 07a2d72a..1e375a25 100644
--- a/graphics/src/text/editor.rs
+++ b/graphics/src/text/editor.rs
@@ -69,6 +69,47 @@ impl editor::Editor for Editor {
})))
}
+ fn line(&self, index: usize) -> Option<&str> {
+ self.buffer()
+ .lines
+ .get(index)
+ .map(cosmic_text::BufferLine::text)
+ }
+
+ fn line_count(&self) -> usize {
+ self.buffer().lines.len()
+ }
+
+ fn selection(&self) -> Option<String> {
+ let internal = self.internal();
+
+ let cursor = internal.editor.cursor();
+ let selection = internal.editor.select_opt()?;
+
+ let (start, end) = if cursor < selection {
+ (cursor, selection)
+ } else {
+ (selection, cursor)
+ };
+
+ Some(
+ internal.editor.buffer().lines[start.line..=end.line]
+ .iter()
+ .enumerate()
+ .map(|(i, line)| {
+ if i == 0 {
+ &line.text()[start.index..]
+ } else if i == end.line - start.line {
+ &line.text()[..end.index]
+ } else {
+ line.text()
+ }
+ })
+ .collect::<Vec<_>>()
+ .join("\n"),
+ )
+ }
+
fn cursor(&self) -> editor::Cursor {
let internal = self.internal();