diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-01-15 17:32:34 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-01-15 17:32:34 +0000 |
commit | 2f1df3cd74603bf1ba7f2e1c8833407a176cc66e (patch) | |
tree | 3fd74d1aa420b04f5c9e3aaf84ebb62e553ddb9c /crates/ra_syntax/src/ast/edit.rs | |
parent | c0661ce7444223b0fff1f5d54adb41022ab788cb (diff) | |
parent | 7d2d3ac3db6ea7bbb3d77569495176da3b2992e6 (diff) |
Merge #2855
2855: More fluent API r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src/ast/edit.rs')
-rw-r--r-- | crates/ra_syntax/src/ast/edit.rs | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index b736098ac..d88a0cf4b 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs | |||
@@ -22,9 +22,8 @@ impl ast::BinExpr { | |||
22 | #[must_use] | 22 | #[must_use] |
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(make::token(op).into()); |
26 | let replace_range = RangeInclusive::new(op_node.clone(), op_node); | 26 | Some(replace_children(self, single_node(op_node), to_insert)) |
27 | Some(replace_children(self, replace_range, to_insert.into_iter())) | ||
28 | } | 27 | } |
29 | } | 28 | } |
30 | 29 | ||
@@ -40,11 +39,10 @@ impl ast::FnDef { | |||
40 | } else { | 39 | } else { |
41 | to_insert.push(make::tokens::single_space().into()); | 40 | to_insert.push(make::tokens::single_space().into()); |
42 | to_insert.push(body.syntax().clone().into()); | 41 | to_insert.push(body.syntax().clone().into()); |
43 | return insert_children(self, InsertPosition::Last, to_insert.into_iter()); | 42 | return insert_children(self, InsertPosition::Last, to_insert); |
44 | }; | 43 | }; |
45 | to_insert.push(body.syntax().clone().into()); | 44 | to_insert.push(body.syntax().clone().into()); |
46 | let replace_range = RangeInclusive::new(old_body_or_semi.clone(), old_body_or_semi); | 45 | replace_children(self, single_node(old_body_or_semi), to_insert) |
47 | replace_children(self, replace_range, to_insert.into_iter()) | ||
48 | } | 46 | } |
49 | } | 47 | } |
50 | 48 | ||
@@ -77,7 +75,7 @@ impl ast::ItemList { | |||
77 | let ws = tokens::WsBuilder::new(&format!("\n{}", indent)); | 75 | let ws = tokens::WsBuilder::new(&format!("\n{}", indent)); |
78 | let to_insert: ArrayVec<[SyntaxElement; 2]> = | 76 | let to_insert: ArrayVec<[SyntaxElement; 2]> = |
79 | [ws.ws().into(), item.syntax().clone().into()].into(); | 77 | [ws.ws().into(), item.syntax().clone().into()].into(); |
80 | insert_children(self, position, to_insert.into_iter()) | 78 | insert_children(self, position, to_insert) |
81 | } | 79 | } |
82 | 80 | ||
83 | fn l_curly(&self) -> Option<SyntaxElement> { | 81 | fn l_curly(&self) -> Option<SyntaxElement> { |
@@ -109,9 +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) => { | 110 | Some(ws) => replace_children(self, single_node(ws), to_insert), |
113 | replace_children(self, RangeInclusive::new(ws.clone().into(), ws.into()), to_insert) | ||
114 | } | ||
115 | } | 111 | } |
116 | } | 112 | } |
117 | } | 113 | } |
@@ -188,7 +184,7 @@ impl ast::RecordFieldList { | |||
188 | InsertPosition::After(anchor) => after_field!(anchor), | 184 | InsertPosition::After(anchor) => after_field!(anchor), |
189 | }; | 185 | }; |
190 | 186 | ||
191 | insert_children(self, position, to_insert.iter().cloned()) | 187 | insert_children(self, position, to_insert) |
192 | } | 188 | } |
193 | 189 | ||
194 | fn l_curly(&self) -> Option<SyntaxElement> { | 190 | fn l_curly(&self) -> Option<SyntaxElement> { |
@@ -207,7 +203,7 @@ impl ast::TypeParam { | |||
207 | Some(it) => it.syntax().clone().into(), | 203 | Some(it) => it.syntax().clone().into(), |
208 | None => colon.clone().into(), | 204 | None => colon.clone().into(), |
209 | }; | 205 | }; |
210 | replace_children(self, RangeInclusive::new(colon.into(), end), iter::empty()) | 206 | replace_children(self, colon.into()..=end, iter::empty()) |
211 | } | 207 | } |
212 | } | 208 | } |
213 | 209 | ||
@@ -224,7 +220,7 @@ fn strip_attrs_and_docs_inner(mut node: SyntaxNode) -> SyntaxNode { | |||
224 | Some(el) if el.kind() == WHITESPACE => el.clone(), | 220 | Some(el) if el.kind() == WHITESPACE => el.clone(), |
225 | Some(_) | None => start.clone(), | 221 | Some(_) | None => start.clone(), |
226 | }; | 222 | }; |
227 | node = algo::replace_children(&node, RangeInclusive::new(start, end), &mut iter::empty()); | 223 | node = algo::replace_children(&node, start..=end, &mut iter::empty()); |
228 | } | 224 | } |
229 | node | 225 | node |
230 | } | 226 | } |
@@ -232,9 +228,10 @@ fn strip_attrs_and_docs_inner(mut node: SyntaxNode) -> SyntaxNode { | |||
232 | #[must_use] | 228 | #[must_use] |
233 | pub fn replace_descendants<N: AstNode, D: AstNode>( | 229 | pub fn replace_descendants<N: AstNode, D: AstNode>( |
234 | parent: &N, | 230 | parent: &N, |
235 | replacement_map: impl Iterator<Item = (D, D)>, | 231 | replacement_map: impl IntoIterator<Item = (D, D)>, |
236 | ) -> N { | 232 | ) -> N { |
237 | let map = replacement_map | 233 | let map = replacement_map |
234 | .into_iter() | ||
238 | .map(|(from, to)| (from.syntax().clone().into(), to.syntax().clone().into())) | 235 | .map(|(from, to)| (from.syntax().clone().into(), to.syntax().clone().into())) |
239 | .collect::<FxHashMap<SyntaxElement, _>>(); | 236 | .collect::<FxHashMap<SyntaxElement, _>>(); |
240 | let new_syntax = algo::replace_descendants(parent.syntax(), &|n| map.get(n).cloned()); | 237 | let new_syntax = algo::replace_descendants(parent.syntax(), &|n| map.get(n).cloned()); |
@@ -348,19 +345,25 @@ fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> { | |||
348 | fn insert_children<N: AstNode>( | 345 | fn insert_children<N: AstNode>( |
349 | parent: &N, | 346 | parent: &N, |
350 | position: InsertPosition<SyntaxElement>, | 347 | position: InsertPosition<SyntaxElement>, |
351 | mut to_insert: impl Iterator<Item = SyntaxElement>, | 348 | to_insert: impl IntoIterator<Item = SyntaxElement>, |
352 | ) -> N { | 349 | ) -> N { |
353 | let new_syntax = algo::insert_children(parent.syntax(), position, &mut to_insert); | 350 | let new_syntax = algo::insert_children(parent.syntax(), position, &mut to_insert.into_iter()); |
354 | N::cast(new_syntax).unwrap() | 351 | N::cast(new_syntax).unwrap() |
355 | } | 352 | } |
356 | 353 | ||
354 | fn single_node(element: impl Into<SyntaxElement>) -> RangeInclusive<SyntaxElement> { | ||
355 | let element = element.into(); | ||
356 | element.clone()..=element | ||
357 | } | ||
358 | |||
357 | #[must_use] | 359 | #[must_use] |
358 | fn replace_children<N: AstNode>( | 360 | fn replace_children<N: AstNode>( |
359 | parent: &N, | 361 | parent: &N, |
360 | to_replace: RangeInclusive<SyntaxElement>, | 362 | to_replace: RangeInclusive<SyntaxElement>, |
361 | mut to_insert: impl Iterator<Item = SyntaxElement>, | 363 | to_insert: impl IntoIterator<Item = SyntaxElement>, |
362 | ) -> N { | 364 | ) -> N { |
363 | let new_syntax = algo::replace_children(parent.syntax(), to_replace, &mut to_insert); | 365 | let new_syntax = |
366 | algo::replace_children(parent.syntax(), to_replace, &mut to_insert.into_iter()); | ||
364 | N::cast(new_syntax).unwrap() | 367 | N::cast(new_syntax).unwrap() |
365 | } | 368 | } |
366 | 369 | ||