From 186a430853176f0ff5f69c4323bd12fb6f07d6ed Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 16 Mar 2021 22:59:57 +0300 Subject: pit-of-successify tree editor --- crates/syntax/src/ast/edit_in_place.rs | 6 +++--- crates/syntax/src/ted.rs | 25 ++++++++++++++----------- 2 files changed, 17 insertions(+), 14 deletions(-) (limited to 'crates') diff --git a/crates/syntax/src/ast/edit_in_place.rs b/crates/syntax/src/ast/edit_in_place.rs index 7adfe5e16..449b058fb 100644 --- a/crates/syntax/src/ast/edit_in_place.rs +++ b/crates/syntax/src/ast/edit_in_place.rs @@ -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()) } } diff --git a/crates/syntax/src/ted.rs b/crates/syntax/src/ted.rs index f2166bbd3..76f950ef9 100644 --- a/crates/syntax/src/ted.rs +++ b/crates/syntax/src/ted.rs @@ -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) { - insert_all_ws(position, vec![elem.into()]) -} pub fn insert(position: Position, elem: impl Into) { insert_all(position, vec![elem.into()]) } -pub fn insert_all_ws(position: Position, mut elements: Vec) { +pub fn insert_raw(position: Position, elem: impl Into) { + insert_all_raw(position, vec![elem.into()]) +} +pub fn insert_all(position: Position, mut elements: Vec) { 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) { elements.push(ws.into()) } } - insert_all(position, elements) + insert_all_raw(position, elements) } -pub fn insert_all(position: Position, elements: Vec) { +pub fn insert_all_raw(position: Position, elements: Vec) { 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, new: Vec parent.splice_children(start..end + 1, new) } -pub fn append_child_ws(node: impl Into, child: impl Into) { - let position = Position::last_child_of(node); - insert_ws(position, child) -} pub fn append_child(node: impl Into, child: impl Into) { let position = Position::last_child_of(node); insert(position, child) } +pub fn append_child_raw(node: impl Into, child: impl Into) { + let position = Position::last_child_of(node); + insert_raw(position, child) +} fn ws_before(position: &Position, new: &SyntaxElement) -> Option { let prev = match &position.repr { -- cgit v1.2.3