2021-01-30 15:19:21 +00:00
|
|
|
//! Structural editing for ast.
|
|
|
|
|
2021-11-19 09:06:36 +00:00
|
|
|
use std::iter::{empty, successors};
|
2021-01-30 15:19:21 +00:00
|
|
|
|
2021-05-14 15:47:08 +00:00
|
|
|
use parser::{SyntaxKind, T};
|
|
|
|
use rowan::SyntaxElement;
|
2021-01-30 15:19:21 +00:00
|
|
|
|
|
|
|
use crate::{
|
2021-11-19 09:06:36 +00:00
|
|
|
algo::{self, neighbor},
|
2021-09-27 10:54:24 +00:00
|
|
|
ast::{self, edit::IndentLevel, make, HasGenericParams},
|
2021-01-30 15:19:21 +00:00
|
|
|
ted::{self, Position},
|
2021-08-14 17:38:31 +00:00
|
|
|
AstNode, AstToken, Direction,
|
|
|
|
SyntaxKind::{ATTR, COMMENT, WHITESPACE},
|
|
|
|
SyntaxNode,
|
2021-01-30 15:19:21 +00:00
|
|
|
};
|
|
|
|
|
2021-09-27 10:54:24 +00:00
|
|
|
use super::HasName;
|
2021-01-30 15:19:21 +00:00
|
|
|
|
2021-09-27 10:54:24 +00:00
|
|
|
pub trait GenericParamsOwnerEdit: ast::HasGenericParams {
|
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-05-14 15:47:08 +00:00
|
|
|
fn get_or_create_where_clause(&self) -> ast::WhereClause {
|
2021-01-30 15:19:21 +00:00
|
|
|
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-10-03 12:39:43 +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-10-03 12:53:01 +00:00
|
|
|
let position = match self.impl_token() {
|
|
|
|
Some(imp_token) => Position::after(imp_token),
|
|
|
|
None => Position::last_child_of(self.syntax()),
|
2021-04-15 10:57:45 +00:00
|
|
|
};
|
2021-04-14 20:56:19 +00:00
|
|
|
create_generic_param_list(position)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:47:08 +00:00
|
|
|
fn get_or_create_where_clause(&self) -> ast::WhereClause {
|
2021-01-30 15:19:21 +00:00
|
|
|
if self.where_clause().is_none() {
|
2021-10-03 12:53:01 +00:00
|
|
|
let position = match self.assoc_item_list() {
|
|
|
|
Some(items) => Position::before(items.syntax()),
|
|
|
|
None => Position::last_child_of(self.syntax()),
|
2021-01-30 15:19:21 +00:00
|
|
|
};
|
2021-10-03 12:39:43 +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-05-14 15:47:08 +00:00
|
|
|
fn get_or_create_where_clause(&self) -> ast::WhereClause {
|
2021-03-16 19:28:04 +00:00
|
|
|
if self.where_clause().is_none() {
|
2021-10-03 12:53:01 +00:00
|
|
|
let position = match self.assoc_item_list() {
|
|
|
|
Some(items) => Position::before(items.syntax()),
|
|
|
|
None => Position::last_child_of(self.syntax()),
|
2021-03-16 19:28:04 +00:00
|
|
|
};
|
2021-10-03 12:39:43 +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-05-14 15:47:08 +00:00
|
|
|
fn get_or_create_where_clause(&self) -> ast::WhereClause {
|
2021-01-30 15:19:21 +00:00
|
|
|
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-10-03 12:39:43 +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-05-14 15:47:08 +00:00
|
|
|
fn get_or_create_where_clause(&self) -> ast::WhereClause {
|
2021-03-16 19:28:04 +00:00
|
|
|
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-10-03 12:39:43 +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
|
|
|
|
}
|
|
|
|
|
2021-09-27 10:54:24 +00:00
|
|
|
pub trait AttrsOwnerEdit: ast::HasAttrs {
|
2021-08-14 17:38:31 +00:00
|
|
|
fn remove_attrs_and_docs(&self) {
|
|
|
|
remove_attrs_and_docs(self.syntax());
|
|
|
|
|
|
|
|
fn remove_attrs_and_docs(node: &SyntaxNode) {
|
|
|
|
let mut remove_next_ws = false;
|
|
|
|
for child in node.children_with_tokens() {
|
|
|
|
match child.kind() {
|
|
|
|
ATTR | COMMENT => {
|
|
|
|
remove_next_ws = true;
|
|
|
|
child.detach();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
WHITESPACE if remove_next_ws => {
|
|
|
|
child.detach();
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
remove_next_ws = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 10:54:24 +00:00
|
|
|
impl<T: ast::HasAttrs> AttrsOwnerEdit for T {}
|
2021-08-14 17:38:31 +00:00
|
|
|
|
2021-04-14 20:56:19 +00:00
|
|
|
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());
|
2021-10-03 12:39:43 +00:00
|
|
|
ted::insert(after_l_angle, generic_param.syntax());
|
2021-04-14 20:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-10-03 12:39:43 +00:00
|
|
|
ted::append_child(self.syntax(), predicate.syntax());
|
2021-01-30 15:19:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ast::TypeBoundList {
|
|
|
|
pub fn remove(&self) {
|
2021-10-03 12:53:01 +00:00
|
|
|
match self.syntax().siblings_with_tokens(Direction::Prev).find(|it| it.kind() == T![:]) {
|
|
|
|
Some(colon) => ted::remove_all(colon..=self.syntax().clone().into()),
|
|
|
|
None => ted::remove(self.syntax()),
|
2021-01-30 15:19:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 09:57:55 +00:00
|
|
|
|
2021-05-18 11:42:41 +00:00
|
|
|
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();
|
2021-10-03 12:39:43 +00:00
|
|
|
ted::append_child(self.syntax(), arg_list.syntax());
|
2021-05-18 11:42:41 +00:00
|
|
|
}
|
|
|
|
self.generic_arg_list().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 09:57:55 +00:00
|
|
|
impl ast::UseTree {
|
|
|
|
pub fn remove(&self) {
|
2021-10-03 12:45:08 +00:00
|
|
|
for dir in [Direction::Next, Direction::Prev] {
|
2021-03-18 09:57:55 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2021-10-03 12:39:43 +00:00
|
|
|
ted::remove(self.syntax());
|
2021-03-18 09:57:55 +00:00
|
|
|
}
|
2021-11-19 09:06:36 +00:00
|
|
|
|
|
|
|
pub fn get_or_create_use_tree_list(&self) -> ast::UseTreeList {
|
|
|
|
match self.use_tree_list() {
|
|
|
|
Some(it) => it,
|
|
|
|
None => {
|
|
|
|
let position = Position::last_child_of(self.syntax());
|
|
|
|
let use_tree_list = make::use_tree_list(empty()).clone_for_update();
|
|
|
|
let mut elements = Vec::with_capacity(2);
|
|
|
|
if self.coloncolon_token().is_none() {
|
|
|
|
elements.push(make::token(T![::]).into());
|
|
|
|
}
|
|
|
|
elements.push(use_tree_list.syntax().clone().into());
|
|
|
|
ted::insert_all_raw(position, elements);
|
|
|
|
use_tree_list
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Splits off the given prefix, making it the path component of the use tree,
|
|
|
|
/// appending the rest of the path to all UseTreeList items.
|
2022-04-01 14:12:50 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// `prefix$0::suffix` -> `prefix::{suffix}`
|
|
|
|
///
|
|
|
|
/// `prefix$0` -> `prefix::{self}`
|
|
|
|
///
|
|
|
|
/// `prefix$0::*` -> `prefix::{*}`
|
2021-11-19 09:06:36 +00:00
|
|
|
pub fn split_prefix(&self, prefix: &ast::Path) {
|
|
|
|
debug_assert_eq!(self.path(), Some(prefix.top_path()));
|
|
|
|
let path = self.path().unwrap();
|
|
|
|
if &path == prefix && self.use_tree_list().is_none() {
|
2022-04-01 14:12:50 +00:00
|
|
|
if self.star_token().is_some() {
|
|
|
|
// path$0::* -> *
|
|
|
|
self.coloncolon_token().map(ted::remove);
|
|
|
|
ted::remove(prefix.syntax());
|
|
|
|
} else {
|
|
|
|
// path$0 -> self
|
|
|
|
let self_suffix =
|
|
|
|
make::path_unqualified(make::path_segment_self()).clone_for_update();
|
|
|
|
ted::replace(path.syntax(), self_suffix.syntax());
|
|
|
|
}
|
2021-11-19 09:06:36 +00:00
|
|
|
} else if split_path_prefix(prefix).is_none() {
|
|
|
|
return;
|
|
|
|
}
|
2022-04-01 14:12:50 +00:00
|
|
|
// At this point, prefix path is detached; _self_ use tree has suffix path.
|
2022-04-02 09:18:42 +00:00
|
|
|
// Next, transform 'suffix' use tree into 'prefix::{suffix}'
|
2021-11-19 09:06:36 +00:00
|
|
|
let subtree = self.clone_subtree().clone_for_update();
|
|
|
|
ted::remove_all_iter(self.syntax().children_with_tokens());
|
|
|
|
ted::insert(Position::first_child_of(self.syntax()), prefix.syntax());
|
|
|
|
self.get_or_create_use_tree_list().add_use_tree(subtree);
|
|
|
|
|
|
|
|
fn split_path_prefix(prefix: &ast::Path) -> Option<()> {
|
|
|
|
let parent = prefix.parent_path()?;
|
|
|
|
let segment = parent.segment()?;
|
|
|
|
if algo::has_errors(segment.syntax()) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
for p in successors(parent.parent_path(), |it| it.parent_path()) {
|
|
|
|
p.segment()?;
|
|
|
|
}
|
|
|
|
prefix.parent_path().and_then(|p| p.coloncolon_token()).map(ted::remove);
|
|
|
|
ted::remove(prefix.syntax());
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ast::UseTreeList {
|
|
|
|
pub fn add_use_tree(&self, use_tree: ast::UseTree) {
|
|
|
|
let (position, elements) = match self.use_trees().last() {
|
|
|
|
Some(last_tree) => (
|
|
|
|
Position::after(last_tree.syntax()),
|
|
|
|
vec![
|
|
|
|
make::token(T![,]).into(),
|
|
|
|
make::tokens::single_space().into(),
|
|
|
|
use_tree.syntax.into(),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
None => {
|
|
|
|
let position = match self.l_curly_token() {
|
|
|
|
Some(l_curly) => Position::after(l_curly),
|
|
|
|
None => Position::last_child_of(self.syntax()),
|
|
|
|
};
|
|
|
|
(position, vec![use_tree.syntax.into()])
|
|
|
|
}
|
|
|
|
};
|
|
|
|
ted::insert_all_raw(position, elements);
|
|
|
|
}
|
2021-03-18 09:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2021-10-03 12:39:43 +00:00
|
|
|
ted::remove(next_ws.syntax());
|
2021-03-18 09:57:55 +00:00
|
|
|
} else {
|
2021-10-03 12:39:43 +00:00
|
|
|
ted::replace(next_ws.syntax(), make::tokens::whitespace(rest));
|
2021-03-18 09:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-03 12:39:43 +00:00
|
|
|
ted::remove(self.syntax());
|
2021-03-18 09:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-15 10:57:45 +00:00
|
|
|
|
2021-05-14 15:47:08 +00:00
|
|
|
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) => {
|
2021-05-16 12:10:18 +00:00
|
|
|
normalize_ws_between_braces(self.syntax());
|
2021-05-14 15:47:08 +00:00
|
|
|
(IndentLevel::from_token(&l_curly) + 1, Position::after(&l_curly), "\n")
|
|
|
|
}
|
|
|
|
None => (IndentLevel::single(), Position::last_child_of(self.syntax()), "\n"),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
let elements: Vec<SyntaxElement<_>> = vec![
|
|
|
|
make::tokens::whitespace(&format!("{}{}", whitespace, indent)).into(),
|
|
|
|
item.syntax().clone().into(),
|
|
|
|
];
|
|
|
|
ted::insert_all(position, elements);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 17:00:35 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-16 12:10:18 +00:00
|
|
|
impl ast::MatchArm {
|
|
|
|
pub fn remove(&self) {
|
|
|
|
if let Some(sibling) = self.syntax().prev_sibling_or_token() {
|
|
|
|
if sibling.kind() == SyntaxKind::WHITESPACE {
|
|
|
|
ted::remove(sibling);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(sibling) = self.syntax().next_sibling_or_token() {
|
|
|
|
if sibling.kind() == T![,] {
|
|
|
|
ted::remove(sibling);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ted::remove(self.syntax());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ast::MatchArmList {
|
|
|
|
pub fn add_arm(&self, arm: ast::MatchArm) {
|
|
|
|
normalize_ws_between_braces(self.syntax());
|
2021-05-24 18:53:42 +00:00
|
|
|
let mut elements = Vec::new();
|
2021-05-16 12:10:18 +00:00
|
|
|
let position = match self.arms().last() {
|
|
|
|
Some(last_arm) => {
|
2021-07-30 13:51:07 +00:00
|
|
|
if needs_comma(&last_arm) {
|
|
|
|
ted::append_child(last_arm.syntax(), make::token(SyntaxKind::COMMA));
|
2021-05-24 18:53:42 +00:00
|
|
|
}
|
2021-07-30 13:51:07 +00:00
|
|
|
Position::after(last_arm.syntax().clone())
|
2021-05-16 12:10:18 +00:00
|
|
|
}
|
|
|
|
None => match self.l_curly_token() {
|
|
|
|
Some(it) => Position::after(it),
|
|
|
|
None => Position::last_child_of(self.syntax()),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
let indent = IndentLevel::from_node(self.syntax()) + 1;
|
2021-05-24 18:53:42 +00:00
|
|
|
elements.push(make::tokens::whitespace(&format!("\n{}", indent)).into());
|
|
|
|
elements.push(arm.syntax().clone().into());
|
|
|
|
if needs_comma(&arm) {
|
2021-07-30 13:51:07 +00:00
|
|
|
ted::append_child(arm.syntax(), make::token(SyntaxKind::COMMA));
|
2021-05-24 18:53:42 +00:00
|
|
|
}
|
2021-05-16 12:10:18 +00:00
|
|
|
ted::insert_all(position, elements);
|
2021-05-24 18:53:42 +00:00
|
|
|
|
|
|
|
fn needs_comma(arm: &ast::MatchArm) -> bool {
|
2021-07-30 13:51:07 +00:00
|
|
|
arm.expr().map_or(false, |e| !e.is_block_like()) && arm.comma_token().is_none()
|
2021-05-24 18:53:42 +00:00
|
|
|
}
|
2021-05-16 12:10:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-16 15:10:56 +00:00
|
|
|
impl ast::RecordExprFieldList {
|
|
|
|
pub fn add_field(&self, field: ast::RecordExprField) {
|
|
|
|
let is_multiline = self.syntax().text().contains_char('\n');
|
|
|
|
let whitespace = if is_multiline {
|
|
|
|
let indent = IndentLevel::from_node(self.syntax()) + 1;
|
|
|
|
make::tokens::whitespace(&format!("\n{}", indent))
|
|
|
|
} else {
|
|
|
|
make::tokens::single_space()
|
|
|
|
};
|
|
|
|
|
2021-05-17 09:41:48 +00:00
|
|
|
if is_multiline {
|
|
|
|
normalize_ws_between_braces(self.syntax());
|
|
|
|
}
|
|
|
|
|
2021-05-16 15:10:56 +00:00
|
|
|
let position = match self.fields().last() {
|
|
|
|
Some(last_field) => {
|
|
|
|
let comma = match last_field
|
|
|
|
.syntax()
|
|
|
|
.siblings_with_tokens(Direction::Next)
|
|
|
|
.filter_map(|it| it.into_token())
|
|
|
|
.find(|it| it.kind() == T![,])
|
|
|
|
{
|
|
|
|
Some(it) => it,
|
|
|
|
None => {
|
|
|
|
let comma = ast::make::token(T![,]);
|
|
|
|
ted::insert(Position::after(last_field.syntax()), &comma);
|
|
|
|
comma
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Position::after(comma)
|
|
|
|
}
|
|
|
|
None => match self.l_curly_token() {
|
|
|
|
Some(it) => Position::after(it),
|
|
|
|
None => Position::last_child_of(self.syntax()),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
ted::insert_all(position, vec![whitespace.into(), field.syntax().clone().into()]);
|
|
|
|
if is_multiline {
|
|
|
|
ted::insert(Position::after(field.syntax()), ast::make::token(T![,]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 13:31:55 +00:00
|
|
|
impl ast::RecordExprField {
|
|
|
|
/// This will either replace the initializer, or in the case that this is a shorthand convert
|
|
|
|
/// the initializer into the name ref and insert the expr as the new initializer.
|
|
|
|
pub fn replace_expr(&self, expr: ast::Expr) {
|
2021-10-03 12:51:30 +00:00
|
|
|
if self.name_ref().is_some() {
|
2021-09-28 13:31:55 +00:00
|
|
|
match self.expr() {
|
|
|
|
Some(prev) => ted::replace(prev.syntax(), expr.syntax()),
|
|
|
|
None => ted::append_child(self.syntax(), expr.syntax()),
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// this is a shorthand
|
|
|
|
if let Some(ast::Expr::PathExpr(path_expr)) = self.expr() {
|
|
|
|
if let Some(path) = path_expr.path() {
|
|
|
|
if let Some(name_ref) = path.as_single_name_ref() {
|
|
|
|
path_expr.syntax().detach();
|
|
|
|
let children = vec![
|
|
|
|
name_ref.syntax().clone().into(),
|
|
|
|
ast::make::token(T![:]).into(),
|
|
|
|
ast::make::tokens::single_space().into(),
|
|
|
|
expr.syntax().clone().into(),
|
|
|
|
];
|
|
|
|
ted::insert_all_raw(Position::last_child_of(self.syntax()), children);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-03 13:11:04 +00:00
|
|
|
impl ast::RecordPatFieldList {
|
|
|
|
pub fn add_field(&self, field: ast::RecordPatField) {
|
|
|
|
let is_multiline = self.syntax().text().contains_char('\n');
|
|
|
|
let whitespace = if is_multiline {
|
|
|
|
let indent = IndentLevel::from_node(self.syntax()) + 1;
|
|
|
|
make::tokens::whitespace(&format!("\n{}", indent))
|
|
|
|
} else {
|
|
|
|
make::tokens::single_space()
|
|
|
|
};
|
|
|
|
|
|
|
|
if is_multiline {
|
|
|
|
normalize_ws_between_braces(self.syntax());
|
|
|
|
}
|
|
|
|
|
|
|
|
let position = match self.fields().last() {
|
|
|
|
Some(last_field) => {
|
|
|
|
let comma = match last_field
|
|
|
|
.syntax()
|
|
|
|
.siblings_with_tokens(Direction::Next)
|
|
|
|
.filter_map(|it| it.into_token())
|
|
|
|
.find(|it| it.kind() == T![,])
|
|
|
|
{
|
|
|
|
Some(it) => it,
|
|
|
|
None => {
|
|
|
|
let comma = ast::make::token(T![,]);
|
|
|
|
ted::insert(Position::after(last_field.syntax()), &comma);
|
|
|
|
comma
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Position::after(comma)
|
|
|
|
}
|
|
|
|
None => match self.l_curly_token() {
|
|
|
|
Some(it) => Position::after(it),
|
|
|
|
None => Position::last_child_of(self.syntax()),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
ted::insert_all(position, vec![whitespace.into(), field.syntax().clone().into()]);
|
|
|
|
if is_multiline {
|
|
|
|
ted::insert(Position::after(field.syntax()), ast::make::token(T![,]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-26 09:12:57 +00:00
|
|
|
impl ast::StmtList {
|
2021-07-03 18:05:00 +00:00
|
|
|
pub fn push_front(&self, statement: ast::Stmt) {
|
|
|
|
ted::insert(Position::after(self.l_curly_token().unwrap()), statement.syntax());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-16 12:10:18 +00:00
|
|
|
fn normalize_ws_between_braces(node: &SyntaxNode) -> Option<()> {
|
|
|
|
let l = node
|
|
|
|
.children_with_tokens()
|
|
|
|
.filter_map(|it| it.into_token())
|
|
|
|
.find(|it| it.kind() == T!['{'])?;
|
|
|
|
let r = node
|
|
|
|
.children_with_tokens()
|
|
|
|
.filter_map(|it| it.into_token())
|
|
|
|
.find(|it| it.kind() == T!['}'])?;
|
|
|
|
|
|
|
|
let indent = IndentLevel::from_node(node);
|
|
|
|
|
|
|
|
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(())
|
|
|
|
}
|
|
|
|
|
2021-08-18 11:05:23 +00:00
|
|
|
pub trait Indent: AstNode + Clone + Sized {
|
|
|
|
fn indent_level(&self) -> IndentLevel {
|
|
|
|
IndentLevel::from_node(self.syntax())
|
|
|
|
}
|
2021-08-24 15:18:43 +00:00
|
|
|
fn indent(&self, by: IndentLevel) {
|
|
|
|
by.increase_indent(self.syntax());
|
2021-08-18 11:05:23 +00:00
|
|
|
}
|
2021-08-24 15:18:43 +00:00
|
|
|
fn dedent(&self, by: IndentLevel) {
|
|
|
|
by.decrease_indent(self.syntax());
|
2021-08-18 11:05:23 +00:00
|
|
|
}
|
2021-08-24 15:18:43 +00:00
|
|
|
fn reindent_to(&self, target_level: IndentLevel) {
|
|
|
|
let current_level = IndentLevel::from_node(self.syntax());
|
|
|
|
self.dedent(current_level);
|
2021-10-03 12:39:43 +00:00
|
|
|
self.indent(target_level);
|
2021-08-18 11:05:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<N: AstNode + Clone> Indent for N {}
|
|
|
|
|
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<> {");
|
|
|
|
}
|
2021-08-18 11:05:23 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_increase_indent() {
|
|
|
|
let arm_list = ast_mut_from_text::<ast::Fn>(
|
|
|
|
"fn foo() {
|
|
|
|
;
|
|
|
|
;
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
arm_list.indent(IndentLevel(2));
|
|
|
|
assert_eq!(
|
|
|
|
arm_list.to_string(),
|
|
|
|
"fn foo() {
|
|
|
|
;
|
|
|
|
;
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
}
|
2021-04-15 10:57:45 +00:00
|
|
|
}
|