Skip to content

Commit 8699331

Browse files
committed
Make ImportPrefix a configuration option
1 parent 67e7161 commit 8699331

File tree

6 files changed

+49
-10
lines changed

6 files changed

+49
-10
lines changed

crates/assists/src/assist_config.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
//! module, and we use to statically check that we only produce snippet
55
//! assists if we are allowed to.
66
7+
use hir::PrefixKind;
8+
79
use crate::{utils::MergeBehaviour, AssistKind};
810

911
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -37,10 +39,11 @@ impl Default for AssistConfig {
3739
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
3840
pub struct InsertUseConfig {
3941
pub merge: Option<MergeBehaviour>,
42+
pub prefix_kind: PrefixKind,
4043
}
4144

4245
impl Default for InsertUseConfig {
4346
fn default() -> Self {
44-
InsertUseConfig { merge: Some(MergeBehaviour::Full) }
47+
InsertUseConfig { merge: Some(MergeBehaviour::Full), prefix_kind: PrefixKind::Plain }
4548
}
4649
}

crates/assists/src/handlers/auto_import.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,16 @@ impl AutoImportAssets {
191191
_ => Some(candidate),
192192
})
193193
.filter_map(|candidate| match candidate {
194-
Either::Left(module_def) => {
195-
self.module_with_name_to_import.find_use_path_prefixed(db, module_def)
196-
}
197-
Either::Right(macro_def) => {
198-
self.module_with_name_to_import.find_use_path_prefixed(db, macro_def)
199-
}
194+
Either::Left(module_def) => self.module_with_name_to_import.find_use_path_prefixed(
195+
db,
196+
module_def,
197+
ctx.config.insert_use.prefix_kind,
198+
),
199+
Either::Right(macro_def) => self.module_with_name_to_import.find_use_path_prefixed(
200+
db,
201+
macro_def,
202+
ctx.config.insert_use.prefix_kind,
203+
),
200204
})
201205
.filter(|use_path| !use_path.segments.is_empty())
202206
.take(20)

crates/hir/src/code_model.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,9 @@ impl Module {
391391
self,
392392
db: &dyn DefDatabase,
393393
item: impl Into<ItemInNs>,
394+
prefix_kind: PrefixKind,
394395
) -> Option<ModPath> {
395-
hir_def::find_path::find_path_prefixed(db, item.into(), self.into(), PrefixKind::Plain)
396+
hir_def::find_path::find_path_prefixed(db, item.into(), self.into(), prefix_kind)
396397
}
397398
}
398399

crates/hir/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub use hir_def::{
4848
body::scope::ExprScopes,
4949
builtin_type::BuiltinType,
5050
docs::Documentation,
51+
find_path::PrefixKind,
5152
item_scope::ItemInNs,
5253
nameres::ModuleSource,
5354
path::ModPath,

crates/rust-analyzer/src/config.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use std::{ffi::OsString, path::PathBuf};
1111

1212
use flycheck::FlycheckConfig;
13+
use hir::PrefixKind;
1314
use ide::{
1415
AssistConfig, CompletionConfig, DiagnosticsConfig, HoverConfig, InlayHintsConfig,
1516
MergeBehaviour,
@@ -289,6 +290,11 @@ impl Config {
289290
MergeBehaviourDef::Full => Some(MergeBehaviour::Full),
290291
MergeBehaviourDef::Last => Some(MergeBehaviour::Last),
291292
};
293+
self.assist.insert_use.prefix_kind = match data.assist_importPrefix {
294+
ImportPrefixDef::Plain => PrefixKind::Plain,
295+
ImportPrefixDef::ByCrate => PrefixKind::ByCrate,
296+
ImportPrefixDef::BySelf => PrefixKind::BySelf,
297+
};
292298

293299
self.call_info_full = data.callInfo_full;
294300

@@ -403,13 +409,21 @@ enum ManifestOrProjectJson {
403409
}
404410

405411
#[derive(Deserialize)]
406-
#[serde(rename_all = "lowercase")]
412+
#[serde(rename_all = "snake_case")]
407413
enum MergeBehaviourDef {
408414
None,
409415
Full,
410416
Last,
411417
}
412418

419+
#[derive(Deserialize)]
420+
#[serde(rename_all = "snake_case")]
421+
enum ImportPrefixDef {
422+
Plain,
423+
BySelf,
424+
ByCrate,
425+
}
426+
413427
macro_rules! config_data {
414428
(struct $name:ident { $($field:ident: $ty:ty = $default:expr,)*}) => {
415429
#[allow(non_snake_case)]
@@ -434,6 +448,7 @@ macro_rules! config_data {
434448
config_data! {
435449
struct ConfigData {
436450
assist_importMergeBehaviour: MergeBehaviourDef = MergeBehaviourDef::None,
451+
assist_importPrefix: ImportPrefixDef = ImportPrefixDef::Plain,
437452

438453
callInfo_full: bool = true,
439454

editors/code/package.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,21 @@
652652
"default": "full",
653653
"description": "The strategy to use when inserting new imports or merging imports."
654654
},
655+
"rust-analyzer.assist.importPrefix": {
656+
"type": "string",
657+
"enum": [
658+
"plain",
659+
"by_self",
660+
"by_crate"
661+
],
662+
"enumDescriptions": [
663+
"Insert import paths relative to the current module, using up to one `super` prefix if the parent module contains the requested item.",
664+
"Prefix all import paths with `self` if they don't begin with `self`, `super`, `crate` or a crate name",
665+
"Force import paths to be absolute by always starting them with `crate` or the crate name they refer to."
666+
],
667+
"default": "plain",
668+
"description": "The path structure for newly inserted paths to use."
669+
},
655670
"rust-analyzer.runnables.overrideCargo": {
656671
"type": [
657672
"null",
@@ -1033,4 +1048,4 @@
10331048
]
10341049
}
10351050
}
1036-
}
1051+
}

0 commit comments

Comments
 (0)