From 78c7940f5c418225dfc419a45639be7a71756509 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 14 Aug 2021 20:29:46 +0300 Subject: [PATCH] internal: remove dead code --- crates/mbe/src/syntax_bridge.rs | 30 +++-------- crates/syntax/src/algo.rs | 93 +-------------------------------- crates/syntax/src/lib.rs | 1 - 3 files changed, 9 insertions(+), 115 deletions(-) diff --git a/crates/mbe/src/syntax_bridge.rs b/crates/mbe/src/syntax_bridge.rs index 2aaa440a71..0421d4c9b0 100644 --- a/crates/mbe/src/syntax_bridge.rs +++ b/crates/mbe/src/syntax_bridge.rs @@ -705,8 +705,8 @@ mod tests { use crate::tests::parse_macro; use parser::TokenSource; use syntax::{ - algo::{insert_children, InsertPosition}, - ast::AstNode, + ast::{make, AstNode}, + ted, }; use test_utils::assert_eq_text; @@ -772,42 +772,26 @@ mod tests { #[test] fn test_token_tree_last_child_is_white_space() { - let source_file = ast::SourceFile::parse("f!({} );").ok().unwrap(); + let source_file = ast::SourceFile::parse("f!{}").ok().unwrap(); let macro_call = source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); let token_tree = macro_call.token_tree().unwrap(); // Token Tree now is : // TokenTree - // - T!['('] // - TokenTree // - T!['{'] // - T!['}'] - // - WHITE_SPACE - // - T![')'] - - let rbrace = - token_tree.syntax().descendants_with_tokens().find(|it| it.kind() == T!['}']).unwrap(); - let space = token_tree - .syntax() - .descendants_with_tokens() - .find(|it| it.kind() == SyntaxKind::WHITESPACE) - .unwrap(); - - // reorder th white space, such that the white is inside the inner token-tree. - let token_tree = insert_children( - &rbrace.parent().unwrap(), - InsertPosition::Last, - std::iter::once(space), - ); + let token_tree = token_tree.clone_for_update(); + ted::append_child(token_tree.syntax(), make::tokens::single_space()); + let token_tree = token_tree.clone_subtree(); // Token Tree now is : // TokenTree // - T!['{'] // - T!['}'] // - WHITE_SPACE - let token_tree = ast::TokenTree::cast(token_tree).unwrap(); - let tt = syntax_node_to_token_tree(token_tree.syntax()).0; + let tt = syntax_node_to_token_tree(token_tree.syntax()).0; assert_eq!(tt.delimiter_kind(), Some(tt::DelimiterKind::Brace)); } diff --git a/crates/syntax/src/algo.rs b/crates/syntax/src/algo.rs index 904a38471e..88d24df8b8 100644 --- a/crates/syntax/src/algo.rs +++ b/crates/syntax/src/algo.rs @@ -8,8 +8,8 @@ use rustc_hash::FxHashMap; use text_edit::TextEditBuilder; use crate::{ - AstNode, Direction, NodeOrToken, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxNodePtr, - SyntaxToken, TextRange, TextSize, + AstNode, Direction, NodeOrToken, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, + TextSize, }; /// Returns ancestors of the node at the offset, sorted by length. This should @@ -92,14 +92,6 @@ pub fn has_errors(node: &SyntaxNode) -> bool { node.children().any(|it| it.kind() == SyntaxKind::ERROR) } -#[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub enum InsertPosition { - First, - Last, - Before(T), - After(T), -} - type FxIndexMap = IndexMap>; #[derive(Debug, Hash, PartialEq, Eq)] @@ -250,87 +242,6 @@ pub fn diff(from: &SyntaxNode, to: &SyntaxNode) -> TreeDiff { } } -/// Adds specified children (tokens or nodes) to the current node at the -/// specific position. -/// -/// This is a type-unsafe low-level editing API, if you need to use it, -/// prefer to create a type-safe abstraction on top of it instead. -pub fn insert_children( - parent: &SyntaxNode, - position: InsertPosition, - to_insert: impl IntoIterator, -) -> SyntaxNode { - let mut to_insert = to_insert.into_iter(); - _insert_children(parent, position, &mut to_insert) -} - -fn _insert_children( - parent: &SyntaxNode, - position: InsertPosition, - to_insert: &mut dyn Iterator, -) -> SyntaxNode { - let mut delta = TextSize::default(); - let to_insert = to_insert.map(|element| { - delta += element.text_range().len(); - to_green_element(element) - }); - - let parent_green = parent.green(); - let mut old_children = parent_green.children().map(|it| match it { - NodeOrToken::Token(it) => NodeOrToken::Token(it.to_owned()), - NodeOrToken::Node(it) => NodeOrToken::Node(it.to_owned()), - }); - - let new_children = match &position { - 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 = old_children.by_ref().take(split_at).collect::>(); - before.into_iter().chain(to_insert).chain(old_children).collect::>() - } - }; - - with_children(parent, new_children) -} - -fn with_children( - parent: &SyntaxNode, - new_children: Vec>, -) -> SyntaxNode { - let _p = profile::span("with_children"); - - let new_green = rowan::GreenNode::new(rowan::SyntaxKind(parent.kind() as u16), new_children); - with_green(parent, new_green) -} - -fn with_green(syntax_node: &SyntaxNode, green: rowan::GreenNode) -> SyntaxNode { - let len = green.children().map(|it| it.text_len()).sum::(); - let new_root_node = syntax_node.replace_with(green); - let new_root_node = SyntaxNode::new_root(new_root_node); - - // FIXME: use a more elegant way to re-fetch the node (#1185), make - // `range` private afterwards - let mut ptr = SyntaxNodePtr::new(syntax_node); - ptr.range = TextRange::at(ptr.range.start(), len); - ptr.to_node(&new_root_node) -} - -fn position_of_child(parent: &SyntaxNode, child: SyntaxElement) -> usize { - parent - .children_with_tokens() - .position(|it| it == child) - .expect("element is not a child of current element") -} - -fn to_green_element(element: SyntaxElement) -> NodeOrToken { - match element { - NodeOrToken::Node(it) => it.green().into(), - NodeOrToken::Token(it) => it.green().to_owned().into(), - } -} - #[cfg(test)] mod tests { use expect_test::{expect, Expect}; diff --git a/crates/syntax/src/lib.rs b/crates/syntax/src/lib.rs index 90de6bef6f..ecf46d8969 100644 --- a/crates/syntax/src/lib.rs +++ b/crates/syntax/src/lib.rs @@ -47,7 +47,6 @@ use stdx::format_to; use text_edit::Indel; pub use crate::{ - algo::InsertPosition, ast::{AstNode, AstToken}, parsing::lexer::{lex_single_syntax_kind, lex_single_valid_syntax_kind, tokenize, Token}, ptr::{AstPtr, SyntaxNodePtr},