]> git.lizzy.rs Git - rust.git/commitdiff
pit-of-successify tree editor
authorAleksey Kladov <aleksey.kladov@gmail.com>
Tue, 16 Mar 2021 19:59:57 +0000 (22:59 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Tue, 16 Mar 2021 19:59:57 +0000 (22:59 +0300)
crates/syntax/src/ast/edit_in_place.rs
crates/syntax/src/ted.rs

index 7adfe5e162077659538951190a138bc7122f4880..449b058fbc9becb38324077d3f1b97293a2e8079 100644 (file)
@@ -102,17 +102,17 @@ fn get_or_create_where_clause(&self) -> WhereClause {
 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())
     }
 }
 
index f2166bbd3bb64643e8b0729f5babdd7e81114f09..76f950ef9b62a0917ae86a39f179a94a300b56d6 100644 (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 @@ pub fn last_child_of(node: impl Into<SyntaxNode>) -> 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 {