aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r--crates/ra_syntax/src/ast/edit.rs12
1 files changed, 12 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs
index 9d0fd1383..d0857d88b 100644
--- a/crates/ra_syntax/src/ast/edit.rs
+++ b/crates/ra_syntax/src/ast/edit.rs
@@ -4,6 +4,7 @@
4use std::{iter, ops::RangeInclusive}; 4use std::{iter, ops::RangeInclusive};
5 5
6use arrayvec::ArrayVec; 6use arrayvec::ArrayVec;
7use rustc_hash::FxHashMap;
7 8
8use crate::{ 9use crate::{
9 algo, 10 algo,
@@ -216,6 +217,17 @@ fn strip_attrs_and_docs_inner(mut node: SyntaxNode) -> SyntaxNode {
216 node 217 node
217} 218}
218 219
220pub fn replace_descendants<N: AstNode, D: AstNode>(
221 parent: &N,
222 replacement_map: impl Iterator<Item = (D, D)>,
223) -> N {
224 let map = replacement_map
225 .map(|(from, to)| (from.syntax().clone().into(), to.syntax().clone().into()))
226 .collect::<FxHashMap<_, _>>();
227 let new_syntax = algo::replace_descendants(parent.syntax(), &map);
228 N::cast(new_syntax).unwrap()
229}
230
219// Note this is copy-pasted from fmt. It seems like fmt should be a separate 231// Note this is copy-pasted from fmt. It seems like fmt should be a separate
220// crate, but basic tree building should be this crate. However, tree building 232// crate, but basic tree building should be this crate. However, tree building
221// might want to call into fmt... 233// might want to call into fmt...