Avoid unnecessary highlighting work

This commit is contained in:
Joshua Barretto 2025-10-30 11:01:58 +00:00
parent f8887047bc
commit f5e9701d01
3 changed files with 22 additions and 12 deletions

View file

@ -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();
}
})
}

View file

@ -144,7 +144,6 @@ pub struct Buffer {
pub diverged: bool,
pub text: Text,
pub lang: LangPack,
pub highlights: Highlights,
pub cursors: HopSlotMap<CursorId, Cursor>,
pub path: Option<PathBuf>,
pub undo: Vec<Change>,
@ -152,6 +151,9 @@ pub struct Buffer {
opened_at: Option<SystemTime>,
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;
}
}
}

View file

@ -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),