diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-03-16 20:01:20 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2021-03-16 20:01:20 +0000 |
commit | 83e6940efb42675226adb8d2856c095b8dce36c5 (patch) | |
tree | 4720353e96dd2b7e200da91896dd7d155d661bd4 | |
parent | 2f9ce4f663b47c69fbf924f3300713773f5f4c10 (diff) | |
parent | 186a430853176f0ff5f69c4323bd12fb6f07d6ed (diff) |
Merge #8062
8062: pit-of-successify tree editor r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
-rw-r--r-- | crates/syntax/src/ast/edit_in_place.rs | 6 | ||||
-rw-r--r-- | crates/syntax/src/ted.rs | 25 |
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 { | |||
102 | fn create_where_clause(position: Position) { | 102 | fn 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 | ||
108 | impl ast::WhereClause { | 108 | impl 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. | ||
2 | use std::ops::RangeInclusive; | 5 | use std::ops::RangeInclusive; |
3 | 6 | ||
4 | use parser::T; | 7 | use parser::T; |
@@ -43,13 +46,13 @@ impl Position { | |||
43 | } | 46 | } |
44 | } | 47 | } |
45 | 48 | ||
46 | pub fn insert_ws(position: Position, elem: impl Into<SyntaxElement>) { | ||
47 | insert_all_ws(position, vec![elem.into()]) | ||
48 | } | ||
49 | pub fn insert(position: Position, elem: impl Into<SyntaxElement>) { | 49 | pub fn insert(position: Position, elem: impl Into<SyntaxElement>) { |
50 | insert_all(position, vec![elem.into()]) | 50 | insert_all(position, vec![elem.into()]) |
51 | } | 51 | } |
52 | pub fn insert_all_ws(position: Position, mut elements: Vec<SyntaxElement>) { | 52 | pub fn insert_raw(position: Position, elem: impl Into<SyntaxElement>) { |
53 | insert_all_raw(position, vec![elem.into()]) | ||
54 | } | ||
55 | pub 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 | } |
65 | pub fn insert_all(position: Position, elements: Vec<SyntaxElement>) { | 68 | pub 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 | ||
92 | pub 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 | } | ||
96 | pub fn append_child(node: impl Into<SyntaxNode>, child: impl Into<SyntaxElement>) { | 95 | pub 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 | } |
99 | pub 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 | ||
101 | fn ws_before(position: &Position, new: &SyntaxElement) -> Option<SyntaxToken> { | 104 | fn ws_before(position: &Position, new: &SyntaxElement) -> Option<SyntaxToken> { |
102 | let prev = match &position.repr { | 105 | let prev = match &position.repr { |