From f52437371f9e82cc0ceb6c2f5e48e23d405faf5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Sat, 3 Aug 2019 22:49:47 +0300 Subject: Return a Ref from SubtreeTokenSource::get --- crates/ra_mbe/src/subtree_source.rs | 62 +++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 33 deletions(-) (limited to 'crates/ra_mbe') 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 @@ use ra_parser::{Token, TokenSource}; use ra_syntax::{classify_literal, SmolStr, SyntaxKind, SyntaxKind::*, T}; -use std::cell::{Cell, RefCell}; +use std::cell::{Cell, Ref, RefCell}; use tt::buffer::{Cursor, TokenBuffer}; #[derive(Debug, Clone, Eq, PartialEq)] @@ -20,9 +20,7 @@ impl<'a> SubtreeTokenSource<'a> { // Helper function used in test #[cfg(test)] pub fn text(&self) -> SmolStr { - let idx = self.get(self.curr.1); - let cached = self.cached.borrow(); - match cached[idx] { + match *self.get(self.curr.1) { Some(ref tt) => tt.text.clone(), _ => SmolStr::new(""), } @@ -43,46 +41,46 @@ impl<'a> SubtreeTokenSource<'a> { } fn mk_token(&self, pos: usize) -> Token { - let idx = self.get(pos); - let cached = self.cached.borrow(); - match cached[idx] { + match *self.get(pos) { Some(ref tt) => Token { kind: tt.kind, is_jointed_to_next: tt.is_joint_to_next }, None => Token { kind: EOF, is_jointed_to_next: false }, } } - fn get(&self, pos: usize) -> usize { - let mut cached = self.cached.borrow_mut(); - if pos < cached.len() { - return pos; + fn get(&self, pos: usize) -> Ref> { + if pos < self.cached.borrow().len() { + return Ref::map(self.cached.borrow(), |c| &c[pos]); } - while pos >= cached.len() { - let cursor = self.cached_cursor.get(); - if cursor.eof() { - cached.push(None); - continue; - } - - match cursor.token_tree() { - Some(tt::TokenTree::Leaf(leaf)) => { - cached.push(Some(convert_leaf(&leaf))); - self.cached_cursor.set(cursor.bump()); - } - Some(tt::TokenTree::Subtree(subtree)) => { - self.cached_cursor.set(cursor.subtree().unwrap()); - cached.push(Some(convert_delim(subtree.delimiter, false))); + { + let mut cached = self.cached.borrow_mut(); + while pos >= cached.len() { + let cursor = self.cached_cursor.get(); + if cursor.eof() { + cached.push(None); + continue; } - None => { - if let Some(subtree) = cursor.end() { - cached.push(Some(convert_delim(subtree.delimiter, true))); + + match cursor.token_tree() { + Some(tt::TokenTree::Leaf(leaf)) => { + cached.push(Some(convert_leaf(&leaf))); self.cached_cursor.set(cursor.bump()); } + Some(tt::TokenTree::Subtree(subtree)) => { + self.cached_cursor.set(cursor.subtree().unwrap()); + cached.push(Some(convert_delim(subtree.delimiter, false))); + } + None => { + if let Some(subtree) = cursor.end() { + cached.push(Some(convert_delim(subtree.delimiter, true))); + self.cached_cursor.set(cursor.bump()); + } + } } } } - pos + Ref::map(self.cached.borrow(), |c| &c[pos]) } } @@ -107,9 +105,7 @@ impl<'a> TokenSource for SubtreeTokenSource<'a> { /// Is the current token a specified keyword? fn is_keyword(&self, kw: &str) -> bool { - let idx = self.get(self.curr.1); - let cached = self.cached.borrow(); - match cached[idx] { + match *self.get(self.curr.1) { Some(ref t) => t.text == *kw, _ => false, } -- cgit v1.2.3