From 86993310143c1347db6308a66c1f31a7a5644f56 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 5 Oct 2020 17:41:49 +0200 Subject: Make ImportPrefix a configuration option --- crates/assists/src/assist_config.rs | 5 ++++- crates/assists/src/handlers/auto_import.rs | 16 ++++++++++------ crates/hir/src/code_model.rs | 3 ++- crates/hir/src/lib.rs | 1 + crates/rust-analyzer/src/config.rs | 17 ++++++++++++++++- editors/code/package.json | 17 ++++++++++++++++- 6 files changed, 49 insertions(+), 10 deletions(-) diff --git a/crates/assists/src/assist_config.rs b/crates/assists/src/assist_config.rs index adf02edab..b24527ec4 100644 --- a/crates/assists/src/assist_config.rs +++ b/crates/assists/src/assist_config.rs @@ -4,6 +4,8 @@ //! module, and we use to statically check that we only produce snippet //! assists if we are allowed to. +use hir::PrefixKind; + use crate::{utils::MergeBehaviour, AssistKind}; #[derive(Clone, Debug, PartialEq, Eq)] @@ -37,10 +39,11 @@ impl Default for AssistConfig { #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct InsertUseConfig { pub merge: Option, + pub prefix_kind: PrefixKind, } impl Default for InsertUseConfig { fn default() -> Self { - InsertUseConfig { merge: Some(MergeBehaviour::Full) } + InsertUseConfig { merge: Some(MergeBehaviour::Full), prefix_kind: PrefixKind::Plain } } } diff --git a/crates/assists/src/handlers/auto_import.rs b/crates/assists/src/handlers/auto_import.rs index fa524ffd9..357ff6392 100644 --- a/crates/assists/src/handlers/auto_import.rs +++ b/crates/assists/src/handlers/auto_import.rs @@ -191,12 +191,16 @@ impl AutoImportAssets { _ => Some(candidate), }) .filter_map(|candidate| match candidate { - Either::Left(module_def) => { - self.module_with_name_to_import.find_use_path_prefixed(db, module_def) - } - Either::Right(macro_def) => { - self.module_with_name_to_import.find_use_path_prefixed(db, macro_def) - } + Either::Left(module_def) => self.module_with_name_to_import.find_use_path_prefixed( + db, + module_def, + ctx.config.insert_use.prefix_kind, + ), + Either::Right(macro_def) => self.module_with_name_to_import.find_use_path_prefixed( + db, + macro_def, + ctx.config.insert_use.prefix_kind, + ), }) .filter(|use_path| !use_path.segments.is_empty()) .take(20) diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index 5f35d9d3c..a445a97b3 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -391,8 +391,9 @@ impl Module { self, db: &dyn DefDatabase, item: impl Into, + prefix_kind: PrefixKind, ) -> Option { - hir_def::find_path::find_path_prefixed(db, item.into(), self.into(), PrefixKind::Plain) + hir_def::find_path::find_path_prefixed(db, item.into(), self.into(), prefix_kind) } } diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index b9d9c7e25..87084fa13 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -48,6 +48,7 @@ pub use hir_def::{ body::scope::ExprScopes, builtin_type::BuiltinType, docs::Documentation, + find_path::PrefixKind, item_scope::ItemInNs, nameres::ModuleSource, path::ModPath, diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 0ab4c37bf..dcbc11c14 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -10,6 +10,7 @@ use std::{ffi::OsString, path::PathBuf}; use flycheck::FlycheckConfig; +use hir::PrefixKind; use ide::{ AssistConfig, CompletionConfig, DiagnosticsConfig, HoverConfig, InlayHintsConfig, MergeBehaviour, @@ -289,6 +290,11 @@ impl Config { MergeBehaviourDef::Full => Some(MergeBehaviour::Full), MergeBehaviourDef::Last => Some(MergeBehaviour::Last), }; + self.assist.insert_use.prefix_kind = match data.assist_importPrefix { + ImportPrefixDef::Plain => PrefixKind::Plain, + ImportPrefixDef::ByCrate => PrefixKind::ByCrate, + ImportPrefixDef::BySelf => PrefixKind::BySelf, + }; self.call_info_full = data.callInfo_full; @@ -403,13 +409,21 @@ enum ManifestOrProjectJson { } #[derive(Deserialize)] -#[serde(rename_all = "lowercase")] +#[serde(rename_all = "snake_case")] enum MergeBehaviourDef { None, Full, Last, } +#[derive(Deserialize)] +#[serde(rename_all = "snake_case")] +enum ImportPrefixDef { + Plain, + BySelf, + ByCrate, +} + macro_rules! config_data { (struct $name:ident { $($field:ident: $ty:ty = $default:expr,)*}) => { #[allow(non_snake_case)] @@ -434,6 +448,7 @@ macro_rules! config_data { config_data! { struct ConfigData { assist_importMergeBehaviour: MergeBehaviourDef = MergeBehaviourDef::None, + assist_importPrefix: ImportPrefixDef = ImportPrefixDef::Plain, callInfo_full: bool = true, diff --git a/editors/code/package.json b/editors/code/package.json index cc2ac3bd2..1f0e7550b 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -652,6 +652,21 @@ "default": "full", "description": "The strategy to use when inserting new imports or merging imports." }, + "rust-analyzer.assist.importPrefix": { + "type": "string", + "enum": [ + "plain", + "by_self", + "by_crate" + ], + "enumDescriptions": [ + "Insert import paths relative to the current module, using up to one `super` prefix if the parent module contains the requested item.", + "Prefix all import paths with `self` if they don't begin with `self`, `super`, `crate` or a crate name", + "Force import paths to be absolute by always starting them with `crate` or the crate name they refer to." + ], + "default": "plain", + "description": "The path structure for newly inserted paths to use." + }, "rust-analyzer.runnables.overrideCargo": { "type": [ "null", @@ -1033,4 +1048,4 @@ ] } } -} +} \ No newline at end of file -- cgit v1.2.3