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.rs65
1 files changed, 46 insertions, 19 deletions
diff --git a/crates/syntax/src/ted.rs b/crates/syntax/src/ted.rs
index 442dfa14a..be2b846b1 100644
--- a/crates/syntax/src/ted.rs
+++ b/crates/syntax/src/ted.rs
@@ -8,6 +8,33 @@ use parser::T;
8 8
9use crate::{ast::make, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken}; 9use crate::{ast::make, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken};
10 10
11/// Utility trait to allow calling `ted` functions with references or owned
12/// nodes. Do not use outside of this module.
13pub trait Element {
14 fn syntax_element(self) -> SyntaxElement;
15}
16
17impl<E: Element + Clone> Element for &'_ E {
18 fn syntax_element(self) -> SyntaxElement {
19 self.clone().syntax_element()
20 }
21}
22impl Element for SyntaxElement {
23 fn syntax_element(self) -> SyntaxElement {
24 self
25 }
26}
27impl Element for SyntaxNode {
28 fn syntax_element(self) -> SyntaxElement {
29 self.into()
30 }
31}
32impl Element for SyntaxToken {
33 fn syntax_element(self) -> SyntaxElement {
34 self.into()
35 }
36}
37
11#[derive(Debug)] 38#[derive(Debug)]
12pub struct Position { 39pub struct Position {
13 repr: PositionRepr, 40 repr: PositionRepr,
@@ -20,24 +47,24 @@ enum PositionRepr {
20} 47}
21 48
22impl Position { 49impl Position {
23 pub fn after(elem: impl Into<SyntaxElement>) -> Position { 50 pub fn after(elem: impl Element) -> Position {
24 let repr = PositionRepr::After(elem.into()); 51 let repr = PositionRepr::After(elem.syntax_element());
25 Position { repr } 52 Position { repr }
26 } 53 }
27 pub fn before(elem: impl Into<SyntaxElement>) -> Position { 54 pub fn before(elem: impl Element) -> Position {
28 let elem = elem.into(); 55 let elem = elem.syntax_element();
29 let repr = match elem.prev_sibling_or_token() { 56 let repr = match elem.prev_sibling_or_token() {
30 Some(it) => PositionRepr::After(it), 57 Some(it) => PositionRepr::After(it),
31 None => PositionRepr::FirstChild(elem.parent().unwrap()), 58 None => PositionRepr::FirstChild(elem.parent().unwrap()),
32 }; 59 };
33 Position { repr } 60 Position { repr }
34 } 61 }
35 pub fn first_child_of(node: impl Into<SyntaxNode>) -> Position { 62 pub fn first_child_of(node: &(impl Into<SyntaxNode> + Clone)) -> Position {
36 let repr = PositionRepr::FirstChild(node.into()); 63 let repr = PositionRepr::FirstChild(node.clone().into());
37 Position { repr } 64 Position { repr }
38 } 65 }
39 pub fn last_child_of(node: impl Into<SyntaxNode>) -> Position { 66 pub fn last_child_of(node: &(impl Into<SyntaxNode> + Clone)) -> Position {
40 let node = node.into(); 67 let node = node.clone().into();
41 let repr = match node.last_child_or_token() { 68 let repr = match node.last_child_or_token() {
42 Some(it) => PositionRepr::After(it), 69 Some(it) => PositionRepr::After(it),
43 None => PositionRepr::FirstChild(node), 70 None => PositionRepr::FirstChild(node),
@@ -46,11 +73,11 @@ impl Position {
46 } 73 }
47} 74}
48 75
49pub fn insert(position: Position, elem: impl Into<SyntaxElement>) { 76pub fn insert(position: Position, elem: impl Element) {
50 insert_all(position, vec![elem.into()]) 77 insert_all(position, vec![elem.syntax_element()])
51} 78}
52pub fn insert_raw(position: Position, elem: impl Into<SyntaxElement>) { 79pub fn insert_raw(position: Position, elem: impl Element) {
53 insert_all_raw(position, vec![elem.into()]) 80 insert_all_raw(position, vec![elem.syntax_element()])
54} 81}
55pub fn insert_all(position: Position, mut elements: Vec<SyntaxElement>) { 82pub fn insert_all(position: Position, mut elements: Vec<SyntaxElement>) {
56 if let Some(first) = elements.first() { 83 if let Some(first) = elements.first() {
@@ -73,17 +100,17 @@ pub fn insert_all_raw(position: Position, elements: Vec<SyntaxElement>) {
73 parent.splice_children(index..index, elements); 100 parent.splice_children(index..index, elements);
74} 101}
75 102
76pub fn remove(elem: impl Into<SyntaxElement>) { 103pub fn remove(elem: impl Element) {
77 let elem = elem.into(); 104 let elem = elem.syntax_element();
78 remove_all(elem.clone()..=elem) 105 remove_all(elem.clone()..=elem)
79} 106}
80pub fn remove_all(range: RangeInclusive<SyntaxElement>) { 107pub fn remove_all(range: RangeInclusive<SyntaxElement>) {
81 replace_all(range, Vec::new()) 108 replace_all(range, Vec::new())
82} 109}
83 110
84pub fn replace(old: impl Into<SyntaxElement>, new: impl Into<SyntaxElement>) { 111pub fn replace(old: impl Element, new: impl Element) {
85 let old = old.into(); 112 let old = old.syntax_element();
86 replace_all(old.clone()..=old, vec![new.into()]) 113 replace_all(old.clone()..=old, vec![new.syntax_element()])
87} 114}
88pub fn replace_all(range: RangeInclusive<SyntaxElement>, new: Vec<SyntaxElement>) { 115pub fn replace_all(range: RangeInclusive<SyntaxElement>, new: Vec<SyntaxElement>) {
89 let start = range.start().index(); 116 let start = range.start().index();
@@ -92,11 +119,11 @@ pub fn replace_all(range: RangeInclusive<SyntaxElement>, new: Vec<SyntaxElement>
92 parent.splice_children(start..end + 1, new) 119 parent.splice_children(start..end + 1, new)
93} 120}
94 121
95pub fn append_child(node: impl Into<SyntaxNode>, child: impl Into<SyntaxElement>) { 122pub fn append_child(node: &(impl Into<SyntaxNode> + Clone), child: impl Element) {
96 let position = Position::last_child_of(node); 123 let position = Position::last_child_of(node);
97 insert(position, child) 124 insert(position, child)
98} 125}
99pub fn append_child_raw(node: impl Into<SyntaxNode>, child: impl Into<SyntaxElement>) { 126pub fn append_child_raw(node: &(impl Into<SyntaxNode> + Clone), child: impl Element) {
100 let position = Position::last_child_of(node); 127 let position = Position::last_child_of(node);
101 insert_raw(position, child) 128 insert_raw(position, child)
102} 129}