diff --git a/crates/hir-def/src/item_scope.rs b/crates/hir-def/src/item_scope.rs index a04f12cab765..d9ece7885c41 100644 --- a/crates/hir-def/src/item_scope.rs +++ b/crates/hir-def/src/item_scope.rs @@ -342,6 +342,13 @@ impl ItemScope { .chain(self.unnamed_trait_imports.keys().copied()) } + pub fn trait_by_name(&self, name: &Name) -> Option { + self.types.get(name).and_then(|(def, _, _)| match def { + ModuleDefId::TraitId(it) => Some(*it), + _ => None, + }) + } + pub(crate) fn resolutions(&self) -> impl Iterator, PerNs)> + '_ { self.entries().map(|(name, res)| (Some(name.clone()), res)).chain( self.unnamed_trait_imports.iter().map(|(tr, (vis, i))| { diff --git a/crates/hir-ty/src/method_resolution.rs b/crates/hir-ty/src/method_resolution.rs index 5a72b97653db..5331599953b9 100644 --- a/crates/hir-ty/src/method_resolution.rs +++ b/crates/hir-ty/src/method_resolution.rs @@ -914,7 +914,7 @@ pub fn iterate_path_candidates( traits_in_scope: &FxHashSet, visible_from_module: VisibleFromModule, name: Option<&Name>, - callback: &mut dyn FnMut(AssocItemId) -> ControlFlow<()>, + callback: &mut dyn MethodCandidateCallback, ) -> ControlFlow<()> { iterate_method_candidates_dyn( ty, @@ -925,7 +925,7 @@ pub fn iterate_path_candidates( name, LookupMode::Path, // the adjustments are not relevant for path lookup - &mut |_, id, _| callback(id), + callback, ) } @@ -937,7 +937,7 @@ pub fn iterate_method_candidates_dyn( visible_from_module: VisibleFromModule, name: Option<&Name>, mode: LookupMode, - callback: &mut dyn FnMut(ReceiverAdjustments, AssocItemId, bool) -> ControlFlow<()>, + callback: &mut dyn MethodCandidateCallback, ) -> ControlFlow<()> { let _p = tracing::info_span!( "iterate_method_candidates_dyn", @@ -1007,7 +1007,7 @@ fn iterate_method_candidates_with_autoref( traits_in_scope: &FxHashSet, visible_from_module: VisibleFromModule, name: Option<&Name>, - mut callback: &mut dyn FnMut(ReceiverAdjustments, AssocItemId, bool) -> ControlFlow<()>, + callback: &mut dyn MethodCandidateCallback, ) -> ControlFlow<()> { if receiver_ty.value.is_general_var(Interner, &receiver_ty.binders) { // don't try to resolve methods on unknown types @@ -1022,7 +1022,7 @@ fn iterate_method_candidates_with_autoref( traits_in_scope, visible_from_module, name, - &mut callback, + callback, ) }; @@ -1052,6 +1052,45 @@ fn iterate_method_candidates_with_autoref( iterate_method_candidates_by_receiver(ref_muted, first_adjustment.with_autoref(Mutability::Mut)) } +pub trait MethodCandidateCallback { + fn on_inherent_method( + &mut self, + adjustments: ReceiverAdjustments, + item: AssocItemId, + is_visible: bool, + ) -> ControlFlow<()>; + + fn on_trait_method( + &mut self, + adjustments: ReceiverAdjustments, + item: AssocItemId, + is_visible: bool, + ) -> ControlFlow<()>; +} + +impl MethodCandidateCallback for F +where + F: FnMut(ReceiverAdjustments, AssocItemId, bool) -> ControlFlow<()>, +{ + fn on_inherent_method( + &mut self, + adjustments: ReceiverAdjustments, + item: AssocItemId, + is_visible: bool, + ) -> ControlFlow<()> { + self(adjustments, item, is_visible) + } + + fn on_trait_method( + &mut self, + adjustments: ReceiverAdjustments, + item: AssocItemId, + is_visible: bool, + ) -> ControlFlow<()> { + self(adjustments, item, is_visible) + } +} + #[tracing::instrument(skip_all, fields(name = ?name))] fn iterate_method_candidates_by_receiver( table: &mut InferenceTable<'_>, @@ -1060,7 +1099,7 @@ fn iterate_method_candidates_by_receiver( traits_in_scope: &FxHashSet, visible_from_module: VisibleFromModule, name: Option<&Name>, - mut callback: &mut dyn FnMut(ReceiverAdjustments, AssocItemId, bool) -> ControlFlow<()>, + callback: &mut dyn MethodCandidateCallback, ) -> ControlFlow<()> { let receiver_ty = table.instantiate_canonical(receiver_ty); // We're looking for methods with *receiver* type receiver_ty. These could @@ -1076,7 +1115,9 @@ fn iterate_method_candidates_by_receiver( Some(&receiver_ty), Some(receiver_adjustments.clone()), visible_from_module, - &mut callback, + &mut |adjustments, item, is_visible| { + callback.on_inherent_method(adjustments, item, is_visible) + }, )? } ControlFlow::Continue(()) @@ -1096,7 +1137,9 @@ fn iterate_method_candidates_by_receiver( name, Some(&receiver_ty), Some(receiver_adjustments.clone()), - &mut callback, + &mut |adjustments, item, is_visible| { + callback.on_trait_method(adjustments, item, is_visible) + }, )? } ControlFlow::Continue(()) @@ -1111,7 +1154,7 @@ fn iterate_method_candidates_for_self_ty( traits_in_scope: &FxHashSet, visible_from_module: VisibleFromModule, name: Option<&Name>, - mut callback: &mut dyn FnMut(ReceiverAdjustments, AssocItemId, bool) -> ControlFlow<()>, + callback: &mut dyn MethodCandidateCallback, ) -> ControlFlow<()> { let mut table = InferenceTable::new(db, env); let self_ty = table.instantiate_canonical(self_ty.clone()); @@ -1122,7 +1165,9 @@ fn iterate_method_candidates_for_self_ty( None, None, visible_from_module, - &mut callback, + &mut |adjustments, item, is_visible| { + callback.on_inherent_method(adjustments, item, is_visible) + }, )?; iterate_trait_method_candidates( &self_ty, @@ -1131,7 +1176,9 @@ fn iterate_method_candidates_for_self_ty( name, None, None, - callback, + &mut |adjustments, item, is_visible| { + callback.on_trait_method(adjustments, item, is_visible) + }, ) } diff --git a/crates/hir/src/attrs.rs b/crates/hir/src/attrs.rs index af60c233e551..a23fdf1b3934 100644 --- a/crates/hir/src/attrs.rs +++ b/crates/hir/src/attrs.rs @@ -258,7 +258,7 @@ fn resolve_impl_trait_item( &traits_in_scope, method_resolution::VisibleFromModule::None, Some(name), - &mut |assoc_item_id| { + &mut |_, assoc_item_id: AssocItemId, _| { // If two traits in scope define the same item, Rustdoc links to no specific trait (for // instance, given two methods `a`, Rustdoc simply links to `method.a` with no // disambiguation) so we just pick the first one we find as well. diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 30e023e1a472..7d87cea8367e 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -4925,28 +4925,49 @@ impl Type { traits_in_scope: &FxHashSet, with_local_impls: Option, name: Option<&Name>, - mut callback: impl FnMut(Function) -> Option, + callback: impl FnMut(Function) -> Option, ) -> Option { + struct Callback { + f: F, + slot: Option, + } + impl MethodCandidateCallback for &'_ mut Callback + where + F: FnMut(Function) -> Option, + { + fn on_inherent_method(&mut self, f: Function) -> ControlFlow<()> { + match (self.f)(f) { + it @ Some(_) => { + self.slot = it; + ControlFlow::Break(()) + } + None => ControlFlow::Continue(()), + } + } + + fn on_trait_method(&mut self, f: Function) -> ControlFlow<()> { + match (self.f)(f) { + it @ Some(_) => { + self.slot = it; + ControlFlow::Break(()) + } + None => ControlFlow::Continue(()), + } + } + } + let _p = tracing::info_span!("iterate_method_candidates_with_traits").entered(); - let mut slot = None; + let mut callback = Callback { slot: None, f: callback }; - self.iterate_method_candidates_dyn( + self.iterate_method_candidates_split_inherent( db, scope, traits_in_scope, with_local_impls, name, - &mut |assoc_item_id| { - if let AssocItemId::FunctionId(func) = assoc_item_id { - if let Some(res) = callback(func.into()) { - slot = Some(res); - return ControlFlow::Break(()); - } - } - ControlFlow::Continue(()) - }, + &mut callback, ); - slot + callback.slot } pub fn iterate_method_candidates( @@ -4967,15 +4988,49 @@ impl Type { ) } - fn iterate_method_candidates_dyn( + /// Allows you to treat inherent and non-inherent methods differently. + /// + /// Note that inherent methods may actually be trait methods! For example, in `dyn Trait`, the trait's methods + /// are considered inherent methods. + pub fn iterate_method_candidates_split_inherent( &self, db: &dyn HirDatabase, scope: &SemanticsScope<'_>, traits_in_scope: &FxHashSet, with_local_impls: Option, name: Option<&Name>, - callback: &mut dyn FnMut(AssocItemId) -> ControlFlow<()>, + callback: impl MethodCandidateCallback, ) { + struct Callback(T); + + impl method_resolution::MethodCandidateCallback for Callback { + fn on_inherent_method( + &mut self, + _adjustments: method_resolution::ReceiverAdjustments, + item: AssocItemId, + _is_visible: bool, + ) -> ControlFlow<()> { + if let AssocItemId::FunctionId(func) = item { + self.0.on_inherent_method(func.into()) + } else { + ControlFlow::Continue(()) + } + } + + fn on_trait_method( + &mut self, + _adjustments: method_resolution::ReceiverAdjustments, + item: AssocItemId, + _is_visible: bool, + ) -> ControlFlow<()> { + if let AssocItemId::FunctionId(func) = item { + self.0.on_trait_method(func.into()) + } else { + ControlFlow::Continue(()) + } + } + } + let _p = tracing::info_span!( "iterate_method_candidates_dyn", with_local_impls = traits_in_scope.len(), @@ -5000,7 +5055,7 @@ impl Type { with_local_impls.and_then(|b| b.id.containing_block()).into(), name, method_resolution::LookupMode::MethodCall, - &mut |_adj, id, _| callback(id), + &mut Callback(callback), ); } @@ -5012,37 +5067,88 @@ impl Type { traits_in_scope: &FxHashSet, with_local_impls: Option, name: Option<&Name>, - mut callback: impl FnMut(AssocItem) -> Option, + callback: impl FnMut(AssocItem) -> Option, ) -> Option { + struct Callback { + f: F, + slot: Option, + } + impl PathCandidateCallback for &'_ mut Callback + where + F: FnMut(AssocItem) -> Option, + { + fn on_inherent_item(&mut self, item: AssocItem) -> ControlFlow<()> { + match (self.f)(item) { + it @ Some(_) => { + self.slot = it; + ControlFlow::Break(()) + } + None => ControlFlow::Continue(()), + } + } + + fn on_trait_item(&mut self, item: AssocItem) -> ControlFlow<()> { + match (self.f)(item) { + it @ Some(_) => { + self.slot = it; + ControlFlow::Break(()) + } + None => ControlFlow::Continue(()), + } + } + } + let _p = tracing::info_span!("iterate_path_candidates").entered(); - let mut slot = None; - self.iterate_path_candidates_dyn( + let mut callback = Callback { slot: None, f: callback }; + + self.iterate_path_candidates_split_inherent( db, scope, traits_in_scope, with_local_impls, name, - &mut |assoc_item_id| { - if let Some(res) = callback(assoc_item_id.into()) { - slot = Some(res); - return ControlFlow::Break(()); - } - ControlFlow::Continue(()) - }, + &mut callback, ); - slot + callback.slot } + /// Iterates over inherent methods. + /// + /// In some circumstances, inherent methods methods may actually be trait methods! + /// For example, when `dyn Trait` is a receiver, _trait_'s methods would be considered + /// to be inherent methods. #[tracing::instrument(skip_all, fields(name = ?name))] - fn iterate_path_candidates_dyn( + pub fn iterate_path_candidates_split_inherent( &self, db: &dyn HirDatabase, scope: &SemanticsScope<'_>, traits_in_scope: &FxHashSet, with_local_impls: Option, name: Option<&Name>, - callback: &mut dyn FnMut(AssocItemId) -> ControlFlow<()>, + callback: impl PathCandidateCallback, ) { + struct Callback(T); + + impl method_resolution::MethodCandidateCallback for Callback { + fn on_inherent_method( + &mut self, + _adjustments: method_resolution::ReceiverAdjustments, + item: AssocItemId, + _is_visible: bool, + ) -> ControlFlow<()> { + self.0.on_inherent_item(item.into()) + } + + fn on_trait_method( + &mut self, + _adjustments: method_resolution::ReceiverAdjustments, + item: AssocItemId, + _is_visible: bool, + ) -> ControlFlow<()> { + self.0.on_trait_item(item.into()) + } + } + let canonical = hir_ty::replace_errors_with_variables(&self.ty); let krate = scope.krate(); @@ -5058,7 +5164,7 @@ impl Type { traits_in_scope, with_local_impls.and_then(|b| b.id.containing_block()).into(), name, - callback, + &mut Callback(callback), ); } @@ -5744,3 +5850,15 @@ pub enum DocLinkDef { Field(Field), SelfType(Trait), } + +pub trait MethodCandidateCallback { + fn on_inherent_method(&mut self, f: Function) -> ControlFlow<()>; + + fn on_trait_method(&mut self, f: Function) -> ControlFlow<()>; +} + +pub trait PathCandidateCallback { + fn on_inherent_item(&mut self, item: AssocItem) -> ControlFlow<()>; + + fn on_trait_item(&mut self, item: AssocItem) -> ControlFlow<()>; +} diff --git a/crates/ide-completion/src/completions/dot.rs b/crates/ide-completion/src/completions/dot.rs index f2c360a9d5bf..923ccf96ca5e 100644 --- a/crates/ide-completion/src/completions/dot.rs +++ b/crates/ide-completion/src/completions/dot.rs @@ -1,6 +1,8 @@ //! Completes references after dot (fields and method calls). -use hir::{sym, Name}; +use std::ops::ControlFlow; + +use hir::{sym, HasContainer, ItemContainer, MethodCandidateCallback, Name}; use ide_db::FxHashSet; use syntax::SmolStr; @@ -158,21 +160,55 @@ fn complete_fields( fn complete_methods( ctx: &CompletionContext<'_>, receiver: &hir::Type, - mut f: impl FnMut(hir::Function), + f: impl FnMut(hir::Function), ) { - let mut seen_methods = FxHashSet::default(); - receiver.iterate_method_candidates_with_traits( + struct Callback<'a, F> { + ctx: &'a CompletionContext<'a>, + f: F, + seen_methods: FxHashSet, + } + + impl MethodCandidateCallback for Callback<'_, F> + where + F: FnMut(hir::Function), + { + // We don't want to exclude inherent trait methods - that is, methods of traits available from + // `where` clauses or `dyn Trait`. + fn on_inherent_method(&mut self, func: hir::Function) -> ControlFlow<()> { + if func.self_param(self.ctx.db).is_some() + && self.seen_methods.insert(func.name(self.ctx.db)) + { + (self.f)(func); + } + ControlFlow::Continue(()) + } + + fn on_trait_method(&mut self, func: hir::Function) -> ControlFlow<()> { + // This needs to come before the `seen_methods` test, so that if we see the same method twice, + // once as inherent and once not, we will include it. + if let ItemContainer::Trait(trait_) = func.container(self.ctx.db) { + if self.ctx.exclude_traits.contains(&trait_) { + return ControlFlow::Continue(()); + } + } + + if func.self_param(self.ctx.db).is_some() + && self.seen_methods.insert(func.name(self.ctx.db)) + { + (self.f)(func); + } + + ControlFlow::Continue(()) + } + } + + receiver.iterate_method_candidates_split_inherent( ctx.db, &ctx.scope, &ctx.traits_in_scope(), Some(ctx.module), None, - |func| { - if func.self_param(ctx.db).is_some() && seen_methods.insert(func.name(ctx.db)) { - f(func); - } - None::<()> - }, + Callback { ctx, f, seen_methods: FxHashSet::default() }, ); } diff --git a/crates/ide-completion/src/completions/expr.rs b/crates/ide-completion/src/completions/expr.rs index ff2c8da42130..ff9e4c778253 100644 --- a/crates/ide-completion/src/completions/expr.rs +++ b/crates/ide-completion/src/completions/expr.rs @@ -1,6 +1,10 @@ //! Completion of names from the current scope in expression position. -use hir::{sym, Name, ScopeDef}; +use std::ops::ControlFlow; + +use hir::{sym, Name, PathCandidateCallback, ScopeDef}; +use ide_db::FxHashSet; +use stdx::IsNoneOr; use syntax::ast; use crate::{ @@ -9,6 +13,39 @@ use crate::{ CompletionContext, Completions, }; +struct PathCallback<'a, F> { + ctx: &'a CompletionContext<'a>, + acc: &'a mut Completions, + add_assoc_item: F, + seen: FxHashSet, +} + +impl PathCandidateCallback for PathCallback<'_, F> +where + F: FnMut(&mut Completions, hir::AssocItem), +{ + fn on_inherent_item(&mut self, item: hir::AssocItem) -> ControlFlow<()> { + if self.seen.insert(item) { + (self.add_assoc_item)(self.acc, item); + } + ControlFlow::Continue(()) + } + + #[allow(unstable_name_collisions)] // FIXME: Remove this when `is_none_or()` reaches stable. + fn on_trait_item(&mut self, item: hir::AssocItem) -> ControlFlow<()> { + // The excluded check needs to come before the `seen` test, so that if we see the same method twice, + // once as inherent and once not, we will include it. + if item + .container_trait(self.ctx.db) + .is_none_or(|trait_| !self.ctx.exclude_traits.contains(&trait_)) + && self.seen.insert(item) + { + (self.add_assoc_item)(self.acc, item); + } + ControlFlow::Continue(()) + } +} + pub(crate) fn complete_expr_path( acc: &mut Completions, ctx: &CompletionContext<'_>, @@ -50,12 +87,18 @@ pub(crate) fn complete_expr_path( }; match qualified { + // We exclude associated types/consts of excluded traits here together with methods, + // even though we don't exclude them when completing in type position, because it's easier. Qualified::TypeAnchor { ty: None, trait_: None } => ctx .traits_in_scope() .iter() - .flat_map(|&it| hir::Trait::from(it).items(ctx.sema.db)) + .copied() + .map(hir::Trait::from) + .filter(|it| !ctx.exclude_traits.contains(it)) + .flat_map(|it| it.items(ctx.sema.db)) .for_each(|item| add_assoc_item(acc, item)), Qualified::TypeAnchor { trait_: Some(trait_), .. } => { + // Don't filter excluded traits here, user requested this specific trait. trait_.items(ctx.sema.db).into_iter().for_each(|item| add_assoc_item(acc, item)) } Qualified::TypeAnchor { ty: Some(ty), trait_: None } => { @@ -64,9 +107,14 @@ pub(crate) fn complete_expr_path( acc.add_enum_variants(ctx, path_ctx, e); } - ctx.iterate_path_candidates(ty, |item| { - add_assoc_item(acc, item); - }); + ty.iterate_path_candidates_split_inherent( + ctx.db, + &ctx.scope, + &ctx.traits_in_scope(), + Some(ctx.module), + None, + PathCallback { ctx, acc, add_assoc_item, seen: FxHashSet::default() }, + ); // Iterate assoc types separately ty.iterate_assoc_items(ctx.db, ctx.krate, |item| { @@ -121,9 +169,14 @@ pub(crate) fn complete_expr_path( // XXX: For parity with Rust bug #22519, this does not complete Ty::AssocType. // (where AssocType is defined on a trait, not an inherent impl) - ctx.iterate_path_candidates(&ty, |item| { - add_assoc_item(acc, item); - }); + ty.iterate_path_candidates_split_inherent( + ctx.db, + &ctx.scope, + &ctx.traits_in_scope(), + Some(ctx.module), + None, + PathCallback { ctx, acc, add_assoc_item, seen: FxHashSet::default() }, + ); // Iterate assoc types separately ty.iterate_assoc_items(ctx.db, ctx.krate, |item| { @@ -134,6 +187,7 @@ pub(crate) fn complete_expr_path( }); } hir::PathResolution::Def(hir::ModuleDef::Trait(t)) => { + // Don't filter excluded traits here, user requested this specific trait. // Handles `Trait::assoc` as well as `::assoc`. for item in t.items(ctx.db) { add_assoc_item(acc, item); @@ -151,9 +205,14 @@ pub(crate) fn complete_expr_path( acc.add_enum_variants(ctx, path_ctx, e); } - ctx.iterate_path_candidates(&ty, |item| { - add_assoc_item(acc, item); - }); + ty.iterate_path_candidates_split_inherent( + ctx.db, + &ctx.scope, + &ctx.traits_in_scope(), + Some(ctx.module), + None, + PathCallback { ctx, acc, add_assoc_item, seen: FxHashSet::default() }, + ); } _ => (), } diff --git a/crates/ide-completion/src/completions/flyimport.rs b/crates/ide-completion/src/completions/flyimport.rs index 2a6b310d3a21..4379a0c68e62 100644 --- a/crates/ide-completion/src/completions/flyimport.rs +++ b/crates/ide-completion/src/completions/flyimport.rs @@ -258,6 +258,8 @@ fn import_on_the_fly( let import_cfg = ctx.config.import_path_config(); + let completed_name = ctx.token.to_string(); + import_assets .search_for_imports(&ctx.sema, import_cfg, ctx.config.insert_use.prefix_kind) .filter(ns_filter) @@ -267,6 +269,21 @@ fn import_on_the_fly( && !ctx.is_item_hidden(original_item) && ctx.check_stability(original_item.attrs(ctx.db).as_deref()) }) + .filter(|import| { + if let Some(ModuleDef::Trait(trait_)) = import.item_to_import.as_module_def() { + let excluded = ctx.exclude_flyimport_traits.contains(&trait_); + let trait_itself_imported = import.item_to_import == import.original_item; + if !excluded || trait_itself_imported { + return true; + } + + let Some(item) = import.original_item.as_module_def() else { return true }; + // Filter that item out, unless its name matches the name the user wrote exactly - in which case preserve it. + item.name(ctx.db).is_some_and(|name| name.eq_ident(&completed_name)) + } else { + true + } + }) .sorted_by(|a, b| { let key = |import_path| { ( @@ -346,12 +363,27 @@ fn import_on_the_fly_method( let cfg = ctx.config.import_path_config(); + let completed_name = ctx.token.to_string(); + import_assets .search_for_imports(&ctx.sema, cfg, ctx.config.insert_use.prefix_kind) .filter(|import| { !ctx.is_item_hidden(&import.item_to_import) && !ctx.is_item_hidden(&import.original_item) }) + .filter(|import| { + if let Some(ModuleDef::Trait(trait_)) = import.item_to_import.as_module_def() { + if !ctx.exclude_flyimport_traits.contains(&trait_) { + return true; + } + + let Some(item) = import.original_item.as_module_def() else { return true }; + // Filter that method out, unless its name matches the name the user wrote exactly - in which case preserve it. + item.name(ctx.db).is_some_and(|name| name.eq_ident(&completed_name)) + } else { + true + } + }) .sorted_by(|a, b| { let key = |import_path| { ( diff --git a/crates/ide-completion/src/config.rs b/crates/ide-completion/src/config.rs index 1d05419c96de..ed36fe8d0283 100644 --- a/crates/ide-completion/src/config.rs +++ b/crates/ide-completion/src/config.rs @@ -10,7 +10,7 @@ use ide_db::{imports::insert_use::InsertUseConfig, SnippetCap}; use crate::{snippet::Snippet, CompletionFieldsToResolve}; #[derive(Clone, Debug, PartialEq, Eq)] -pub struct CompletionConfig { +pub struct CompletionConfig<'a> { pub enable_postfix_completions: bool, pub enable_imports_on_the_fly: bool, pub enable_self_on_the_fly: bool, @@ -28,6 +28,8 @@ pub struct CompletionConfig { pub snippets: Vec, pub limit: Option, pub fields_to_resolve: CompletionFieldsToResolve, + pub exclude_flyimport_traits: &'a [String], + pub exclude_traits: &'a [String], } #[derive(Clone, Debug, PartialEq, Eq)] @@ -36,7 +38,7 @@ pub enum CallableSnippets { AddParentheses, } -impl CompletionConfig { +impl CompletionConfig<'_> { pub fn postfix_snippets(&self) -> impl Iterator { self.snippets .iter() diff --git a/crates/ide-completion/src/context.rs b/crates/ide-completion/src/context.rs index e49a9e3b0640..2b34fb330f03 100644 --- a/crates/ide-completion/src/context.rs +++ b/crates/ide-completion/src/context.rs @@ -7,8 +7,8 @@ mod tests; use std::{iter, ops::ControlFlow}; use hir::{ - HasAttrs, Local, ModuleSource, Name, PathResolution, ScopeDef, Semantics, SemanticsScope, Type, - TypeInfo, + db::DefDatabase, HasAttrs, Local, ModuleSource, Name, PathResolution, ScopeDef, Semantics, + SemanticsScope, Type, TypeInfo, }; use ide_db::{ base_db::SourceDatabase, famous_defs::FamousDefs, helpers::is_editable_crate, FilePosition, @@ -426,7 +426,7 @@ pub(crate) struct CompletionContext<'a> { pub(crate) sema: Semantics<'a, RootDatabase>, pub(crate) scope: SemanticsScope<'a>, pub(crate) db: &'a RootDatabase, - pub(crate) config: &'a CompletionConfig, + pub(crate) config: &'a CompletionConfig<'a>, pub(crate) position: FilePosition, /// The token before the cursor, in the original file. @@ -459,6 +459,17 @@ pub(crate) struct CompletionContext<'a> { /// Here depth will be 2 pub(crate) depth_from_crate_root: usize, + /// Traits whose methods will be excluded from flyimport. Flyimport should not suggest + /// importing those traits. + /// + /// Note the trait *themselves* are not excluded, only their methods are. + pub(crate) exclude_flyimport_traits: FxHashSet, + /// Traits whose methods should always be excluded, even when in scope (compare `exclude_flyimport_traits`). + /// They will *not* be excluded, however, if they are available as a generic bound. + /// + /// Note the trait *themselves* are not excluded, only their methods are. + pub(crate) exclude_traits: FxHashSet, + /// Whether and how to complete semicolon for unit-returning functions. pub(crate) complete_semicolon: CompleteSemicolon, } @@ -667,7 +678,7 @@ impl<'a> CompletionContext<'a> { pub(crate) fn new( db: &'a RootDatabase, position @ FilePosition { file_id, offset }: FilePosition, - config: &'a CompletionConfig, + config: &'a CompletionConfig<'a>, ) -> Option<(CompletionContext<'a>, CompletionAnalysis)> { let _p = tracing::info_span!("CompletionContext::new").entered(); let sema = Semantics::new(db); @@ -751,6 +762,11 @@ impl<'a> CompletionContext<'a> { // exclude `m` itself .saturating_sub(1); + let exclude_traits = resolve_exclude_traits_list(db, config.exclude_traits); + let mut exclude_flyimport_traits = + resolve_exclude_traits_list(db, config.exclude_flyimport_traits); + exclude_flyimport_traits.extend(exclude_traits.iter().copied()); + let complete_semicolon = if config.add_semicolon_to_unit { let inside_closure_ret = token.parent_ancestors().try_for_each(|ancestor| { match_ast! { @@ -815,12 +831,79 @@ impl<'a> CompletionContext<'a> { qualifier_ctx, locals, depth_from_crate_root, + exclude_flyimport_traits, + exclude_traits, complete_semicolon, }; Some((ctx, analysis)) } } +fn resolve_exclude_traits_list(db: &RootDatabase, traits: &[String]) -> FxHashSet { + let _g = tracing::debug_span!("resolve_exclude_trait_list", ?traits).entered(); + let crate_graph = db.crate_graph(); + let mut crate_name_to_def_map = FxHashMap::default(); + let mut result = FxHashSet::default(); + 'process_traits: for trait_ in traits { + let mut segments = trait_.split("::").peekable(); + let Some(crate_name) = segments.next() else { + tracing::error!( + ?trait_, + "error resolving trait from traits exclude list: invalid path" + ); + continue; + }; + let Some(def_map) = crate_name_to_def_map.entry(crate_name).or_insert_with(|| { + let krate = crate_graph + .iter() + .find(|&krate| crate_graph[krate].display_name.as_deref() == Some(crate_name)); + let def_map = krate.map(|krate| db.crate_def_map(krate)); + if def_map.is_none() { + tracing::error!( + "error resolving `{trait_}` from trait exclude lists: crate could not be found" + ); + } + def_map + }) else { + // Do not report more than one error for the same crate. + continue; + }; + let mut module = &def_map[hir::DefMap::ROOT]; + let trait_name = 'lookup_mods: { + while let Some(segment) = segments.next() { + if segments.peek().is_none() { + break 'lookup_mods segment; + } + + let Some(&inner) = + module.children.get(&Name::new_symbol_root(hir::Symbol::intern(segment))) + else { + tracing::error!( + "error resolving `{trait_}` from trait exclude lists: could not find module `{segment}`" + ); + continue 'process_traits; + }; + module = &def_map[inner]; + } + + tracing::error!("error resolving `{trait_}` from trait exclude lists: invalid path"); + continue 'process_traits; + }; + let resolved_trait = module + .scope + .trait_by_name(&Name::new_symbol_root(hir::Symbol::intern(trait_name))) + .map(hir::Trait::from); + let Some(resolved_trait) = resolved_trait else { + tracing::error!( + "error resolving `{trait_}` from trait exclude lists: trait could not be found" + ); + continue; + }; + result.insert(resolved_trait); + } + result +} + const OP_TRAIT_LANG_NAMES: &[&str] = &[ "add_assign", "add", diff --git a/crates/ide-completion/src/lib.rs b/crates/ide-completion/src/lib.rs index a78976d3fd8b..3ef71bc32878 100644 --- a/crates/ide-completion/src/lib.rs +++ b/crates/ide-completion/src/lib.rs @@ -170,7 +170,7 @@ impl CompletionFieldsToResolve { /// analysis. pub fn completions( db: &RootDatabase, - config: &CompletionConfig, + config: &CompletionConfig<'_>, position: FilePosition, trigger_character: Option, ) -> Option> { @@ -255,7 +255,7 @@ pub fn completions( /// This is used for import insertion done via completions like flyimport and custom user snippets. pub fn resolve_completion_edits( db: &RootDatabase, - config: &CompletionConfig, + config: &CompletionConfig<'_>, FilePosition { file_id, offset }: FilePosition, imports: impl IntoIterator, ) -> Option> { diff --git a/crates/ide-completion/src/tests.rs b/crates/ide-completion/src/tests.rs index f371012de3f5..45564230efb9 100644 --- a/crates/ide-completion/src/tests.rs +++ b/crates/ide-completion/src/tests.rs @@ -61,7 +61,7 @@ fn function() {} union Union { field: i32 } "#; -pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig { +pub(crate) const TEST_CONFIG: CompletionConfig<'_> = CompletionConfig { enable_postfix_completions: true, enable_imports_on_the_fly: true, enable_self_on_the_fly: true, @@ -85,6 +85,8 @@ pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig { snippets: Vec::new(), limit: None, fields_to_resolve: CompletionFieldsToResolve::empty(), + exclude_flyimport_traits: &[], + exclude_traits: &[], }; pub(crate) fn completion_list(ra_fixture: &str) -> String { @@ -109,7 +111,7 @@ pub(crate) fn completion_list_with_trigger_character( } fn completion_list_with_config_raw( - config: CompletionConfig, + config: CompletionConfig<'_>, ra_fixture: &str, include_keywords: bool, trigger_character: Option, @@ -126,7 +128,7 @@ fn completion_list_with_config_raw( } fn completion_list_with_config( - config: CompletionConfig, + config: CompletionConfig<'_>, ra_fixture: &str, include_keywords: bool, trigger_character: Option, @@ -155,7 +157,7 @@ pub(crate) fn do_completion(code: &str, kind: CompletionItemKind) -> Vec, code: &str, kind: CompletionItemKind, ) -> Vec { @@ -211,7 +213,7 @@ pub(crate) fn check_edit(what: &str, ra_fixture_before: &str, ra_fixture_after: #[track_caller] pub(crate) fn check_edit_with_config( - config: CompletionConfig, + config: CompletionConfig<'_>, what: &str, ra_fixture_before: &str, ra_fixture_after: &str, @@ -248,7 +250,7 @@ fn check_empty(ra_fixture: &str, expect: Expect) { } pub(crate) fn get_all_items( - config: CompletionConfig, + config: CompletionConfig<'_>, code: &str, trigger_character: Option, ) -> Vec { diff --git a/crates/ide-completion/src/tests/expression.rs b/crates/ide-completion/src/tests/expression.rs index 545c2a2a8a08..dd3338f6e6a9 100644 --- a/crates/ide-completion/src/tests/expression.rs +++ b/crates/ide-completion/src/tests/expression.rs @@ -1,13 +1,29 @@ //! Completion tests for expressions. use expect_test::{expect, Expect}; -use crate::tests::{check_edit, check_empty, completion_list, BASE_ITEMS_FIXTURE}; +use crate::{ + tests::{ + check_edit, check_empty, completion_list, completion_list_with_config, BASE_ITEMS_FIXTURE, + TEST_CONFIG, + }, + CompletionConfig, +}; fn check(ra_fixture: &str, expect: Expect) { let actual = completion_list(&format!("{BASE_ITEMS_FIXTURE}{ra_fixture}")); expect.assert_eq(&actual) } +fn check_with_config(config: CompletionConfig<'_>, ra_fixture: &str, expect: Expect) { + let actual = completion_list_with_config( + config, + &format!("{BASE_ITEMS_FIXTURE}{ra_fixture}"), + true, + None, + ); + expect.assert_eq(&actual) +} + #[test] fn complete_literal_struct_with_a_private_field() { // `FooDesc.bar` is private, the completion should not be triggered. @@ -1320,3 +1336,366 @@ fn main() { "#]], ); } + +#[test] +fn excluded_trait_method_is_excluded() { + check_with_config( + CompletionConfig { exclude_traits: &["test::ExcludedTrait".to_owned()], ..TEST_CONFIG }, + r#" +trait ExcludedTrait { + fn foo(&self) {} + fn bar(&self) {} + fn baz(&self) {} +} + +impl ExcludedTrait for T {} + +struct Foo; +impl Foo { + fn inherent(&self) {} +} + +fn foo() { + Foo.$0 +} + "#, + expect![[r#" + me inherent() fn(&self) + sn box Box::new(expr) + sn call function(expr) + sn dbg dbg!(expr) + sn dbgr dbg!(&expr) + sn deref *expr + sn let let + sn letm let mut + sn match match expr {} + sn ref &expr + sn refm &mut expr + sn return return expr + sn unsafe unsafe {} + "#]], + ); +} + +#[test] +fn excluded_trait_not_excluded_when_inherent() { + check_with_config( + CompletionConfig { exclude_traits: &["test::ExcludedTrait".to_owned()], ..TEST_CONFIG }, + r#" +trait ExcludedTrait { + fn foo(&self) {} + fn bar(&self) {} + fn baz(&self) {} +} + +impl ExcludedTrait for T {} + +fn foo(v: &dyn ExcludedTrait) { + v.$0 +} + "#, + expect![[r#" + me bar() (as ExcludedTrait) fn(&self) + me baz() (as ExcludedTrait) fn(&self) + me foo() (as ExcludedTrait) fn(&self) + sn box Box::new(expr) + sn call function(expr) + sn dbg dbg!(expr) + sn dbgr dbg!(&expr) + sn deref *expr + sn let let + sn letm let mut + sn match match expr {} + sn ref &expr + sn refm &mut expr + sn return return expr + sn unsafe unsafe {} + "#]], + ); + check_with_config( + CompletionConfig { exclude_traits: &["test::ExcludedTrait".to_owned()], ..TEST_CONFIG }, + r#" +trait ExcludedTrait { + fn foo(&self) {} + fn bar(&self) {} + fn baz(&self) {} +} + +impl ExcludedTrait for T {} + +fn foo(v: impl ExcludedTrait) { + v.$0 +} + "#, + expect![[r#" + me bar() (as ExcludedTrait) fn(&self) + me baz() (as ExcludedTrait) fn(&self) + me foo() (as ExcludedTrait) fn(&self) + sn box Box::new(expr) + sn call function(expr) + sn dbg dbg!(expr) + sn dbgr dbg!(&expr) + sn deref *expr + sn let let + sn letm let mut + sn match match expr {} + sn ref &expr + sn refm &mut expr + sn return return expr + sn unsafe unsafe {} + "#]], + ); + check_with_config( + CompletionConfig { exclude_traits: &["test::ExcludedTrait".to_owned()], ..TEST_CONFIG }, + r#" +trait ExcludedTrait { + fn foo(&self) {} + fn bar(&self) {} + fn baz(&self) {} +} + +impl ExcludedTrait for T {} + +fn foo(v: T) { + v.$0 +} + "#, + expect![[r#" + me bar() (as ExcludedTrait) fn(&self) + me baz() (as ExcludedTrait) fn(&self) + me foo() (as ExcludedTrait) fn(&self) + sn box Box::new(expr) + sn call function(expr) + sn dbg dbg!(expr) + sn dbgr dbg!(&expr) + sn deref *expr + sn let let + sn letm let mut + sn match match expr {} + sn ref &expr + sn refm &mut expr + sn return return expr + sn unsafe unsafe {} + "#]], + ); +} + +#[test] +fn excluded_trait_method_is_excluded_from_flyimport() { + check_with_config( + CompletionConfig { + exclude_traits: &["test::module2::ExcludedTrait".to_owned()], + ..TEST_CONFIG + }, + r#" +mod module2 { + pub trait ExcludedTrait { + fn foo(&self) {} + fn bar(&self) {} + fn baz(&self) {} + } + + impl ExcludedTrait for T {} +} + +struct Foo; +impl Foo { + fn inherent(&self) {} +} + +fn foo() { + Foo.$0 +} + "#, + expect![[r#" + me inherent() fn(&self) + sn box Box::new(expr) + sn call function(expr) + sn dbg dbg!(expr) + sn dbgr dbg!(&expr) + sn deref *expr + sn let let + sn letm let mut + sn match match expr {} + sn ref &expr + sn refm &mut expr + sn return return expr + sn unsafe unsafe {} + "#]], + ); +} + +#[test] +fn flyimport_excluded_trait_method_is_excluded_from_flyimport() { + check_with_config( + CompletionConfig { + exclude_flyimport_traits: &["test::module2::ExcludedTrait".to_owned()], + ..TEST_CONFIG + }, + r#" +mod module2 { + pub trait ExcludedTrait { + fn foo(&self) {} + fn bar(&self) {} + fn baz(&self) {} + } + + impl ExcludedTrait for T {} +} + +struct Foo; +impl Foo { + fn inherent(&self) {} +} + +fn foo() { + Foo.$0 +} + "#, + expect![[r#" + me inherent() fn(&self) + sn box Box::new(expr) + sn call function(expr) + sn dbg dbg!(expr) + sn dbgr dbg!(&expr) + sn deref *expr + sn let let + sn letm let mut + sn match match expr {} + sn ref &expr + sn refm &mut expr + sn return return expr + sn unsafe unsafe {} + "#]], + ); +} + +#[test] +fn excluded_trait_method_is_excluded_from_path_completion() { + check_with_config( + CompletionConfig { exclude_traits: &["test::ExcludedTrait".to_owned()], ..TEST_CONFIG }, + r#" +pub trait ExcludedTrait { + fn foo(&self) {} + fn bar(&self) {} + fn baz(&self) {} +} + +impl ExcludedTrait for T {} + +struct Foo; +impl Foo { + fn inherent(&self) {} +} + +fn foo() { + Foo::$0 +} + "#, + expect![[r#" + me inherent(…) fn(&self) + "#]], + ); +} + +#[test] +fn excluded_trait_method_is_not_excluded_when_trait_is_specified() { + check_with_config( + CompletionConfig { exclude_traits: &["test::ExcludedTrait".to_owned()], ..TEST_CONFIG }, + r#" +pub trait ExcludedTrait { + fn foo(&self) {} + fn bar(&self) {} + fn baz(&self) {} +} + +impl ExcludedTrait for T {} + +struct Foo; +impl Foo { + fn inherent(&self) {} +} + +fn foo() { + ExcludedTrait::$0 +} + "#, + expect![[r#" + me bar(…) (as ExcludedTrait) fn(&self) + me baz(…) (as ExcludedTrait) fn(&self) + me foo(…) (as ExcludedTrait) fn(&self) + "#]], + ); + check_with_config( + CompletionConfig { exclude_traits: &["test::ExcludedTrait".to_owned()], ..TEST_CONFIG }, + r#" +pub trait ExcludedTrait { + fn foo(&self) {} + fn bar(&self) {} + fn baz(&self) {} +} + +impl ExcludedTrait for T {} + +struct Foo; +impl Foo { + fn inherent(&self) {} +} + +fn foo() { + ::$0 +} + "#, + expect![[r#" + me bar(…) (as ExcludedTrait) fn(&self) + me baz(…) (as ExcludedTrait) fn(&self) + me foo(…) (as ExcludedTrait) fn(&self) + "#]], + ); +} + +#[test] +fn excluded_trait_not_excluded_when_inherent_path() { + check_with_config( + CompletionConfig { exclude_traits: &["test::ExcludedTrait".to_owned()], ..TEST_CONFIG }, + r#" +trait ExcludedTrait { + fn foo(&self) {} + fn bar(&self) {} + fn baz(&self) {} +} + +impl ExcludedTrait for T {} + +fn foo() { + ::$0 +} + "#, + expect![[r#" + me bar(…) (as ExcludedTrait) fn(&self) + me baz(…) (as ExcludedTrait) fn(&self) + me foo(…) (as ExcludedTrait) fn(&self) + "#]], + ); + check_with_config( + CompletionConfig { exclude_traits: &["test::ExcludedTrait".to_owned()], ..TEST_CONFIG }, + r#" +trait ExcludedTrait { + fn foo(&self) {} + fn bar(&self) {} + fn baz(&self) {} +} + +impl ExcludedTrait for T {} + +fn foo() { + T::$0 +} + "#, + expect![[r#" + me bar(…) (as ExcludedTrait) fn(&self) + me baz(…) (as ExcludedTrait) fn(&self) + me foo(…) (as ExcludedTrait) fn(&self) + "#]], + ); +} diff --git a/crates/ide-completion/src/tests/flyimport.rs b/crates/ide-completion/src/tests/flyimport.rs index 0b532064fb2b..00a81b38e29a 100644 --- a/crates/ide-completion/src/tests/flyimport.rs +++ b/crates/ide-completion/src/tests/flyimport.rs @@ -3,10 +3,14 @@ use expect_test::{expect, Expect}; use crate::{ context::{CompletionAnalysis, NameContext, NameKind, NameRefKind}, tests::{check_edit, check_edit_with_config, TEST_CONFIG}, + CompletionConfig, }; fn check(ra_fixture: &str, expect: Expect) { - let config = TEST_CONFIG; + check_with_config(TEST_CONFIG, ra_fixture, expect); +} + +fn check_with_config(config: CompletionConfig<'_>, ra_fixture: &str, expect: Expect) { let (db, position) = crate::tests::position(ra_fixture); let (ctx, analysis) = crate::context::CompletionContext::new(&db, position, &config).unwrap(); @@ -1669,3 +1673,31 @@ mod module { "#]], ); } + +#[test] +fn excluded_trait_item_included_when_exact_match() { + check_with_config( + CompletionConfig { + exclude_traits: &["test::module2::ExcludedTrait".to_owned()], + ..TEST_CONFIG + }, + r#" +mod module2 { + pub trait ExcludedTrait { + fn foo(&self) {} + fn bar(&self) {} + fn baz(&self) {} + } + + impl ExcludedTrait for T {} +} + +fn foo() { + true.foo$0 +} + "#, + expect![[r#" + me foo() (use module2::ExcludedTrait) fn(&self) + "#]], + ); +} diff --git a/crates/ide-completion/src/tests/special.rs b/crates/ide-completion/src/tests/special.rs index 508f6248dd41..bd05fea37818 100644 --- a/crates/ide-completion/src/tests/special.rs +++ b/crates/ide-completion/src/tests/special.rs @@ -1346,7 +1346,7 @@ struct Foo = { let mut x = TEST_CONFIG; x.full_function_signatures = true; x diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index c46c4c8ce94b..c82b779e445f 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -661,7 +661,7 @@ impl Analysis { /// Computes completions at the given position. pub fn completions( &self, - config: &CompletionConfig, + config: &CompletionConfig<'_>, position: FilePosition, trigger_character: Option, ) -> Cancellable>> { @@ -673,7 +673,7 @@ impl Analysis { /// Resolves additional completion data at the position given. pub fn resolve_completion_edits( &self, - config: &CompletionConfig, + config: &CompletionConfig<'_>, position: FilePosition, imports: impl IntoIterator + std::panic::UnwindSafe, ) -> Cancellable> { diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index ef2e542cf222..1d42f394ef91 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -429,11 +429,64 @@ config_data! { /// Toggles the additional completions that automatically add imports when completed. /// Note that your client must specify the `additionalTextEdits` LSP client capability to truly have this feature enabled. completion_autoimport_enable: bool = true, + /// A list of full paths to traits to exclude from flyimport. + /// + /// Traits in this list won't be suggested to be imported by flyimport for their methods. Methods from them won't be available in flyimport completion. They will still be available if in scope. + /// + /// Note that the trait themselves can still be suggested by flyimport. + /// + /// This setting also inherits `#rust-analyzer.completion.excludeTraits#`. + /// + /// This setting defaults to: + /// + /// - [`core::borrow::Borrow`](https://doc.rust-lang.org/nightly/core/borrow/trait.Borrow.html) + /// - [`core::borrow::BorrowMut`](https://doc.rust-lang.org/nightly/core/borrow/trait.BorrowMut.html) + /// - [`core::cmp::PartialEq`](https://doc.rust-lang.org/nightly/core/cmp/trait.PartialEq.html) + /// - All operator traits (in [`core::ops`](https://doc.rust-lang.org/nightly/core/ops)) + /// + /// Note that if you override this setting, those traits won't be automatically inserted, so you may want to insert them manually. + completion_autoimport_excludeTraits: Vec = vec![ + "core::borrow::Borrow".to_owned(), + "core::borrow::BorrowMut".to_owned(), + "core::cmp::PartialEq".to_owned(), + "core::ops::Add".to_owned(), + "core::ops::AddAssign".to_owned(), + "core::ops::BitAnd".to_owned(), + "core::ops::BitAndAssign".to_owned(), + "core::ops::BitOr".to_owned(), + "core::ops::BitOrAssign".to_owned(), + "core::ops::BitXor".to_owned(), + "core::ops::BitXorAssign".to_owned(), + "core::ops::Div".to_owned(), + "core::ops::DivAssign".to_owned(), + "core::ops::Mul".to_owned(), + "core::ops::MulAssign".to_owned(), + "core::ops::Rem".to_owned(), + "core::ops::RemAssign".to_owned(), + "core::ops::Shl".to_owned(), + "core::ops::ShlAssign".to_owned(), + "core::ops::Shr".to_owned(), + "core::ops::ShrAssign".to_owned(), + "core::ops::Sub".to_owned(), + "core::ops::SubAssign".to_owned(), + "core::ops::Neg".to_owned(), + "core::ops::Not".to_owned(), + "core::ops::Index".to_owned(), + "core::ops::IndexMut".to_owned(), + "core::ops::Deref".to_owned(), + "core::ops::DerefMut".to_owned(), + ], /// Toggles the additional completions that automatically show method calls and field accesses /// with `self` prefixed to them when inside a method. completion_autoself_enable: bool = true, /// Whether to add parenthesis and argument snippets when completing function. completion_callable_snippets: CallableCompletionDef = CallableCompletionDef::FillArguments, + /// A list of full paths to traits to exclude from completion. + /// + /// Methods from these traits won't be completed, even if the trait is in scope. However, they will still be suggested on expressions whose type is `dyn Trait`, `impl Trait` or `T where T: Trait`. + /// + /// Note that the trait themselves can still be completed. + completion_excludeTraits: Vec = Vec::new(), /// Whether to show full function/method signatures in completion docs. completion_fullFunctionSignatures_enable: bool = false, /// Whether to omit deprecated items from autocompletion. By default they are marked as deprecated but not hidden. @@ -1392,7 +1445,7 @@ impl Config { } } - pub fn completion(&self, source_root: Option) -> CompletionConfig { + pub fn completion(&self, source_root: Option) -> CompletionConfig<'_> { let client_capability_fields = self.completion_resolve_support_properties(); CompletionConfig { enable_postfix_completions: self.completion_postfix_enable(source_root).to_owned(), @@ -1427,6 +1480,8 @@ impl Config { resolve_text_edit: client_capability_fields.contains("textEdit"), resolve_command: client_capability_fields.contains("command"), }, + exclude_flyimport_traits: self.completion_autoimport_excludeTraits(source_root), + exclude_traits: self.completion_excludeTraits(source_root), } } diff --git a/crates/rust-analyzer/src/integrated_benchmarks.rs b/crates/rust-analyzer/src/integrated_benchmarks.rs index 8946c7acb938..bd164fe44081 100644 --- a/crates/rust-analyzer/src/integrated_benchmarks.rs +++ b/crates/rust-analyzer/src/integrated_benchmarks.rs @@ -174,6 +174,8 @@ fn integrated_completion_benchmark() { limit: None, add_semicolon_to_unit: true, fields_to_resolve: CompletionFieldsToResolve::empty(), + exclude_flyimport_traits: &[], + exclude_traits: &[], }; let position = FilePosition { file_id, offset: TextSize::try_from(completion_offset).unwrap() }; @@ -222,6 +224,8 @@ fn integrated_completion_benchmark() { limit: None, add_semicolon_to_unit: true, fields_to_resolve: CompletionFieldsToResolve::empty(), + exclude_flyimport_traits: &[], + exclude_traits: &[], }; let position = FilePosition { file_id, offset: TextSize::try_from(completion_offset).unwrap() }; @@ -268,6 +272,8 @@ fn integrated_completion_benchmark() { limit: None, add_semicolon_to_unit: true, fields_to_resolve: CompletionFieldsToResolve::empty(), + exclude_flyimport_traits: &[], + exclude_traits: &[], }; let position = FilePosition { file_id, offset: TextSize::try_from(completion_offset).unwrap() }; diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc index 708fc2b7891c..58887945dd89 100644 --- a/docs/user/generated_config.adoc +++ b/docs/user/generated_config.adoc @@ -278,6 +278,61 @@ In `match` arms it completes a comma instead. -- Toggles the additional completions that automatically add imports when completed. Note that your client must specify the `additionalTextEdits` LSP client capability to truly have this feature enabled. +-- +[[rust-analyzer.completion.autoimport.excludeTraits]]rust-analyzer.completion.autoimport.excludeTraits:: ++ +-- +Default: +---- +[ + "core::borrow::Borrow", + "core::borrow::BorrowMut", + "core::cmp::PartialEq", + "core::ops::Add", + "core::ops::AddAssign", + "core::ops::BitAnd", + "core::ops::BitAndAssign", + "core::ops::BitOr", + "core::ops::BitOrAssign", + "core::ops::BitXor", + "core::ops::BitXorAssign", + "core::ops::Div", + "core::ops::DivAssign", + "core::ops::Mul", + "core::ops::MulAssign", + "core::ops::Rem", + "core::ops::RemAssign", + "core::ops::Shl", + "core::ops::ShlAssign", + "core::ops::Shr", + "core::ops::ShrAssign", + "core::ops::Sub", + "core::ops::SubAssign", + "core::ops::Neg", + "core::ops::Not", + "core::ops::Index", + "core::ops::IndexMut", + "core::ops::Deref", + "core::ops::DerefMut" +] +---- +A list of full paths to traits to exclude from flyimport. + +Traits in this list won't be suggested to be imported by flyimport for their methods. Methods from them won't be available in flyimport completion. They will still be available if in scope. + +Note that the trait themselves can still be suggested by flyimport. + +This setting also inherits `#rust-analyzer.completion.excludeTraits#`. + +This setting defaults to: + + - [`core::borrow::Borrow`](https://doc.rust-lang.org/nightly/core/borrow/trait.Borrow.html) + - [`core::borrow::BorrowMut`](https://doc.rust-lang.org/nightly/core/borrow/trait.BorrowMut.html) + - [`core::cmp::PartialEq`](https://doc.rust-lang.org/nightly/core/cmp/trait.PartialEq.html) + - All operator traits (in [`core::ops`](https://doc.rust-lang.org/nightly/core/ops)) + +Note that if you override this setting, those traits won't be automatically inserted, so you may want to insert them manually. + -- [[rust-analyzer.completion.autoself.enable]]rust-analyzer.completion.autoself.enable (default: `true`):: + @@ -290,6 +345,15 @@ with `self` prefixed to them when inside a method. -- Whether to add parenthesis and argument snippets when completing function. -- +[[rust-analyzer.completion.excludeTraits]]rust-analyzer.completion.excludeTraits (default: `[]`):: ++ +-- +A list of full paths to traits to exclude from completion. + +Methods from these traits won't be completed, even if the trait is in scope. However, they will still be suggested on expressions whose type is `dyn Trait`, `impl Trait` or `T where T: Trait`. + +Note that the trait themselves can still be completed. +-- [[rust-analyzer.completion.fullFunctionSignatures.enable]]rust-analyzer.completion.fullFunctionSignatures.enable (default: `false`):: + -- diff --git a/editors/code/package.json b/editors/code/package.json index a823e5bb96c3..0a61e160af97 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -1062,6 +1062,49 @@ } } }, + { + "title": "completion", + "properties": { + "rust-analyzer.completion.autoimport.excludeTraits": { + "markdownDescription": "A list of full paths to traits to exclude from flyimport.\n\nTraits in this list won't be suggested to be imported by flyimport for their methods. Methods from them won't be available in flyimport completion. They will still be available if in scope.\n\nNote that the trait themselves can still be suggested by flyimport.\n\nThis setting also inherits `#rust-analyzer.completion.excludeTraits#`.\n\nThis setting defaults to:\n\n - [`core::borrow::Borrow`](https://doc.rust-lang.org/nightly/core/borrow/trait.Borrow.html)\n - [`core::borrow::BorrowMut`](https://doc.rust-lang.org/nightly/core/borrow/trait.BorrowMut.html)\n - [`core::cmp::PartialEq`](https://doc.rust-lang.org/nightly/core/cmp/trait.PartialEq.html)\n - All operator traits (in [`core::ops`](https://doc.rust-lang.org/nightly/core/ops))\n\nNote that if you override this setting, those traits won't be automatically inserted, so you may want to insert them manually.", + "default": [ + "core::borrow::Borrow", + "core::borrow::BorrowMut", + "core::cmp::PartialEq", + "core::ops::Add", + "core::ops::AddAssign", + "core::ops::BitAnd", + "core::ops::BitAndAssign", + "core::ops::BitOr", + "core::ops::BitOrAssign", + "core::ops::BitXor", + "core::ops::BitXorAssign", + "core::ops::Div", + "core::ops::DivAssign", + "core::ops::Mul", + "core::ops::MulAssign", + "core::ops::Rem", + "core::ops::RemAssign", + "core::ops::Shl", + "core::ops::ShlAssign", + "core::ops::Shr", + "core::ops::ShrAssign", + "core::ops::Sub", + "core::ops::SubAssign", + "core::ops::Neg", + "core::ops::Not", + "core::ops::Index", + "core::ops::IndexMut", + "core::ops::Deref", + "core::ops::DerefMut" + ], + "type": "array", + "items": { + "type": "string" + } + } + } + }, { "title": "completion", "properties": { @@ -1092,6 +1135,19 @@ } } }, + { + "title": "completion", + "properties": { + "rust-analyzer.completion.excludeTraits": { + "markdownDescription": "A list of full paths to traits to exclude from completion.\n\nMethods from these traits won't be completed, even if the trait is in scope. However, they will still be suggested on expressions whose type is `dyn Trait`, `impl Trait` or `T where T: Trait`.\n\nNote that the trait themselves can still be completed.", + "default": [], + "type": "array", + "items": { + "type": "string" + } + } + } + }, { "title": "completion", "properties": {