aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-02-29 12:52:56 +0000
committerGitHub <[email protected]>2020-02-29 12:52:56 +0000
commit0ec7f760fcd8bb9c2273e004468faa2a8cbeb29d (patch)
tree8ad45f841ffa0648b29fb143c3de0b59bde400fb
parentb53ff214aa9fe30eeedb64f78cc89561060c0083 (diff)
parent9abcab16693ac5e162c4ed28c4373ed6bd507380 (diff)
Merge #3375
3375: Cleanup editing API a bit r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
-rw-r--r--crates/ra_assists/src/ast_transform.rs2
-rw-r--r--crates/ra_assists/src/handlers/early_return.rs2
-rw-r--r--crates/ra_assists/src/handlers/move_bounds.rs2
-rw-r--r--crates/ra_ide/src/expand_macro.rs2
-rw-r--r--crates/ra_mbe/src/syntax_bridge.rs2
-rw-r--r--crates/ra_syntax/src/algo.rs31
-rw-r--r--crates/ra_syntax/src/ast/edit.rs11
-rw-r--r--crates/ra_syntax/src/ast/make.rs2
8 files changed, 39 insertions, 15 deletions
diff --git a/crates/ra_assists/src/ast_transform.rs b/crates/ra_assists/src/ast_transform.rs
index 7846e9798..a74ac42d5 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> {
157 157
158pub fn apply<'a, N: AstNode>(transformer: &dyn AstTransform<'a>, node: N) -> N { 158pub fn apply<'a, N: AstNode>(transformer: &dyn AstTransform<'a>, node: N) -> N {
159 let syntax = node.syntax(); 159 let syntax = node.syntax();
160 let result = ra_syntax::algo::replace_descendants(syntax, &|element| match element { 160 let result = ra_syntax::algo::replace_descendants(syntax, |element| match element {
161 ra_syntax::SyntaxElement::Node(n) => { 161 ra_syntax::SyntaxElement::Node(n) => {
162 let replacement = transformer.get_substitution(&n)?; 162 let replacement = transformer.get_substitution(&n)?;
163 Some(replacement.into()) 163 Some(replacement.into())
diff --git a/crates/ra_assists/src/handlers/early_return.rs b/crates/ra_assists/src/handlers/early_return.rs
index f57ecd07a..f3167b4e5 100644
--- a/crates/ra_assists/src/handlers/early_return.rs
+++ b/crates/ra_assists/src/handlers/early_return.rs
@@ -120,7 +120,7 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx) -> Option<Assist> {
120 let expr = { 120 let expr = {
121 let name_ref = make::name_ref("it"); 121 let name_ref = make::name_ref("it");
122 let segment = make::path_segment(name_ref); 122 let segment = make::path_segment(name_ref);
123 let path = make::path_unqalified(segment); 123 let path = make::path_unqualified(segment);
124 make::expr_path(path) 124 make::expr_path(path)
125 }; 125 };
126 make::match_arm(once(pat.into()), expr) 126 make::match_arm(once(pat.into()), expr)
diff --git a/crates/ra_assists/src/handlers/move_bounds.rs b/crates/ra_assists/src/handlers/move_bounds.rs
index 616b95770..86b235366 100644
--- a/crates/ra_assists/src/handlers/move_bounds.rs
+++ b/crates/ra_assists/src/handlers/move_bounds.rs
@@ -75,7 +75,7 @@ fn build_predicate(param: ast::TypeParam) -> Option<ast::WherePred> {
75 let path = { 75 let path = {
76 let name_ref = make::name_ref(&param.name()?.syntax().to_string()); 76 let name_ref = make::name_ref(&param.name()?.syntax().to_string());
77 let segment = make::path_segment(name_ref); 77 let segment = make::path_segment(name_ref);
78 make::path_unqalified(segment) 78 make::path_unqualified(segment)
79 }; 79 };
80 let predicate = make::where_pred(path, param.type_bound_list()?.bounds()); 80 let predicate = make::where_pred(path, param.type_bound_list()?.bounds());
81 Some(predicate) 81 Some(predicate)
diff --git a/crates/ra_ide/src/expand_macro.rs b/crates/ra_ide/src/expand_macro.rs
index f2814e684..5a079de27 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(
52 } 52 }
53 } 53 }
54 54
55 Some(replace_descendants(&expanded, &|n| replaces.get(n).cloned())) 55 Some(replace_descendants(&expanded, |n| replaces.get(n).cloned()))
56} 56}
57 57
58// FIXME: It would also be cool to share logic here and in the mbe tests, 58// 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 ea2cac069..2aaf0215f 100644
--- a/crates/ra_mbe/src/syntax_bridge.rs
+++ b/crates/ra_mbe/src/syntax_bridge.rs
@@ -570,7 +570,7 @@ mod tests {
570 let token_tree = insert_children( 570 let token_tree = insert_children(
571 &rbrace.parent().unwrap(), 571 &rbrace.parent().unwrap(),
572 InsertPosition::Last, 572 InsertPosition::Last,
573 &mut std::iter::once(space), 573 std::iter::once(space),
574 ); 574 );
575 575
576 // Token Tree now is : 576 // Token Tree now is :
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
@@ -144,6 +144,15 @@ pub fn diff(from: &SyntaxNode, to: &SyntaxNode) -> TreeDiff {
144pub fn insert_children( 144pub fn insert_children(
145 parent: &SyntaxNode, 145 parent: &SyntaxNode,
146 position: InsertPosition<SyntaxElement>, 146 position: InsertPosition<SyntaxElement>,
147 to_insert: impl IntoIterator<Item = SyntaxElement>,
148) -> SyntaxNode {
149 let mut to_insert = to_insert.into_iter();
150 _insert_children(parent, position, &mut to_insert)
151}
152
153fn _insert_children(
154 parent: &SyntaxNode,
155 position: InsertPosition<SyntaxElement>,
147 to_insert: &mut dyn Iterator<Item = SyntaxElement>, 156 to_insert: &mut dyn Iterator<Item = SyntaxElement>,
148) -> SyntaxNode { 157) -> SyntaxNode {
149 let mut delta = TextUnit::default(); 158 let mut delta = TextUnit::default();
@@ -178,6 +187,15 @@ pub fn insert_children(
178pub fn replace_children( 187pub fn replace_children(
179 parent: &SyntaxNode, 188 parent: &SyntaxNode,
180 to_delete: RangeInclusive<SyntaxElement>, 189 to_delete: RangeInclusive<SyntaxElement>,
190 to_insert: impl IntoIterator<Item = SyntaxElement>,
191) -> SyntaxNode {
192 let mut to_insert = to_insert.into_iter();
193 _replace_children(parent, to_delete, &mut to_insert)
194}
195
196fn _replace_children(
197 parent: &SyntaxNode,
198 to_delete: RangeInclusive<SyntaxElement>,
181 to_insert: &mut dyn Iterator<Item = SyntaxElement>, 199 to_insert: &mut dyn Iterator<Item = SyntaxElement>,
182) -> SyntaxNode { 200) -> SyntaxNode {
183 let start = position_of_child(parent, to_delete.start().clone()); 201 let start = position_of_child(parent, to_delete.start().clone());
@@ -202,14 +220,21 @@ pub fn replace_children(
202/// to create a type-safe abstraction on top of it instead. 220/// to create a type-safe abstraction on top of it instead.
203pub fn replace_descendants( 221pub fn replace_descendants(
204 parent: &SyntaxNode, 222 parent: &SyntaxNode,
205 map: &impl Fn(&SyntaxElement) -> Option<SyntaxElement>, 223 map: impl Fn(&SyntaxElement) -> Option<SyntaxElement>,
224) -> SyntaxNode {
225 _replace_descendants(parent, &map)
226}
227
228fn _replace_descendants(
229 parent: &SyntaxNode,
230 map: &dyn Fn(&SyntaxElement) -> Option<SyntaxElement>,
206) -> SyntaxNode { 231) -> SyntaxNode {
207 // FIXME: this could be made much faster. 232 // FIXME: this could be made much faster.
208 let new_children = parent.children_with_tokens().map(|it| go(map, it)).collect::<Vec<_>>(); 233 let new_children = parent.children_with_tokens().map(|it| go(map, it)).collect::<Vec<_>>();
209 return with_children(parent, new_children); 234 return with_children(parent, new_children);
210 235
211 fn go( 236 fn go(
212 map: &impl Fn(&SyntaxElement) -> Option<SyntaxElement>, 237 map: &dyn Fn(&SyntaxElement) -> Option<SyntaxElement>,
213 element: SyntaxElement, 238 element: SyntaxElement,
214 ) -> NodeOrToken<rowan::GreenNode, rowan::GreenToken> { 239 ) -> NodeOrToken<rowan::GreenNode, rowan::GreenToken> {
215 if let Some(replacement) = map(&element) { 240 if let Some(replacement) = map(&element) {
@@ -221,7 +246,7 @@ pub fn replace_descendants(
221 match element { 246 match element {
222 NodeOrToken::Token(it) => NodeOrToken::Token(it.green().clone()), 247 NodeOrToken::Token(it) => NodeOrToken::Token(it.green().clone()),
223 NodeOrToken::Node(it) => { 248 NodeOrToken::Node(it) => {
224 NodeOrToken::Node(replace_descendants(&it, map).green().clone()) 249 NodeOrToken::Node(_replace_descendants(&it, map).green().clone())
225 } 250 }
226 } 251 }
227 } 252 }
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<N: AstNode, D: AstNode>(
276 .into_iter() 276 .into_iter()
277 .map(|(from, to)| (from.syntax().clone().into(), to.syntax().clone().into())) 277 .map(|(from, to)| (from.syntax().clone().into(), to.syntax().clone().into()))
278 .collect::<FxHashMap<SyntaxElement, _>>(); 278 .collect::<FxHashMap<SyntaxElement, _>>();
279 let new_syntax = algo::replace_descendants(parent.syntax(), &|n| map.get(n).cloned()); 279 let new_syntax = algo::replace_descendants(parent.syntax(), |n| map.get(n).cloned());
280 N::cast(new_syntax).unwrap() 280 N::cast(new_syntax).unwrap()
281} 281}
282 282
@@ -331,7 +331,7 @@ impl IndentLevel {
331 ) 331 )
332 }) 332 })
333 .collect(); 333 .collect();
334 algo::replace_descendants(&node, &|n| replacements.get(n).cloned()) 334 algo::replace_descendants(&node, |n| replacements.get(n).cloned())
335 } 335 }
336 336
337 pub fn decrease_indent<N: AstNode>(self, node: N) -> N { 337 pub fn decrease_indent<N: AstNode>(self, node: N) -> N {
@@ -359,7 +359,7 @@ impl IndentLevel {
359 ) 359 )
360 }) 360 })
361 .collect(); 361 .collect();
362 algo::replace_descendants(&node, &|n| replacements.get(n).cloned()) 362 algo::replace_descendants(&node, |n| replacements.get(n).cloned())
363 } 363 }
364} 364}
365 365
@@ -389,7 +389,7 @@ fn insert_children<N: AstNode>(
389 position: InsertPosition<SyntaxElement>, 389 position: InsertPosition<SyntaxElement>,
390 to_insert: impl IntoIterator<Item = SyntaxElement>, 390 to_insert: impl IntoIterator<Item = SyntaxElement>,
391) -> N { 391) -> N {
392 let new_syntax = algo::insert_children(parent.syntax(), position, &mut to_insert.into_iter()); 392 let new_syntax = algo::insert_children(parent.syntax(), position, to_insert);
393 N::cast(new_syntax).unwrap() 393 N::cast(new_syntax).unwrap()
394} 394}
395 395
@@ -404,8 +404,7 @@ fn replace_children<N: AstNode>(
404 to_replace: RangeInclusive<SyntaxElement>, 404 to_replace: RangeInclusive<SyntaxElement>,
405 to_insert: impl IntoIterator<Item = SyntaxElement>, 405 to_insert: impl IntoIterator<Item = SyntaxElement>,
406) -> N { 406) -> N {
407 let new_syntax = 407 let new_syntax = algo::replace_children(parent.syntax(), to_replace, to_insert);
408 algo::replace_children(parent.syntax(), to_replace, &mut to_insert.into_iter());
409 N::cast(new_syntax).unwrap() 408 N::cast(new_syntax).unwrap()
410} 409}
411 410
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 {
15pub fn path_segment(name_ref: ast::NameRef) -> ast::PathSegment { 15pub fn path_segment(name_ref: ast::NameRef) -> ast::PathSegment {
16 ast_from_text(&format!("use {};", name_ref.syntax())) 16 ast_from_text(&format!("use {};", name_ref.syntax()))
17} 17}
18pub fn path_unqalified(segment: ast::PathSegment) -> ast::Path { 18pub fn path_unqualified(segment: ast::PathSegment) -> ast::Path {
19 path_from_text(&format!("use {}", segment.syntax())) 19 path_from_text(&format!("use {}", segment.syntax()))
20} 20}
21pub fn path_qualified(qual: ast::Path, segment: ast::PathSegment) -> ast::Path { 21pub fn path_qualified(qual: ast::Path, segment: ast::PathSegment) -> ast::Path {