mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-26 08:44:35 +00:00
Return a Ref from SubtreeTokenSource::get
This commit is contained in:
parent
e58baaa5a1
commit
f52437371f
@ -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,20 +41,19 @@ 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<Option<TtToken>> {
|
||||
if pos < self.cached.borrow().len() {
|
||||
return Ref::map(self.cached.borrow(), |c| &c[pos]);
|
||||
}
|
||||
|
||||
{
|
||||
let mut cached = self.cached.borrow_mut();
|
||||
while pos >= cached.len() {
|
||||
let cursor = self.cached_cursor.get();
|
||||
if cursor.eof() {
|
||||
@ -81,8 +78,9 @@ impl<'a> SubtreeTokenSource<'a> {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user