From e6fc0bdffb213f6e94c5bb4081e6d175ccbd518f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 19 May 2020 23:12:01 +0200 Subject: Moderate cleanup of add_function --- crates/ra_syntax/src/algo.rs | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) (limited to 'crates/ra_syntax/src/algo.rs') diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs index 2a8dac757..664894d1f 100644 --- a/crates/ra_syntax/src/algo.rs +++ b/crates/ra_syntax/src/algo.rs @@ -266,6 +266,15 @@ impl<'a> SyntaxRewriter<'a> { let replacement = Replacement::Single(with.clone().into()); self.replacements.insert(what, replacement); } + pub fn replace_with_many>( + &mut self, + what: &T, + with: Vec, + ) { + let what = what.clone().into(); + let replacement = Replacement::Many(with); + self.replacements.insert(what, replacement); + } pub fn replace_ast(&mut self, what: &T, with: &T) { self.replace(what.syntax(), with.syntax()) } @@ -302,31 +311,41 @@ impl<'a> SyntaxRewriter<'a> { fn rewrite_children(&self, node: &SyntaxNode) -> SyntaxNode { // FIXME: this could be made much faster. - let new_children = - node.children_with_tokens().flat_map(|it| self.rewrite_self(&it)).collect::>(); + let mut new_children = Vec::new(); + for child in node.children_with_tokens() { + self.rewrite_self(&mut new_children, &child); + } with_children(node, new_children) } fn rewrite_self( &self, + acc: &mut Vec>, element: &SyntaxElement, - ) -> Option> { + ) { if let Some(replacement) = self.replacement(&element) { - return match replacement { + match replacement { Replacement::Single(NodeOrToken::Node(it)) => { - Some(NodeOrToken::Node(it.green().clone())) + acc.push(NodeOrToken::Node(it.green().clone())) } Replacement::Single(NodeOrToken::Token(it)) => { - Some(NodeOrToken::Token(it.green().clone())) + acc.push(NodeOrToken::Token(it.green().clone())) } - Replacement::Delete => None, + Replacement::Many(replacements) => { + acc.extend(replacements.iter().map(|it| match it { + NodeOrToken::Node(it) => NodeOrToken::Node(it.green().clone()), + NodeOrToken::Token(it) => NodeOrToken::Token(it.green().clone()), + })) + } + Replacement::Delete => (), }; + return; } let res = match element { NodeOrToken::Token(it) => NodeOrToken::Token(it.green().clone()), NodeOrToken::Node(it) => NodeOrToken::Node(self.rewrite_children(it).green().clone()), }; - Some(res) + acc.push(res) } } @@ -341,6 +360,7 @@ impl ops::AddAssign for SyntaxRewriter<'_> { enum Replacement { Delete, Single(SyntaxElement), + Many(Vec), } fn with_children( -- cgit v1.2.3