summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-10-14 13:24:38 +0200
committerLaurenz <laurmaedje@gmail.com>2022-10-14 13:25:49 +0200
commit86990d2287979a40f7b7dd73eb31b6a06bab4055 (patch)
tree9703500de8c26a50df1a3a313c586d8c4d5c7e5d /src
parent0a0feb75fe1006bdf6ca816a79d3af402da0bfd5 (diff)
Also hash syntax tree in source
Diffstat (limited to 'src')
-rw-r--r--src/source.rs21
1 files changed, 14 insertions, 7 deletions
diff --git a/src/source.rs b/src/source.rs
index 978b9986..8e22c01d 100644
--- a/src/source.rs
+++ b/src/source.rs
@@ -23,7 +23,7 @@ pub struct Source {
path: PathBuf,
text: Prehashed<String>,
lines: Vec<Line>,
- root: SyntaxNode,
+ root: Prehashed<SyntaxNode>,
}
impl Source {
@@ -39,9 +39,9 @@ impl Source {
Self {
id,
path: path.normalize(),
- root,
text: Prehashed::new(text),
lines,
+ root: Prehashed::new(root),
}
}
@@ -53,7 +53,9 @@ impl Source {
/// Create a source file with the same synthetic span for all nodes.
pub fn synthesized(text: impl Into<String>, span: Span) -> Self {
let mut file = Self::detached(text);
- file.root.synthesize(span);
+ let mut root = file.root.into_inner();
+ root.synthesize(span);
+ file.root = Prehashed::new(root);
file.id = span.source();
file
}
@@ -98,8 +100,9 @@ impl Source {
self.text = Prehashed::new(text);
self.lines = vec![Line { byte_idx: 0, utf16_idx: 0 }];
self.lines.extend(lines(0, 0, &self.text));
- self.root = parse(&self.text);
- self.root.numberize(self.id(), Span::FULL).unwrap();
+ let mut root = parse(&self.text);
+ root.numberize(self.id(), Span::FULL).unwrap();
+ self.root = Prehashed::new(root);
}
/// Edit the source file by replacing the given range and increase the
@@ -129,7 +132,10 @@ impl Source {
.extend(lines(start_byte, start_utf16, &self.text[start_byte ..]));
// Incrementally reparse the replaced range.
- reparse(&mut self.root, &self.text, replace, with.len())
+ let mut root = std::mem::take(&mut self.root).into_inner();
+ let range = reparse(&mut root, &self.text, replace, with.len());
+ self.root = Prehashed::new(root);
+ range
}
/// Get the length of the file in UTF-8 encoded bytes.
@@ -249,6 +255,7 @@ impl Hash for Source {
self.id.hash(state);
self.path.hash(state);
self.text.hash(state);
+ self.root.hash(state);
}
}
@@ -399,8 +406,8 @@ mod tests {
let result = Source::detached(after);
source.edit(range, with);
assert_eq!(source.text, result.text);
- assert_eq!(source.root, result.root);
assert_eq!(source.lines, result.lines);
+ assert_eq!(*source.root, *result.root);
}
// Test inserting at the begining.