@@ -5,11 +5,14 @@ use base_db::CrateId;
5
5
use cfg:: CfgExpr ;
6
6
use either:: Either ;
7
7
use intern:: Interned ;
8
- use mbe:: { syntax_node_to_token_tree, DelimiterKind , DocCommentDesugarMode , Punct } ;
8
+ use mbe:: {
9
+ desugar_doc_comment_text, syntax_node_to_token_tree, token_to_literal, DelimiterKind ,
10
+ DocCommentDesugarMode , Punct ,
11
+ } ;
9
12
use smallvec:: { smallvec, SmallVec } ;
10
13
use span:: { Span , SyntaxContextId } ;
11
14
use syntax:: unescape;
12
- use syntax:: { ast, format_smolstr , match_ast, AstNode , AstToken , SmolStr , SyntaxNode } ;
15
+ use syntax:: { ast, match_ast, AstNode , AstToken , SyntaxNode } ;
13
16
use triomphe:: ThinArc ;
14
17
15
18
use crate :: {
@@ -52,11 +55,15 @@ impl RawAttrs {
52
55
}
53
56
Either :: Right ( comment) => comment. doc_comment ( ) . map ( |doc| {
54
57
let span = span_map. span_for_range ( comment. syntax ( ) . text_range ( ) ) ;
58
+ let ( text, kind) =
59
+ desugar_doc_comment_text ( doc, DocCommentDesugarMode :: ProcMacro ) ;
55
60
Attr {
56
61
id,
57
62
input : Some ( Box :: new ( AttrInput :: Literal ( tt:: Literal {
58
- text : SmolStr :: new ( format_smolstr ! ( " \" {} \" " , Self :: escape_chars ( doc ) ) ) ,
63
+ text,
59
64
span,
65
+ kind,
66
+ suffix : None ,
60
67
} ) ) ) ,
61
68
path : Interned :: new ( ModPath :: from ( crate :: name!( doc) ) ) ,
62
69
ctxt : span. ctx ,
@@ -74,10 +81,6 @@ impl RawAttrs {
74
81
RawAttrs { entries }
75
82
}
76
83
77
- fn escape_chars ( s : & str ) -> String {
78
- s. replace ( '\\' , r#"\\"# ) . replace ( '"' , r#"\""# )
79
- }
80
-
81
84
pub fn from_attrs_owner (
82
85
db : & dyn ExpandDatabase ,
83
86
owner : InFile < & dyn ast:: HasAttrs > ,
@@ -234,10 +237,8 @@ impl Attr {
234
237
} ) ?) ;
235
238
let span = span_map. span_for_range ( range) ;
236
239
let input = if let Some ( ast:: Expr :: Literal ( lit) ) = ast. expr ( ) {
237
- Some ( Box :: new ( AttrInput :: Literal ( tt:: Literal {
238
- text : lit. token ( ) . text ( ) . into ( ) ,
239
- span,
240
- } ) ) )
240
+ let token = lit. token ( ) ;
241
+ Some ( Box :: new ( AttrInput :: Literal ( token_to_literal ( token. text ( ) . into ( ) , span) ) ) )
241
242
} else if let Some ( tt) = ast. token_tree ( ) {
242
243
let tree = syntax_node_to_token_tree (
243
244
tt. syntax ( ) ,
@@ -306,24 +307,21 @@ impl Attr {
306
307
/// #[path = "string"]
307
308
pub fn string_value ( & self ) -> Option < & str > {
308
309
match self . input . as_deref ( ) ? {
309
- AttrInput :: Literal ( it) => match it. text . strip_prefix ( 'r' ) {
310
- Some ( it) => it. trim_matches ( '#' ) ,
311
- None => it. text . as_str ( ) ,
312
- }
313
- . strip_prefix ( '"' ) ?
314
- . strip_suffix ( '"' ) ,
310
+ AttrInput :: Literal ( tt:: Literal {
311
+ text,
312
+ kind : tt:: LitKind :: Str | tt:: LitKind :: StrRaw ( _) ,
313
+ ..
314
+ } ) => Some ( text) ,
315
315
_ => None ,
316
316
}
317
317
}
318
318
319
319
pub fn string_value_unescape ( & self ) -> Option < Cow < ' _ , str > > {
320
320
match self . input . as_deref ( ) ? {
321
- AttrInput :: Literal ( it) => match it. text . strip_prefix ( 'r' ) {
322
- Some ( it) => {
323
- it. trim_matches ( '#' ) . strip_prefix ( '"' ) ?. strip_suffix ( '"' ) . map ( Cow :: Borrowed )
324
- }
325
- None => it. text . strip_prefix ( '"' ) ?. strip_suffix ( '"' ) . and_then ( unescape) ,
326
- } ,
321
+ AttrInput :: Literal ( tt:: Literal { text, kind : tt:: LitKind :: StrRaw ( _) , .. } ) => {
322
+ Some ( Cow :: Borrowed ( text) )
323
+ }
324
+ AttrInput :: Literal ( tt:: Literal { text, kind : tt:: LitKind :: Str , .. } ) => unescape ( text) ,
327
325
_ => None ,
328
326
}
329
327
}
0 commit comments