From 99d91bc550d1c12e4e5e23d47b71338f5a62b902 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 7 Apr 2022 20:02:33 +0200 Subject: [PATCH] flyimport: omit types when completing where-clause --- .../src/completions/flyimport.rs | 11 +++++++++-- crates/ide_completion/src/tests/flyimport.rs | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/crates/ide_completion/src/completions/flyimport.rs b/crates/ide_completion/src/completions/flyimport.rs index 2154a1b3cb..fb857aeccd 100644 --- a/crates/ide_completion/src/completions/flyimport.rs +++ b/crates/ide_completion/src/completions/flyimport.rs @@ -1,5 +1,5 @@ //! See [`import_on_the_fly`]. -use hir::ItemInNs; +use hir::{ItemInNs, ModuleDef}; use ide_db::imports::{ import_assets::{ImportAssets, ImportCandidate, LocatedImport}, insert_use::ImportScope, @@ -9,6 +9,7 @@ use syntax::{AstNode, SyntaxNode, T}; use crate::{ context::{CompletionContext, PathKind}, + patterns::ImmediateLocation, render::{render_resolution_with_import, RenderContext}, }; @@ -170,7 +171,13 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext) (PathKind::Pat, ItemInNs::Types(_)) => true, (PathKind::Pat, ItemInNs::Values(def)) => matches!(def, hir::ModuleDef::Const(_)), - (PathKind::Type, ItemInNs::Types(_)) => true, + (PathKind::Type, ItemInNs::Types(ty)) => { + if matches!(ctx.completion_location, Some(ImmediateLocation::TypeBound)) { + matches!(ty, ModuleDef::Trait(_)) + } else { + true + } + } (PathKind::Type, ItemInNs::Values(_)) => false, (PathKind::Attr { .. }, ItemInNs::Macros(mac)) => mac.is_attr(ctx.db), diff --git a/crates/ide_completion/src/tests/flyimport.rs b/crates/ide_completion/src/tests/flyimport.rs index b47a710006..41e6cf7aee 100644 --- a/crates/ide_completion/src/tests/flyimport.rs +++ b/crates/ide_completion/src/tests/flyimport.rs @@ -1170,3 +1170,22 @@ struct Foo; "#, ); } + +#[test] +fn flyimport_in_type_bound_omits_types() { + check( + r#" +mod module { + pub struct CompletemeStruct; + pub type CompletemeType = (); + pub enum CompletemeEnum {} + pub trait CompletemeTrait {} +} + +fn f() where T: Comp$0 +"#, + expect![[r#" + tt CompletemeTrait (use module::CompletemeTrait) + "#]], + ); +}