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