From 1d74ef1d989b3d37f3f08d32e88670ee0f1f7ab6 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 8 Jun 2021 16:50:10 +0200 Subject: Don't complete values in type position --- crates/ide_completion/src/completions.rs | 48 +++++++++++++++----------------- 1 file changed, 22 insertions(+), 26 deletions(-) (limited to 'crates/ide_completion/src/completions.rs') diff --git a/crates/ide_completion/src/completions.rs b/crates/ide_completion/src/completions.rs index 7a4d71e91..e07a4c403 100644 --- a/crates/ide_completion/src/completions.rs +++ b/crates/ide_completion/src/completions.rs @@ -56,10 +56,16 @@ impl Builder { } impl Completions { - pub(crate) fn add(&mut self, item: CompletionItem) { + fn add(&mut self, item: CompletionItem) { self.buf.push(item) } + fn add_opt(&mut self, item: Option) { + if let Some(item) = item { + self.buf.push(item) + } + } + pub(crate) fn add_all(&mut self, items: I) where I: IntoIterator, @@ -103,9 +109,10 @@ impl Completions { local_name: hir::Name, resolution: &hir::ScopeDef, ) { - if let Some(item) = render_resolution(RenderContext::new(ctx), local_name, resolution) { - self.add(item); + if ctx.expects_type() && resolution.is_value_def() { + return; } + self.add_opt(render_resolution(RenderContext::new(ctx), local_name, resolution)); } pub(crate) fn add_macro( @@ -118,9 +125,7 @@ impl Completions { Some(it) => it, None => return, }; - if let Some(item) = render_macro(RenderContext::new(ctx), None, name, macro_) { - self.add(item); - } + self.add_opt(render_macro(RenderContext::new(ctx), None, name, macro_)); } pub(crate) fn add_function( @@ -129,9 +134,10 @@ impl Completions { func: hir::Function, local_name: Option, ) { - if let Some(item) = render_fn(RenderContext::new(ctx), None, local_name, func) { - self.add(item) + if ctx.expects_type() { + return; } + self.add_opt(render_fn(RenderContext::new(ctx), None, local_name, func)); } pub(crate) fn add_method( @@ -141,10 +147,7 @@ impl Completions { receiver: Option, local_name: Option, ) { - if let Some(item) = render_method(RenderContext::new(ctx), None, receiver, local_name, func) - { - self.add(item) - } + self.add_opt(render_method(RenderContext::new(ctx), None, receiver, local_name, func)); } pub(crate) fn add_variant_pat( @@ -153,9 +156,7 @@ impl Completions { variant: hir::Variant, local_name: Option, ) { - if let Some(item) = render_variant_pat(RenderContext::new(ctx), variant, local_name, None) { - self.add(item); - } + self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, local_name, None)); } pub(crate) fn add_qualified_variant_pat( @@ -164,9 +165,7 @@ impl Completions { variant: hir::Variant, path: hir::ModPath, ) { - if let Some(item) = render_variant_pat(RenderContext::new(ctx), variant, None, Some(path)) { - self.add(item); - } + self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, None, Some(path))); } pub(crate) fn add_struct_pat( @@ -175,21 +174,18 @@ impl Completions { strukt: hir::Struct, local_name: Option, ) { - if let Some(item) = render_struct_pat(RenderContext::new(ctx), strukt, local_name) { - self.add(item); - } + self.add_opt(render_struct_pat(RenderContext::new(ctx), strukt, local_name)); } pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) { - if let Some(item) = render_const(RenderContext::new(ctx), constant) { - self.add(item); + if ctx.expects_type() { + return; } + self.add_opt(render_const(RenderContext::new(ctx), constant)); } pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) { - if let Some(item) = render_type_alias(RenderContext::new(ctx), type_alias) { - self.add(item) - } + self.add_opt(render_type_alias(RenderContext::new(ctx), type_alias)); } pub(crate) fn add_qualified_enum_variant( -- cgit v1.2.3 From 1a26af15ef190bf605d3a04005aea02664815fb0 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 8 Jun 2021 20:27:25 +0200 Subject: Add tests checking no value completion in type pos --- crates/ide_completion/src/completions.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'crates/ide_completion/src/completions.rs') diff --git a/crates/ide_completion/src/completions.rs b/crates/ide_completion/src/completions.rs index e07a4c403..fbd499900 100644 --- a/crates/ide_completion/src/completions.rs +++ b/crates/ide_completion/src/completions.rs @@ -204,6 +204,9 @@ impl Completions { variant: hir::Variant, local_name: Option, ) { + if ctx.expects_type() { + return; + } let item = render_variant(RenderContext::new(ctx), None, local_name, variant, None); self.add(item); } -- cgit v1.2.3