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/ra_syntax') 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 From 774537fb01a8839d3cb933017067541bd0fd542b Mon Sep 17 00:00:00 2001 From: Shotaro Yamada Date: Sun, 16 Jun 2019 01:45:38 +0900 Subject: Fix formatting --- crates/ra_syntax/src/ast/expr_extensions.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs index 66fe77a19..d88671d45 100644 --- a/crates/ra_syntax/src/ast/expr_extensions.rs +++ b/crates/ra_syntax/src/ast/expr_extensions.rs @@ -229,7 +229,8 @@ pub enum LiteralKind { impl ast::Literal { pub fn token(&self) -> SyntaxToken { - let elem = self.syntax() + let elem = self + .syntax() .children_with_tokens() .find(|e| e.kind() != ATTR && !e.kind().is_trivia()); match elem { @@ -274,12 +275,7 @@ 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(); + let lit = parse.tree.syntax().descendants().find_map(ast::Literal::cast).unwrap(); assert_eq!(lit.token().text(), r#""Hello""#); } -- cgit v1.2.3