From 83d6be6cecb19659e289eba63f12ac33dceb3b56 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Feb 2019 15:43:45 +0300 Subject: keep-text --- crates/ra_mbe/src/syntax_bridge.rs | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 7a4ba9e93..24a043175 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -98,7 +98,7 @@ struct TtTokenSource { struct Tok { kind: SyntaxKind, is_joint_to_next: bool, - text: Option, + text: SmolStr, } impl TtTokenSource { @@ -123,27 +123,34 @@ impl TtTokenSource { tt::Leaf::Literal(l) => Tok { kind: SyntaxKind::INT_NUMBER, // FIXME is_joint_to_next: false, - text: Some(l.text.clone()), + text: l.text.clone(), }, tt::Leaf::Punct(p) => Tok { kind: SyntaxKind::from_char(p.char).unwrap(), is_joint_to_next: p.spacing == tt::Spacing::Joint, - text: None, + text: { + let mut buf = [0u8; 4]; + let s: &str = p.char.encode_utf8(&mut buf); + SmolStr::new(s) + }, }, tt::Leaf::Ident(ident) => { - Tok { kind: IDENT, is_joint_to_next: false, text: Some(ident.text.clone()) } + Tok { kind: IDENT, is_joint_to_next: false, text: ident.text.clone() } } }; self.tokens.push(tok) } fn push_delim(&mut self, d: tt::Delimiter, closing: bool) { - let kinds = match d { - tt::Delimiter::Parenthesis => [L_PAREN, R_PAREN], - tt::Delimiter::Brace => [L_CURLY, R_CURLY], - tt::Delimiter::Bracket => [L_BRACK, R_BRACK], + let (kinds, texts) = match d { + tt::Delimiter::Parenthesis => ([L_PAREN, R_PAREN], "()"), + tt::Delimiter::Brace => ([L_CURLY, R_CURLY], "{}"), + tt::Delimiter::Bracket => ([L_BRACK, R_BRACK], "[]"), tt::Delimiter::None => return, }; - let tok = Tok { kind: kinds[closing as usize], is_joint_to_next: false, text: None }; + let idx = closing as usize; + let kind = kinds[idx]; + let text = &texts[idx..texts.len() - (1 - idx)]; + let tok = Tok { kind, is_joint_to_next: false, text: SmolStr::new(text) }; self.tokens.push(tok) } } @@ -156,6 +163,6 @@ impl TokenSource for TtTokenSource { self.tokens[pos].is_joint_to_next } fn is_keyword(&self, pos: usize, kw: &str) -> bool { - self.tokens[pos].text.as_ref().map(|it| it.as_str()) == Some(kw) + self.tokens[pos].text == *kw } } -- cgit v1.2.3