1
1
use std:: iter:: once;
2
2
3
3
use hir:: {
4
- Adt , AsAssocItem , AssocItemContainer , FieldSource , HasSource , HirDisplay , ModuleDef ,
5
- ModuleSource , Semantics ,
4
+ Adt , AsAssocItem , AssocItemContainer , Documentation , FieldSource , HasSource , HirDisplay ,
5
+ ModuleDef , ModuleSource , Semantics ,
6
6
} ;
7
7
use itertools:: Itertools ;
8
8
use ra_db:: SourceDatabase ;
9
9
use ra_ide_db:: {
10
10
defs:: { classify_name, classify_name_ref, Definition } ,
11
11
RootDatabase ,
12
12
} ;
13
- use ra_syntax:: {
14
- ast:: { self , DocCommentsOwner } ,
15
- match_ast, AstNode ,
16
- SyntaxKind :: * ,
17
- SyntaxToken , TokenAtOffset ,
18
- } ;
13
+ use ra_syntax:: { ast, match_ast, AstNode , SyntaxKind :: * , SyntaxToken , TokenAtOffset } ;
19
14
20
15
use crate :: {
21
16
display:: { macro_label, rust_code_markup, rust_code_markup_with_doc, ShortLabel } ,
@@ -169,18 +164,14 @@ fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option<Strin
169
164
return match def {
170
165
Definition :: Macro ( it) => {
171
166
let src = it. source ( db) ;
172
- let doc_comment_text = src. value . doc_comment_text ( ) ;
173
- let doc_attr_text = expand_doc_attrs ( & src. value ) ;
174
- let docs = merge_doc_comments_and_attrs ( doc_comment_text, doc_attr_text) ;
167
+ let docs = Documentation :: from_ast ( & src. value ) . map ( Into :: into) ;
175
168
hover_text ( docs, Some ( macro_label ( & src. value ) ) , mod_path)
176
169
}
177
170
Definition :: Field ( it) => {
178
171
let src = it. source ( db) ;
179
172
match src. value {
180
173
FieldSource :: Named ( it) => {
181
- let doc_comment_text = it. doc_comment_text ( ) ;
182
- let doc_attr_text = expand_doc_attrs ( & it) ;
183
- let docs = merge_doc_comments_and_attrs ( doc_comment_text, doc_attr_text) ;
174
+ let docs = Documentation :: from_ast ( & it) . map ( Into :: into) ;
184
175
hover_text ( docs, it. short_label ( ) , mod_path)
185
176
}
186
177
_ => None ,
@@ -189,9 +180,7 @@ fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option<Strin
189
180
Definition :: ModuleDef ( it) => match it {
190
181
ModuleDef :: Module ( it) => match it. definition_source ( db) . value {
191
182
ModuleSource :: Module ( it) => {
192
- let doc_comment_text = it. doc_comment_text ( ) ;
193
- let doc_attr_text = expand_doc_attrs ( & it) ;
194
- let docs = merge_doc_comments_and_attrs ( doc_comment_text, doc_attr_text) ;
183
+ let docs = Documentation :: from_ast ( & it) . map ( Into :: into) ;
195
184
hover_text ( docs, it. short_label ( ) , mod_path)
196
185
}
197
186
_ => None ,
@@ -220,46 +209,11 @@ fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option<Strin
220
209
A : ast:: DocCommentsOwner + ast:: NameOwner + ShortLabel + ast:: AttrsOwner ,
221
210
{
222
211
let src = def. source ( db) ;
223
- let doc_comment_text = src. value . doc_comment_text ( ) ;
224
- let doc_attr_text = expand_doc_attrs ( & src. value ) ;
225
- let docs = merge_doc_comments_and_attrs ( doc_comment_text, doc_attr_text) ;
212
+ let docs = Documentation :: from_ast ( & src. value ) . map ( Into :: into) ;
226
213
hover_text ( docs, src. value . short_label ( ) , mod_path)
227
214
}
228
215
}
229
216
230
- fn merge_doc_comments_and_attrs (
231
- doc_comment_text : Option < String > ,
232
- doc_attr_text : Option < String > ,
233
- ) -> Option < String > {
234
- match ( doc_comment_text, doc_attr_text) {
235
- ( Some ( mut comment_text) , Some ( attr_text) ) => {
236
- comment_text. push_str ( "\n \n " ) ;
237
- comment_text. push_str ( & attr_text) ;
238
- Some ( comment_text)
239
- }
240
- ( Some ( comment_text) , None ) => Some ( comment_text) ,
241
- ( None , Some ( attr_text) ) => Some ( attr_text) ,
242
- ( None , None ) => None ,
243
- }
244
- }
245
-
246
- fn expand_doc_attrs ( owner : & dyn ast:: AttrsOwner ) -> Option < String > {
247
- let mut docs = String :: new ( ) ;
248
- for attr in owner. attrs ( ) {
249
- if let Some ( ( "doc" , value) ) =
250
- attr. as_simple_key_value ( ) . as_ref ( ) . map ( |( k, v) | ( k. as_str ( ) , v. as_str ( ) ) )
251
- {
252
- docs. push_str ( value) ;
253
- docs. push_str ( "\n \n " ) ;
254
- }
255
- }
256
- if docs. is_empty ( ) {
257
- None
258
- } else {
259
- Some ( docs. trim_end_matches ( "\n \n " ) . to_owned ( ) )
260
- }
261
- }
262
-
263
217
fn pick_best ( tokens : TokenAtOffset < SyntaxToken > ) -> Option < SyntaxToken > {
264
218
return tokens. max_by_key ( priority) ;
265
219
fn priority ( n : & SyntaxToken ) -> usize {
0 commit comments