@@ -5,11 +5,14 @@ use base_db::CrateId;
5
5
use cfg:: CfgExpr ;
6
6
use either:: Either ;
7
7
use intern:: { sym, 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 :: name:: Name ;
@@ -53,11 +56,15 @@ impl RawAttrs {
53
56
}
54
57
Either :: Right ( comment) => comment. doc_comment ( ) . map ( |doc| {
55
58
let span = span_map. span_for_range ( comment. syntax ( ) . text_range ( ) ) ;
59
+ let ( text, kind) =
60
+ desugar_doc_comment_text ( doc, DocCommentDesugarMode :: ProcMacro ) ;
56
61
Attr {
57
62
id,
58
63
input : Some ( Box :: new ( AttrInput :: Literal ( tt:: Literal {
59
- text : SmolStr :: new ( format_smolstr ! ( " \" {} \" " , Self :: escape_chars ( doc ) ) ) ,
64
+ text,
60
65
span,
66
+ kind,
67
+ suffix : None ,
61
68
} ) ) ) ,
62
69
path : Interned :: new ( ModPath :: from ( Name :: new_symbol (
63
70
sym:: doc. clone ( ) ,
@@ -78,10 +85,6 @@ impl RawAttrs {
78
85
RawAttrs { entries }
79
86
}
80
87
81
- fn escape_chars ( s : & str ) -> String {
82
- s. replace ( '\\' , r#"\\"# ) . replace ( '"' , r#"\""# )
83
- }
84
-
85
88
pub fn from_attrs_owner (
86
89
db : & dyn ExpandDatabase ,
87
90
owner : InFile < & dyn ast:: HasAttrs > ,
@@ -238,10 +241,8 @@ impl Attr {
238
241
} ) ?) ;
239
242
let span = span_map. span_for_range ( range) ;
240
243
let input = if let Some ( ast:: Expr :: Literal ( lit) ) = ast. expr ( ) {
241
- Some ( Box :: new ( AttrInput :: Literal ( tt:: Literal {
242
- text : lit. token ( ) . text ( ) . into ( ) ,
243
- span,
244
- } ) ) )
244
+ let token = lit. token ( ) ;
245
+ Some ( Box :: new ( AttrInput :: Literal ( token_to_literal ( token. text ( ) . into ( ) , span) ) ) )
245
246
} else if let Some ( tt) = ast. token_tree ( ) {
246
247
let tree = syntax_node_to_token_tree (
247
248
tt. syntax ( ) ,
@@ -310,12 +311,11 @@ impl Attr {
310
311
/// #[path = "string"]
311
312
pub fn string_value ( & self ) -> Option < & str > {
312
313
match self . input . as_deref ( ) ? {
313
- AttrInput :: Literal ( it) => match it. text . strip_prefix ( 'r' ) {
314
- Some ( it) => it. trim_matches ( '#' ) ,
315
- None => it. text . as_str ( ) ,
316
- }
317
- . strip_prefix ( '"' ) ?
318
- . strip_suffix ( '"' ) ,
314
+ AttrInput :: Literal ( tt:: Literal {
315
+ text,
316
+ kind : tt:: LitKind :: Str | tt:: LitKind :: StrRaw ( _) ,
317
+ ..
318
+ } ) => Some ( text) ,
319
319
_ => None ,
320
320
}
321
321
}
@@ -336,12 +336,10 @@ impl Attr {
336
336
337
337
pub fn string_value_unescape ( & self ) -> Option < Cow < ' _ , str > > {
338
338
match self . input . as_deref ( ) ? {
339
- AttrInput :: Literal ( it) => match it. text . strip_prefix ( 'r' ) {
340
- Some ( it) => {
341
- it. trim_matches ( '#' ) . strip_prefix ( '"' ) ?. strip_suffix ( '"' ) . map ( Cow :: Borrowed )
342
- }
343
- None => it. text . strip_prefix ( '"' ) ?. strip_suffix ( '"' ) . and_then ( unescape) ,
344
- } ,
339
+ AttrInput :: Literal ( tt:: Literal { text, kind : tt:: LitKind :: StrRaw ( _) , .. } ) => {
340
+ Some ( Cow :: Borrowed ( text) )
341
+ }
342
+ AttrInput :: Literal ( tt:: Literal { text, kind : tt:: LitKind :: Str , .. } ) => unescape ( text) ,
345
343
_ => None ,
346
344
}
347
345
}
0 commit comments