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.
This commit is contained in:
Hojjat 2026-03-31 13:25:00 -06:00 committed by Jeremy Soller
parent 463387d6a1
commit 19e029a58a

View file

@ -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<bool> {
self.lines.get(line)?.shape_opt().map(|shape| shape.rtl)
}
/// Apply a [`Motion`] to a [`Cursor`]
pub fn cursor_motion(
&mut self,