Skip to content

Commit

Permalink
Reduce the default autoimport exclusion list
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Jan 1, 2025
1 parent 528ed6b commit 13af22d
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 186 deletions.
7 changes: 0 additions & 7 deletions src/tools/rust-analyzer/crates/hir-def/src/item_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,6 @@ impl ItemScope {
.chain(self.unnamed_trait_imports.keys().copied())
}

pub fn trait_by_name(&self, name: &Name) -> Option<TraitId> {
self.types.get(name).and_then(|def| match def.def {
ModuleDefId::TraitId(it) => Some(it),
_ => None,
})
}

pub(crate) fn resolutions(&self) -> impl Iterator<Item = (Option<Name>, PerNs)> + '_ {
self.entries().map(|(name, res)| (Some(name.clone()), res)).chain(
self.unnamed_trait_imports.iter().map(|(tr, trait_)| {
Expand Down
154 changes: 83 additions & 71 deletions src/tools/rust-analyzer/crates/ide-completion/src/tests/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
"#]],
);
}
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -1534,18 +1537,18 @@ fn foo<T: ExcludedTrait>(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 {}
"#]],
);
}
Expand Down Expand Up @@ -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 {}
"#]],
);
}
Expand Down Expand Up @@ -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 {}
"#]],
);
}
Expand Down Expand Up @@ -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)
"#]],
);
}

Expand Down
41 changes: 2 additions & 39 deletions src/tools/rust-analyzer/crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = 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.
Expand Down
43 changes: 3 additions & 40 deletions src/tools/rust-analyzer/docs/user/generated_config.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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`)::
+
Expand Down
31 changes: 2 additions & 29 deletions src/tools/rust-analyzer/editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 13af22d

Please sign in to comment.