]> git.lizzy.rs Git - rust.git/commitdiff
Make MergeBehaviour configurable
authorLukas Wirth <lukastw97@gmail.com>
Sat, 12 Sep 2020 09:55:01 +0000 (11:55 +0200)
committerLukas Wirth <lukastw97@gmail.com>
Sat, 12 Sep 2020 10:11:16 +0000 (12:11 +0200)
crates/assists/src/assist_config.rs
crates/assists/src/handlers/auto_import.rs
crates/assists/src/handlers/extract_struct_from_enum_variant.rs
crates/assists/src/handlers/replace_qualified_name_with_use.rs
crates/assists/src/utils.rs
crates/assists/src/utils/insert_use.rs
crates/ide/src/lib.rs
crates/rust-analyzer/src/config.rs
editors/code/package.json

index cda2abfb9c2fdf49ff322a248c8bf30729150cf0..adf02edabe7b8e6eb0ab6d10c43abeb1ec5a3154 100644 (file)
@@ -4,12 +4,13 @@
 //! module, and we use to statically check that we only produce snippet
 //! assists if we are allowed to.
 
-use crate::AssistKind;
+use crate::{utils::MergeBehaviour, AssistKind};
 
 #[derive(Clone, Debug, PartialEq, Eq)]
 pub struct AssistConfig {
     pub snippet_cap: Option<SnippetCap>,
     pub allowed: Option<Vec<AssistKind>>,
+    pub insert_use: InsertUseConfig,
 }
 
 impl AssistConfig {
@@ -25,6 +26,21 @@ pub struct SnippetCap {
 
 impl Default for AssistConfig {
     fn default() -> Self {
-        AssistConfig { snippet_cap: Some(SnippetCap { _private: () }), allowed: None }
+        AssistConfig {
+            snippet_cap: Some(SnippetCap { _private: () }),
+            allowed: None,
+            insert_use: InsertUseConfig::default(),
+        }
+    }
+}
+
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub struct InsertUseConfig {
+    pub merge: Option<MergeBehaviour>,
+}
+
+impl Default for InsertUseConfig {
+    fn default() -> Self {
+        InsertUseConfig { merge: Some(MergeBehaviour::Full) }
     }
 }
index 66e8191548a5c391749c51080330fd4d5cdbb10a..b5eb2c7226eee0efce5b9eec7cba1eb5e5dff110 100644 (file)
     SyntaxNode,
 };
 
-use crate::{
-    utils::{insert_use, MergeBehaviour},
-    AssistContext, AssistId, AssistKind, Assists, GroupLabel,
-};
+use crate::{utils::insert_use, AssistContext, AssistId, AssistKind, Assists, GroupLabel};
 
 // Assist: auto_import
 //
@@ -60,7 +57,7 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
                 let new_syntax = insert_use(
                     &scope,
                     make::path_from_text(&import.to_string()),
-                    Some(MergeBehaviour::Full),
+                    ctx.config.insert_use.merge,
                 );
                 builder.replace(syntax.text_range(), new_syntax.to_string())
             },
index 80c62d8bba7ff78ded65d04ca5dad2fb5e93701b..3ea50f375c1923d595bd587f66a4d84ea44a1174 100644 (file)
@@ -10,9 +10,7 @@
 };
 
 use crate::{
-    assist_context::AssistBuilder,
-    utils::{insert_use, MergeBehaviour},
-    AssistContext, AssistId, AssistKind, Assists,
+    assist_context::AssistBuilder, utils::insert_use, AssistContext, AssistId, AssistKind, Assists,
 };
 use ast::make;
 use insert_use::ImportScope;
