From 144afa55a6031bb633c515c1cd20b110c123c5e5 Mon Sep 17 00:00:00 2001 From: Dawer <7803845+iDawer@users.noreply.github.com> Date: Thu, 15 Apr 2021 01:56:19 +0500 Subject: Switch introduce_named_lifetime assist to use mutable syntax tree --- crates/syntax/src/ast/edit_in_place.rs | 64 ++++++++++++++++++++++++++++++++++ crates/syntax/src/ted.rs | 7 ++++ 2 files changed, 71 insertions(+) (limited to 'crates/syntax') diff --git a/crates/syntax/src/ast/edit_in_place.rs b/crates/syntax/src/ast/edit_in_place.rs index 529bd0eb1..27350e2b9 100644 --- a/crates/syntax/src/ast/edit_in_place.rs +++ b/crates/syntax/src/ast/edit_in_place.rs @@ -14,10 +14,21 @@ use crate::{ use super::NameOwner; pub trait GenericParamsOwnerEdit: ast::GenericParamsOwner + AstNodeEdit { + fn get_or_create_generic_param_list(&self) -> ast::GenericParamList; fn get_or_create_where_clause(&self) -> ast::WhereClause; } impl GenericParamsOwnerEdit for ast::Fn { + fn get_or_create_generic_param_list(&self) -> ast::GenericParamList { + match self.generic_param_list() { + Some(it) => it, + None => { + let position = Position::after(self.name().unwrap().syntax); + create_generic_param_list(position) + } + } + } + fn get_or_create_where_clause(&self) -> WhereClause { if self.where_clause().is_none() { let position = if let Some(ty) = self.ret_type() { @@ -34,6 +45,16 @@ impl GenericParamsOwnerEdit for ast::Fn { } impl GenericParamsOwnerEdit for ast::Impl { + fn get_or_create_generic_param_list(&self) -> ast::GenericParamList { + match self.generic_param_list() { + Some(it) => it, + None => { + let position = Position::after(self.impl_token().unwrap()); + create_generic_param_list(position) + } + } + } + fn get_or_create_where_clause(&self) -> WhereClause { if self.where_clause().is_none() { let position = if let Some(items) = self.assoc_item_list() { @@ -48,6 +69,10 @@ impl GenericParamsOwnerEdit for ast::Impl { } impl GenericParamsOwnerEdit for ast::Trait { + fn get_or_create_generic_param_list(&self) -> ast::GenericParamList { + todo!() + } + fn get_or_create_where_clause(&self) -> WhereClause { if self.where_clause().is_none() { let position = if let Some(items) = self.assoc_item_list() { @@ -62,6 +87,10 @@ impl GenericParamsOwnerEdit for ast::Trait { } impl GenericParamsOwnerEdit for ast::Struct { + fn get_or_create_generic_param_list(&self) -> ast::GenericParamList { + todo!() + } + fn get_or_create_where_clause(&self) -> WhereClause { if self.where_clause().is_none() { let tfl = self.field_list().and_then(|fl| match fl { @@ -84,6 +113,10 @@ impl GenericParamsOwnerEdit for ast::Struct { } impl GenericParamsOwnerEdit for ast::Enum { + fn get_or_create_generic_param_list(&self) -> ast::GenericParamList { + todo!() + } + fn get_or_create_where_clause(&self) -> WhereClause { if self.where_clause().is_none() { let position = if let Some(gpl) = self.generic_param_list() { @@ -104,6 +137,37 @@ fn create_where_clause(position: Position) { ted::insert(position, where_clause.syntax()); } +fn create_generic_param_list(position: Position) -> ast::GenericParamList { + let gpl = make::generic_param_list(empty()).clone_for_update(); + ted::insert_raw(position, gpl.syntax()); + gpl +} + +impl ast::GenericParamList { + pub fn add_generic_param(&self, generic_param: ast::GenericParam) { + match self.generic_params().last() { + Some(last_param) => { + let mut elems = Vec::new(); + if !last_param + .syntax() + .siblings_with_tokens(Direction::Next) + .any(|it| it.kind() == T![,]) + { + elems.push(make::token(T![,]).into()); + elems.push(make::tokens::single_space().into()); + }; + elems.push(generic_param.syntax().clone().into()); + let after_last_param = Position::after(last_param.syntax()); + ted::insert_all(after_last_param, elems); + } + None => { + let after_l_angle = Position::after(self.l_angle_token().unwrap()); + ted::insert(after_l_angle, generic_param.syntax()) + } + } + } +} + impl ast::WhereClause { pub fn add_predicate(&self, predicate: ast::WherePred) { if let Some(pred) = self.predicates().last() { diff --git a/crates/syntax/src/ted.rs b/crates/syntax/src/ted.rs index 177d4ff67..450f2e447 100644 --- a/crates/syntax/src/ted.rs +++ b/crates/syntax/src/ted.rs @@ -165,6 +165,13 @@ fn ws_between(left: &SyntaxElement, right: &SyntaxElement) -> Option] { + return None; + } + if left.kind() == T![&] && right.kind() == SyntaxKind::LIFETIME { + return None; + } + if right.kind() == SyntaxKind::USE { let indent = IndentLevel::from_element(left); return Some(make::tokens::whitespace(&format!("\n{}", indent))); -- cgit v1.2.3 From cedbf2e1c5ead457caf7fe99486da1cc2f2d04f0 Mon Sep 17 00:00:00 2001 From: Dawer <7803845+iDawer@users.noreply.github.com> Date: Thu, 15 Apr 2021 15:57:45 +0500 Subject: Finish GenericParamsOwnerEdit impls --- crates/syntax/src/ast/edit_in_place.rs | 99 ++++++++++++++++++++++++++++++++-- 1 file changed, 94 insertions(+), 5 deletions(-) (limited to 'crates/syntax') diff --git a/crates/syntax/src/ast/edit_in_place.rs b/crates/syntax/src/ast/edit_in_place.rs index 27350e2b9..04f97f368 100644 --- a/crates/syntax/src/ast/edit_in_place.rs +++ b/crates/syntax/src/ast/edit_in_place.rs @@ -23,7 +23,15 @@ impl GenericParamsOwnerEdit for ast::Fn { match self.generic_param_list() { Some(it) => it, None => { - let position = Position::after(self.name().unwrap().syntax); + let position = if let Some(name) = self.name() { + Position::after(name.syntax) + } else if let Some(fn_token) = self.fn_token() { + Position::after(fn_token) + } else if let Some(param_list) = self.param_list() { + Position::before(param_list.syntax) + } else { + Position::last_child_of(self.syntax()) + }; create_generic_param_list(position) } } @@ -49,7 +57,11 @@ impl GenericParamsOwnerEdit for ast::Impl { match self.generic_param_list() { Some(it) => it, None => { - let position = Position::after(self.impl_token().unwrap()); + let position = if let Some(imp_token) = self.impl_token() { + Position::after(imp_token) + } else { + Position::last_child_of(self.syntax()) + }; create_generic_param_list(position) } } @@ -70,7 +82,19 @@ impl GenericParamsOwnerEdit for ast::Impl { impl GenericParamsOwnerEdit for ast::Trait { fn get_or_create_generic_param_list(&self) -> ast::GenericParamList { - todo!() + match self.generic_param_list() { + Some(it) => it, + None => { + let position = if let Some(name) = self.name() { + Position::after(name.syntax) + } else if let Some(trait_token) = self.trait_token() { + Position::after(trait_token) + } else { + Position::last_child_of(self.syntax()) + }; + create_generic_param_list(position) + } + } } fn get_or_create_where_clause(&self) -> WhereClause { @@ -88,7 +112,19 @@ impl GenericParamsOwnerEdit for ast::Trait { impl GenericParamsOwnerEdit for ast::Struct { fn get_or_create_generic_param_list(&self) -> ast::GenericParamList { - todo!() + match self.generic_param_list() { + Some(it) => it, + None => { + let position = if let Some(name) = self.name() { + Position::after(name.syntax) + } else if let Some(struct_token) = self.struct_token() { + Position::after(struct_token) + } else { + Position::last_child_of(self.syntax()) + }; + create_generic_param_list(position) + } + } } fn get_or_create_where_clause(&self) -> WhereClause { @@ -114,7 +150,19 @@ impl GenericParamsOwnerEdit for ast::Struct { impl GenericParamsOwnerEdit for ast::Enum { fn get_or_create_generic_param_list(&self) -> ast::GenericParamList { - todo!() + match self.generic_param_list() { + Some(it) => it, + None => { + let position = if let Some(name) = self.name() { + Position::after(name.syntax) + } else if let Some(enum_token) = self.enum_token() { + Position::after(enum_token) + } else { + Position::last_child_of(self.syntax()) + }; + create_generic_param_list(position) + } + } } fn get_or_create_where_clause(&self) -> WhereClause { @@ -228,3 +276,44 @@ impl ast::Use { ted::remove(self.syntax()) } } + +#[cfg(test)] +mod tests { + use std::fmt; + + use crate::SourceFile; + + use super::*; + + fn ast_mut_from_text(text: &str) -> N { + let parse = SourceFile::parse(text); + parse.tree().syntax().descendants().find_map(N::cast).unwrap().clone_for_update() + } + + #[test] + fn test_create_generic_param_list() { + fn check_create_gpl(before: &str, after: &str) { + let gpl_owner = ast_mut_from_text::(before); + gpl_owner.get_or_create_generic_param_list(); + assert_eq!(gpl_owner.to_string(), after); + } + + check_create_gpl::("fn foo", "fn foo<>"); + check_create_gpl::("fn foo() {}", "fn foo<>() {}"); + + check_create_gpl::("impl", "impl<>"); + check_create_gpl::("impl Struct {}", "impl<> Struct {}"); + check_create_gpl::("impl Trait for Struct {}", "impl<> Trait for Struct {}"); + + check_create_gpl::("trait Trait<>", "trait Trait<>"); + check_create_gpl::("trait Trait<> {}", "trait Trait<> {}"); + + check_create_gpl::("struct A", "struct A<>"); + check_create_gpl::("struct A;", "struct A<>;"); + check_create_gpl::("struct A();", "struct A<>();"); + check_create_gpl::("struct A {}", "struct A<> {}"); + + check_create_gpl::("enum E", "enum E<>"); + check_create_gpl::("enum E {", "enum E<> {"); + } +} -- cgit v1.2.3