File tree Expand file tree Collapse file tree 1 file changed +13
-2
lines changed Expand file tree Collapse file tree 1 file changed +13
-2
lines changed Original file line number Diff line number Diff line change @@ -229,8 +229,12 @@ pub enum LiteralKind {
229
229
230
230
impl ast:: Literal {
231
231
pub fn token ( & self ) -> SyntaxToken {
232
- match self . syntax ( ) . first_child_or_token ( ) . unwrap ( ) {
233
- SyntaxElement :: Token ( token) => token,
232
+ let elem = self
233
+ . syntax ( )
234
+ . children_with_tokens ( )
235
+ . find ( |e| e. kind ( ) != ATTR && !e. kind ( ) . is_trivia ( ) ) ;
236
+ match elem {
237
+ Some ( SyntaxElement :: Token ( token) ) => token,
234
238
_ => unreachable ! ( ) ,
235
239
}
236
240
}
@@ -268,6 +272,13 @@ impl ast::Literal {
268
272
}
269
273
}
270
274
275
+ #[ test]
276
+ fn test_literal_with_attr ( ) {
277
+ let parse = ast:: SourceFile :: parse ( r#"const _: &str = { #[attr] "Hello" };"# ) ;
278
+ let lit = parse. tree . syntax ( ) . descendants ( ) . find_map ( ast:: Literal :: cast) . unwrap ( ) ;
279
+ assert_eq ! ( lit. token( ) . text( ) , r#""Hello""# ) ;
280
+ }
281
+
271
282
impl ast:: NamedField {
272
283
pub fn parent_struct_lit ( & self ) -> & ast:: StructLit {
273
284
self . syntax ( ) . ancestors ( ) . find_map ( ast:: StructLit :: cast) . unwrap ( )
You can’t perform that action at this time.
0 commit comments