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
//! 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<MergeBehaviour>,
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 }
}
}

View file

@ -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)

View file

@ -391,8 +391,9 @@ impl Module {
self,
db: &dyn DefDatabase,
item: impl Into<ItemInNs>,
prefix_kind: PrefixKind,
) -> 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,
builtin_type::BuiltinType,
docs::Documentation,
find_path::PrefixKind,
item_scope::ItemInNs,
nameres::ModuleSource,
path::ModPath,

View file

@ -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,

View file

@ -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 @@
]
}
}
}
}