Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 2d5d16f

Browse files
author
Jonas Schievink
committed
Remove ast::Literal::token
1 parent 7dfd1cb commit 2d5d16f

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

crates/ide-completion/src/completions/postfix.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod format_like;
55
use hir::{Documentation, HasAttrs};
66
use ide_db::{imports::insert_use::ImportScope, ty_filter::TryEnum, SnippetCap};
77
use syntax::{
8-
ast::{self, AstNode, AstToken},
8+
ast::{self, AstNode, LiteralKind},
99
SyntaxKind::{EXPR_STMT, STMT_LIST},
1010
TextRange, TextSize,
1111
};
@@ -191,7 +191,7 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
191191
}
192192

193193
if let ast::Expr::Literal(literal) = dot_receiver.clone() {
194-
if let Some(literal_text) = ast::String::cast(literal.token()) {
194+
if let LiteralKind::String(literal_text) = literal.kind() {
195195
add_format_like_completions(acc, ctx, &dot_receiver, cap, &literal_text);
196196
}
197197
}

crates/ide-completion/src/patterns.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ pub(crate) fn determine_location(
230230
let receiver = find_in_original_file(it.expr(), original_file);
231231
let receiver_is_ambiguous_float_literal = if let Some(ast::Expr::Literal(l)) = &receiver {
232232
match l.kind() {
233-
ast::LiteralKind::FloatNumber { .. } => l.token().text().ends_with('.'),
233+
ast::LiteralKind::FloatNumber { .. } => l.to_string().ends_with('.'),
234234
_ => false,
235235
}
236236
} else {

crates/syntax/src/ast/expr_ext.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
operators::{ArithOp, BinaryOp, CmpOp, LogicOp, Ordering, RangeOp, UnaryOp},
99
support, AstChildren, AstNode,
1010
},
11-
AstToken,
11+
AstToken, SyntaxElement,
1212
SyntaxKind::*,
1313
SyntaxNode, SyntaxToken, T,
1414
};
@@ -289,16 +289,17 @@ pub enum LiteralKind {
289289
}
290290

291291
impl ast::Literal {
292-
pub fn token(&self) -> SyntaxToken {
292+
pub fn value(&self) -> SyntaxElement {
293293
self.syntax()
294294
.children_with_tokens()
295295
.find(|e| e.kind() != ATTR && !e.kind().is_trivia())
296-
.and_then(|e| e.into_token())
297296
.unwrap()
298297
}
299-
300298
pub fn kind(&self) -> LiteralKind {
301-
let token = self.token();
299+
let token = match self.value() {
300+
rowan::NodeOrToken::Node(_node) => unreachable!(),
301+
rowan::NodeOrToken::Token(token) => token,
302+
};
302303

303304
if let Some(t) = ast::IntNumber::cast(token.clone()) {
304305
return LiteralKind::IntNumber(t);
@@ -364,7 +365,7 @@ impl ast::BlockExpr {
364365
fn test_literal_with_attr() {
365366
let parse = ast::SourceFile::parse(r#"const _: &str = { #[attr] "Hello" };"#);
366367
let lit = parse.tree().syntax().descendants().find_map(ast::Literal::cast).unwrap();
367-
assert_eq!(lit.token().text(), r#""Hello""#);
368+
assert_eq!(lit.value().to_string(), r#""Hello""#);
368369
}
369370

370371
impl ast::RecordExprField {

crates/syntax/src/validation.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,15 @@ fn validate_literal(literal: ast::Literal, acc: &mut Vec<SyntaxError>) {
119119
text.rfind(end_delimiter).and_then(|end| text.get(prefix_len..end))
120120
}
121121

122-
let token = literal.token();
123-
let text = token.text();
122+
let token = literal.value();
123+
let text;
124+
let text = match &token {
125+
rowan::NodeOrToken::Node(node) => {
126+
text = node.text().to_string();
127+
&*text
128+
}
129+
rowan::NodeOrToken::Token(token) => token.text(),
130+
};
124131

125132
// FIXME: lift this lambda refactor to `fn` (https://github.com/rust-analyzer/rust-analyzer/pull/2834#discussion_r366199205)
126133
let mut push_err = |prefix_len, (off, err): (usize, unescape::EscapeError)| {

0 commit comments

Comments
 (0)