1
1
use ra_parser:: { Token , TokenSource } ;
2
2
use ra_syntax:: { classify_literal, SmolStr , SyntaxKind , SyntaxKind :: * , T } ;
3
- use std:: cell:: { Cell , RefCell } ;
3
+ use std:: cell:: { Cell , Ref , RefCell } ;
4
4
use tt:: buffer:: { Cursor , TokenBuffer } ;
5
5
6
6
#[ derive( Debug , Clone , Eq , PartialEq ) ]
@@ -20,8 +20,8 @@ impl<'a> SubtreeTokenSource<'a> {
20
20
// Helper function used in test
21
21
#[ cfg( test) ]
22
22
pub fn text ( & self ) -> SmolStr {
23
- match self . get ( self . curr . 1 ) {
24
- Some ( tt) => tt. text ,
23
+ match * self . get ( self . curr . 1 ) {
24
+ Some ( ref tt) => tt. text . clone ( ) ,
25
25
_ => SmolStr :: new ( "" ) ,
26
26
}
27
27
}
@@ -41,44 +41,46 @@ impl<'a> SubtreeTokenSource<'a> {
41
41
}
42
42
43
43
fn mk_token ( & self , pos : usize ) -> Token {
44
- match self . get ( pos) {
45
- Some ( tt) => Token { kind : tt. kind , is_jointed_to_next : tt. is_joint_to_next } ,
44
+ match * self . get ( pos) {
45
+ Some ( ref tt) => Token { kind : tt. kind , is_jointed_to_next : tt. is_joint_to_next } ,
46
46
None => Token { kind : EOF , is_jointed_to_next : false } ,
47
47
}
48
48
}
49
49
50
- fn get ( & self , pos : usize ) -> Option < TtToken > {
51
- let mut cached = self . cached . borrow_mut ( ) ;
52
- if pos < cached. len ( ) {
53
- return cached[ pos] . clone ( ) ;
50
+ fn get ( & self , pos : usize ) -> Ref < Option < TtToken > > {
51
+ if pos < self . cached . borrow ( ) . len ( ) {
52
+ return Ref :: map ( self . cached . borrow ( ) , |c| & c[ pos] ) ;
54
53
}
55
54
56
- while pos >= cached. len ( ) {
57
- let cursor = self . cached_cursor . get ( ) ;
58
- if cursor. eof ( ) {
59
- cached. push ( None ) ;
60
- continue ;
61
- }
62
-
63
- match cursor. token_tree ( ) {
64
- Some ( tt:: TokenTree :: Leaf ( leaf) ) => {
65
- cached. push ( Some ( convert_leaf ( & leaf) ) ) ;
66
- self . cached_cursor . set ( cursor. bump ( ) ) ;
67
- }
68
- Some ( tt:: TokenTree :: Subtree ( subtree) ) => {
69
- self . cached_cursor . set ( cursor. subtree ( ) . unwrap ( ) ) ;
70
- cached. push ( Some ( convert_delim ( subtree. delimiter , false ) ) ) ;
55
+ {
56
+ let mut cached = self . cached . borrow_mut ( ) ;
57
+ while pos >= cached. len ( ) {
58
+ let cursor = self . cached_cursor . get ( ) ;
59
+ if cursor. eof ( ) {
60
+ cached. push ( None ) ;
61
+ continue ;
71
62
}
72
- None => {
73
- if let Some ( subtree) = cursor. end ( ) {
74
- cached. push ( Some ( convert_delim ( subtree. delimiter , true ) ) ) ;
63
+
64
+ match cursor. token_tree ( ) {
65
+ Some ( tt:: TokenTree :: Leaf ( leaf) ) => {
66
+ cached. push ( Some ( convert_leaf ( & leaf) ) ) ;
75
67
self . cached_cursor . set ( cursor. bump ( ) ) ;
76
68
}
69
+ Some ( tt:: TokenTree :: Subtree ( subtree) ) => {
70
+ self . cached_cursor . set ( cursor. subtree ( ) . unwrap ( ) ) ;
71
+ cached. push ( Some ( convert_delim ( subtree. delimiter , false ) ) ) ;
72
+ }
73
+ None => {
74
+ if let Some ( subtree) = cursor. end ( ) {
75
+ cached. push ( Some ( convert_delim ( subtree. delimiter , true ) ) ) ;
76
+ self . cached_cursor . set ( cursor. bump ( ) ) ;
77
+ }
78
+ }
77
79
}
78
80
}
79
81
}
80
82
81
- cached[ pos] . clone ( )
83
+ Ref :: map ( self . cached . borrow ( ) , |c| & c [ pos] )
82
84
}
83
85
}
84
86
@@ -103,8 +105,8 @@ impl<'a> TokenSource for SubtreeTokenSource<'a> {
103
105
104
106
/// Is the current token a specified keyword?
105
107
fn is_keyword ( & self , kw : & str ) -> bool {
106
- match self . get ( self . curr . 1 ) {
107
- Some ( t) => t. text == * kw,
108
+ match * self . get ( self . curr . 1 ) {
109
+ Some ( ref t) => t. text == * kw,
108
110
_ => false ,
109
111
}
110
112
}
0 commit comments