Skip to content

Commit 7266fdb

Browse files
author
Côme ALLART
committed
refactor: use hir to compare returned and self types
1 parent 9c0f9d0 commit 7266fdb

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

crates/ide_assists/src/handlers/generate_documentation_template.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub(crate) fn generate_documentation_template(
6060
|builder| {
6161
let mut doc_lines = Vec::new();
6262
// Introduction / short function description before the sections
63-
doc_lines.push(introduction_builder(&ast_func));
63+
doc_lines.push(introduction_builder(&ast_func, ctx));
6464
// Then come the sections
6565
if let Some(mut lines) = examples_builder(&ast_func, ctx) {
6666
doc_lines.push("".into());
@@ -78,18 +78,24 @@ pub(crate) fn generate_documentation_template(
7878
}
7979

8080
/// 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,
8993
}
94+
} else {
95+
None
9096
}
91-
}
92-
".".into()
97+
}()
98+
.unwrap_or_else(|| ".".into())
9399
}
94100

95101
/// Builds an `# Examples` section. An option is returned to be able to manage an error in the AST.

0 commit comments

Comments
 (0)