From 53bb46fa853bee99f673a0ed0a53798c46847d99 Mon Sep 17 00:00:00 2001 From: Josh Mcguigan Date: Sat, 6 Mar 2021 16:56:07 -0800 Subject: show function params in completion detail --- crates/ide_completion/src/render/function.rs | 31 ++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) (limited to 'crates/ide_completion/src/render') diff --git a/crates/ide_completion/src/render/function.rs b/crates/ide_completion/src/render/function.rs index f4dabe3d1..e154d6302 100644 --- a/crates/ide_completion/src/render/function.rs +++ b/crates/ide_completion/src/render/function.rs @@ -2,6 +2,7 @@ use hir::{HasSource, HirDisplay, Type}; use ide_db::SymbolKind; +use itertools::Itertools; use syntax::ast::Fn; use crate::{ @@ -59,8 +60,34 @@ impl<'a> FunctionRender<'a> { } fn detail(&self) -> String { - let ty = self.func.ret_type(self.ctx.db()); - format!("-> {}", ty.display(self.ctx.db())) + let params = if let Some(self_param) = self.func.self_param(self.ctx.db()) { + let params = self + .func + .assoc_fn_params(self.ctx.db()) + .into_iter() + .skip(1) // skip the self param because we are manually handling that + .map(|p| p.ty().display(self.ctx.db()).to_string()); + + std::iter::once(self_param.display(self.ctx.db()).to_owned()).chain(params).join(", ") + } else { + let params = self + .func + .assoc_fn_params(self.ctx.db()) + .into_iter() + .map(|p| p.ty().display(self.ctx.db()).to_string()) + .join(", "); + params + }; + + let ret_ty = self.func.ret_type(self.ctx.db()); + let ret = if ret_ty.is_unit() { + // Omit the `-> ()` for unit return types + String::new() + } else { + format!(" -> {}", ret_ty.display(self.ctx.db())) + }; + + format!("fn({}){}", params, ret) } fn add_arg(&self, arg: &str, ty: &Type) -> String { -- cgit v1.2.3 From d5f0f58e6330efd8200f93a6904caafc600a904e Mon Sep 17 00:00:00 2001 From: Josh Mcguigan Date: Fri, 12 Mar 2021 13:46:40 -0800 Subject: add params_display and ty_display --- crates/ide_completion/src/render/function.rs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'crates/ide_completion/src/render') diff --git a/crates/ide_completion/src/render/function.rs b/crates/ide_completion/src/render/function.rs index e154d6302..b6b67e7a7 100644 --- a/crates/ide_completion/src/render/function.rs +++ b/crates/ide_completion/src/render/function.rs @@ -60,7 +60,19 @@ impl<'a> FunctionRender<'a> { } fn detail(&self) -> String { - let params = if let Some(self_param) = self.func.self_param(self.ctx.db()) { + let ret_ty = self.func.ret_type(self.ctx.db()); + let ret = if ret_ty.is_unit() { + // Omit the return type if it is the unit type + String::new() + } else { + format!(" {}", self.ty_display()) + }; + + format!("fn({}){}", self.params_display(), ret) + } + + fn params_display(&self) -> String { + if let Some(self_param) = self.func.self_param(self.ctx.db()) { let params = self .func .assoc_fn_params(self.ctx.db()) @@ -77,17 +89,13 @@ impl<'a> FunctionRender<'a> { .map(|p| p.ty().display(self.ctx.db()).to_string()) .join(", "); params - }; + } + } + fn ty_display(&self) -> String { let ret_ty = self.func.ret_type(self.ctx.db()); - let ret = if ret_ty.is_unit() { - // Omit the `-> ()` for unit return types - String::new() - } else { - format!(" -> {}", ret_ty.display(self.ctx.db())) - }; - format!("fn({}){}", params, ret) + format!("-> {}", ret_ty.display(self.ctx.db())) } fn add_arg(&self, arg: &str, ty: &Type) -> String { -- cgit v1.2.3