From 5f8b37563e6d82872a18afaf4d9ca25a4ebefac0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 29 Feb 2020 13:49:43 +0100 Subject: Cleanup editing API --- crates/ra_syntax/src/algo.rs | 31 ++++++++++++++++++++++++++++--- crates/ra_syntax/src/ast/edit.rs | 11 +++++------ 2 files changed, 33 insertions(+), 9 deletions(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs index ebf59288a..b87c7084e 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 0e78d8b63..d2630e9e9 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() } -- cgit v1.2.3 From 9abcab16693ac5e162c4ed28c4373ed6bd507380 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 29 Feb 2020 13:50:47 +0100 Subject: Fix typo --- crates/ra_syntax/src/ast/make.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 60cd9d260..3f11b747f 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs @@ -15,7 +15,7 @@ pub fn name_ref(text: &str) -> ast::NameRef { pub fn path_segment(name_ref: ast::NameRef) -> ast::PathSegment { ast_from_text(&format!("use {};", name_ref.syntax())) } -pub fn path_unqalified(segment: ast::PathSegment) -> ast::Path { +pub fn path_unqualified(segment: ast::PathSegment) -> ast::Path { path_from_text(&format!("use {}", segment.syntax())) } pub fn path_qualified(qual: ast::Path, segment: ast::PathSegment) -> ast::Path { -- cgit v1.2.3