From 13af22df98c790875d0a31f49c19cfd84e14d424 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 1 Jan 2025 14:09:05 +0100 Subject: [PATCH] Reduce the default autoimport exclusion list --- .../crates/hir-def/src/item_scope.rs | 7 - .../ide-completion/src/tests/expression.rs | 154 ++++++++++-------- .../crates/rust-analyzer/src/config.rs | 41 +---- .../docs/user/generated_config.adoc | 43 +---- .../rust-analyzer/editors/code/package.json | 31 +--- 5 files changed, 90 insertions(+), 186 deletions(-) diff --git a/src/tools/rust-analyzer/crates/hir-def/src/item_scope.rs b/src/tools/rust-analyzer/crates/hir-def/src/item_scope.rs index f600efea9f24..2c3eb5c8e5e4 100644 --- a/src/tools/rust-analyzer/crates/hir-def/src/item_scope.rs +++ b/src/tools/rust-analyzer/crates/hir-def/src/item_scope.rs @@ -354,13 +354,6 @@ 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.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, trait_)| { diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/tests/expression.rs b/src/tools/rust-analyzer/crates/ide-completion/src/tests/expression.rs index 6c95980507c7..d1bbf1e8d0bc 100644 --- a/src/tools/rust-analyzer/crates/ide-completion/src/tests/expression.rs +++ b/src/tools/rust-analyzer/crates/ide-completion/src/tests/expression.rs @@ -1430,19 +1430,22 @@ fn foo() { } "#, 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 {} + me bar() (as ExcludedTrait) fn(&self) + me baz() (as ExcludedTrait) fn(&self) + me foo() (as ExcludedTrait) fn(&self) + 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 {} "#]], ); } @@ -1468,18 +1471,18 @@ fn foo(v: &dyn ExcludedTrait) { 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 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 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 {} + sn unsafe unsafe {} "#]], ); check_with_config( @@ -1501,18 +1504,18 @@ fn foo(v: impl ExcludedTrait) { 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 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 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 {} + sn unsafe unsafe {} "#]], ); check_with_config( @@ -1534,18 +1537,18 @@ fn foo(v: T) { 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 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 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 {} + sn unsafe unsafe {} "#]], ); } @@ -1578,19 +1581,22 @@ fn foo() { } "#, 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 {} + me bar() (use module2::ExcludedTrait) fn(&self) + me baz() (use module2::ExcludedTrait) fn(&self) + me foo() (use module2::ExcludedTrait) fn(&self) + 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 {} "#]], ); } @@ -1623,19 +1629,22 @@ fn foo() { } "#, 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 {} + me bar() (use module2::ExcludedTrait) fn(&self) + me baz() (use module2::ExcludedTrait) fn(&self) + me foo() (use module2::ExcludedTrait) fn(&self) + 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 {} "#]], ); } @@ -1663,8 +1672,11 @@ fn foo() { } "#, expect![[r#" - me inherent(…) fn(&self) - "#]], + me bar(…) (as ExcludedTrait) fn(&self) + me baz(…) (as ExcludedTrait) fn(&self) + me foo(…) (as ExcludedTrait) fn(&self) + me inherent(…) fn(&self) + "#]], ); } diff --git a/src/tools/rust-analyzer/crates/rust-analyzer/src/config.rs b/src/tools/rust-analyzer/crates/rust-analyzer/src/config.rs index c53ca230409d..b6678c12c703 100644 --- a/src/tools/rust-analyzer/crates/rust-analyzer/src/config.rs +++ b/src/tools/rust-analyzer/crates/rust-analyzer/src/config.rs @@ -442,50 +442,13 @@ config_data! { 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. + /// Traits in this list won't have their methods suggested in completions unless the trait + /// is in scope. /// /// 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. diff --git a/src/tools/rust-analyzer/docs/user/generated_config.adoc b/src/tools/rust-analyzer/docs/user/generated_config.adoc index 27ade71b331a..bce26f7dd74f 100644 --- a/src/tools/rust-analyzer/docs/user/generated_config.adoc +++ b/src/tools/rust-analyzer/docs/user/generated_config.adoc @@ -293,53 +293,16 @@ 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" + "core::borrow::BorrowMut" ] ---- 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. +Traits in this list won't have their methods suggested in completions unless the trait +is in scope. 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`):: + diff --git a/src/tools/rust-analyzer/editors/code/package.json b/src/tools/rust-analyzer/editors/code/package.json index a6e92838a068..31419a1942d4 100644 --- a/src/tools/rust-analyzer/editors/code/package.json +++ b/src/tools/rust-analyzer/editors/code/package.json @@ -1143,37 +1143,10 @@ "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.", + "markdownDescription": "A list of full paths to traits to exclude from flyimport.\n\nTraits in this list won't have their methods suggested in completions unless the trait\nis in scope.\n\nThis setting also inherits `#rust-analyzer.completion.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" + "core::borrow::BorrowMut" ], "type": "array", "items": {