mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 13:25:09 +00:00
Return a Ref from SubtreeTokenSource::get
This commit is contained in:
parent
e58baaa5a1
commit
f52437371f
1 changed files with 29 additions and 33 deletions
|
@ -1,6 +1,6 @@
|
||||||
use ra_parser::{Token, TokenSource};
|
use ra_parser::{Token, TokenSource};
|
||||||
use ra_syntax::{classify_literal, SmolStr, SyntaxKind, SyntaxKind::*, T};
|
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};
|
use tt::buffer::{Cursor, TokenBuffer};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||||
|
@ -20,9 +20,7 @@ impl<'a> SubtreeTokenSource<'a> {
|
||||||
// Helper function used in test
|
// Helper function used in test
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub fn text(&self) -> SmolStr {
|
pub fn text(&self) -> SmolStr {
|
||||||
let idx = self.get(self.curr.1);
|
match *self.get(self.curr.1) {
|
||||||
let cached = self.cached.borrow();
|
|
||||||
match cached[idx] {
|
|
||||||
Some(ref tt) => tt.text.clone(),
|
Some(ref tt) => tt.text.clone(),
|
||||||
_ => SmolStr::new(""),
|
_ => SmolStr::new(""),
|
||||||
}
|
}
|
||||||
|
@ -43,20 +41,19 @@ impl<'a> SubtreeTokenSource<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mk_token(&self, pos: usize) -> Token {
|
fn mk_token(&self, pos: usize) -> Token {
|
||||||
let idx = self.get(pos);
|
match *self.get(pos) {
|
||||||
let cached = self.cached.borrow();
|
|
||||||
match cached[idx] {
|
|
||||||
Some(ref tt) => Token { kind: tt.kind, is_jointed_to_next: tt.is_joint_to_next },
|
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 },
|
None => Token { kind: EOF, is_jointed_to_next: false },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(&self, pos: usize) -> usize {
|
fn get(&self, pos: usize) -> Ref<Option<TtToken>> {
|
||||||
let mut cached = self.cached.borrow_mut();
|
if pos < self.cached.borrow().len() {
|
||||||
if pos < cached.len() {
|
return Ref::map(self.cached.borrow(), |c| &c[pos]);
|
||||||
return pos;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut cached = self.cached.borrow_mut();
|
||||||
while pos >= cached.len() {
|
while pos >= cached.len() {
|
||||||
let cursor = self.cached_cursor.get();
|
let cursor = self.cached_cursor.get();
|
||||||
if cursor.eof() {
|
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?
|
/// Is the current token a specified keyword?
|
||||||
fn is_keyword(&self, kw: &str) -> bool {
|
fn is_keyword(&self, kw: &str) -> bool {
|
||||||
let idx = self.get(self.curr.1);
|
match *self.get(self.curr.1) {
|
||||||
let cached = self.cached.borrow();
|
|
||||||
match cached[idx] {
|
|
||||||
Some(ref t) => t.text == *kw,
|
Some(ref t) => t.text == *kw,
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue