From 474a04615cf6883de24c4b8ffeae15d8540ba9f5 Mon Sep 17 00:00:00 2001 From: Shotaro Yamada Date: Sat, 15 Jun 2019 22:22:31 +0900 Subject: Skip attrs in `Literal::token` --- crates/ra_syntax/src/ast/expr_extensions.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs index 17763809d..66fe77a19 100644 --- a/crates/ra_syntax/src/ast/expr_extensions.rs +++ b/crates/ra_syntax/src/ast/expr_extensions.rs @@ -229,8 +229,11 @@ pub enum LiteralKind { impl ast::Literal { pub fn token(&self) -> SyntaxToken { - match self.syntax().first_child_or_token().unwrap() { - SyntaxElement::Token(token) => token, + let elem = self.syntax() + .children_with_tokens() + .find(|e| e.kind() != ATTR && !e.kind().is_trivia()); + match elem { + Some(SyntaxElement::Token(token)) => token, _ => unreachable!(), } } @@ -268,6 +271,18 @@ impl ast::Literal { } } +#[test] +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""#); +} + impl ast::NamedField { pub fn parent_struct_lit(&self) -> &ast::StructLit { self.syntax().ancestors().find_map(ast::StructLit::cast).unwrap() -- cgit v1.2.3