2021-01-30 15:19:21 +00:00
|
|
|
//! Structural editing for ast.
|
|
|
|
|
|
|
|
use std::iter::empty;
|
|
|
|
|
|
|
|
use parser::T;
|
|
|
|
|
|
|
|
use crate::{
|
2021-03-18 09:57:55 +00:00
|
|
|
algo::neighbor,
|
|
|
|
ast::{self, edit::AstNodeEdit, make, GenericParamsOwner, WhereClause},
|
2021-01-30 15:19:21 +00:00
|
|
|
ted::{self, Position},
|
2021-03-18 09:57:55 +00:00
|
|
|
AstNode, AstToken, Direction,
|
2021-01-30 15:19:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
use super::NameOwner;
|
|
|
|
|
|
|
|
pub trait GenericParamsOwnerEdit: ast::GenericParamsOwner + AstNodeEdit {
|
2021-04-14 20:56:19 +00:00
|
|
|
fn get_or_create_generic_param_list(&self) -> ast::GenericParamList;
|
2021-01-30 15:19:21 +00:00
|
|
|
fn get_or_create_where_clause(&self) -> ast::WhereClause;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GenericParamsOwnerEdit for ast::Fn {
|
2021-04-14 20:56:19 +00:00
|
|
|
fn get_or_create_generic_param_list(&self) -> ast::GenericParamList {
|
|
|
|
match self.generic_param_list() {
|
|
|
|
Some(it) => it,
|
|
|
|
None => {
|
2021-04-15 10:57:45 +00:00
|
|
|
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())
|
|
|
|
};
|
2021-04-14 20:56:19 +00:00
|
|
|
create_generic_param_list(position)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 15:19:21 +00:00
|
|
|
fn get_or_create_where_clause(&self) -> WhereClause {
|
|
|
|
if self.where_clause().is_none() {
|
|
|
|
let position = if let Some(ty) = self.ret_type() {
|
2021-03-19 17:46:18 +00:00
|
|
|
Position::after(ty.syntax())
|
2021-01-30 15:19:21 +00:00
|
|
|
} else if let Some(param_list) = self.param_list() {
|
2021-03-19 17:46:18 +00:00
|
|
|
Position::after(param_list.syntax())
|
2021-01-30 15:19:21 +00:00
|
|
|
} else {
|
2021-03-19 17:46:18 +00:00
|
|
|
Position::last_child_of(self.syntax())
|
2021-01-30 15:19:21 +00:00
|
|
|
};
|
2021-03-16 19:51:37 +00:00
|
|
|
create_where_clause(position)
|
2021-01-30 15:19:21 +00:00
|
|
|
}
|
|
|
|
self.where_clause().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GenericParamsOwnerEdit for ast::Impl {
|
2021-04-14 20:56:19 +00:00
|
|
|
fn get_or_create_generic_param_list(&self) -> ast::GenericParamList {
|
|
|
|
match self.generic_param_list() {
|
|
|
|
Some(it) => it,
|
|
|
|
None => {
|
2021-04-15 10:57:45 +00:00
|
|
|
let position = if let Some(imp_token) = self.impl_token() {
|
|
|
|
Position::after(imp_token)
|
|
|
|
} else {
|
|
|
|
Position::last_child_of(self.syntax())
|
|
|
|
};
|
2021-04-14 20:56:19 +00:00
|
|
|
create_generic_param_list(position)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 15:19:21 +00:00
|
|
|
fn get_or_create_where_clause(&self) -> WhereClause {
|
|
|
|
if self.where_clause().is_none() {
|
2021-03-16 19:28:04 +00:00
|
|
|
let position = if let Some(items) = self.assoc_item_list() {
|
2021-03-19 17:46:18 +00:00
|
|
|
Position::before(items.syntax())
|
2021-01-30 15:19:21 +00:00
|
|
|
} else {
|
2021-03-19 17:46:18 +00:00
|
|
|
Position::last_child_of(self.syntax())
|
2021-01-30 15:19:21 +00:00
|
|
|
};
|
2021-03-16 19:51:37 +00:00
|
|
|
create_where_clause(position)
|
2021-01-30 15:19:21 +00:00
|
|
|
}
|
|
|
|
self.where_clause().unwrap()
|
|
|
|
}
|
|
|
|
}
|
2021-03-16 19:28:04 +00:00
|
|
|
|
|
|
|
impl GenericParamsOwnerEdit for ast::Trait {
|
2021-04-14 20:56:19 +00:00
|
|
|
fn get_or_create_generic_param_list(&self) -> ast::GenericParamList {
|
2021-04-15 10:57:45 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2021-04-14 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 19:28:04 +00:00
|
|
|
fn get_or_create_where_clause(&self) -> WhereClause {
|
|
|
|
if self.where_clause().is_none() {
|
|
|
|
let position = if let Some(items) = self.assoc_item_list() {
|
2021-03-19 17:46:18 +00:00
|
|
|
Position::before(items.syntax())
|
2021-03-16 19:28:04 +00:00
|
|
|
} else {
|
2021-03-19 17:46:18 +00:00
|
|
|
Position::last_child_of(self.syntax())
|
2021-03-16 19:28:04 +00:00
|
|
|
};
|
2021-03-16 19:51:37 +00:00
|
|
|
create_where_clause(position)
|
2021-03-16 19:28:04 +00:00
|
|
|
}
|
|
|
|
self.where_clause().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 15:19:21 +00:00
|
|
|
impl GenericParamsOwnerEdit for ast::Struct {
|
2021-04-14 20:56:19 +00:00
|
|
|
fn get_or_create_generic_param_list(&self) -> ast::GenericParamList {
|
2021-04-15 10:57:45 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2021-04-14 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2021-01-30 15:19:21 +00:00
|
|
|
fn get_or_create_where_clause(&self) -> WhereClause {
|
|
|
|
if self.where_clause().is_none() {
|
|
|
|
let tfl = self.field_list().and_then(|fl| match fl {
|
|
|
|
ast::FieldList::RecordFieldList(_) => None,
|
|
|
|
ast::FieldList::TupleFieldList(it) => Some(it),
|
|
|
|
});
|
|
|
|
let position = if let Some(tfl) = tfl {
|
2021-03-19 17:46:18 +00:00
|
|
|
Position::after(tfl.syntax())
|
2021-01-30 15:19:21 +00:00
|
|
|
} else if let Some(gpl) = self.generic_param_list() {
|
2021-03-19 17:46:18 +00:00
|
|
|
Position::after(gpl.syntax())
|
2021-01-30 15:19:21 +00:00
|
|
|
} else if let Some(name) = self.name() {
|
2021-03-19 17:46:18 +00:00
|
|
|
Position::after(name.syntax())
|
2021-01-30 15:19:21 +00:00
|
|
|
} else {
|
2021-03-19 17:46:18 +00:00
|
|
|
Position::last_child_of(self.syntax())
|
2021-01-30 15:19:21 +00:00
|
|
|
};
|
2021-03-16 19:51:37 +00:00
|
|
|
create_where_clause(position)
|
2021-03-16 19:28:04 +00:00
|
|
|
}
|
|
|
|
self.where_clause().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GenericParamsOwnerEdit for ast::Enum {
|
2021-04-14 20:56:19 +00:00
|
|
|
fn get_or_create_generic_param_list(&self) -> ast::GenericParamList {
|
2021-04-15 10:57:45 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2021-04-14 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 19:28:04 +00:00
|
|
|
fn get_or_create_where_clause(&self) -> WhereClause {
|
|
|
|
if self.where_clause().is_none() {
|
|
|
|
let position = if let Some(gpl) = self.generic_param_list() {
|
2021-03-19 17:46:18 +00:00
|
|
|
Position::after(gpl.syntax())
|
2021-03-16 19:28:04 +00:00
|
|
|
} else if let Some(name) = self.name() {
|
2021-03-19 17:46:18 +00:00
|
|
|
Position::after(name.syntax())
|
2021-03-16 19:28:04 +00:00
|
|
|
} else {
|
2021-03-19 17:46:18 +00:00
|
|
|
Position::last_child_of(self.syntax())
|
2021-03-16 19:28:04 +00:00
|
|
|
};
|
2021-03-16 19:51:37 +00:00
|
|
|
create_where_clause(position)
|
2021-01-30 15:19:21 +00:00
|
|
|
}
|
|
|
|
self.where_clause().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 19:51:37 +00:00
|
|
|
fn create_where_clause(position: Position) {
|
2021-03-19 17:46:18 +00:00
|
|
|
let where_clause = make::where_clause(empty()).clone_for_update();
|
|
|
|
ted::insert(position, where_clause.syntax());
|
2021-01-30 15:19:21 +00:00
|
|
|
}
|
|
|
|
|
2021-04-14 20:56:19 +00:00
|
|
|
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) => {
|
2021-05-09 15:20:37 +00:00
|
|
|
let position = Position::after(last_param.syntax());
|
|
|
|
let elements = vec![
|
|
|
|
make::token(T![,]).into(),
|
|
|
|
make::tokens::single_space().into(),
|
|
|
|
generic_param.syntax().clone().into(),
|
|
|
|
];
|
|
|
|
ted::insert_all(position, elements);
|
2021-04-14 20:56:19 +00:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let after_l_angle = Position::after(self.l_angle_token().unwrap());
|
|
|
|
ted::insert(after_l_angle, generic_param.syntax())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 15:19:21 +00:00
|
|
|
impl ast::WhereClause {
|
|
|
|
pub fn add_predicate(&self, predicate: ast::WherePred) {
|
|
|
|
if let Some(pred) = self.predicates().last() {
|
|
|
|
if !pred.syntax().siblings_with_tokens(Direction::Next).any(|it| it.kind() == T![,]) {
|
2021-03-19 17:46:18 +00:00
|
|
|
ted::append_child_raw(self.syntax(), make::token(T![,]));
|
2021-01-30 15:19:21 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-19 17:46:18 +00:00
|
|
|
ted::append_child(self.syntax(), predicate.syntax())
|
2021-01-30 15:19:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ast::TypeBoundList {
|
|
|
|
pub fn remove(&self) {
|
|
|
|
if let Some(colon) =
|
|
|
|
self.syntax().siblings_with_tokens(Direction::Prev).find(|it| it.kind() == T![:])
|
|
|
|
{
|
|
|
|
ted::remove_all(colon..=self.syntax().clone().into())
|
|
|
|
} else {
|
2021-03-19 17:46:18 +00:00
|
|
|
ted::remove(self.syntax())
|
2021-01-30 15:19:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 09:57:55 +00:00
|
|
|
|
|
|
|
impl ast::UseTree {
|
|
|
|
pub fn remove(&self) {
|
|
|
|
for &dir in [Direction::Next, Direction::Prev].iter() {
|
|
|
|
if let Some(next_use_tree) = neighbor(self, dir) {
|
|
|
|
let separators = self
|
|
|
|
.syntax()
|
|
|
|
.siblings_with_tokens(dir)
|
|
|
|
.skip(1)
|
|
|
|
.take_while(|it| it.as_node() != Some(next_use_tree.syntax()));
|
|
|
|
ted::remove_all_iter(separators);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ted::remove(self.syntax())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ast::Use {
|
|
|
|
pub fn remove(&self) {
|
|
|
|
let next_ws = self
|
|
|
|
.syntax()
|
|
|
|
.next_sibling_or_token()
|
|
|
|
.and_then(|it| it.into_token())
|
|
|
|
.and_then(ast::Whitespace::cast);
|
|
|
|
if let Some(next_ws) = next_ws {
|
|
|
|
let ws_text = next_ws.syntax().text();
|
|
|
|
if let Some(rest) = ws_text.strip_prefix('\n') {
|
|
|
|
if rest.is_empty() {
|
|
|
|
ted::remove(next_ws.syntax())
|
|
|
|
} else {
|
|
|
|
ted::replace(next_ws.syntax(), make::tokens::whitespace(rest))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ted::remove(self.syntax())
|
|
|
|
}
|
|
|
|
}
|
2021-04-15 10:57:45 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
use crate::SourceFile;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
fn ast_mut_from_text<N: AstNode>(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<N: GenericParamsOwnerEdit + fmt::Display>(before: &str, after: &str) {
|
|
|
|
let gpl_owner = ast_mut_from_text::<N>(before);
|
|
|
|
gpl_owner.get_or_create_generic_param_list();
|
|
|
|
assert_eq!(gpl_owner.to_string(), after);
|
|
|
|
}
|
|
|
|
|
|
|
|
check_create_gpl::<ast::Fn>("fn foo", "fn foo<>");
|
|
|
|
check_create_gpl::<ast::Fn>("fn foo() {}", "fn foo<>() {}");
|
|
|
|
|
|
|
|
check_create_gpl::<ast::Impl>("impl", "impl<>");
|
|
|
|
check_create_gpl::<ast::Impl>("impl Struct {}", "impl<> Struct {}");
|
|
|
|
check_create_gpl::<ast::Impl>("impl Trait for Struct {}", "impl<> Trait for Struct {}");
|
|
|
|
|
|
|
|
check_create_gpl::<ast::Trait>("trait Trait<>", "trait Trait<>");
|
|
|
|
check_create_gpl::<ast::Trait>("trait Trait<> {}", "trait Trait<> {}");
|
|
|
|
|
|
|
|
check_create_gpl::<ast::Struct>("struct A", "struct A<>");
|
|
|
|
check_create_gpl::<ast::Struct>("struct A;", "struct A<>;");
|
|
|
|
check_create_gpl::<ast::Struct>("struct A();", "struct A<>();");
|
|
|
|
check_create_gpl::<ast::Struct>("struct A {}", "struct A<> {}");
|
|
|
|
|
|
|
|
check_create_gpl::<ast::Enum>("enum E", "enum E<>");
|
|
|
|
check_create_gpl::<ast::Enum>("enum E {", "enum E<> {");
|
|
|
|
}
|
|
|
|
}
|