@@ -60,7 +60,7 @@ pub(crate) fn generate_documentation_template(
60
60
|builder| {
61
61
let mut doc_lines = Vec :: new ( ) ;
62
62
// Introduction / short function description before the sections
63
- doc_lines. push ( introduction_builder ( & ast_func) ) ;
63
+ doc_lines. push ( introduction_builder ( & ast_func, ctx ) ) ;
64
64
// Then come the sections
65
65
if let Some ( mut lines) = examples_builder ( & ast_func, ctx) {
66
66
doc_lines. push ( "" . into ( ) ) ;
@@ -78,18 +78,24 @@ pub(crate) fn generate_documentation_template(
78
78
}
79
79
80
80
/// Builds an introduction, trying to be smart if the function is `::new()`
81
- fn introduction_builder ( ast_func : & ast:: Fn ) -> String {
82
- let is_new = ast_func. name ( ) . map ( |name| & name. to_string ( ) == "new" ) . unwrap_or ( false ) ;
83
- if is_new {
84
- let ret_type = return_type ( ast_func) . map ( |ret_type| ret_type. to_string ( ) ) ;
85
- let self_type = self_type ( ast_func) ;
86
- if ret_type. as_deref ( ) == Some ( "Self" ) || ret_type == self_type {
87
- if let Some ( self_type) = self_type {
88
- return format ! ( "Creates a new [`{}`]." , self_type) ;
81
+ fn introduction_builder ( ast_func : & ast:: Fn , ctx : & AssistContext ) -> String {
82
+ || -> Option < String > {
83
+ let hir_func = ctx. sema . to_def ( ast_func) ?;
84
+ let container = hir_func. as_assoc_item ( ctx. db ( ) ) ?. container ( ctx. db ( ) ) ;
85
+ if let hir:: AssocItemContainer :: Impl ( implementation) = container {
86
+ let ret_ty = hir_func. ret_type ( ctx. db ( ) ) ;
87
+ let self_ty = implementation. self_ty ( ctx. db ( ) ) ;
88
+
89
+ let is_new = ast_func. name ( ) ?. to_string ( ) == "new" ;
90
+ match is_new && ret_ty == self_ty {
91
+ true => Some ( format ! ( "Creates a new [`{}`]." , self_type( ast_func) ?) ) ,
92
+ false => None ,
89
93
}
94
+ } else {
95
+ None
90
96
}
91
- }
92
- "." . into ( )
97
+ } ( )
98
+ . unwrap_or_else ( || "." . into ( ) )
93
99
}
94
100
95
101
/// Builds an `# Examples` section. An option is returned to be able to manage an error in the AST.
0 commit comments