Avoid unnecessary highlighting work
This commit is contained in:
parent
f8887047bc
commit
f5e9701d01
3 changed files with 22 additions and 12 deletions
|
|
@ -42,7 +42,6 @@ fn main() -> Result<(), Error> {
|
||||||
|
|
||||||
// Wait for a while
|
// Wait for a while
|
||||||
term.wait_at_least(Duration::from_millis(250));
|
term.wait_at_least(Duration::from_millis(250));
|
||||||
state.tick();
|
|
||||||
|
|
||||||
while let Some(ev) = term.get_event() {
|
while let Some(ev) = term.get_event() {
|
||||||
// Resize events are special and need handling by the terminal
|
// Resize events are special and need handling by the terminal
|
||||||
|
|
@ -59,6 +58,8 @@ fn main() -> Result<(), Error> {
|
||||||
Err(_) => {}
|
Err(_) => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
state.tick();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
25
src/state.rs
25
src/state.rs
|
|
@ -144,7 +144,6 @@ pub struct Buffer {
|
||||||
pub diverged: bool,
|
pub diverged: bool,
|
||||||
pub text: Text,
|
pub text: Text,
|
||||||
pub lang: LangPack,
|
pub lang: LangPack,
|
||||||
pub highlights: Highlights,
|
|
||||||
pub cursors: HopSlotMap<CursorId, Cursor>,
|
pub cursors: HopSlotMap<CursorId, Cursor>,
|
||||||
pub path: Option<PathBuf>,
|
pub path: Option<PathBuf>,
|
||||||
pub undo: Vec<Change>,
|
pub undo: Vec<Change>,
|
||||||
|
|
@ -152,6 +151,9 @@ pub struct Buffer {
|
||||||
opened_at: Option<SystemTime>,
|
opened_at: Option<SystemTime>,
|
||||||
action_counter: usize,
|
action_counter: usize,
|
||||||
most_recent_rank: usize,
|
most_recent_rank: usize,
|
||||||
|
|
||||||
|
pub highlights: Highlights,
|
||||||
|
highlights_stale: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Change {
|
pub struct Change {
|
||||||
|
|
@ -185,6 +187,7 @@ impl Buffer {
|
||||||
unsaved,
|
unsaved,
|
||||||
diverged: false,
|
diverged: false,
|
||||||
highlights: lang.highlighter.highlight(&chars),
|
highlights: lang.highlighter.highlight(&chars),
|
||||||
|
highlights_stale: false,
|
||||||
lang,
|
lang,
|
||||||
text: Text { chars },
|
text: Text { chars },
|
||||||
cursors: HopSlotMap::default(),
|
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) {
|
pub fn reset(&mut self) {
|
||||||
self.unsaved = true;
|
self.unsaved = true;
|
||||||
|
|
||||||
self.text.chars.clear();
|
self.text.chars.clear();
|
||||||
self.update_highlights();
|
self.highlights_stale = true;
|
||||||
// Reset cursors
|
// Reset cursors
|
||||||
self.cursors.values_mut().for_each(|cursor| {
|
self.cursors.values_mut().for_each(|cursor| {
|
||||||
*cursor = Cursor::default();
|
*cursor = Cursor::default();
|
||||||
|
|
@ -482,7 +481,7 @@ impl Buffer {
|
||||||
*c = *to;
|
*c = *to;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.update_highlights();
|
self.highlights_stale = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn undo_or_redo(&mut self, is_undo: bool) -> bool {
|
fn undo_or_redo(&mut self, is_undo: bool) -> bool {
|
||||||
|
|
@ -534,7 +533,7 @@ impl Buffer {
|
||||||
self.text.chars.insert(base + n, *c);
|
self.text.chars.insert(base + n, *c);
|
||||||
n += 1;
|
n += 1;
|
||||||
}
|
}
|
||||||
self.update_highlights();
|
self.highlights_stale = true;
|
||||||
Change {
|
Change {
|
||||||
kind: ChangeKind::Insert(base, chars),
|
kind: ChangeKind::Insert(base, chars),
|
||||||
action_id: self.action_counter,
|
action_id: self.action_counter,
|
||||||
|
|
@ -568,7 +567,7 @@ impl Buffer {
|
||||||
|
|
||||||
// TODO: Bell if false?
|
// TODO: Bell if false?
|
||||||
let removed = self.text.chars.drain(range.clone()).collect();
|
let removed = self.text.chars.drain(range.clone()).collect();
|
||||||
self.update_highlights();
|
self.highlights_stale = true;
|
||||||
Change {
|
Change {
|
||||||
kind: ChangeKind::Remove(range.start, removed),
|
kind: ChangeKind::Remove(range.start, removed),
|
||||||
action_id: self.action_counter,
|
action_id: self.action_counter,
|
||||||
|
|
@ -860,7 +859,7 @@ impl Buffer {
|
||||||
self.unsaved = false;
|
self.unsaved = false;
|
||||||
self.undo.clear();
|
self.undo.clear();
|
||||||
self.redo.clear();
|
self.redo.clear();
|
||||||
self.update_highlights();
|
self.highlights_stale = true;
|
||||||
} else {
|
} else {
|
||||||
self.diverged = true;
|
self.diverged = true;
|
||||||
self.unsaved = true;
|
self.unsaved = true;
|
||||||
|
|
@ -880,6 +879,12 @@ impl Buffer {
|
||||||
(Err(_), _) => {}
|
(Err(_), _) => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update highlights, if necessary
|
||||||
|
if self.highlights_stale {
|
||||||
|
self.highlights = self.lang.highlighter.highlight(self.text.chars());
|
||||||
|
self.highlights_stale = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -344,7 +344,11 @@ impl Finder {
|
||||||
|
|
||||||
impl Visual for Finder {
|
impl Visual for Finder {
|
||||||
fn render(&mut self, state: &State, frame: &mut Rect) {
|
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(
|
self.input.render(
|
||||||
state,
|
state,
|
||||||
Some(&title),
|
Some(&title),
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue