mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-08-19 01:50:32 +00:00
Remove ast::Literal::token
This commit is contained in:
parent
7dfd1cb572
commit
2d5d16f18c
4 changed files with 19 additions and 11 deletions
|
@ -8,7 +8,7 @@ use crate::{
|
|||
operators::{ArithOp, BinaryOp, CmpOp, LogicOp, Ordering, RangeOp, UnaryOp},
|
||||
support, AstChildren, AstNode,
|
||||
},
|
||||
AstToken,
|
||||
AstToken, SyntaxElement,
|
||||
SyntaxKind::*,
|
||||
SyntaxNode, SyntaxToken, T,
|
||||
};
|
||||
|
@ -289,16 +289,17 @@ pub enum LiteralKind {
|
|||
}
|
||||
|
||||
impl ast::Literal {
|
||||
pub fn token(&self) -> SyntaxToken {
|
||||
pub fn value(&self) -> SyntaxElement {
|
||||
self.syntax()
|
||||
.children_with_tokens()
|
||||
.find(|e| e.kind() != ATTR && !e.kind().is_trivia())
|
||||
.and_then(|e| e.into_token())
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub fn kind(&self) -> LiteralKind {
|
||||
let token = self.token();
|
||||
let token = match self.value() {
|
||||
rowan::NodeOrToken::Node(_node) => unreachable!(),
|
||||
rowan::NodeOrToken::Token(token) => token,
|
||||
};
|
||||
|
||||
if let Some(t) = ast::IntNumber::cast(token.clone()) {
|
||||
return LiteralKind::IntNumber(t);
|
||||
|
@ -364,7 +365,7 @@ impl ast::BlockExpr {
|
|||
fn test_literal_with_attr() {
|
||||
let parse = ast::SourceFile::parse(r#"const _: &str = { #[attr] "Hello" };"#);
|
||||
let lit = parse.tree().syntax().descendants().find_map(ast::Literal::cast).unwrap();
|
||||
assert_eq!(lit.token().text(), r#""Hello""#);
|
||||
assert_eq!(lit.value().to_string(), r#""Hello""#);
|
||||
}
|
||||
|
||||
impl ast::RecordExprField {
|
||||
|
|
|
@ -119,8 +119,15 @@ fn validate_literal(literal: ast::Literal, acc: &mut Vec<SyntaxError>) {
|
|||
text.rfind(end_delimiter).and_then(|end| text.get(prefix_len..end))
|
||||
}
|
||||
|
||||
let token = literal.token();
|
||||
let text = token.text();
|
||||
let token = literal.value();
|
||||
let text;
|
||||
let text = match &token {
|
||||
rowan::NodeOrToken::Node(node) => {
|
||||
text = node.text().to_string();
|
||||
&*text
|
||||
}
|
||||
rowan::NodeOrToken::Token(token) => token.text(),
|
||||
};
|
||||
|
||||
// FIXME: lift this lambda refactor to `fn` (https://github.com/rust-analyzer/rust-analyzer/pull/2834#discussion_r366199205)
|
||||
let mut push_err = |prefix_len, (off, err): (usize, unescape::EscapeError)| {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue