From cea589b3b52ff5c4e358db52dc6de150eb48a9a0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 14 May 2021 18:47:08 +0300 Subject: internal: rewrite assoc item manipulaion to use mutable trees --- crates/syntax/src/ast/edit.rs | 151 ++------------------------------- crates/syntax/src/ast/edit_in_place.rs | 72 ++++++++++++++-- crates/syntax/src/ast/make.rs | 2 +- 3 files changed, 72 insertions(+), 153 deletions(-) (limited to 'crates/syntax/src') diff --git a/crates/syntax/src/ast/edit.rs b/crates/syntax/src/ast/edit.rs index 10ec94cd2..7e4b8252e 100644 --- a/crates/syntax/src/ast/edit.rs +++ b/crates/syntax/src/ast/edit.rs @@ -80,81 +80,6 @@ where } } -impl ast::Impl { - #[must_use] - pub fn with_assoc_item_list(&self, items: ast::AssocItemList) -> ast::Impl { - let mut to_insert: ArrayVec = ArrayVec::new(); - if let Some(old_items) = self.assoc_item_list() { - let to_replace: SyntaxElement = old_items.syntax().clone().into(); - to_insert.push(items.syntax().clone().into()); - self.replace_children(single_node(to_replace), to_insert) - } else { - to_insert.push(make::tokens::single_space().into()); - to_insert.push(items.syntax().clone().into()); - self.insert_children(InsertPosition::Last, to_insert) - } - } -} - -impl ast::AssocItemList { - #[must_use] - pub fn append_items( - &self, - items: impl IntoIterator, - ) -> ast::AssocItemList { - let mut res = self.clone(); - if !self.syntax().text().contains_char('\n') { - res = make_multiline(res); - } - items.into_iter().for_each(|it| res = res.append_item(it)); - res.fixup_trailing_whitespace().unwrap_or(res) - } - - #[must_use] - pub fn append_item(&self, item: ast::AssocItem) -> ast::AssocItemList { - let (indent, position, whitespace) = match self.assoc_items().last() { - Some(it) => ( - leading_indent(it.syntax()).unwrap_or_default().to_string(), - InsertPosition::After(it.syntax().clone().into()), - "\n\n", - ), - None => match self.l_curly_token() { - Some(it) => ( - " ".to_string() + &leading_indent(self.syntax()).unwrap_or_default(), - InsertPosition::After(it.into()), - "\n", - ), - None => return self.clone(), - }, - }; - let ws = tokens::WsBuilder::new(&format!("{}{}", whitespace, indent)); - let to_insert: ArrayVec = - [ws.ws().into(), item.syntax().clone().into()].into(); - self.insert_children(position, to_insert) - } - - /// Remove extra whitespace between last item and closing curly brace. - fn fixup_trailing_whitespace(&self) -> Option { - let first_token_after_items = - self.assoc_items().last()?.syntax().next_sibling_or_token()?; - let last_token_before_curly = self.r_curly_token()?.prev_sibling_or_token()?; - if last_token_before_curly != first_token_after_items { - // there is something more between last item and - // right curly than just whitespace - bail out - return None; - } - let whitespace = - last_token_before_curly.clone().into_token().and_then(ast::Whitespace::cast)?; - let text = whitespace.syntax().text(); - let newline = text.rfind('\n')?; - let keep = tokens::WsBuilder::new(&text[newline..]); - Some(self.replace_children( - first_token_after_items..=last_token_before_curly, - std::iter::once(keep.ws().into()), - )) - } -} - impl ast::RecordExprFieldList { #[must_use] pub fn append_field(&self, field: &ast::RecordExprField) -> ast::RecordExprFieldList { @@ -246,21 +171,6 @@ impl ast::TypeAlias { } } -impl ast::TypeParam { - #[must_use] - pub fn remove_bounds(&self) -> ast::TypeParam { - let colon = match self.colon_token() { - Some(it) => it, - None => return self.clone(), - }; - let end = match self.type_bound_list() { - Some(it) => it.syntax().clone().into(), - None => colon.clone().into(), - }; - self.replace_children(colon.into()..=end, iter::empty()) - } -} - impl ast::Path { #[must_use] pub fn with_segment(&self, segment: ast::PathSegment) -> ast::Path { @@ -411,61 +321,6 @@ impl ast::MatchArmList { } } -impl ast::GenericParamList { - #[must_use] - pub fn append_params( - &self, - params: impl IntoIterator, - ) -> ast::GenericParamList { - let mut res = self.clone(); - params.into_iter().for_each(|it| res = res.append_param(it)); - res - } - - #[must_use] - pub fn append_param(&self, item: ast::GenericParam) -> ast::GenericParamList { - let space = tokens::single_space(); - - let mut to_insert: ArrayVec = ArrayVec::new(); - if self.generic_params().next().is_some() { - to_insert.push(space.into()); - } - to_insert.push(item.syntax().clone().into()); - - macro_rules! after_l_angle { - () => {{ - let anchor = match self.l_angle_token() { - Some(it) => it.into(), - None => return self.clone(), - }; - InsertPosition::After(anchor) - }}; - } - - macro_rules! after_field { - ($anchor:expr) => { - if let Some(comma) = $anchor - .syntax() - .siblings_with_tokens(Direction::Next) - .find(|it| it.kind() == T![,]) - { - InsertPosition::After(comma) - } else { - to_insert.insert(0, make::token(T![,]).into()); - InsertPosition::After($anchor.syntax().clone().into()) - } - }; - } - - let position = match self.generic_params().last() { - Some(it) => after_field!(it), - None => after_l_angle!(), - }; - - self.insert_children(position, to_insert) - } -} - #[must_use] pub fn remove_attrs_and_docs(node: &N) -> N { N::cast(remove_attrs_and_docs_inner(node.syntax().clone())).unwrap() @@ -516,6 +371,12 @@ impl ops::Add for IndentLevel { } impl IndentLevel { + pub fn single() -> IndentLevel { + IndentLevel(0) + } + pub fn is_zero(&self) -> bool { + self.0 == 0 + } pub fn from_element(element: &SyntaxElement) -> IndentLevel { match element { rowan::NodeOrToken::Node(it) => IndentLevel::from_node(it), diff --git a/crates/syntax/src/ast/edit_in_place.rs b/crates/syntax/src/ast/edit_in_place.rs index 168355555..9812e00c9 100644 --- a/crates/syntax/src/ast/edit_in_place.rs +++ b/crates/syntax/src/ast/edit_in_place.rs @@ -2,11 +2,16 @@ use std::iter::empty; -use parser::T; +use parser::{SyntaxKind, T}; +use rowan::SyntaxElement; use crate::{ algo::neighbor, - ast::{self, edit::AstNodeEdit, make, GenericParamsOwner, WhereClause}, + ast::{ + self, + edit::{AstNodeEdit, IndentLevel}, + make, GenericParamsOwner, + }, ted::{self, Position}, AstNode, AstToken, Direction, }; @@ -37,7 +42,7 @@ impl GenericParamsOwnerEdit for ast::Fn { } } - fn get_or_create_where_clause(&self) -> WhereClause { + fn get_or_create_where_clause(&self) -> ast::WhereClause { if self.where_clause().is_none() { let position = if let Some(ty) = self.ret_type() { Position::after(ty.syntax()) @@ -67,7 +72,7 @@ impl GenericParamsOwnerEdit for ast::Impl { } } - fn get_or_create_where_clause(&self) -> WhereClause { + fn get_or_create_where_clause(&self) -> ast::WhereClause { if self.where_clause().is_none() { let position = if let Some(items) = self.assoc_item_list() { Position::before(items.syntax()) @@ -97,7 +102,7 @@ impl GenericParamsOwnerEdit for ast::Trait { } } - fn get_or_create_where_clause(&self) -> WhereClause { + fn get_or_create_where_clause(&self) -> ast::WhereClause { if self.where_clause().is_none() { let position = if let Some(items) = self.assoc_item_list() { Position::before(items.syntax()) @@ -127,7 +132,7 @@ impl GenericParamsOwnerEdit for ast::Struct { } } - fn get_or_create_where_clause(&self) -> WhereClause { + fn get_or_create_where_clause(&self) -> ast::WhereClause { if self.where_clause().is_none() { let tfl = self.field_list().and_then(|fl| match fl { ast::FieldList::RecordFieldList(_) => None, @@ -165,7 +170,7 @@ impl GenericParamsOwnerEdit for ast::Enum { } } - fn get_or_create_where_clause(&self) -> WhereClause { + fn get_or_create_where_clause(&self) -> ast::WhereClause { if self.where_clause().is_none() { let position = if let Some(gpl) = self.generic_param_list() { Position::after(gpl.syntax()) @@ -272,6 +277,59 @@ impl ast::Use { } } +impl ast::Impl { + pub fn get_or_create_assoc_item_list(&self) -> ast::AssocItemList { + if self.assoc_item_list().is_none() { + let assoc_item_list = make::assoc_item_list().clone_for_update(); + ted::append_child(self.syntax(), assoc_item_list.syntax()); + } + self.assoc_item_list().unwrap() + } +} + +impl ast::AssocItemList { + pub fn add_item(&self, item: ast::AssocItem) { + let (indent, position, whitespace) = match self.assoc_items().last() { + Some(last_item) => ( + IndentLevel::from_node(last_item.syntax()), + Position::after(last_item.syntax()), + "\n\n", + ), + None => match self.l_curly_token() { + Some(l_curly) => { + self.normalize_ws_between_braces(); + (IndentLevel::from_token(&l_curly) + 1, Position::after(&l_curly), "\n") + } + None => (IndentLevel::single(), Position::last_child_of(self.syntax()), "\n"), + }, + }; + let elements: Vec> = vec![ + make::tokens::whitespace(&format!("{}{}", whitespace, indent)).into(), + item.syntax().clone().into(), + ]; + ted::insert_all(position, elements); + } + + fn normalize_ws_between_braces(&self) -> Option<()> { + let l = self.l_curly_token()?; + let r = self.r_curly_token()?; + let indent = IndentLevel::from_node(self.syntax()); + + match l.next_sibling_or_token() { + Some(ws) if ws.kind() == SyntaxKind::WHITESPACE => { + if ws.next_sibling_or_token()?.into_token()? == r { + ted::replace(ws, make::tokens::whitespace(&format!("\n{}", indent))); + } + } + Some(ws) if ws.kind() == T!['}'] => { + ted::insert(Position::after(l), make::tokens::whitespace(&format!("\n{}", indent))); + } + _ => (), + } + Some(()) + } +} + #[cfg(test)] mod tests { use std::fmt; diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index de04c8620..d13926ded 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -99,7 +99,7 @@ fn ty_from_text(text: &str) -> ast::Type { } pub fn assoc_item_list() -> ast::AssocItemList { - ast_from_text("impl C for D {};") + ast_from_text("impl C for D {}") } pub fn impl_trait(trait_: ast::Path, ty: ast::Path) -> ast::Impl { -- cgit v1.2.3