aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src
diff options
context:
space:
mode:
authorLaurenČ›iu Nicola <[email protected]>2019-08-03 20:49:47 +0100
committerLaurenČ›iu Nicola <[email protected]>2019-08-03 20:49:47 +0100
commitf52437371f9e82cc0ceb6c2f5e48e23d405faf5e (patch)
tree32f1da901791a86911259af1d64e21a9f90a5e13 /crates/ra_mbe/src
parente58baaa5a15375d84b2734c9f7fc529200b8713a (diff)
Return a Ref from SubtreeTokenSource::get
Diffstat (limited to 'crates/ra_mbe/src')
-rw-r--r--crates/ra_mbe/src/subtree_source.rs62
1 files changed, 29 insertions, 33 deletions
diff --git a/crates/ra_mbe/src/subtree_source.rs b/crates/ra_mbe/src/subtree_source.rs
index 31e0df3ec..9d6d0133f 100644
--- a/crates/ra_mbe/src/subtree_source.rs
+++ b/crates/ra_mbe/src/subtree_source.rs
@@ -1,6 +1,6 @@
1use ra_parser::{Token, TokenSource}; 1use ra_parser::{Token, TokenSource};
2use ra_syntax::{classify_literal, SmolStr, SyntaxKind, SyntaxKind::*, T}; 2use ra_syntax::{classify_literal, SmolStr, SyntaxKind, SyntaxKind::*, T};
3use std::cell::{Cell, RefCell}; 3use std::cell::{Cell, Ref, RefCell};
4use tt::buffer::{Cursor, TokenBuffer}; 4use tt::buffer::{Cursor, TokenBuffer};
5 5
6#[derive(Debug, Clone, Eq, PartialEq)] 6#[derive(Debug, Clone, Eq, PartialEq)]
@@ -20,9 +20,7 @@ impl<'a> SubtreeTokenSource<'a> {
20 // Helper function used in test 20 // Helper function used in test
21 #[cfg(test)] 21 #[cfg(test)]
22 pub fn text(&self) -> SmolStr { 22 pub fn text(&self) -> SmolStr {
23 let idx = self.get(self.curr.1); 23 match *self.get(self.curr.1) {
24 let cached = self.cached.borrow();
25 match cached[idx] {
26 Some(ref tt) => tt.text.clone(), 24 Some(ref tt) => tt.text.clone(),
27 _ => SmolStr::new(""), 25 _ => SmolStr::new(""),
28 } 26 }
@@ -43,46 +41,46 @@ impl<'a> SubtreeTokenSource<'a> {
43 } 41 }
44 42
45 fn mk_token(&self, pos: usize) -> Token { 43 fn mk_token(&self, pos: usize) -> Token {
46 let idx = self.get(pos); 44 match *self.get(pos) {
47 let cached = self.cached.borrow();
48 match cached[idx] {
49 Some(ref tt) => Token { kind: tt.kind, is_jointed_to_next: tt.is_joint_to_next }, 45 Some(ref tt) => Token { kind: tt.kind, is_jointed_to_next: tt.is_joint_to_next },
50 None => Token { kind: EOF, is_jointed_to_next: false }, 46 None => Token { kind: EOF, is_jointed_to_next: false },
51 } 47 }
52 } 48 }
53 49
54 fn get(&self, pos: usize) -> usize { 50 fn get(&self, pos: usize) -> Ref<Option<TtToken>> {
55 let mut cached = self.cached.borrow_mut(); 51 if pos < self.cached.borrow().len() {
56 if pos < cached.len() { 52 return Ref::map(self.cached.borrow(), |c| &c[pos]);
57 return pos;
58 } 53 }
59 54
60 while pos >= cached.len() { 55 {
61 let cursor = self.cached_cursor.get(); 56 let mut cached = self.cached.borrow_mut();
62 if cursor.eof() { 57 while pos >= cached.len() {
63 cached.push(None); 58 let cursor = self.cached_cursor.get();
64 continue; 59 if cursor.eof() {
65 } 60 cached.push(None);
66 61 continue;
67 match cursor.token_tree() {
68 Some(tt::TokenTree::Leaf(leaf)) => {
69 cached.push(Some(convert_leaf(&leaf)));
70 self.cached_cursor.set(cursor.bump());
71 }
72 Some(tt::TokenTree::Subtree(subtree)) => {
73 self.cached_cursor.set(cursor.subtree().unwrap());
74 cached.push(Some(convert_delim(subtree.delimiter, false)));
75 } 62 }
76 None => { 63
77 if let Some(subtree) = cursor.end() { 64 match cursor.token_tree() {
78 cached.push(Some(convert_delim(subtree.delimiter, true))); 65 Some(tt::TokenTree::Leaf(leaf)) => {
66 cached.push(Some(convert_leaf(&leaf)));
79 self.cached_cursor.set(cursor.bump()); 67 self.cached_cursor.set(cursor.bump());
80 } 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 }
81 } 79 }
82 } 80 }
83 } 81 }
84 82
85 pos 83 Ref::map(self.cached.borrow(), |c| &c[pos])
86 } 84 }
87} 85}
88 86
@@ -107,9 +105,7 @@ impl<'a> TokenSource for SubtreeTokenSource<'a> {
107 105
108 /// Is the current token a specified keyword? 106 /// Is the current token a specified keyword?
109 fn is_keyword(&self, kw: &str) -> bool { 107 fn is_keyword(&self, kw: &str) -> bool {
110 let idx = self.get(self.curr.1); 108 match *self.get(self.curr.1) {
111 let cached = self.cached.borrow();
112 match cached[idx] {
113 Some(ref t) => t.text == *kw, 109 Some(ref t) => t.text == *kw,
114 _ => false, 110 _ => false,
115 } 111 }