aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_mbe/src/subtree_source.rs62
1 files changed, 32 insertions, 30 deletions
diff --git a/crates/ra_mbe/src/subtree_source.rs b/crates/ra_mbe/src/subtree_source.rs
index 6603ff34d..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,8 +20,8 @@ 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 match self.get(self.curr.1) { 23 match *self.get(self.curr.1) {
24 Some(tt) => tt.text, 24 Some(ref tt) => tt.text.clone(),
25 _ => SmolStr::new(""), 25 _ => SmolStr::new(""),
26 } 26 }
27 } 27 }
@@ -41,44 +41,46 @@ impl<'a> SubtreeTokenSource<'a> {
41 } 41 }
42 42
43 fn mk_token(&self, pos: usize) -> Token { 43 fn mk_token(&self, pos: usize) -> Token {
44 match self.get(pos) { 44 match *self.get(pos) {
45 Some(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 },
46 None => Token { kind: EOF, is_jointed_to_next: false }, 46 None => Token { kind: EOF, is_jointed_to_next: false },
47 } 47 }
48 } 48 }
49 49
50 fn get(&self, pos: usize) -> Option<TtToken> { 50 fn get(&self, pos: usize) -> Ref<Option<TtToken>> {
51 let mut cached = self.cached.borrow_mut(); 51 if pos < self.cached.borrow().len() {
52 if pos < cached.len() { 52 return Ref::map(self.cached.borrow(), |c| &c[pos]);
53 return cached[pos].clone();
54 } 53 }
55 54
56 while pos >= cached.len() { 55 {
57 let cursor = self.cached_cursor.get(); 56 let mut cached = self.cached.borrow_mut();
58 if cursor.eof() { 57 while pos >= cached.len() {
59 cached.push(None); 58 let cursor = self.cached_cursor.get();
60 continue; 59 if cursor.eof() {
61 } 60 cached.push(None);
62 61 continue;
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)));
71 } 62 }
72 None => { 63
73 if let Some(subtree) = cursor.end() { 64 match cursor.token_tree() {
74 cached.push(Some(convert_delim(subtree.delimiter, true))); 65 Some(tt::TokenTree::Leaf(leaf)) => {
66 cached.push(Some(convert_leaf(&leaf)));
75 self.cached_cursor.set(cursor.bump()); 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 /// Is the current token a specified keyword? 106 /// Is the current token a specified keyword?
105 fn is_keyword(&self, kw: &str) -> bool { 107 fn is_keyword(&self, kw: &str) -> bool {
106 match self.get(self.curr.1) { 108 match *self.get(self.curr.1) {
107 Some(t) => t.text == *kw, 109 Some(ref t) => t.text == *kw,
108 _ => false, 110 _ => false,
109 } 111 }
110 } 112 }