Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-view/src/editor.rs')
-rw-r--r--helix-view/src/editor.rs39
1 files changed, 28 insertions, 11 deletions
diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs
index 29059095..fb8438be 100644
--- a/helix-view/src/editor.rs
+++ b/helix-view/src/editor.rs
@@ -1070,10 +1070,10 @@ pub struct Editor {
/// This cache is only a performance optimization to
/// avoid calculating the cursor position multiple
/// times during rendering and should not be set by other functions.
- pub cursor_cache: Cell<Option<Option<Position>>>,
pub handlers: Handlers,
pub mouse_down_range: Option<Range>,
+ pub cursor_cache: CursorCache,
}
pub type Motion = Box<dyn Fn(&mut Editor)>;
@@ -1188,9 +1188,9 @@ impl Editor {
exit_code: 0,
config_events: unbounded_channel(),
needs_redraw: false,
- cursor_cache: Cell::new(None),
handlers,
mouse_down_range: None,
+ cursor_cache: CursorCache::default(),
}
}
@@ -1985,15 +1985,7 @@ impl Editor {
pub fn cursor(&self) -> (Option<Position>, CursorKind) {
let config = self.config();
let (view, doc) = current_ref!(self);
- let cursor = doc
- .selection(view.id)
- .primary()
- .cursor(doc.text().slice(..));
- let pos = self
- .cursor_cache
- .get()
- .unwrap_or_else(|| view.screen_coords_at_pos(doc, doc.text().slice(..), cursor));
- if let Some(mut pos) = pos {
+ if let Some(mut pos) = self.cursor_cache.get(view, doc) {
let inner = view.inner_area(doc);
pos.col += inner.x as usize;
pos.row += inner.y as usize;
@@ -2188,3 +2180,28 @@ fn try_restore_indent(doc: &mut Document, view: &mut View) {
doc.apply(&transaction, view.id);
}
}
+
+#[derive(Default)]
+pub struct CursorCache(Cell<Option<Option<Position>>>);
+
+impl CursorCache {
+ pub fn get(&self, view: &View, doc: &Document) -> Option<Position> {
+ if let Some(pos) = self.0.get() {
+ return pos;
+ }
+
+ let text = doc.text().slice(..);
+ let cursor = doc.selection(view.id).primary().cursor(text);
+ let res = view.screen_coords_at_pos(doc, text, cursor);
+ self.set(res);
+ res
+ }
+
+ pub fn set(&self, cursor_pos: Option<Position>) {
+ self.0.set(Some(cursor_pos))
+ }
+
+ pub fn reset(&self) {
+ self.0.set(None)
+ }
+}