Skip to content

Commit

Permalink
do not reparse unmodified injections
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalkuthe committed Oct 9, 2022
1 parent 18cfe86 commit 9182f6c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 21 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions helix-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ tree-sitter = "0.20"
once_cell = "1.15"
arc-swap = "1"
regex = "1"
bitflags = "1.3"

log = "0.4"
serde = { version = "1.0", features = ["derive"] }
Expand Down
73 changes: 52 additions & 21 deletions helix-core/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ use crate::{
};

use arc_swap::{ArcSwap, Guard};
use bitflags::bitflags;
use slotmap::{DefaultKey as LayerId, HopSlotMap};

use std::{
borrow::Cow,
cell::RefCell,
collections::{HashMap, HashSet, VecDeque},
collections::{HashMap, VecDeque},
fmt,
mem::replace,
path::Path,
str::FromStr,
sync::Arc,
Expand Down Expand Up @@ -594,6 +596,7 @@ impl Syntax {
tree: None,
config,
depth: 0,
flags: LayerUpdateFlags::empty(),
ranges: vec![Range {
start_byte: 0,
end_byte: usize::MAX,
Expand Down Expand Up @@ -656,9 +659,10 @@ impl Syntax {
}
}

for layer in &mut self.layers.values_mut() {
for layer in self.layers.values_mut() {
// The root layer always covers the whole range (0..usize::MAX)
if layer.depth == 0 {
layer.flags = LayerUpdateFlags::MODIFIED;
continue;
}

Expand Down Expand Up @@ -689,6 +693,8 @@ impl Syntax {
edit.new_end_position,
point_sub(range.end_point, edit.old_end_position),
);

layer.flags |= LayerUpdateFlags::MOVED;
}
// if the edit starts in the space before and extends into the range
else if edit.start_byte < range.start_byte {
Expand All @@ -703,11 +709,13 @@ impl Syntax {
edit.new_end_position,
point_sub(range.end_point, edit.old_end_position),
);
layer.flags = LayerUpdateFlags::MODIFIED;
}
// If the edit is an insertion at the start of the tree, shift
else if edit.start_byte == range.start_byte && is_pure_insertion {
range.start_byte = edit.new_end_byte;
range.start_point = edit.new_end_position;
layer.flags |= LayerUpdateFlags::MOVED;
} else {
range.end_byte = range
.end_byte
Expand All @@ -717,6 +725,7 @@ impl Syntax {
edit.new_end_position,
point_sub(range.end_point, edit.old_end_position),
);
layer.flags = LayerUpdateFlags::MODIFIED;
}
}
}
Expand All @@ -731,27 +740,33 @@ impl Syntax {

let source_slice = source.slice(..);

let mut touched = HashSet::new();

// TODO: we should be able to avoid editing & parsing layers with ranges earlier in the document before the edit

while let Some(layer_id) = queue.pop_front() {
// Mark the layer as touched
touched.insert(layer_id);

let layer = &mut self.layers[layer_id];

// Mark the layer as touched
layer.flags |= LayerUpdateFlags::TOUCHED;

// If a tree already exists, notify it of changes.
if let Some(tree) = &mut layer.tree {
for edit in edits.iter().rev() {
// Apply the edits in reverse.
// If we applied them in order then edit 1 would disrupt the positioning of edit 2.
tree.edit(edit);
if layer
.flags
.intersects(LayerUpdateFlags::MODIFIED | LayerUpdateFlags::MOVED)
{
for edit in edits.iter().rev() {
// Apply the edits in reverse.
// If we applied them in order then edit 1 would disrupt the positioning of edit 2.
tree.edit(edit);
}
}
}

// Re-parse the tree.
layer.parse(&mut ts_parser.parser, source)?;
if layer.flags.contains(LayerUpdateFlags::MODIFIED) {
// Re-parse the tree.
layer.parse(&mut ts_parser.parser, source)?;
}
} else {
// always parse if this layer has never been parsed before
layer.parse(&mut ts_parser.parser, source)?;
}

// Switch to an immutable borrow.
let layer = &self.layers[layer_id];
Expand Down Expand Up @@ -855,6 +870,8 @@ impl Syntax {
config,
depth,
ranges,
// set the modified flag to ensure the layer is parsed
flags: LayerUpdateFlags::empty(),
})
});

Expand All @@ -868,8 +885,11 @@ impl Syntax {
// Return the cursor back in the pool.
ts_parser.cursors.push(cursor);

// Remove all untouched layers
self.layers.retain(|id, _| touched.contains(&id));
// Reset all `LayerUpdateFlags` and remove all untouched layers
self.layers.retain(|_, layer| {
replace(&mut layer.flags, LayerUpdateFlags::empty())
.contains(LayerUpdateFlags::TOUCHED)
});

Ok(())
})
Expand Down Expand Up @@ -968,14 +988,25 @@ impl Syntax {
// TODO: Folding
}

bitflags! {
/// Flags that track the status of a layer
/// in the `Sytaxn::update` function
struct LayerUpdateFlags : u32{
const MODIFIED = 0b001;
const MOVED = 0b010;
const TOUCHED = 0b100;
}
}

#[derive(Debug)]
pub struct LanguageLayer {
// mode
// grammar
pub config: Arc<HighlightConfiguration>,
pub(crate) tree: Option<Tree>,
pub ranges: Vec<Range>,
pub depth: usize,
pub depth: u32,
flags: LayerUpdateFlags,
}

impl LanguageLayer {
Expand Down Expand Up @@ -1191,7 +1222,7 @@ struct HighlightIter<'a> {
layers: Vec<HighlightIterLayer<'a>>,
iter_count: usize,
next_event: Option<HighlightEvent>,
last_highlight_range: Option<(usize, usize, usize)>,
last_highlight_range: Option<(usize, usize, u32)>,
}

// Adapter to convert rope chunks to bytes
Expand Down Expand Up @@ -1224,7 +1255,7 @@ struct HighlightIterLayer<'a> {
config: &'a HighlightConfiguration,
highlight_end_stack: Vec<usize>,
scope_stack: Vec<LocalScope<'a>>,
depth: usize,
depth: u32,
ranges: &'a [Range],
}

Expand Down

0 comments on commit 9182f6c

Please sign in to comment.