From 47d7434dde215460fc95916f2703c6925f58dcce Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 18 May 2021 14:42:41 +0300 Subject: internal: replace AstTransformer with mutable syntax trees --- crates/syntax/src/ast/edit.rs | 56 ++-------------------------------- crates/syntax/src/ast/edit_in_place.rs | 10 ++++++ crates/syntax/src/ast/make.rs | 4 +++ crates/syntax/src/ted.rs | 3 ++ 4 files changed, 19 insertions(+), 54 deletions(-) (limited to 'crates/syntax') diff --git a/crates/syntax/src/ast/edit.rs b/crates/syntax/src/ast/edit.rs index 61952377f..19107ee38 100644 --- a/crates/syntax/src/ast/edit.rs +++ b/crates/syntax/src/ast/edit.rs @@ -6,14 +6,12 @@ use std::{ ops::{self, RangeInclusive}, }; -use arrayvec::ArrayVec; - use crate::{ algo, ast::{self, make, AstNode}, - ted, AstToken, InsertPosition, NodeOrToken, SyntaxElement, SyntaxKind, + ted, AstToken, NodeOrToken, SyntaxElement, SyntaxKind, SyntaxKind::{ATTR, COMMENT, WHITESPACE}, - SyntaxNode, SyntaxToken, T, + SyntaxNode, SyntaxToken, }; impl ast::BinExpr { @@ -25,46 +23,6 @@ impl ast::BinExpr { } } -impl ast::Path { - #[must_use] - pub fn with_segment(&self, segment: ast::PathSegment) -> ast::Path { - if let Some(old) = self.segment() { - return self.replace_children( - single_node(old.syntax().clone()), - iter::once(segment.syntax().clone().into()), - ); - } - self.clone() - } -} - -impl ast::PathSegment { - #[must_use] - pub fn with_generic_args(&self, type_args: ast::GenericArgList) -> ast::PathSegment { - self._with_generic_args(type_args, false) - } - - #[must_use] - pub fn with_turbo_fish(&self, type_args: ast::GenericArgList) -> ast::PathSegment { - self._with_generic_args(type_args, true) - } - - fn _with_generic_args(&self, type_args: ast::GenericArgList, turbo: bool) -> ast::PathSegment { - if let Some(old) = self.generic_arg_list() { - return self.replace_children( - single_node(old.syntax().clone()), - iter::once(type_args.syntax().clone().into()), - ); - } - let mut to_insert: ArrayVec = ArrayVec::new(); - if turbo { - to_insert.push(make::token(T![::]).into()); - } - to_insert.push(type_args.syntax().clone().into()); - self.insert_children(InsertPosition::Last, to_insert) - } -} - impl ast::UseTree { /// Splits off the given prefix, making it the path component of the use tree, appending the rest of the path to all UseTreeList items. #[must_use] @@ -233,16 +191,6 @@ fn prev_tokens(token: SyntaxToken) -> impl Iterator { } pub trait AstNodeEdit: AstNode + Clone + Sized { - #[must_use] - fn insert_children( - &self, - position: InsertPosition, - to_insert: impl IntoIterator, - ) -> Self { - let new_syntax = algo::insert_children(self.syntax(), position, to_insert); - Self::cast(new_syntax).unwrap() - } - #[must_use] fn replace_children( &self, diff --git a/crates/syntax/src/ast/edit_in_place.rs b/crates/syntax/src/ast/edit_in_place.rs index 2676ed8c9..ca8103668 100644 --- a/crates/syntax/src/ast/edit_in_place.rs +++ b/crates/syntax/src/ast/edit_in_place.rs @@ -239,6 +239,16 @@ impl ast::TypeBoundList { } } +impl ast::PathSegment { + pub fn get_or_create_generic_arg_list(&self) -> ast::GenericArgList { + if self.generic_arg_list().is_none() { + let arg_list = make::generic_arg_list().clone_for_update(); + ted::append_child(self.syntax(), arg_list.syntax()) + } + self.generic_arg_list().unwrap() + } +} + impl ast::UseTree { pub fn remove(&self) { for &dir in [Direction::Next, Direction::Prev].iter() { diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index d13926ded..0cf170626 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -106,6 +106,10 @@ pub fn impl_trait(trait_: ast::Path, ty: ast::Path) -> ast::Impl { ast_from_text(&format!("impl {} for {} {{}}", trait_, ty)) } +pub(crate) fn generic_arg_list() -> ast::GenericArgList { + ast_from_text("const S: T<> = ();") +} + pub fn path_segment(name_ref: ast::NameRef) -> ast::PathSegment { ast_from_text(&format!("use {};", name_ref)) } diff --git a/crates/syntax/src/ted.rs b/crates/syntax/src/ted.rs index a50c0dbca..ae970f44f 100644 --- a/crates/syntax/src/ted.rs +++ b/crates/syntax/src/ted.rs @@ -184,6 +184,9 @@ fn ws_between(left: &SyntaxElement, right: &SyntaxElement) -> Option