aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2021-03-16 19:59:57 +0000
committerAleksey Kladov <[email protected]>2021-03-16 19:59:57 +0000
commit186a430853176f0ff5f69c4323bd12fb6f07d6ed (patch)
treec7c2d2521f76f56f1e808edc8f88a2fd29dd166a /crates
parent34555593caeea25d460703e25c446b13132b1c5b (diff)
pit-of-successify tree editor
Diffstat (limited to 'crates')
-rw-r--r--crates/syntax/src/ast/edit_in_place.rs6
-rw-r--r--crates/syntax/src/ted.rs25
2 files changed, 17 insertions, 14 deletions
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 {
102fn create_where_clause(position: Position) { 102fn create_where_clause(position: Position) {
103 let where_clause: SyntaxElement = 103 let where_clause: SyntaxElement =
104 make::where_clause(empty()).clone_for_update().syntax().clone().into(); 104 make::where_clause(empty()).clone_for_update().syntax().clone().into();
105 ted::insert_ws(position, where_clause); 105 ted::insert(position, where_clause);
106} 106}
107 107
108impl ast::WhereClause { 108impl ast::WhereClause {
109 pub fn add_predicate(&self, predicate: ast::WherePred) { 109 pub fn add_predicate(&self, predicate: ast::WherePred) {
110 if let Some(pred) = self.predicates().last() { 110 if let Some(pred) = self.predicates().last() {
111 if !pred.syntax().siblings_with_tokens(Direction::Next).any(|it| it.kind() == T![,]) { 111 if !pred.syntax().siblings_with_tokens(Direction::Next).any(|it| it.kind() == T![,]) {
112 ted::append_child(self.syntax().clone(), make::token(T![,])); 112 ted::append_child_raw(self.syntax().clone(), make::token(T![,]));
113 } 113 }
114 } 114 }
115 ted::append_child_ws(self.syntax().clone(), predicate.syntax().clone()) 115 ted::append_child(self.syntax().clone(), predicate.syntax().clone())
116 } 116 }
117} 117}
118 118
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 @@
1//! Primitive tree editor, ed for trees 1//! Primitive tree editor, ed for trees.
2//!
3//! The `_raw`-suffixed functions insert elements as is, unsuffixed versions fix
4//! up elements around the edges.
2use std::ops::RangeInclusive; 5use std::ops::RangeInclusive;
3 6
4use parser::T; 7use parser::T;
@@ -43,13 +46,13 @@ impl Position {
43 } 46 }
44} 47}
45 48
46pub fn insert_ws(position: Position, elem: impl Into<SyntaxElement>) {
47 insert_all_ws(position, vec![elem.into()])
48}
49pub fn insert(position: Position, elem: impl Into<SyntaxElement>) { 49pub fn insert(position: Position, elem: impl Into<SyntaxElement>) {
50 insert_all(position, vec![elem.into()]) 50 insert_all(position, vec![elem.into()])
51} 51}
52pub fn insert_all_ws(position: Position, mut elements: Vec<SyntaxElement>) { 52pub fn insert_raw(position: Position, elem: impl Into<SyntaxElement>) {
53 insert_all_raw(position, vec![elem.into()])
54}
55pub fn insert_all(position: Position, mut elements: Vec<SyntaxElement>) {
53 if let Some(first) = elements.first() { 56 if let Some(first) = elements.first() {
54 if let Some(ws) = ws_before(&position, first) { 57 if let Some(ws) = ws_before(&position, first) {
55 elements.insert(0, ws.into()) 58 elements.insert(0, ws.into())
@@ -60,9 +63,9 @@ pub fn insert_all_ws(position: Position, mut elements: Vec<SyntaxElement>) {
60 elements.push(ws.into()) 63 elements.push(ws.into())
61 } 64 }
62 } 65 }
63 insert_all(position, elements) 66 insert_all_raw(position, elements)
64} 67}
65pub fn insert_all(position: Position, elements: Vec<SyntaxElement>) { 68pub fn insert_all_raw(position: Position, elements: Vec<SyntaxElement>) {
66 let (parent, index) = match position.repr { 69 let (parent, index) = match position.repr {
67 PositionRepr::FirstChild(parent) => (parent, 0), 70 PositionRepr::FirstChild(parent) => (parent, 0),
68 PositionRepr::After(child) => (child.parent().unwrap(), child.index() + 1), 71 PositionRepr::After(child) => (child.parent().unwrap(), child.index() + 1),
@@ -89,14 +92,14 @@ pub fn replace_all(range: RangeInclusive<SyntaxElement>, new: Vec<SyntaxElement>
89 parent.splice_children(start..end + 1, new) 92 parent.splice_children(start..end + 1, new)
90} 93}
91 94
92pub fn append_child_ws(node: impl Into<SyntaxNode>, child: impl Into<SyntaxElement>) {
93 let position = Position::last_child_of(node);
94 insert_ws(position, child)
95}
96pub fn append_child(node: impl Into<SyntaxNode>, child: impl Into<SyntaxElement>) { 95pub fn append_child(node: impl Into<SyntaxNode>, child: impl Into<SyntaxElement>) {
97 let position = Position::last_child_of(node); 96 let position = Position::last_child_of(node);
98 insert(position, child) 97 insert(position, child)
99} 98}
99pub fn append_child_raw(node: impl Into<SyntaxNode>, child: impl Into<SyntaxElement>) {
100 let position = Position::last_child_of(node);
101 insert_raw(position, child)
102}
100 103
101fn ws_before(position: &Position, new: &SyntaxElement) -> Option<SyntaxToken> { 104fn ws_before(position: &Position, new: &SyntaxElement) -> Option<SyntaxToken> {
102 let prev = match &position.repr { 105 let prev = match &position.repr {