From 6f51f728a114078a0c3a029fc66cfb8c4daf9a28 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 1 Dec 2020 13:53:12 +0300 Subject: Type-safer API for dealing with parameter lists with optional self --- crates/completion/src/completions/trait_impl.rs | 2 +- crates/completion/src/render/function.rs | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) (limited to 'crates/completion') diff --git a/crates/completion/src/completions/trait_impl.rs b/crates/completion/src/completions/trait_impl.rs index a14be9c73..e2fe44aff 100644 --- a/crates/completion/src/completions/trait_impl.rs +++ b/crates/completion/src/completions/trait_impl.rs @@ -139,7 +139,7 @@ fn add_function_impl( ) { let fn_name = func.name(ctx.db).to_string(); - let label = if func.params(ctx.db).is_empty() { + let label = if func.assoc_fn_params(ctx.db).is_empty() { format!("fn {}()", fn_name) } else { format!("fn {}(..)", fn_name) diff --git a/crates/completion/src/render/function.rs b/crates/completion/src/render/function.rs index 542383d7e..09d9f85bc 100644 --- a/crates/completion/src/render/function.rs +++ b/crates/completion/src/render/function.rs @@ -22,7 +22,7 @@ pub(crate) fn render_fn<'a>( struct FunctionRender<'a> { ctx: RenderContext<'a>, name: String, - fn_: hir::Function, + func: hir::Function, ast_node: Fn, } @@ -35,15 +35,15 @@ impl<'a> FunctionRender<'a> { let name = local_name.unwrap_or_else(|| fn_.name(ctx.db()).to_string()); let ast_node = fn_.source(ctx.db()).value; - FunctionRender { ctx, name, fn_, ast_node } + FunctionRender { ctx, name, func: fn_, ast_node } } fn render(self, import_to_add: Option) -> CompletionItem { let params = self.params(); CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), self.name.clone()) .kind(self.kind()) - .set_documentation(self.ctx.docs(self.fn_)) - .set_deprecated(self.ctx.is_deprecated(self.fn_)) + .set_documentation(self.ctx.docs(self.func)) + .set_deprecated(self.ctx.is_deprecated(self.func)) .detail(self.detail()) .add_call_parens(self.ctx.completion, self.name, params) .add_import(import_to_add) @@ -67,7 +67,11 @@ impl<'a> FunctionRender<'a> { } fn params(&self) -> Params { - let params_ty = self.fn_.params(self.ctx.db()); + let params_ty = if self.ctx.completion.dot_receiver.is_some() { + self.func.method_params(self.ctx.db()).unwrap_or_default() + } else { + self.func.assoc_fn_params(self.ctx.db()) + }; let params = self .ast_node .param_list() @@ -87,7 +91,7 @@ impl<'a> FunctionRender<'a> { } fn kind(&self) -> CompletionItemKind { - if self.fn_.self_param(self.ctx.db()).is_some() { + if self.func.self_param(self.ctx.db()).is_some() { CompletionItemKind::Method } else { CompletionItemKind::Function -- cgit v1.2.3