From 883dd1568f3f1824b88a136e69248c7e7d476e7e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 14 May 2021 20:00:35 +0300 Subject: internal: use more mutable APIs --- crates/syntax/src/ast/edit.rs | 36 +--------------------------------- crates/syntax/src/ast/edit_in_place.rs | 16 +++++++++++++++ 2 files changed, 17 insertions(+), 35 deletions(-) (limited to 'crates/syntax') diff --git a/crates/syntax/src/ast/edit.rs b/crates/syntax/src/ast/edit.rs index 7e4b8252e..4b5f5c571 100644 --- a/crates/syntax/src/ast/edit.rs +++ b/crates/syntax/src/ast/edit.rs @@ -13,7 +13,7 @@ use crate::{ ast::{ self, make::{self, tokens}, - AstNode, TypeBoundsOwner, + AstNode, }, ted, AstToken, Direction, InsertPosition, NodeOrToken, SmolStr, SyntaxElement, SyntaxKind, SyntaxKind::{ATTR, COMMENT, WHITESPACE}, @@ -29,25 +29,6 @@ impl ast::BinExpr { } } -impl ast::Fn { - #[must_use] - pub fn with_body(&self, body: ast::BlockExpr) -> ast::Fn { - let mut to_insert: ArrayVec = ArrayVec::new(); - let old_body_or_semi: SyntaxElement = if let Some(old_body) = self.body() { - old_body.syntax().clone().into() - } else if let Some(semi) = self.semicolon_token() { - to_insert.push(make::tokens::single_space().into()); - semi.into() - } else { - to_insert.push(make::tokens::single_space().into()); - to_insert.push(body.syntax().clone().into()); - return self.insert_children(InsertPosition::Last, to_insert); - }; - to_insert.push(body.syntax().clone().into()); - self.replace_children(single_node(old_body_or_semi), to_insert) - } -} - fn make_multiline(node: N) -> N where N: AstNode + Clone, @@ -156,21 +137,6 @@ impl ast::RecordExprFieldList { } } -impl ast::TypeAlias { - #[must_use] - pub fn remove_bounds(&self) -> ast::TypeAlias { - 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 { diff --git a/crates/syntax/src/ast/edit_in_place.rs b/crates/syntax/src/ast/edit_in_place.rs index 9812e00c9..ca777d057 100644 --- a/crates/syntax/src/ast/edit_in_place.rs +++ b/crates/syntax/src/ast/edit_in_place.rs @@ -330,6 +330,22 @@ impl ast::AssocItemList { } } +impl ast::Fn { + pub fn get_or_create_body(&self) -> ast::BlockExpr { + if self.body().is_none() { + let body = make::ext::empty_block_expr().clone_for_update(); + match self.semicolon_token() { + Some(semi) => { + ted::replace(semi, body.syntax()); + ted::insert(Position::before(body.syntax), make::tokens::single_space()); + } + None => ted::append_child(self.syntax(), body.syntax()), + } + } + self.body().unwrap() + } +} + #[cfg(test)] mod tests { use std::fmt; -- cgit v1.2.3