2021-03-16 14:59:57 -05:00
|
|
|
//! Primitive tree editor, ed for trees.
|
|
|
|
//!
|
|
|
|
//! The `_raw`-suffixed functions insert elements as is, unsuffixed versions fix
|
|
|
|
//! up elements around the edges.
|
2021-03-18 04:57:55 -05:00
|
|
|
use std::{mem, ops::RangeInclusive};
|
2021-01-30 09:19:21 -06:00
|
|
|
|
2021-03-16 14:51:37 -05:00
|
|
|
use parser::T;
|
|
|
|
|
2021-03-18 04:57:55 -05:00
|
|
|
use crate::{
|
2021-04-19 19:05:22 -05:00
|
|
|
ast::{self, edit::IndentLevel, make, AstNode},
|
2021-03-18 04:57:55 -05:00
|
|
|
SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken,
|
|
|
|
};
|
2021-01-30 09:19:21 -06:00
|
|
|
|
2021-03-19 12:46:18 -05:00
|
|
|
/// Utility trait to allow calling `ted` functions with references or owned
|
|
|
|
/// nodes. Do not use outside of this module.
|
|
|
|
pub trait Element {
|
|
|
|
fn syntax_element(self) -> SyntaxElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E: Element + Clone> Element for &'_ E {
|
|
|
|
fn syntax_element(self) -> SyntaxElement {
|
|
|
|
self.clone().syntax_element()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Element for SyntaxElement {
|
|
|
|
fn syntax_element(self) -> SyntaxElement {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Element for SyntaxNode {
|
|
|
|
fn syntax_element(self) -> SyntaxElement {
|
|
|
|
self.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Element for SyntaxToken {
|
|
|
|
fn syntax_element(self) -> SyntaxElement {
|
|
|
|
self.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 09:19:21 -06:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Position {
|
|
|
|
repr: PositionRepr,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum PositionRepr {
|
|
|
|
FirstChild(SyntaxNode),
|
|
|
|
After(SyntaxElement),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Position {
|
2021-03-19 12:46:18 -05:00
|
|
|
pub fn after(elem: impl Element) -> Position {
|
|
|
|
let repr = PositionRepr::After(elem.syntax_element());
|
2021-01-30 09:19:21 -06:00
|
|
|
Position { repr }
|
|
|
|
}
|
2021-03-19 12:46:18 -05:00
|
|
|
pub fn before(elem: impl Element) -> Position {
|
|
|
|
let elem = elem.syntax_element();
|
2021-01-30 09:19:21 -06:00
|
|
|
let repr = match elem.prev_sibling_or_token() {
|
|
|
|
Some(it) => PositionRepr::After(it),
|
|
|
|
None => PositionRepr::FirstChild(elem.parent().unwrap()),
|
|
|
|
};
|
|
|
|
Position { repr }
|
|
|
|
}
|
2021-03-19 12:46:18 -05:00
|
|
|
pub fn first_child_of(node: &(impl Into<SyntaxNode> + Clone)) -> Position {
|
|
|
|
let repr = PositionRepr::FirstChild(node.clone().into());
|
2021-01-30 09:19:21 -06:00
|
|
|
Position { repr }
|
|
|
|
}
|
2021-03-19 12:46:18 -05:00
|
|
|
pub fn last_child_of(node: &(impl Into<SyntaxNode> + Clone)) -> Position {
|
|
|
|
let node = node.clone().into();
|
2021-01-30 09:19:21 -06:00
|
|
|
let repr = match node.last_child_or_token() {
|
|
|
|
Some(it) => PositionRepr::After(it),
|
|
|
|
None => PositionRepr::FirstChild(node),
|
|
|
|
};
|
|
|
|
Position { repr }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 12:46:18 -05:00
|
|
|
pub fn insert(position: Position, elem: impl Element) {
|
2021-10-03 07:39:43 -05:00
|
|
|
insert_all(position, vec![elem.syntax_element()]);
|
2021-01-30 09:19:21 -06:00
|
|
|
}
|
2021-03-19 12:46:18 -05:00
|
|
|
pub fn insert_raw(position: Position, elem: impl Element) {
|
2021-10-03 07:39:43 -05:00
|
|
|
insert_all_raw(position, vec![elem.syntax_element()]);
|
2021-03-16 14:59:57 -05:00
|
|
|
}
|
|
|
|
pub fn insert_all(position: Position, mut elements: Vec<SyntaxElement>) {
|
2021-03-16 14:51:37 -05:00
|
|
|
if let Some(first) = elements.first() {
|
|
|
|
if let Some(ws) = ws_before(&position, first) {
|
2021-10-03 07:39:43 -05:00
|
|
|
elements.insert(0, ws.into());
|
2021-03-16 14:51:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(last) = elements.last() {
|
|
|
|
if let Some(ws) = ws_after(&position, last) {
|
2021-10-03 07:39:43 -05:00
|
|
|
elements.push(ws.into());
|
2021-03-16 14:51:37 -05:00
|
|
|
}
|
|
|
|
}
|
2021-10-03 07:39:43 -05:00
|
|
|
insert_all_raw(position, elements);
|
2021-03-16 14:51:37 -05:00
|
|
|
}
|
2021-03-16 14:59:57 -05:00
|
|
|
pub fn insert_all_raw(position: Position, elements: Vec<SyntaxElement>) {
|
2021-01-30 09:19:21 -06:00
|
|
|
let (parent, index) = match position.repr {
|
|
|
|
PositionRepr::FirstChild(parent) => (parent, 0),
|
|
|
|
PositionRepr::After(child) => (child.parent().unwrap(), child.index() + 1),
|
|
|
|
};
|
|
|
|
parent.splice_children(index..index, elements);
|
|
|
|
}
|
|
|
|
|
2021-03-19 12:46:18 -05:00
|
|
|
pub fn remove(elem: impl Element) {
|
2021-10-03 07:39:43 -05:00
|
|
|
elem.syntax_element().detach();
|
2021-01-30 09:19:21 -06:00
|
|
|
}
|
|
|
|
pub fn remove_all(range: RangeInclusive<SyntaxElement>) {
|
2021-10-03 07:39:43 -05:00
|
|
|
replace_all(range, Vec::new());
|
2021-01-30 09:19:21 -06:00
|
|
|
}
|
2021-03-18 04:57:55 -05:00
|
|
|
pub fn remove_all_iter(range: impl IntoIterator<Item = SyntaxElement>) {
|
|
|
|
let mut it = range.into_iter();
|
|
|
|
if let Some(mut first) = it.next() {
|
|
|
|
match it.last() {
|
|
|
|
Some(mut last) => {
|
|
|
|
if first.index() > last.index() {
|
2021-10-03 07:39:43 -05:00
|
|
|
mem::swap(&mut first, &mut last);
|
2021-03-18 04:57:55 -05:00
|
|
|
}
|
2021-10-03 07:39:43 -05:00
|
|
|
remove_all(first..=last);
|
2021-03-18 04:57:55 -05:00
|
|
|
}
|
|
|
|
None => remove(first),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-30 09:19:21 -06:00
|
|
|
|
2021-03-19 12:46:18 -05:00
|
|
|
pub fn replace(old: impl Element, new: impl Element) {
|
2021-10-03 07:39:43 -05:00
|
|
|
replace_with_many(old, vec![new.syntax_element()]);
|
2021-05-08 06:38:56 -05:00
|
|
|
}
|
|
|
|
pub fn replace_with_many(old: impl Element, new: Vec<SyntaxElement>) {
|
2021-03-19 12:46:18 -05:00
|
|
|
let old = old.syntax_element();
|
2021-10-03 07:39:43 -05:00
|
|
|
replace_all(old.clone()..=old, new);
|
2021-01-30 09:19:21 -06:00
|
|
|
}
|
|
|
|
pub fn replace_all(range: RangeInclusive<SyntaxElement>, new: Vec<SyntaxElement>) {
|
|
|
|
let start = range.start().index();
|
|
|
|
let end = range.end().index();
|
|
|
|
let parent = range.start().parent().unwrap();
|
2021-10-03 07:39:43 -05:00
|
|
|
parent.splice_children(start..end + 1, new);
|
2021-01-30 09:19:21 -06:00
|
|
|
}
|
|
|
|
|
2021-03-19 12:46:18 -05:00
|
|
|
pub fn append_child(node: &(impl Into<SyntaxNode> + Clone), child: impl Element) {
|
2021-01-30 09:19:21 -06:00
|
|
|
let position = Position::last_child_of(node);
|
2021-10-03 07:39:43 -05:00
|
|
|
insert(position, child);
|
2021-01-30 09:19:21 -06:00
|
|
|
}
|
2021-03-19 12:46:18 -05:00
|
|
|
pub fn append_child_raw(node: &(impl Into<SyntaxNode> + Clone), child: impl Element) {
|
2021-03-16 14:59:57 -05:00
|
|
|
let position = Position::last_child_of(node);
|
2021-10-03 07:39:43 -05:00
|
|
|
insert_raw(position, child);
|
2021-03-16 14:59:57 -05:00
|
|
|
}
|
2021-03-16 14:51:37 -05:00
|
|
|
|
|
|
|
fn ws_before(position: &Position, new: &SyntaxElement) -> Option<SyntaxToken> {
|
|
|
|
let prev = match &position.repr {
|
|
|
|
PositionRepr::FirstChild(_) => return None,
|
|
|
|
PositionRepr::After(it) => it,
|
|
|
|
};
|
2021-07-04 09:32:59 -05:00
|
|
|
|
|
|
|
if prev.kind() == T!['{'] && new.kind() == SyntaxKind::USE {
|
|
|
|
if let Some(item_list) = prev.parent().and_then(ast::ItemList::cast) {
|
|
|
|
let mut indent = IndentLevel::from_element(&item_list.syntax().clone().into());
|
|
|
|
indent.0 += 1;
|
|
|
|
return Some(make::tokens::whitespace(&format!("\n{}", indent)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-05 07:24:25 -05:00
|
|
|
if prev.kind() == T!['{'] && ast::Stmt::can_cast(new.kind()) {
|
2021-09-26 04:12:57 -05:00
|
|
|
if let Some(stmt_list) = prev.parent().and_then(ast::StmtList::cast) {
|
|
|
|
let mut indent = IndentLevel::from_element(&stmt_list.syntax().clone().into());
|
2021-07-05 07:24:25 -05:00
|
|
|
indent.0 += 1;
|
|
|
|
return Some(make::tokens::whitespace(&format!("\n{}", indent)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 14:51:37 -05:00
|
|
|
ws_between(prev, new)
|
|
|
|
}
|
|
|
|
fn ws_after(position: &Position, new: &SyntaxElement) -> Option<SyntaxToken> {
|
|
|
|
let next = match &position.repr {
|
|
|
|
PositionRepr::FirstChild(parent) => parent.first_child_or_token()?,
|
|
|
|
PositionRepr::After(sibling) => sibling.next_sibling_or_token()?,
|
|
|
|
};
|
|
|
|
ws_between(new, &next)
|
|
|
|
}
|
|
|
|
fn ws_between(left: &SyntaxElement, right: &SyntaxElement) -> Option<SyntaxToken> {
|
|
|
|
if left.kind() == SyntaxKind::WHITESPACE || right.kind() == SyntaxKind::WHITESPACE {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
if right.kind() == T![;] || right.kind() == T![,] {
|
|
|
|
return None;
|
|
|
|
}
|
2021-04-14 15:56:19 -05:00
|
|
|
if left.kind() == T![<] || right.kind() == T![>] {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
if left.kind() == T![&] && right.kind() == SyntaxKind::LIFETIME {
|
|
|
|
return None;
|
|
|
|
}
|
2021-05-18 06:42:41 -05:00
|
|
|
if right.kind() == SyntaxKind::GENERIC_ARG_LIST {
|
|
|
|
return None;
|
|
|
|
}
|
2021-04-14 15:56:19 -05:00
|
|
|
|
2021-03-18 04:57:55 -05:00
|
|
|
if right.kind() == SyntaxKind::USE {
|
2021-04-19 19:05:22 -05:00
|
|
|
let mut indent = IndentLevel::from_element(left);
|
|
|
|
if left.kind() == SyntaxKind::USE {
|
|
|
|
indent.0 = IndentLevel::from_element(right).0.max(indent.0);
|
|
|
|
}
|
2021-03-18 04:57:55 -05:00
|
|
|
return Some(make::tokens::whitespace(&format!("\n{}", indent)));
|
|
|
|
}
|
2021-03-16 19:27:56 -05:00
|
|
|
Some(make::tokens::single_space())
|
2021-03-16 14:51:37 -05:00
|
|
|
}
|