on master help sign in
Update range markers so we can determine which layers can be reused
commit7315f6f3e419d8f433a5cc99d929289ce710f442 authorBlaž Hrastnik <blaz@mxxn.io> dateFri 7 Jan 2022, 09:19 (5 years ago) parent8a53e34
1 file changed+73−11
helix-core/src/syntax.rs+73−11
helix-core/src/syntax.rs+73−11
@@ -14,7 +14,7 @@ use slotmap::{DefaultKey as LayerId, HopSlotMap};
1414use std::{
1515 borrow::Cow,
1616 cell::RefCell,
17− collections::{HashMap, HashSet},
17+ collections::{HashMap, HashSet, VecDeque},
1818 fmt,
1919 path::Path,
2020 sync::Arc,
@@ -459,7 +459,6 @@ impl Syntax {
459459 source: &Rope,
460460 changeset: &ChangeSet,
461461 ) -> Result<(), Error> {
462− use std::collections::{HashSet, VecDeque};
463462 let mut queue = VecDeque::new();
464463 queue.push_back(self.root);
465464
@@ -472,23 +471,84 @@ impl Syntax {
472471 };
473472
474473 // HAXX: for now, clear all layers except root so they get re-parsed
475− self.layers.retain(|id, _| id == self.root);
474+ // self.layers.retain(|id, _| id == self.root);
476475
477476 // Convert the changeset into tree sitter edits.
478477 let edits = generate_edits(old_source, changeset);
479478
480479 // TODO: use the edits to update all layers markers
481480 if !edits.is_empty() {
481+ fn point_add(a: Point, b: Point) -> Point {
482+ if b.row > 0 {
483+ Point::new(a.row.saturating_add(b.row), b.column)
484+ } else {
485+ Point::new(0, a.column.saturating_add(b.column))
486+ }
487+ }
488+ fn point_sub(a: Point, b: Point) -> Point {
489+ if a.row > b.row {
490+ Point::new(a.row.saturating_sub(b.row), a.column)
491+ } else {
492+ Point::new(0, a.column.saturating_sub(b.column))
493+ }
494+ }
495+
482496 for layer in &mut self.layers.values_mut() {
483497 for range in &mut layer.ranges {
484− for edit in &edits {
498+ for edit in edits.iter().rev() {
499+ let is_pure_insertion = edit.old_end_byte == edit.start_byte;
500+
485501 // if edit is after range, skip
486502 if edit.start_byte > range.end_byte {
503+ // TODO: || (is_noop && edit.start_byte == range.end_byte)
487504 continue;
488505 }
489506
490507 // if edit is before range, shift entire range by len
491− if edit.old_end_byte < range.start_byte {}
508+ if edit.old_end_byte < range.start_byte {
509+ range.start_byte =
510+ edit.new_end_byte + (range.start_byte - edit.old_end_byte);
511+ range.start_point = point_add(
512+ edit.new_end_position,
513+ point_sub(range.start_point, edit.old_end_position),
514+ );
515+
516+ range.end_byte = edit
517+ .new_end_byte
518+ .saturating_add(range.end_byte - edit.old_end_byte);
519+ range.end_point = point_add(
520+ edit.new_end_position,
521+ point_sub(range.end_point, edit.old_end_position),
522+ );
523+ }
524+ // if the edit starts in the space before and extends into the range
525+ else if edit.start_byte < range.start_byte {
526+ range.start_byte = edit.new_end_byte;
527+ range.start_point = edit.new_end_position;
528+
529+ range.end_byte = range
530+ .end_byte
531+ .saturating_sub(edit.old_end_byte)
532+ .saturating_add(edit.new_end_byte);
533+ range.end_point = point_add(
534+ edit.new_end_position,
535+ point_sub(range.end_point, edit.old_end_position),
536+ );
537+ }
538+ // If the edit is an insertion at the start of the tree, shift
539+ else if edit.start_byte == range.start_byte && is_pure_insertion {
540+ range.start_byte = edit.new_end_byte;
541+ range.start_point = edit.new_end_position;
542+ } else {
543+ range.end_byte = range
544+ .end_byte
545+ .saturating_sub(edit.old_end_byte)
546+ .saturating_add(edit.new_end_byte);
547+ range.end_point = point_add(
548+ edit.new_end_position,
549+ point_sub(range.end_point, edit.old_end_position),
550+ );
551+ }
492552 }
493553 }
494554 }
@@ -506,6 +566,9 @@ impl Syntax {
506566 // TODO: we should be able to avoid editing & parsing layers with ranges earlier in the document before the edit
507567
508568 while let Some(layer_id) = queue.pop_front() {
569+ // Mark the layer as touched
570+ touched.insert(layer_id);
571+
509572 let layer = &mut self.layers[layer_id];
510573
511574 // If a tree already exists, notify it of changes.
@@ -610,10 +673,14 @@ impl Syntax {
610673 layer.depth == depth && // TODO: track parent id instead
611674 layer.config.language == config.language && layer.ranges == ranges
612675 })
613− .map(|(id, _layer)| id);
676+ .map(|(id, _layer)| {
677+ log::info!("match! {:?}", id);
678+ id
679+ });
614680
615681 // ...or insert a new one.
616682 let layer_id = layer.unwrap_or_else(|| {
683+ log::info!("miss! {:?}", ranges);
617684 self.layers.insert(LanguageLayer {
618685 tree: None,
619686 config,
@@ -625,9 +692,6 @@ impl Syntax {
625692 queue.push_back(layer_id);
626693 }
627694
628− // Mark the layer as touched
629− touched.insert(layer_id);
630−
631695 // TODO: pre-process local scopes at this time, rather than highlight?
632696 // would solve problems with locals not working across boundaries
633697 }
@@ -723,8 +787,6 @@ impl Syntax {
723787 })
724788 .collect::<Vec<_>>();
725789
726− log::info!("--");
727−
728790 // HAXX: arrange layers by byte range, with deeper layers positioned first
729791 layers.sort_by_key(|layer| {
730792 (