diff options
Diffstat (limited to 'crates/syntax/src/algo.rs')
-rw-r--r-- | crates/syntax/src/algo.rs | 35 |
1 files changed, 18 insertions, 17 deletions
diff --git a/crates/syntax/src/algo.rs b/crates/syntax/src/algo.rs index b13252eec..82ebf9037 100644 --- a/crates/syntax/src/algo.rs +++ b/crates/syntax/src/algo.rs | |||
@@ -4,7 +4,6 @@ use std::{ | |||
4 | fmt, | 4 | fmt, |
5 | hash::BuildHasherDefault, | 5 | hash::BuildHasherDefault, |
6 | ops::{self, RangeInclusive}, | 6 | ops::{self, RangeInclusive}, |
7 | ptr, | ||
8 | }; | 7 | }; |
9 | 8 | ||
10 | use indexmap::IndexMap; | 9 | use indexmap::IndexMap; |
@@ -27,7 +26,7 @@ pub fn ancestors_at_offset( | |||
27 | offset: TextSize, | 26 | offset: TextSize, |
28 | ) -> impl Iterator<Item = SyntaxNode> { | 27 | ) -> impl Iterator<Item = SyntaxNode> { |
29 | node.token_at_offset(offset) | 28 | node.token_at_offset(offset) |
30 | .map(|token| token.parent().ancestors()) | 29 | .map(|token| token.ancestors()) |
31 | .kmerge_by(|node1, node2| node1.text_range().len() < node2.text_range().len()) | 30 | .kmerge_by(|node1, node2| node1.text_range().len() < node2.text_range().len()) |
32 | } | 31 | } |
33 | 32 | ||
@@ -171,7 +170,7 @@ pub fn diff(from: &SyntaxNode, to: &SyntaxNode) -> TreeDiff { | |||
171 | && lhs.text_range().len() == rhs.text_range().len() | 170 | && lhs.text_range().len() == rhs.text_range().len() |
172 | && match (&lhs, &rhs) { | 171 | && match (&lhs, &rhs) { |
173 | (NodeOrToken::Node(lhs), NodeOrToken::Node(rhs)) => { | 172 | (NodeOrToken::Node(lhs), NodeOrToken::Node(rhs)) => { |
174 | ptr::eq(lhs.green(), rhs.green()) || lhs.text() == rhs.text() | 173 | lhs == rhs || lhs.text() == rhs.text() |
175 | } | 174 | } |
176 | (NodeOrToken::Token(lhs), NodeOrToken::Token(rhs)) => lhs.text() == rhs.text(), | 175 | (NodeOrToken::Token(lhs), NodeOrToken::Token(rhs)) => lhs.text() == rhs.text(), |
177 | _ => false, | 176 | _ => false, |
@@ -280,9 +279,10 @@ fn _insert_children( | |||
280 | to_green_element(element) | 279 | to_green_element(element) |
281 | }); | 280 | }); |
282 | 281 | ||
283 | let mut old_children = parent.green().children().map(|it| match it { | 282 | let parent_green = parent.green(); |
284 | NodeOrToken::Token(it) => NodeOrToken::Token(it.clone()), | 283 | let mut old_children = parent_green.children().map(|it| match it { |
285 | NodeOrToken::Node(it) => NodeOrToken::Node(it.clone()), | 284 | NodeOrToken::Token(it) => NodeOrToken::Token(it.to_owned()), |
285 | NodeOrToken::Node(it) => NodeOrToken::Node(it.to_owned()), | ||
286 | }); | 286 | }); |
287 | 287 | ||
288 | let new_children = match &position { | 288 | let new_children = match &position { |
@@ -319,9 +319,10 @@ fn _replace_children( | |||
319 | ) -> SyntaxNode { | 319 | ) -> SyntaxNode { |
320 | let start = position_of_child(parent, to_delete.start().clone()); | 320 | let start = position_of_child(parent, to_delete.start().clone()); |
321 | let end = position_of_child(parent, to_delete.end().clone()); | 321 | let end = position_of_child(parent, to_delete.end().clone()); |
322 | let mut old_children = parent.green().children().map(|it| match it { | 322 | let parent_green = parent.green(); |
323 | NodeOrToken::Token(it) => NodeOrToken::Token(it.clone()), | 323 | let mut old_children = parent_green.children().map(|it| match it { |
324 | NodeOrToken::Node(it) => NodeOrToken::Node(it.clone()), | 324 | NodeOrToken::Token(it) => NodeOrToken::Token(it.to_owned()), |
325 | NodeOrToken::Node(it) => NodeOrToken::Node(it.to_owned()), | ||
325 | }); | 326 | }); |
326 | 327 | ||
327 | let before = old_children.by_ref().take(start).collect::<Vec<_>>(); | 328 | let before = old_children.by_ref().take(start).collect::<Vec<_>>(); |
@@ -487,9 +488,9 @@ impl<'a> SyntaxRewriter<'a> { | |||
487 | /// Returns `None` when there are no replacements. | 488 | /// Returns `None` when there are no replacements. |
488 | pub fn rewrite_root(&self) -> Option<SyntaxNode> { | 489 | pub fn rewrite_root(&self) -> Option<SyntaxNode> { |
489 | let _p = profile::span("rewrite_root"); | 490 | let _p = profile::span("rewrite_root"); |
490 | fn element_to_node_or_parent(element: &SyntaxElement) -> SyntaxNode { | 491 | fn element_to_node_or_parent(element: &SyntaxElement) -> Option<SyntaxNode> { |
491 | match element { | 492 | match element { |
492 | SyntaxElement::Node(it) => it.clone(), | 493 | SyntaxElement::Node(it) => Some(it.clone()), |
493 | SyntaxElement::Token(it) => it.parent(), | 494 | SyntaxElement::Token(it) => it.parent(), |
494 | } | 495 | } |
495 | } | 496 | } |
@@ -497,9 +498,9 @@ impl<'a> SyntaxRewriter<'a> { | |||
497 | assert!(self.f.is_none()); | 498 | assert!(self.f.is_none()); |
498 | self.replacements | 499 | self.replacements |
499 | .keys() | 500 | .keys() |
500 | .map(element_to_node_or_parent) | 501 | .filter_map(element_to_node_or_parent) |
501 | .chain(self.insertions.keys().map(|pos| match pos { | 502 | .chain(self.insertions.keys().filter_map(|pos| match pos { |
502 | InsertPos::FirstChildOf(it) => it.clone(), | 503 | InsertPos::FirstChildOf(it) => Some(it.clone()), |
503 | InsertPos::After(it) => element_to_node_or_parent(it), | 504 | InsertPos::After(it) => element_to_node_or_parent(it), |
504 | })) | 505 | })) |
505 | // If we only have one replacement/insertion, we must return its parent node, since `rewrite` does | 506 | // If we only have one replacement/insertion, we must return its parent node, since `rewrite` does |
@@ -552,7 +553,7 @@ impl<'a> SyntaxRewriter<'a> { | |||
552 | }; | 553 | }; |
553 | } else { | 554 | } else { |
554 | match element { | 555 | match element { |
555 | NodeOrToken::Token(it) => acc.push(NodeOrToken::Token(it.green().clone())), | 556 | NodeOrToken::Token(it) => acc.push(NodeOrToken::Token(it.green().to_owned())), |
556 | NodeOrToken::Node(it) => { | 557 | NodeOrToken::Node(it) => { |
557 | acc.push(NodeOrToken::Node(self.rewrite_children(it))); | 558 | acc.push(NodeOrToken::Node(self.rewrite_children(it))); |
558 | } | 559 | } |
@@ -567,7 +568,7 @@ impl<'a> SyntaxRewriter<'a> { | |||
567 | fn element_to_green(element: SyntaxElement) -> NodeOrToken<rowan::GreenNode, rowan::GreenToken> { | 568 | fn element_to_green(element: SyntaxElement) -> NodeOrToken<rowan::GreenNode, rowan::GreenToken> { |
568 | match element { | 569 | match element { |
569 | NodeOrToken::Node(it) => NodeOrToken::Node(it.green().to_owned()), | 570 | NodeOrToken::Node(it) => NodeOrToken::Node(it.green().to_owned()), |
570 | NodeOrToken::Token(it) => NodeOrToken::Token(it.green().clone()), | 571 | NodeOrToken::Token(it) => NodeOrToken::Token(it.green().to_owned()), |
571 | } | 572 | } |
572 | } | 573 | } |
573 | 574 | ||
@@ -625,7 +626,7 @@ fn position_of_child(parent: &SyntaxNode, child: SyntaxElement) -> usize { | |||
625 | fn to_green_element(element: SyntaxElement) -> NodeOrToken<rowan::GreenNode, rowan::GreenToken> { | 626 | fn to_green_element(element: SyntaxElement) -> NodeOrToken<rowan::GreenNode, rowan::GreenToken> { |
626 | match element { | 627 | match element { |
627 | NodeOrToken::Node(it) => it.green().to_owned().into(), | 628 | NodeOrToken::Node(it) => it.green().to_owned().into(), |
628 | NodeOrToken::Token(it) => it.green().clone().into(), | 629 | NodeOrToken::Token(it) => it.green().to_owned().into(), |
629 | } | 630 | } |
630 | } | 631 | } |
631 | 632 | ||