@@ -8,6 +8,33 @@ use parser::T;
88
99use crate :: { ast:: make, SyntaxElement , SyntaxKind , SyntaxNode , SyntaxToken } ;
1010
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+
1138#[ derive( Debug ) ]
1239pub struct Position {
1340 repr : PositionRepr ,
@@ -20,24 +47,24 @@ enum PositionRepr {
2047}
2148
2249impl 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 ( ) ) ;
2552 Position { repr }
2653 }
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 ( ) ;
2956 let repr = match elem. prev_sibling_or_token ( ) {
3057 Some ( it) => PositionRepr :: After ( it) ,
3158 None => PositionRepr :: FirstChild ( elem. parent ( ) . unwrap ( ) ) ,
3259 } ;
3360 Position { repr }
3461 }
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 ( ) ) ;
3764 Position { repr }
3865 }
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 ( ) ;
4168 let repr = match node. last_child_or_token ( ) {
4269 Some ( it) => PositionRepr :: After ( it) ,
4370 None => PositionRepr :: FirstChild ( node) ,
@@ -46,11 +73,11 @@ impl Position {
4673 }
4774}
4875
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 ( ) ] )
5178}
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 ( ) ] )
5481}
5582pub fn insert_all ( position : Position , mut elements : Vec < SyntaxElement > ) {
5683 if let Some ( first) = elements. first ( ) {
@@ -73,17 +100,17 @@ pub fn insert_all_raw(position: Position, elements: Vec<SyntaxElement>) {
73100 parent. splice_children ( index..index, elements) ;
74101}
75102
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 ( ) ;
78105 remove_all ( elem. clone ( ) ..=elem)
79106}
80107pub fn remove_all ( range : RangeInclusive < SyntaxElement > ) {
81108 replace_all ( range, Vec :: new ( ) )
82109}
83110
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 ( ) ] )
87114}
88115pub fn replace_all ( range : RangeInclusive < SyntaxElement > , new : Vec < SyntaxElement > ) {
89116 let start = range. start ( ) . index ( ) ;
@@ -92,11 +119,11 @@ pub fn replace_all(range: RangeInclusive<SyntaxElement>, new: Vec<SyntaxElement>
92119 parent. splice_children ( start..end + 1 , new)
93120}
94121
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 ) {
96123 let position = Position :: last_child_of ( node) ;
97124 insert ( position, child)
98125}
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 ) {
100127 let position = Position :: last_child_of ( node) ;
101128 insert_raw ( position, child)
102129}
0 commit comments