diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_syntax/src/ast/edit.rs | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index 5438b8650..bd7f0aedc 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs | |||
@@ -23,8 +23,7 @@ impl ast::BinExpr { | |||
23 | pub fn replace_op(&self, op: SyntaxKind) -> Option<ast::BinExpr> { | 23 | pub fn replace_op(&self, op: SyntaxKind) -> Option<ast::BinExpr> { |
24 | let op_node: SyntaxElement = self.op_details()?.0.into(); | 24 | let op_node: SyntaxElement = self.op_details()?.0.into(); |
25 | let to_insert: Option<SyntaxElement> = Some(tokens::op(op).into()); | 25 | let to_insert: Option<SyntaxElement> = Some(tokens::op(op).into()); |
26 | let replace_range = op_node.clone()..=op_node; | 26 | Some(replace_children(self, single_node(op_node), to_insert.into_iter())) |
27 | Some(replace_children(self, replace_range, to_insert.into_iter())) | ||
28 | } | 27 | } |
29 | } | 28 | } |
30 | 29 | ||
@@ -43,8 +42,7 @@ impl ast::FnDef { | |||
43 | return insert_children(self, InsertPosition::Last, to_insert.into_iter()); | 42 | return insert_children(self, InsertPosition::Last, to_insert.into_iter()); |
44 | }; | 43 | }; |
45 | to_insert.push(body.syntax().clone().into()); | 44 | to_insert.push(body.syntax().clone().into()); |
46 | let replace_range = old_body_or_semi.clone()..=old_body_or_semi; | 45 | replace_children(self, single_node(old_body_or_semi), to_insert.into_iter()) |
47 | replace_children(self, replace_range, to_insert.into_iter()) | ||
48 | } | 46 | } |
49 | } | 47 | } |
50 | 48 | ||
@@ -109,7 +107,7 @@ impl ast::ItemList { | |||
109 | let to_insert = iter::once(ws.ws().into()); | 107 | let to_insert = iter::once(ws.ws().into()); |
110 | match existing_ws { | 108 | match existing_ws { |
111 | None => insert_children(self, InsertPosition::After(l_curly), to_insert), | 109 | None => insert_children(self, InsertPosition::After(l_curly), to_insert), |
112 | Some(ws) => replace_children(self, ws.clone().into()..=ws.into(), to_insert), | 110 | Some(ws) => replace_children(self, single_node(ws), to_insert), |
113 | } | 111 | } |
114 | } | 112 | } |
115 | } | 113 | } |
@@ -352,6 +350,11 @@ fn insert_children<N: AstNode>( | |||
352 | N::cast(new_syntax).unwrap() | 350 | N::cast(new_syntax).unwrap() |
353 | } | 351 | } |
354 | 352 | ||
353 | fn single_node(element: impl Into<SyntaxElement>) -> RangeInclusive<SyntaxElement> { | ||
354 | let element = element.into(); | ||
355 | element.clone()..=element | ||
356 | } | ||
357 | |||
355 | #[must_use] | 358 | #[must_use] |
356 | fn replace_children<N: AstNode>( | 359 | fn replace_children<N: AstNode>( |
357 | parent: &N, | 360 | parent: &N, |