Make ImportPrefix a configuration option

This commit is contained in:
Lukas Wirth 2020-10-05 17:41:49 +02:00
parent 67e71619b9
commit 8699331014
6 changed files with 49 additions and 10 deletions

View file

@ -4,6 +4,8 @@
//! module, and we use to statically check that we only produce snippet //! module, and we use to statically check that we only produce snippet
//! assists if we are allowed to. //! assists if we are allowed to.
use hir::PrefixKind;
use crate::{utils::MergeBehaviour, AssistKind}; use crate::{utils::MergeBehaviour, AssistKind};
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
@ -37,10 +39,11 @@ impl Default for AssistConfig {
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct InsertUseConfig { pub struct InsertUseConfig {
pub merge: Option<MergeBehaviour>, pub merge: Option<MergeBehaviour>,
pub prefix_kind: PrefixKind,
} }
impl Default for InsertUseConfig { impl Default for InsertUseConfig {
fn default() -> Self { fn default() -> Self {
InsertUseConfig { merge: Some(MergeBehaviour::Full) } InsertUseConfig { merge: Some(MergeBehaviour::Full), prefix_kind: PrefixKind::Plain }
} }
} }

View file

@ -191,12 +191,16 @@ impl AutoImportAssets {
_ => Some(candidate), _ => Some(candidate),
}) })
.filter_map(|candidate| match candidate { .filter_map(|candidate| match candidate {
Either::Left(module_def) => { Either::Left(module_def) => self.module_with_name_to_import.find_use_path_prefixed(
self.module_with_name_to_import.find_use_path_prefixed(db, module_def) db,
} module_def,
Either::Right(macro_def) => { ctx.config.insert_use.prefix_kind,
self.module_with_name_to_import.find_use_path_prefixed(db, macro_def) ),
} 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()) .filter(|use_path| !use_path.segments.is_empty())
.take(20) .take(20)

View file

@ -391,8 +391,9 @@ impl Module {
self, self,
db: &dyn DefDatabase, db: &dyn DefDatabase,
item: impl Into<ItemInNs>, item: impl Into<ItemInNs>,
prefix_kind: PrefixKind,
) -> Option<ModPath> { ) -> Option<ModPath> {
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)
} }
} }

View file

@ -48,6 +48,7 @@ pub use hir_def::{
body::scope::ExprScopes, body::scope::ExprScopes,
builtin_type::BuiltinType, builtin_type::BuiltinType,
docs::Documentation, docs::Documentation,
find_path::PrefixKind,
item_scope::ItemInNs, item_scope::ItemInNs,
nameres::ModuleSource, nameres::ModuleSource,
path::ModPath, path::ModPath,

View file

@ -10,6 +10,7 @@
use std::{ffi::OsString, path::PathBuf}; use std::{ffi::OsString, path::PathBuf};
use flycheck::FlycheckConfig; use flycheck::FlycheckConfig;
use hir::PrefixKind;
use ide::{ use ide::{
AssistConfig, CompletionConfig, DiagnosticsConfig, HoverConfig, InlayHintsConfig, AssistConfig, CompletionConfig, DiagnosticsConfig, HoverConfig, InlayHintsConfig,
MergeBehaviour, MergeBehaviour,
@ -289,6 +290,11 @@ impl Config {
MergeBehaviourDef::Full => Some(MergeBehaviour::Full), MergeBehaviourDef::Full => Some(MergeBehaviour::Full),
MergeBehaviourDef::Last => Some(MergeBehaviour::Last), 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; self.call_info_full = data.callInfo_full;
@ -403,13 +409,21 @@ enum ManifestOrProjectJson {
} }
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(rename_all = "lowercase")] #[serde(rename_all = "snake_case")]
enum MergeBehaviourDef { enum MergeBehaviourDef {
None, None,
Full, Full,
Last, Last,
} }
#[derive(Deserialize)]
#[serde(rename_all = "snake_case")]
enum ImportPrefixDef {
Plain,
BySelf,
ByCrate,
}
macro_rules! config_data { macro_rules! config_data {
(struct $name:ident { $($field:ident: $ty:ty = $default:expr,)*}) => { (struct $name:ident { $($field:ident: $ty:ty = $default:expr,)*}) => {
#[allow(non_snake_case)] #[allow(non_snake_case)]
@ -434,6 +448,7 @@ macro_rules! config_data {
config_data! { config_data! {
struct ConfigData { struct ConfigData {
assist_importMergeBehaviour: MergeBehaviourDef = MergeBehaviourDef::None, assist_importMergeBehaviour: MergeBehaviourDef = MergeBehaviourDef::None,
assist_importPrefix: ImportPrefixDef = ImportPrefixDef::Plain,
callInfo_full: bool = true, callInfo_full: bool = true,

View file

@ -652,6 +652,21 @@
"default": "full", "default": "full",
"description": "The strategy to use when inserting new imports or merging imports." "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": { "rust-analyzer.runnables.overrideCargo": {
"type": [ "type": [
"null", "null",