aboutsummaryrefslogtreecommitdiff
path: root/crates/syntax/src/ted.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/syntax/src/ted.rs')
-rw-r--r--crates/syntax/src/ted.rs25
1 files changed, 14 insertions, 11 deletions
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 {