feat: add syntax highlighting to text buffers

This commit is contained in:
Valdemar Lorenzen
2026-10-09 10:51:33 +02:00
parent 981011096e
commit 7c1e4c3ca0
7 changed files with 1359 additions and 54 deletions
+15 -1
View File
@@ -693,6 +693,7 @@ struct Buffer {
id: usize,
lines: Vec<String>,
saved_lines: Vec<String>,
text_revision: u64,
targets: Vec<Target>,
primary: usize,
mode: Mode,
@@ -731,6 +732,7 @@ impl Buffer {
id,
saved_lines: lines.clone(),
lines,
text_revision: 0,
targets: vec![Target::caret(Cursor::default())],
primary: 0,
mode: Mode::Normal,
@@ -938,6 +940,10 @@ impl Editor {
pub fn lines(&self) -> &[String] {
&self.active.lines
}
/// Changes only when this buffer's text changes, independent of cursor/view state.
pub fn text_revision(&self) -> u64 {
self.active.text_revision
}
pub fn cursor(&self) -> Cursor {
self.active.targets[self.active.primary].active
}
@@ -2267,7 +2273,11 @@ impl Editor {
match effect {
Effect::Insert(text) => self.insert_text(&normalize_newlines(&text)),
Effect::Replace(text) => {
self.active.lines = split_lines(&text);
let lines = split_lines(&text);
if lines != self.active.lines {
self.active.text_revision = self.active.text_revision.wrapping_add(1);
}
self.active.lines = lines;
self.active.search_match = None;
self.normalize_targets();
}
@@ -3165,6 +3175,9 @@ impl Editor {
}
fn restore(&mut self, snapshot: Snapshot) {
if self.active.lines != snapshot.lines {
self.active.text_revision = self.active.text_revision.wrapping_add(1);
}
self.active.lines = snapshot.lines;
self.active.targets = snapshot.targets;
self.active.primary = snapshot.primary;
@@ -3377,6 +3390,7 @@ impl Editor {
destinations[edit.target] = (remap(edit.start, false) + edit.caret, edit.round_up);
}
if text != old {
self.active.text_revision = self.active.text_revision.wrapping_add(1);
self.active.search_match = None;
}
self.active.lines = text.split('\n').map(str::to_owned).collect();