From 7266fdb5a400170563622da48ec1b2d1b524c7c3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=B4me=20ALLART?= Date: Sat, 11 Dec 2021 20:33:08 +0100 Subject: [PATCH] refactor: use hir to compare returned and self types --- .../generate_documentation_template.rs | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/crates/ide_assists/src/handlers/generate_documentation_template.rs b/crates/ide_assists/src/handlers/generate_documentation_template.rs index 113fe7c92c5..5205a8ac7ed 100644 --- a/crates/ide_assists/src/handlers/generate_documentation_template.rs +++ b/crates/ide_assists/src/handlers/generate_documentation_template.rs @@ -60,7 +60,7 @@ pub(crate) fn generate_documentation_template( |builder| { let mut doc_lines = Vec::new(); // Introduction / short function description before the sections - doc_lines.push(introduction_builder(&ast_func)); + doc_lines.push(introduction_builder(&ast_func, ctx)); // Then come the sections if let Some(mut lines) = examples_builder(&ast_func, ctx) { doc_lines.push("".into()); @@ -78,18 +78,24 @@ pub(crate) fn generate_documentation_template( } /// Builds an introduction, trying to be smart if the function is `::new()` -fn introduction_builder(ast_func: &ast::Fn) -> String { - let is_new = ast_func.name().map(|name| &name.to_string() == "new").unwrap_or(false); - if is_new { - let ret_type = return_type(ast_func).map(|ret_type| ret_type.to_string()); - let self_type = self_type(ast_func); - if ret_type.as_deref() == Some("Self") || ret_type == self_type { - if let Some(self_type) = self_type { - return format!("Creates a new [`{}`].", self_type); +fn introduction_builder(ast_func: &ast::Fn, ctx: &AssistContext) -> String { + || -> Option { + let hir_func = ctx.sema.to_def(ast_func)?; + let container = hir_func.as_assoc_item(ctx.db())?.container(ctx.db()); + if let hir::AssocItemContainer::Impl(implementation) = container { + let ret_ty = hir_func.ret_type(ctx.db()); + let self_ty = implementation.self_ty(ctx.db()); + + let is_new = ast_func.name()?.to_string() == "new"; + match is_new && ret_ty == self_ty { + true => Some(format!("Creates a new [`{}`].", self_type(ast_func)?)), + false => None, } + } else { + None } - } - ".".into() + }() + .unwrap_or_else(|| ".".into()) } /// Builds an `# Examples` section. An option is returned to be able to manage an error in the AST. -- 2.44.0