From 448575aa4aed72a935f7681ba33419c8d08c1492 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 15 Jan 2020 17:54:25 +0100 Subject: Simplify --- crates/ra_syntax/src/ast/edit.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'crates/ra_syntax/src/ast/edit.rs') diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index b736098ac..5438b8650 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs @@ -23,7 +23,7 @@ impl ast::BinExpr { pub fn replace_op(&self, op: SyntaxKind) -> Option { let op_node: SyntaxElement = self.op_details()?.0.into(); let to_insert: Option = Some(tokens::op(op).into()); - let replace_range = RangeInclusive::new(op_node.clone(), op_node); + let replace_range = op_node.clone()..=op_node; Some(replace_children(self, replace_range, to_insert.into_iter())) } } @@ -43,7 +43,7 @@ impl ast::FnDef { return insert_children(self, InsertPosition::Last, to_insert.into_iter()); }; to_insert.push(body.syntax().clone().into()); - let replace_range = RangeInclusive::new(old_body_or_semi.clone(), old_body_or_semi); + let replace_range = old_body_or_semi.clone()..=old_body_or_semi; replace_children(self, replace_range, to_insert.into_iter()) } } @@ -109,9 +109,7 @@ impl ast::ItemList { let to_insert = iter::once(ws.ws().into()); match existing_ws { None => insert_children(self, InsertPosition::After(l_curly), to_insert), - Some(ws) => { - replace_children(self, RangeInclusive::new(ws.clone().into(), ws.into()), to_insert) - } + Some(ws) => replace_children(self, ws.clone().into()..=ws.into(), to_insert), } } } @@ -207,7 +205,7 @@ impl ast::TypeParam { Some(it) => it.syntax().clone().into(), None => colon.clone().into(), }; - replace_children(self, RangeInclusive::new(colon.into(), end), iter::empty()) + replace_children(self, colon.into()..=end, iter::empty()) } } @@ -224,7 +222,7 @@ fn strip_attrs_and_docs_inner(mut node: SyntaxNode) -> SyntaxNode { Some(el) if el.kind() == WHITESPACE => el.clone(), Some(_) | None => start.clone(), }; - node = algo::replace_children(&node, RangeInclusive::new(start, end), &mut iter::empty()); + node = algo::replace_children(&node, start..=end, &mut iter::empty()); } node } -- cgit v1.2.3 From 8296d3208d30b2b21c897d73e1c847d9549aef21 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 15 Jan 2020 17:56:25 +0100 Subject: Simplify --- crates/ra_syntax/src/ast/edit.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'crates/ra_syntax/src/ast/edit.rs') 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 { pub fn replace_op(&self, op: SyntaxKind) -> Option { let op_node: SyntaxElement = self.op_details()?.0.into(); let to_insert: Option = Some(tokens::op(op).into()); - let replace_range = op_node.clone()..=op_node; - Some(replace_children(self, replace_range, to_insert.into_iter())) + Some(replace_children(self, single_node(op_node), to_insert.into_iter())) } } @@ -43,8 +42,7 @@ impl ast::FnDef { return insert_children(self, InsertPosition::Last, to_insert.into_iter()); }; to_insert.push(body.syntax().clone().into()); - let replace_range = old_body_or_semi.clone()..=old_body_or_semi; - replace_children(self, replace_range, to_insert.into_iter()) + replace_children(self, single_node(old_body_or_semi), to_insert.into_iter()) } } @@ -109,7 +107,7 @@ impl ast::ItemList { let to_insert = iter::once(ws.ws().into()); match existing_ws { None => insert_children(self, InsertPosition::After(l_curly), to_insert), - Some(ws) => replace_children(self, ws.clone().into()..=ws.into(), to_insert), + Some(ws) => replace_children(self, single_node(ws), to_insert), } } } @@ -352,6 +350,11 @@ fn insert_children( N::cast(new_syntax).unwrap() } +fn single_node(element: impl Into) -> RangeInclusive { + let element = element.into(); + element.clone()..=element +} + #[must_use] fn replace_children( parent: &N, -- cgit v1.2.3 From c84010e246c38f868baa777e1f1fda4172d21d53 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 15 Jan 2020 18:14:49 +0100 Subject: Slightly more fluent API --- crates/ra_syntax/src/ast/edit.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_syntax/src/ast/edit.rs') diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index bd7f0aedc..3037f2715 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs @@ -22,7 +22,7 @@ impl ast::BinExpr { #[must_use] pub fn replace_op(&self, op: SyntaxKind) -> Option { let op_node: SyntaxElement = self.op_details()?.0.into(); - let to_insert: Option = Some(tokens::op(op).into()); + let to_insert: Option = Some(make::token(op).into()); Some(replace_children(self, single_node(op_node), to_insert.into_iter())) } } -- cgit v1.2.3 From 7d2d3ac3db6ea7bbb3d77569495176da3b2992e6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 15 Jan 2020 18:30:23 +0100 Subject: More fluent API --- crates/ra_syntax/src/ast/edit.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'crates/ra_syntax/src/ast/edit.rs') diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index 3037f2715..d88a0cf4b 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs @@ -23,7 +23,7 @@ impl ast::BinExpr { pub fn replace_op(&self, op: SyntaxKind) -> Option { let op_node: SyntaxElement = self.op_details()?.0.into(); let to_insert: Option = Some(make::token(op).into()); - Some(replace_children(self, single_node(op_node), to_insert.into_iter())) + Some(replace_children(self, single_node(op_node), to_insert)) } } @@ -39,10 +39,10 @@ impl ast::FnDef { } else { to_insert.push(make::tokens::single_space().into()); to_insert.push(body.syntax().clone().into()); - return insert_children(self, InsertPosition::Last, to_insert.into_iter()); + return insert_children(self, InsertPosition::Last, to_insert); }; to_insert.push(body.syntax().clone().into()); - replace_children(self, single_node(old_body_or_semi), to_insert.into_iter()) + replace_children(self, single_node(old_body_or_semi), to_insert) } } @@ -75,7 +75,7 @@ impl ast::ItemList { let ws = tokens::WsBuilder::new(&format!("\n{}", indent)); let to_insert: ArrayVec<[SyntaxElement; 2]> = [ws.ws().into(), item.syntax().clone().into()].into(); - insert_children(self, position, to_insert.into_iter()) + insert_children(self, position, to_insert) } fn l_curly(&self) -> Option { @@ -184,7 +184,7 @@ impl ast::RecordFieldList { InsertPosition::After(anchor) => after_field!(anchor), }; - insert_children(self, position, to_insert.iter().cloned()) + insert_children(self, position, to_insert) } fn l_curly(&self) -> Option { @@ -228,9 +228,10 @@ fn strip_attrs_and_docs_inner(mut node: SyntaxNode) -> SyntaxNode { #[must_use] pub fn replace_descendants( parent: &N, - replacement_map: impl Iterator, + replacement_map: impl IntoIterator, ) -> N { let map = replacement_map + .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()); @@ -344,9 +345,9 @@ fn prev_tokens(token: SyntaxToken) -> impl Iterator { fn insert_children( parent: &N, position: InsertPosition, - mut to_insert: impl Iterator, + to_insert: impl IntoIterator, ) -> N { - let new_syntax = algo::insert_children(parent.syntax(), position, &mut to_insert); + let new_syntax = algo::insert_children(parent.syntax(), position, &mut to_insert.into_iter()); N::cast(new_syntax).unwrap() } @@ -359,9 +360,10 @@ fn single_node(element: impl Into) -> RangeInclusive( parent: &N, to_replace: RangeInclusive, - mut to_insert: impl Iterator, + to_insert: impl IntoIterator, ) -> N { - let new_syntax = algo::replace_children(parent.syntax(), to_replace, &mut to_insert); + let new_syntax = + algo::replace_children(parent.syntax(), to_replace, &mut to_insert.into_iter()); N::cast(new_syntax).unwrap() } -- cgit v1.2.3