diff options
author | Lukas Wirth <[email protected]> | 2020-10-05 16:41:49 +0100 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2020-10-05 16:41:49 +0100 |
commit | 86993310143c1347db6308a66c1f31a7a5644f56 (patch) | |
tree | 19154fee7049c1424ee3c043db12b5b6eb335963 | |
parent | 67e71619b95dc674c93bd11fb21b311bfc2fb95a (diff) |
Make ImportPrefix a configuration option
-rw-r--r-- | crates/assists/src/assist_config.rs | 5 | ||||
-rw-r--r-- | crates/assists/src/handlers/auto_import.rs | 16 | ||||
-rw-r--r-- | crates/hir/src/code_model.rs | 3 | ||||
-rw-r--r-- | crates/hir/src/lib.rs | 1 | ||||
-rw-r--r-- | crates/rust-analyzer/src/config.rs | 17 | ||||
-rw-r--r-- | 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 @@ | |||
4 | //! module, and we use to statically check that we only produce snippet | 4 | //! module, and we use to statically check that we only produce snippet |
5 | //! assists if we are allowed to. | 5 | //! assists if we are allowed to. |
6 | 6 | ||
7 | use hir::PrefixKind; | ||
8 | |||
7 | use crate::{utils::MergeBehaviour, AssistKind}; | 9 | use crate::{utils::MergeBehaviour, AssistKind}; |
8 | 10 | ||
9 | #[derive(Clone, Debug, PartialEq, Eq)] | 11 | #[derive(Clone, Debug, PartialEq, Eq)] |
@@ -37,10 +39,11 @@ impl Default for AssistConfig { | |||
37 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | 39 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
38 | pub struct InsertUseConfig { | 40 | pub struct InsertUseConfig { |
39 | pub merge: Option<MergeBehaviour>, | 41 | pub merge: Option<MergeBehaviour>, |
42 | pub prefix_kind: PrefixKind, | ||
40 | } | 43 | } |
41 | 44 | ||
42 | impl Default for InsertUseConfig { | 45 | impl Default for InsertUseConfig { |
43 | fn default() -> Self { | 46 | fn default() -> Self { |
44 | InsertUseConfig { merge: Some(MergeBehaviour::Full) } | 47 | InsertUseConfig { merge: Some(MergeBehaviour::Full), prefix_kind: PrefixKind::Plain } |
45 | } | 48 | } |
46 | } | 49 | } |
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 { | |||
191 | _ => Some(candidate), | 191 | _ => Some(candidate), |
192 | }) | 192 | }) |
193 | .filter_map(|candidate| match candidate { | 193 | .filter_map(|candidate| match candidate { |
194 | Either::Left(module_def) => { | 194 | Either::Left(module_def) => self.module_with_name_to_import.find_use_path_prefixed( |
195 | self.module_with_name_to_import.find_use_path_prefixed(db, module_def) | 195 | db, |
196 | } | 196 | module_def, |
197 | Either::Right(macro_def) => { | 197 | ctx.config.insert_use.prefix_kind, |
198 | self.module_with_name_to_import.find_use_path_prefixed(db, macro_def) | 198 | ), |
199 | } | 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 | ), | ||
200 | }) | 204 | }) |
201 | .filter(|use_path| !use_path.segments.is_empty()) | 205 | .filter(|use_path| !use_path.segments.is_empty()) |
202 | .take(20) | 206 | .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 { | |||
391 | self, | 391 | self, |
392 | db: &dyn DefDatabase, | 392 | db: &dyn DefDatabase, |
393 | item: impl Into<ItemInNs>, | 393 | item: impl Into<ItemInNs>, |
394 | prefix_kind: PrefixKind, | ||
394 | ) -> Option<ModPath> { | 395 | ) -> 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) |
396 | } | 397 | } |
397 | } | 398 | } |
398 | 399 | ||
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::{ | |||
48 | body::scope::ExprScopes, | 48 | body::scope::ExprScopes, |
49 | builtin_type::BuiltinType, | 49 | builtin_type::BuiltinType, |
50 | docs::Documentation, | 50 | docs::Documentation, |
51 | find_path::PrefixKind, | ||
51 | item_scope::ItemInNs, | 52 | item_scope::ItemInNs, |
52 | nameres::ModuleSource, | 53 | nameres::ModuleSource, |
53 | path::ModPath, | 54 | 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 @@ | |||
10 | use std::{ffi::OsString, path::PathBuf}; | 10 | use std::{ffi::OsString, path::PathBuf}; |
11 | 11 | ||
12 | use flycheck::FlycheckConfig; | 12 | use flycheck::FlycheckConfig; |
13 | use hir::PrefixKind; | ||
13 | use ide::{ | 14 | use ide::{ |
14 | AssistConfig, CompletionConfig, DiagnosticsConfig, HoverConfig, InlayHintsConfig, | 15 | AssistConfig, CompletionConfig, DiagnosticsConfig, HoverConfig, InlayHintsConfig, |
15 | MergeBehaviour, | 16 | MergeBehaviour, |
@@ -289,6 +290,11 @@ impl Config { | |||
289 | MergeBehaviourDef::Full => Some(MergeBehaviour::Full), | 290 | MergeBehaviourDef::Full => Some(MergeBehaviour::Full), |
290 | MergeBehaviourDef::Last => Some(MergeBehaviour::Last), | 291 | MergeBehaviourDef::Last => Some(MergeBehaviour::Last), |
291 | }; | 292 | }; |
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 | }; | ||
292 | 298 | ||
293 | self.call_info_full = data.callInfo_full; | 299 | self.call_info_full = data.callInfo_full; |
294 | 300 | ||
@@ -403,13 +409,21 @@ enum ManifestOrProjectJson { | |||
403 | } | 409 | } |
404 | 410 | ||
405 | #[derive(Deserialize)] | 411 | #[derive(Deserialize)] |
406 | #[serde(rename_all = "lowercase")] | 412 | #[serde(rename_all = "snake_case")] |
407 | enum MergeBehaviourDef { | 413 | enum MergeBehaviourDef { |
408 | None, | 414 | None, |
409 | Full, | 415 | Full, |
410 | Last, | 416 | Last, |
411 | } | 417 | } |
412 | 418 | ||
419 | #[derive(Deserialize)] | ||
420 | #[serde(rename_all = "snake_case")] | ||
421 | enum ImportPrefixDef { | ||
422 | Plain, | ||
423 | BySelf, | ||
424 | ByCrate, | ||
425 | } | ||
426 | |||
413 | macro_rules! config_data { | 427 | macro_rules! config_data { |
414 | (struct $name:ident { $($field:ident: $ty:ty = $default:expr,)*}) => { | 428 | (struct $name:ident { $($field:ident: $ty:ty = $default:expr,)*}) => { |
415 | #[allow(non_snake_case)] | 429 | #[allow(non_snake_case)] |
@@ -434,6 +448,7 @@ macro_rules! config_data { | |||
434 | config_data! { | 448 | config_data! { |
435 | struct ConfigData { | 449 | struct ConfigData { |
436 | assist_importMergeBehaviour: MergeBehaviourDef = MergeBehaviourDef::None, | 450 | assist_importMergeBehaviour: MergeBehaviourDef = MergeBehaviourDef::None, |
451 | assist_importPrefix: ImportPrefixDef = ImportPrefixDef::Plain, | ||
437 | 452 | ||
438 | callInfo_full: bool = true, | 453 | callInfo_full: bool = true, |
439 | 454 | ||
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 @@ | |||
652 | "default": "full", | 652 | "default": "full", |
653 | "description": "The strategy to use when inserting new imports or merging imports." | 653 | "description": "The strategy to use when inserting new imports or merging imports." |
654 | }, | 654 | }, |
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 | }, | ||
655 | "rust-analyzer.runnables.overrideCargo": { | 670 | "rust-analyzer.runnables.overrideCargo": { |
656 | "type": [ | 671 | "type": [ |
657 | "null", | 672 | "null", |
@@ -1033,4 +1048,4 @@ | |||
1033 | ] | 1048 | ] |
1034 | } | 1049 | } |
1035 | } | 1050 | } |
1036 | } | 1051 | } \ No newline at end of file |