From f5e9701d01888b8d07e052d3bf6bf7469315f036 Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Thu, 30 Oct 2025 11:01:58 +0000 Subject: [PATCH] Avoid unnecessary highlighting work --- src/main.rs | 3 ++- src/state.rs | 25 +++++++++++++++---------- src/ui/doc.rs | 6 +++++- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index b6b7981..11aca16 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,7 +42,6 @@ fn main() -> Result<(), Error> { // Wait for a while term.wait_at_least(Duration::from_millis(250)); - state.tick(); while let Some(ev) = term.get_event() { // Resize events are special and need handling by the terminal @@ -59,6 +58,8 @@ fn main() -> Result<(), Error> { Err(_) => {} } } + + state.tick(); } }) } diff --git a/src/state.rs b/src/state.rs index 4bfa31c..1c0ecf2 100644 --- a/src/state.rs +++ b/src/state.rs @@ -144,7 +144,6 @@ pub struct Buffer { pub diverged: bool, pub text: Text, pub lang: LangPack, - pub highlights: Highlights, pub cursors: HopSlotMap, pub path: Option, pub undo: Vec, @@ -152,6 +151,9 @@ pub struct Buffer { opened_at: Option, action_counter: usize, most_recent_rank: usize, + + pub highlights: Highlights, + highlights_stale: bool, } pub struct Change { @@ -185,6 +187,7 @@ impl Buffer { unsaved, diverged: false, highlights: lang.highlighter.highlight(&chars), + highlights_stale: false, lang, text: Text { chars }, cursors: HopSlotMap::default(), @@ -235,15 +238,11 @@ impl Buffer { ) } - fn update_highlights(&mut self) { - self.highlights = self.lang.highlighter.highlight(self.text.chars()); - } - pub fn reset(&mut self) { self.unsaved = true; self.text.chars.clear(); - self.update_highlights(); + self.highlights_stale = true; // Reset cursors self.cursors.values_mut().for_each(|cursor| { *cursor = Cursor::default(); @@ -482,7 +481,7 @@ impl Buffer { *c = *to; } } - self.update_highlights(); + self.highlights_stale = true; } fn undo_or_redo(&mut self, is_undo: bool) -> bool { @@ -534,7 +533,7 @@ impl Buffer { self.text.chars.insert(base + n, *c); n += 1; } - self.update_highlights(); + self.highlights_stale = true; Change { kind: ChangeKind::Insert(base, chars), action_id: self.action_counter, @@ -568,7 +567,7 @@ impl Buffer { // TODO: Bell if false? let removed = self.text.chars.drain(range.clone()).collect(); - self.update_highlights(); + self.highlights_stale = true; Change { kind: ChangeKind::Remove(range.start, removed), action_id: self.action_counter, @@ -860,7 +859,7 @@ impl Buffer { self.unsaved = false; self.undo.clear(); self.redo.clear(); - self.update_highlights(); + self.highlights_stale = true; } else { self.diverged = true; self.unsaved = true; @@ -880,6 +879,12 @@ impl Buffer { (Err(_), _) => {} } } + + // Update highlights, if necessary + if self.highlights_stale { + self.highlights = self.lang.highlighter.highlight(self.text.chars()); + self.highlights_stale = false; + } } } diff --git a/src/ui/doc.rs b/src/ui/doc.rs index 822c713..8cc4c2a 100644 --- a/src/ui/doc.rs +++ b/src/ui/doc.rs @@ -344,7 +344,11 @@ impl Finder { impl Visual for Finder { fn render(&mut self, state: &State, frame: &mut Rect) { - let title = format!("{} of {} results", self.selected + 1, self.results.len()); + let title = if self.results.is_empty() { + format!("No results found") + } else { + format!("{} of {} results", self.selected + 1, self.results.len()) + }; self.input.render( state, Some(&title),