From 19e029a58a95cdb90077d77e2857b880bf91f05a Mon Sep 17 00:00:00 2001 From: Hojjat Date: Tue, 31 Mar 2026 13:25:00 -0600 Subject: [PATCH] feat: add cursor_position and is_rtl methods to buffer cursor_position returns (x,y) of the cursor. This is needed for clients that do not want to use a fully fledged editor (for example TextInput in Iced). is_rtl is necessary to know the base direction of a paragraph. --- src/buffer.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/buffer.rs b/src/buffer.rs index 9d8b49a..da84a66 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -1234,6 +1234,21 @@ impl Buffer { new_cursor_opt } + /// Returns the visual (x, y) position of a cursor within the buffer. + /// y is the top of the line containing the cursor. + /// This is a convenience wrapper around [`LayoutRun::cursor_position`]. + pub fn cursor_position(&self, cursor: &Cursor) -> Option<(f32, f32)> { + self.layout_runs() + .filter(|run| run.line_i == cursor.line) + .find_map(|run| run.cursor_position(cursor).map(|x| (x, run.line_top))) + } + + /// Returns if the text direction for a given line is RTL + /// Returns `None` if the line doesn't exist or hasn't been shaped yet. + pub fn is_rtl(&self, line: usize) -> Option { + self.lines.get(line)?.shape_opt().map(|shape| shape.rtl) + } + /// Apply a [`Motion`] to a [`Cursor`] pub fn cursor_motion( &mut self,