@@ -29,6 +29,13 @@ impl Documentation {
29
29
Documentation ( s. into ( ) )
30
30
}
31
31
32
+ pub fn from_ast < N > ( node : & N ) -> Option < Documentation >
33
+ where
34
+ N : ast:: DocCommentsOwner + ast:: AttrsOwner ,
35
+ {
36
+ docs_from_ast ( node)
37
+ }
38
+
32
39
pub fn as_str ( & self ) -> & str {
33
40
& * self . 0
34
41
}
@@ -70,6 +77,45 @@ impl Documentation {
70
77
}
71
78
}
72
79
73
- pub ( crate ) fn docs_from_ast ( node : & impl ast:: DocCommentsOwner ) -> Option < Documentation > {
74
- node. doc_comment_text ( ) . map ( |it| Documentation :: new ( & it) )
80
+ pub ( crate ) fn docs_from_ast < N > ( node : & N ) -> Option < Documentation >
81
+ where
82
+ N : ast:: DocCommentsOwner + ast:: AttrsOwner ,
83
+ {
84
+ let doc_comment_text = node. doc_comment_text ( ) ;
85
+ let doc_attr_text = expand_doc_attrs ( node) ;
86
+ let docs = merge_doc_comments_and_attrs ( doc_comment_text, doc_attr_text) ;
87
+ docs. map ( |it| Documentation :: new ( & it) )
88
+ }
89
+
90
+ fn merge_doc_comments_and_attrs (
91
+ doc_comment_text : Option < String > ,
92
+ doc_attr_text : Option < String > ,
93
+ ) -> Option < String > {
94
+ match ( doc_comment_text, doc_attr_text) {
95
+ ( Some ( mut comment_text) , Some ( attr_text) ) => {
96
+ comment_text. push_str ( "\n \n " ) ;
97
+ comment_text. push_str ( & attr_text) ;
98
+ Some ( comment_text)
99
+ }
100
+ ( Some ( comment_text) , None ) => Some ( comment_text) ,
101
+ ( None , Some ( attr_text) ) => Some ( attr_text) ,
102
+ ( None , None ) => None ,
103
+ }
104
+ }
105
+
106
+ fn expand_doc_attrs ( owner : & dyn ast:: AttrsOwner ) -> Option < String > {
107
+ let mut docs = String :: new ( ) ;
108
+ for attr in owner. attrs ( ) {
109
+ if let Some ( ( "doc" , value) ) =
110
+ attr. as_simple_key_value ( ) . as_ref ( ) . map ( |( k, v) | ( k. as_str ( ) , v. as_str ( ) ) )
111
+ {
112
+ docs. push_str ( value) ;
113
+ docs. push_str ( "\n \n " ) ;
114
+ }
115
+ }
116
+ if docs. is_empty ( ) {
117
+ None
118
+ } else {
119
+ Some ( docs. trim_end_matches ( "\n \n " ) . to_owned ( ) )
120
+ }
75
121
}
0 commit comments