diff --git a/crates/ra_assists/src/ast_transform.rs b/crates/ra_assists/src/ast_transform.rs index 7846e97981..a74ac42d5d 100644 --- a/crates/ra_assists/src/ast_transform.rs +++ b/crates/ra_assists/src/ast_transform.rs @@ -157,7 +157,7 @@ impl<'a> QualifyPaths<'a> { pub fn apply<'a, N: AstNode>(transformer: &dyn AstTransform<'a>, node: N) -> N { let syntax = node.syntax(); - let result = ra_syntax::algo::replace_descendants(syntax, &|element| match element { + let result = ra_syntax::algo::replace_descendants(syntax, |element| match element { ra_syntax::SyntaxElement::Node(n) => { let replacement = transformer.get_substitution(&n)?; Some(replacement.into()) diff --git a/crates/ra_ide/src/expand_macro.rs b/crates/ra_ide/src/expand_macro.rs index f2814e6842..5a079de270 100644 --- a/crates/ra_ide/src/expand_macro.rs +++ b/crates/ra_ide/src/expand_macro.rs @@ -52,7 +52,7 @@ fn expand_macro_recur( } } - Some(replace_descendants(&expanded, &|n| replaces.get(n).cloned())) + Some(replace_descendants(&expanded, |n| replaces.get(n).cloned())) } // FIXME: It would also be cool to share logic here and in the mbe tests, diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index ea2cac069e..2aaf0215fa 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -570,7 +570,7 @@ mod tests { let token_tree = insert_children( &rbrace.parent().unwrap(), InsertPosition::Last, - &mut std::iter::once(space), + std::iter::once(space), ); // Token Tree now is : diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs index ebf59288a3..b87c7084e6 100644 --- a/crates/ra_syntax/src/algo.rs +++ b/crates/ra_syntax/src/algo.rs @@ -142,6 +142,15 @@ pub fn diff(from: &SyntaxNode, to: &SyntaxNode) -> TreeDiff { /// This is a type-unsafe low-level editing API, if you need to use it, /// prefer to create a type-safe abstraction on top of it instead. pub fn insert_children( + parent: &SyntaxNode, + position: InsertPosition, + to_insert: impl IntoIterator, +) -> SyntaxNode { + let mut to_insert = to_insert.into_iter(); + _insert_children(parent, position, &mut to_insert) +} + +fn _insert_children( parent: &SyntaxNode, position: InsertPosition, to_insert: &mut dyn Iterator, @@ -176,6 +185,15 @@ pub fn insert_children( /// This is a type-unsafe low-level editing API, if you need to use it, /// prefer to create a type-safe abstraction on top of it instead. pub fn replace_children( + parent: &SyntaxNode, + to_delete: RangeInclusive, + to_insert: impl IntoIterator, +) -> SyntaxNode { + let mut to_insert = to_insert.into_iter(); + _replace_children(parent, to_delete, &mut to_insert) +} + +fn _replace_children( parent: &SyntaxNode, to_delete: RangeInclusive, to_insert: &mut dyn Iterator, @@ -202,14 +220,21 @@ pub fn replace_children( /// to create a type-safe abstraction on top of it instead. pub fn replace_descendants( parent: &SyntaxNode, - map: &impl Fn(&SyntaxElement) -> Option, + map: impl Fn(&SyntaxElement) -> Option, +) -> SyntaxNode { + _replace_descendants(parent, &map) +} + +fn _replace_descendants( + parent: &SyntaxNode, + map: &dyn Fn(&SyntaxElement) -> Option, ) -> SyntaxNode { // FIXME: this could be made much faster. let new_children = parent.children_with_tokens().map(|it| go(map, it)).collect::>(); return with_children(parent, new_children); fn go( - map: &impl Fn(&SyntaxElement) -> Option, + map: &dyn Fn(&SyntaxElement) -> Option, element: SyntaxElement, ) -> NodeOrToken { if let Some(replacement) = map(&element) { @@ -221,7 +246,7 @@ pub fn replace_descendants( match element { NodeOrToken::Token(it) => NodeOrToken::Token(it.green().clone()), NodeOrToken::Node(it) => { - NodeOrToken::Node(replace_descendants(&it, map).green().clone()) + NodeOrToken::Node(_replace_descendants(&it, map).green().clone()) } } } diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index 0e78d8b637..d2630e9e99 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs @@ -276,7 +276,7 @@ pub fn replace_descendants( .into_iter() .map(|(from, to)| (from.syntax().clone().into(), to.syntax().clone().into())) .collect::>(); - let new_syntax = algo::replace_descendants(parent.syntax(), &|n| map.get(n).cloned()); + let new_syntax = algo::replace_descendants(parent.syntax(), |n| map.get(n).cloned()); N::cast(new_syntax).unwrap() } @@ -331,7 +331,7 @@ impl IndentLevel { ) }) .collect(); - algo::replace_descendants(&node, &|n| replacements.get(n).cloned()) + algo::replace_descendants(&node, |n| replacements.get(n).cloned()) } pub fn decrease_indent(self, node: N) -> N { @@ -359,7 +359,7 @@ impl IndentLevel { ) }) .collect(); - algo::replace_descendants(&node, &|n| replacements.get(n).cloned()) + algo::replace_descendants(&node, |n| replacements.get(n).cloned()) } } @@ -389,7 +389,7 @@ fn insert_children( position: InsertPosition, to_insert: impl IntoIterator, ) -> N { - let new_syntax = algo::insert_children(parent.syntax(), position, &mut to_insert.into_iter()); + let new_syntax = algo::insert_children(parent.syntax(), position, to_insert); N::cast(new_syntax).unwrap() } @@ -404,8 +404,7 @@ fn replace_children( to_replace: RangeInclusive, to_insert: impl IntoIterator, ) -> N { - let new_syntax = - algo::replace_children(parent.syntax(), to_replace, &mut to_insert.into_iter()); + let new_syntax = algo::replace_children(parent.syntax(), to_replace, to_insert); N::cast(new_syntax).unwrap() }