pit-of-successify tree editor

This commit is contained in:
Aleksey Kladov 2021-03-16 22:59:57 +03:00
parent 34555593ca
commit 186a430853
2 changed files with 17 additions and 14 deletions

View file

@ -102,17 +102,17 @@ impl GenericParamsOwnerEdit for ast::Enum {
fn create_where_clause(position: Position) {
let where_clause: SyntaxElement =
make::where_clause(empty()).clone_for_update().syntax().clone().into();
ted::insert_ws(position, where_clause);
ted::insert(position, where_clause);
}
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![,]) {
ted::append_child(self.syntax().clone(), make::token(T![,]));
ted::append_child_raw(self.syntax().clone(), make::token(T![,]));
}
}
ted::append_child_ws(self.syntax().clone(), predicate.syntax().clone())
ted::append_child(self.syntax().clone(), predicate.syntax().clone())
}
}

View file

@ -1,4 +1,7 @@
//! Primitive tree editor, ed for trees
//! Primitive tree editor, ed for trees.
//!
//! The `_raw`-suffixed functions insert elements as is, unsuffixed versions fix
//! up elements around the edges.
use std::ops::RangeInclusive;
use parser::T;
@ -43,13 +46,13 @@ impl Position {
}
}
pub fn insert_ws(position: Position, elem: impl Into<SyntaxElement>) {
insert_all_ws(position, vec![elem.into()])
}
pub fn insert(position: Position, elem: impl Into<SyntaxElement>) {
insert_all(position, vec![elem.into()])
}
pub fn insert_all_ws(position: Position, mut elements: Vec<SyntaxElement>) {
pub fn insert_raw(position: Position, elem: impl Into<SyntaxElement>) {
insert_all_raw(position, vec![elem.into()])
}
pub fn insert_all(position: Position, mut elements: Vec<SyntaxElement>) {
if let Some(first) = elements.first() {
if let Some(ws) = ws_before(&position, first) {
elements.insert(0, ws.into())
@ -60,9 +63,9 @@ pub fn insert_all_ws(position: Position, mut elements: Vec<SyntaxElement>) {
elements.push(ws.into())
}
}
insert_all(position, elements)
insert_all_raw(position, elements)
}
pub fn insert_all(position: Position, elements: Vec<SyntaxElement>) {
pub fn insert_all_raw(position: Position, elements: Vec<SyntaxElement>) {
let (parent, index) = match position.repr {
PositionRepr::FirstChild(parent) => (parent, 0),
PositionRepr::After(child) => (child.parent().unwrap(), child.index() + 1),
@ -89,14 +92,14 @@ pub fn replace_all(range: RangeInclusive<SyntaxElement>, new: Vec<SyntaxElement>
parent.splice_children(start..end + 1, new)
}
pub fn append_child_ws(node: impl Into<SyntaxNode>, child: impl Into<SyntaxElement>) {
let position = Position::last_child_of(node);
insert_ws(position, child)
}
pub fn append_child(node: impl Into<SyntaxNode>, child: impl Into<SyntaxElement>) {
let position = Position::last_child_of(node);
insert(position, child)
}
pub fn append_child_raw(node: impl Into<SyntaxNode>, child: impl Into<SyntaxElement>) {
let position = Position::last_child_of(node);
insert_raw(position, child)
}
fn ws_before(position: &Position, new: &SyntaxElement) -> Option<SyntaxToken> {
let prev = match &position.repr {