From c3e5987c433cdd0ea95a6b1057b442f4f0fe1ffc Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 25 Aug 2018 14:45:17 +0300 Subject: incremental reparse --- crates/libsyntax2/src/yellow/syntax.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'crates/libsyntax2/src/yellow') diff --git a/crates/libsyntax2/src/yellow/syntax.rs b/crates/libsyntax2/src/yellow/syntax.rs index 8f1b1d79a..0045598d4 100644 --- a/crates/libsyntax2/src/yellow/syntax.rs +++ b/crates/libsyntax2/src/yellow/syntax.rs @@ -3,7 +3,7 @@ use std::{fmt, sync::Arc}; use smol_str::SmolStr; use { - yellow::{RedNode, TreeRoot, SyntaxRoot, RedPtr, RefRoot, OwnedRoot}, + yellow::{GreenNode, RedNode, TreeRoot, SyntaxRoot, RedPtr, RefRoot, OwnedRoot}, SyntaxKind::{self, *}, TextRange, TextUnit, }; @@ -141,6 +141,27 @@ impl SyntaxNode { self.red().green().leaf_text() } + pub(crate) fn replace_with(&self, green: GreenNode) -> GreenNode { + assert_eq!(self.kind(), green.kind()); + match self.parent() { + None => green, + Some(parent) => { + let children: Vec<_> = parent.children().map(|child| { + if child == *self { + green.clone() + } else { + child.red().green().clone() + } + }).collect(); + let new_parent = GreenNode::new_branch( + parent.kind(), + children.into_boxed_slice(), + ); + parent.replace_with(new_parent) + }, + } + } + fn red(&self) -> &RedNode { unsafe { self.red.get(&self.root) } } -- cgit v1.2.3