diff options
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r-- | crates/ra_syntax/src/ast/expr_extensions.rs | 19 |
1 files changed, 17 insertions, 2 deletions
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 { | |||
229 | 229 | ||
230 | impl ast::Literal { | 230 | impl ast::Literal { |
231 | pub fn token(&self) -> SyntaxToken { | 231 | pub fn token(&self) -> SyntaxToken { |
232 | match self.syntax().first_child_or_token().unwrap() { | 232 | let elem = self.syntax() |
233 | SyntaxElement::Token(token) => token, | 233 | .children_with_tokens() |
234 | .find(|e| e.kind() != ATTR && !e.kind().is_trivia()); | ||
235 | match elem { | ||
236 | Some(SyntaxElement::Token(token)) => token, | ||
234 | _ => unreachable!(), | 237 | _ => unreachable!(), |
235 | } | 238 | } |
236 | } | 239 | } |
@@ -268,6 +271,18 @@ impl ast::Literal { | |||
268 | } | 271 | } |
269 | } | 272 | } |
270 | 273 | ||
274 | #[test] | ||
275 | fn test_literal_with_attr() { | ||
276 | let parse = ast::SourceFile::parse(r#"const _: &str = { #[attr] "Hello" };"#); | ||
277 | let lit = parse | ||
278 | .tree | ||
279 | .syntax() | ||
280 | .descendants() | ||
281 | .find_map(ast::Literal::cast) | ||
282 | .unwrap(); | ||
283 | assert_eq!(lit.token().text(), r#""Hello""#); | ||
284 | } | ||
285 | |||
271 | impl ast::NamedField { | 286 | impl ast::NamedField { |
272 | pub fn parent_struct_lit(&self) -> &ast::StructLit { | 287 | pub fn parent_struct_lit(&self) -> &ast::StructLit { |
273 | self.syntax().ancestors().find_map(ast::StructLit::cast).unwrap() | 288 | self.syntax().ancestors().find_map(ast::StructLit::cast).unwrap() |