@@ -117,7 +115,7 @@ fn insert_import(
         let new_syntax = insert_use(
             &scope,
             make::path_from_text(&mod_path.to_string()),
-            Some(MergeBehaviour::Full),
+            ctx.config.insert_use.merge,
         );
         // FIXME: this will currently panic as multiple imports will have overlapping text ranges
         builder.replace(syntax.text_range(), new_syntax.to_string())
index 85c70d16b627fea1d0ff16dbf59e063a7d4d9741..e48407fcc3696711e7ea54a62ef1603771c43d2e 100644 (file)
@@ -2,7 +2,7 @@
 use test_utils::mark;
 
 use crate::{
-    utils::{insert_use, ImportScope, MergeBehaviour},
+    utils::{insert_use, ImportScope},
     AssistContext, AssistId, AssistKind, Assists,
 };
 use ast::make;
@@ -60,7 +60,7 @@ pub(crate) fn replace_qualified_name_with_use(
                 let new_syntax = insert_use(
                     import_scope,
                     make::path_from_text(path_to_import),
-                    Some(MergeBehaviour::Full),
+                    ctx.config.insert_use.merge,
                 );
                 builder.replace(syntax.text_range(), new_syntax.to_string())
             }
index 7559ddd6381177c963dc04caf436201a47b09584..b0511ceb680798873231dceaefa01dbd63759e06 100644 (file)
@@ -16,7 +16,8 @@
 
 use crate::assist_config::SnippetCap;
 
-pub(crate) use insert_use::{insert_use, ImportScope, MergeBehaviour};
+pub use insert_use::MergeBehaviour;
+pub(crate) use insert_use::{insert_use, ImportScope};
 
 pub(crate) fn unwrap_trivial_block(block: ast::BlockExpr) -> ast::Expr {
     extract_trivial_expression(&block)
index 98553b2e0866a67de4ba14fdbaa0d781c79d8ce6..6d110aaaf728973db3edd39511b3ad4bfc81f8f1 100644 (file)
@@ -236,7 +236,7 @@ fn common_prefix(lhs: &ast::Path, rhs: &ast::Path) -> Option<(ast::Path, ast::Pa
 }
 
 /// What type of merges are allowed.
-#[derive(Copy, Clone, PartialEq, Eq)]
+#[derive(Copy, Clone, Debug, PartialEq, Eq)]
 pub enum MergeBehaviour {
     /// Merge everything together creating deeply nested imports.
     Full,
index 570790384ee59d2e80afb023b3beb6f0f2d88d19..3b97e087f88335a30ca69c80f3662c38761c3d90 100644 (file)
@@ -81,7 +81,9 @@ macro_rules! eprintln {
     },
 };
 
-pub use assists::{Assist, AssistConfig, AssistId, AssistKind, ResolvedAssist};
+pub use assists::{
+    utils::MergeBehaviour, Assist, AssistConfig, AssistId, AssistKind, ResolvedAssist,
+};
 pub use base_db::{
     Canceled, CrateGraph, CrateId, Edition, FileId, FilePosition, FileRange, SourceRoot,
     SourceRootId,
index 187273bbcb438caeb3ee29487d9c9b5ee945dec5..1a74286f5367b0b966fb081366635f428ddeb893 100644 (file)
 use std::{ffi::OsString, path::PathBuf};
 
 use flycheck::FlycheckConfig;
-use ide::{AssistConfig, CompletionConfig, DiagnosticsConfig, HoverConfig, InlayHintsConfig};
+use ide::{
+    AssistConfig, CompletionConfig, DiagnosticsConfig, HoverConfig, InlayHintsConfig,
+    MergeBehaviour,
+};
 use lsp_types::ClientCapabilities;
 use project_model::{CargoConfig, ProjectJson, ProjectJsonData, ProjectManifest};
 use rustc_hash::FxHashSet;
@@ -263,6 +266,12 @@ pub fn update(&mut self, json: serde_json::Value) {
         self.completion.add_call_parenthesis = data.completion_addCallParenthesis;
         self.completion.add_call_argument_snippets = data.completion_addCallArgumentSnippets;
 
+        self.assist.insert_use.merge = match data.assist_importMergeBehaviour {
+            MergeBehaviourDef::None => None,
+            MergeBehaviourDef::Full => Some(MergeBehaviour::Full),
+            MergeBehaviourDef::Last => Some(MergeBehaviour::Last),
+        };
+
         self.call_info_full = data.callInfo_full;
 
         self.lens = LensConfig {
@@ -370,6 +379,14 @@ enum ManifestOrProjectJson {
     ProjectJson(ProjectJsonData),
 }
 
+#[derive(Deserialize)]
+#[serde(rename_all = "lowercase")]
+enum MergeBehaviourDef {
+    None,
+    Full,
+    Last,
+}
+
 macro_rules! config_data {
     (struct $name:ident { $($field:ident: $ty:ty = $default:expr,)*}) => {
         #[allow(non_snake_case)]
@@ -393,6 +410,8 @@ fn from_json(mut json: serde_json::Value) -> $name {
 
 config_data! {
     struct ConfigData {
+        assist_importMergeBehaviour: MergeBehaviourDef = MergeBehaviourDef::None,
+
         callInfo_full: bool = true,
 
         cargo_autoreload: bool           = true,
index 6fc4464dfacb0f85904f2383a88c3aaedd959263..858e4c4f5eeb457feeffd3587695499a83cc155f 100644 (file)
                     },
                     "description": "List of warnings that should be displayed with hint severity.\nThe warnings will be indicated by faded text or three dots in code and will not show up in the problems panel.",
                     "default": []
+                },
+                "rust-analyzer.assist.importMergeBehaviour": {
+                    "type": "string",
+                    "enum": [
+                        "none",
+                        "full",
+                        "last"
+                    ],
+                    "enumDescriptions": [
+                        "No merging",
+                        "Merge all layers of the import trees",
+                        "Only merge the last layer of the import trees"
+                    ],
+                    "default": "full",
+                    "description": "The strategy to use when inserting new imports or merging imports."
                 }
             }
         },