From 83a8430e0a4f4c88e45a017a2212c981b42a4a7a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 19 Nov 2019 21:13:36 +0300 Subject: :arrow_up: rowan --- crates/ra_syntax/Cargo.toml | 2 +- crates/ra_syntax/src/algo.rs | 33 ++++++++++++++++----------------- crates/ra_syntax/src/ast/extensions.rs | 2 +- 3 files changed, 18 insertions(+), 19 deletions(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/Cargo.toml b/crates/ra_syntax/Cargo.toml index 45a18a73f..5db2b58c0 100644 --- a/crates/ra_syntax/Cargo.toml +++ b/crates/ra_syntax/Cargo.toml @@ -12,7 +12,7 @@ doctest = false [dependencies] itertools = "0.8.0" -rowan = "0.6.1" +rowan = "0.7.0" rustc_lexer = "0.1.0" rustc-hash = "1.0.1" arrayvec = "0.5.1" diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs index 7cfea70f9..1c075082a 100644 --- a/crates/ra_syntax/src/algo.rs +++ b/crates/ra_syntax/src/algo.rs @@ -134,23 +134,19 @@ pub fn insert_children( to_green_element(element) }); - let old_children = parent.green().children(); + let mut old_children = parent.green().children().map(|it| match it { + NodeOrToken::Token(it) => NodeOrToken::Token(it.clone()), + NodeOrToken::Node(it) => NodeOrToken::Node(it.clone()), + }); let new_children = match &position { - InsertPosition::First => { - to_insert.chain(old_children.iter().cloned()).collect::>() - } - InsertPosition::Last => old_children.iter().cloned().chain(to_insert).collect::>(), + InsertPosition::First => to_insert.chain(old_children).collect::>(), + InsertPosition::Last => old_children.chain(to_insert).collect::>(), InsertPosition::Before(anchor) | InsertPosition::After(anchor) => { let take_anchor = if let InsertPosition::After(_) = position { 1 } else { 0 }; let split_at = position_of_child(parent, anchor.clone()) + take_anchor; - let (before, after) = old_children.split_at(split_at); - before - .iter() - .cloned() - .chain(to_insert) - .chain(after.iter().cloned()) - .collect::>() + let before = old_children.by_ref().take(split_at).collect::>(); + before.into_iter().chain(to_insert).chain(old_children).collect::>() } }; @@ -168,13 +164,16 @@ pub fn replace_children( ) -> SyntaxNode { let start = position_of_child(parent, to_delete.start().clone()); let end = position_of_child(parent, to_delete.end().clone()); - let old_children = parent.green().children(); + let mut old_children = parent.green().children().map(|it| match it { + NodeOrToken::Token(it) => NodeOrToken::Token(it.clone()), + NodeOrToken::Node(it) => NodeOrToken::Node(it.clone()), + }); - let new_children = old_children[..start] - .iter() - .cloned() + let before = old_children.by_ref().take(start).collect::>(); + let new_children = before + .into_iter() .chain(to_insert.map(to_green_element)) - .chain(old_children[end + 1..].iter().cloned()) + .chain(old_children.skip(end + 1 - start)) .collect::>(); with_children(parent, new_children) } diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs index 761b2435c..4851dacb2 100644 --- a/crates/ra_syntax/src/ast/extensions.rs +++ b/crates/ra_syntax/src/ast/extensions.rs @@ -32,7 +32,7 @@ impl ast::NameRef { } fn text_of_first_token(node: &SyntaxNode) -> &SmolStr { - node.green().children().first().and_then(|it| it.as_token()).unwrap().text() + node.green().children().next().and_then(|it| it.into_token()).unwrap().text() } impl ast::Attr { -- cgit v1.2.3