Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #22416 from A4-Tacks/exclude-module-items
feat: completions imports exclude supports sub items
| -rw-r--r-- | crates/ide-completion/src/config.rs | 1 | ||||
| -rw-r--r-- | crates/ide-completion/src/context.rs | 15 | ||||
| -rw-r--r-- | crates/ide-completion/src/tests/expression.rs | 78 | ||||
| -rw-r--r-- | crates/rust-analyzer/src/config.rs | 12 | ||||
| -rw-r--r-- | docs/book/src/configuration_generated.md | 3 | ||||
| -rw-r--r-- | editors/code/package.json | 8 |
6 files changed, 112 insertions, 5 deletions
diff --git a/crates/ide-completion/src/config.rs b/crates/ide-completion/src/config.rs index 21f624be2c..ac52c816e5 100644 --- a/crates/ide-completion/src/config.rs +++ b/crates/ide-completion/src/config.rs @@ -44,6 +44,7 @@ pub struct CompletionConfig<'a> { pub enum AutoImportExclusionType { Always, Methods, + SubItems, } #[derive(Clone, Debug, PartialEq, Eq)] diff --git a/crates/ide-completion/src/context.rs b/crates/ide-completion/src/context.rs index df62829269..135e222a36 100644 --- a/crates/ide-completion/src/context.rs +++ b/crates/ide-completion/src/context.rs @@ -855,8 +855,23 @@ impl<'a, 'db> CompletionContext<'a, 'db> { .map(|it| (it.into_module_def(), *kind)) }) .collect(); + let exclude_subitems = exclude_flyimport + .iter() + .flat_map(|it| match it { + (ModuleDef::Module(module), AutoImportExclusionType::SubItems) => { + module.scope(db, None) + } + _ => vec![], + }) + .filter_map(|(_, def)| match def { + ScopeDef::ModuleDef(module_def) => Some(module_def), + _ => None, + }) + .collect::<Vec<_>>(); exclude_flyimport .extend(exclude_traits.iter().map(|&t| (t.into(), AutoImportExclusionType::Always))); + exclude_flyimport + .extend(exclude_subitems.into_iter().map(|it| (it, AutoImportExclusionType::Always))); // FIXME: This should be part of `CompletionAnalysis` / `expand_and_analyze` let complete_semicolon = if !config.add_semicolon_to_unit { diff --git a/crates/ide-completion/src/tests/expression.rs b/crates/ide-completion/src/tests/expression.rs index 595c864ae3..eb99664c35 100644 --- a/crates/ide-completion/src/tests/expression.rs +++ b/crates/ide-completion/src/tests/expression.rs @@ -2964,6 +2964,84 @@ fn foo() { } #[test] +fn flyimport_excluded_mod_items_from_flyimport() { + check_with_config( + CompletionConfig { + exclude_flyimport: vec![( + "ra_test_fixture::xpack::xmodule2".to_owned(), + AutoImportExclusionType::SubItems, + )], + ..TEST_CONFIG + }, + r#" +mod xpack { + mod xmodule1 { + pub struct XOther; + } + pub mod xmodule2 { + pub use super::xmodule1::*; + pub struct XStruct; + pub fn xfn() {} + } +} + +fn foo() { + x$0 +} + "#, + expect![[r#" + ct CONST Unit + en Enum Enum + fn foo() fn() + fn function() fn() + ma makro!(…) macro_rules! makro + md module:: + md xmodule2:: (use xpack::xmodule2) + md xpack:: + sc STATIC Unit + st Record Record + st Tuple Tuple + st Unit Unit + un Union Union + ev TupleV(…) TupleV(u32) + bt u32 u32 + kw async + kw const + kw crate:: + kw enum + kw extern + kw false + kw fn + kw for + kw if + kw if let + kw impl + kw impl for + kw let + kw letm + kw loop + kw match + kw mod + kw return + kw self:: + kw static + kw struct + kw trait + kw true + kw type + kw union + kw unsafe + kw use + kw while + kw while let + sn macro_rules + sn pd + sn ppd + "#]], + ); +} + +#[test] fn excluded_trait_method_is_excluded_from_path_completion() { check_with_config( CompletionConfig { diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index f4c3f24ce6..6f532e4224 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -662,6 +662,9 @@ config_data! { /// For traits the type "methods" can be used to only exclude the methods but not the trait /// itself. /// + /// For modules the type "subItems" can be used to only exclude the all items in it but not the module + /// itself. This does not include items defined in nested modules. + /// /// This setting also inherits `#rust-analyzer.completion.excludeTraits#`. completion_autoimport_exclude: Vec<AutoImportExclusion> = vec![ AutoImportExclusion::Verbose { path: "core::borrow::Borrow".to_owned(), r#type: AutoImportExclusionType::Methods }, @@ -1938,6 +1941,9 @@ impl Config { AutoImportExclusionType::Methods => { ide_completion::AutoImportExclusionType::Methods } + AutoImportExclusionType::SubItems => { + ide_completion::AutoImportExclusionType::SubItems + } }, ), }) @@ -2997,6 +3003,7 @@ pub enum AutoImportExclusion { pub enum AutoImportExclusionType { Always, Methods, + SubItems, } #[derive(Serialize, Deserialize, Debug, Clone)] @@ -4128,10 +4135,11 @@ fn field_props(field: &str, ty: &str, doc: &[&str], default: &str) -> serde_json }, "type": { "type": "string", - "enum": ["always", "methods"], + "enum": ["always", "methods", "subItems"], "enumDescriptions": [ "Do not show this item or its methods (if it is a trait) in auto-import completions.", - "Do not show this traits methods in auto-import completions." + "Do not show this trait's methods in auto-import completions.", + "Do not show this module's all items in it in auto-import completions." ], }, } diff --git a/docs/book/src/configuration_generated.md b/docs/book/src/configuration_generated.md index 069c8211db..7bbb9e0258 100644 --- a/docs/book/src/configuration_generated.md +++ b/docs/book/src/configuration_generated.md @@ -446,6 +446,9 @@ verbose form `{ "path": "path::to::item", type: "always" }`. For traits the type "methods" can be used to only exclude the methods but not the trait itself. +For modules the type "subItems" can be used to only exclude the all items in it but not the module +itself. This does not include items defined in nested modules. + This setting also inherits `#rust-analyzer.completion.excludeTraits#`. diff --git a/editors/code/package.json b/editors/code/package.json index 14369e6f33..8df606d4c4 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -1328,7 +1328,7 @@ "title": "Completion", "properties": { "rust-analyzer.completion.autoimport.exclude": { - "markdownDescription": "A list of full paths to items to exclude from auto-importing completions.\n\nTraits in this list won't have their methods suggested in completions unless the trait\nis in scope.\n\nYou can either specify a string path which defaults to type \"always\" or use the more\nverbose form `{ \"path\": \"path::to::item\", type: \"always\" }`.\n\nFor traits the type \"methods\" can be used to only exclude the methods but not the trait\nitself.\n\nThis setting also inherits `#rust-analyzer.completion.excludeTraits#`.", + "markdownDescription": "A list of full paths to items to exclude from auto-importing completions.\n\nTraits in this list won't have their methods suggested in completions unless the trait\nis in scope.\n\nYou can either specify a string path which defaults to type \"always\" or use the more\nverbose form `{ \"path\": \"path::to::item\", type: \"always\" }`.\n\nFor traits the type \"methods\" can be used to only exclude the methods but not the trait\nitself.\n\nFor modules the type \"subItems\" can be used to only exclude the all items in it but not the module\nitself. This does not include items defined in nested modules.\n\nThis setting also inherits `#rust-analyzer.completion.excludeTraits#`.", "default": [ { "path": "core::borrow::Borrow", @@ -1355,11 +1355,13 @@ "type": "string", "enum": [ "always", - "methods" + "methods", + "subItems" ], "enumDescriptions": [ "Do not show this item or its methods (if it is a trait) in auto-import completions.", - "Do not show this traits methods in auto-import completions." + "Do not show this trait's methods in auto-import completions.", + "Do not show this module's all items in it in auto-import completions." ] } } |