From adc4c6b9d7fd91827c4cf383a1e95dc7e27138e0 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 12 Sep 2020 11:55:01 +0200 Subject: [PATCH] Make MergeBehaviour configurable --- crates/assists/src/assist_config.rs | 20 ++++++++++++++++-- crates/assists/src/handlers/auto_import.rs | 7 ++----- .../extract_struct_from_enum_variant.rs | 6 ++---- .../replace_qualified_name_with_use.rs | 4 ++-- crates/assists/src/utils.rs | 3 ++- crates/assists/src/utils/insert_use.rs | 2 +- crates/ide/src/lib.rs | 4 +++- crates/rust-analyzer/src/config.rs | 21 ++++++++++++++++++- editors/code/package.json | 15 +++++++++++++ 9 files changed, 65 insertions(+), 17 deletions(-) diff --git a/crates/assists/src/assist_config.rs b/crates/assists/src/assist_config.rs index cda2abfb9c..adf02edabe 100644 --- a/crates/assists/src/assist_config.rs +++ b/crates/assists/src/assist_config.rs @@ -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, pub allowed: Option>, + 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, +} + +impl Default for InsertUseConfig { + fn default() -> Self { + InsertUseConfig { merge: Some(MergeBehaviour::Full) } } } diff --git a/crates/assists/src/handlers/auto_import.rs b/crates/assists/src/handlers/auto_import.rs index 66e8191548..b5eb2c7226 100644 --- a/crates/assists/src/handlers/auto_import.rs +++ b/crates/assists/src/handlers/auto_import.rs @@ -14,10 +14,7 @@ use syntax::{ 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()) }, diff --git a/crates/assists/src/handlers/extract_struct_from_enum_variant.rs b/crates/assists/src/handlers/extract_struct_from_enum_variant.rs index 80c62d8bba..3ea50f375c 100644 --- a/crates/assists/src/handlers/extract_struct_from_enum_variant.rs +++ b/crates/assists/src/handlers/extract_struct_from_enum_variant.rs @@ -10,9 +10,7 @@ use syntax::{ }; 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()) diff --git a/crates/assists/src/handlers/replace_qualified_name_with_use.rs b/crates/assists/src/handlers/replace_qualified_name_with_use.rs index 85c70d16b6..e48407fcc3 100644 --- a/crates/assists/src/handlers/replace_qualified_name_with_use.rs +++ b/crates/assists/src/handlers/replace_qualified_name_with_use.rs @@ -2,7 +2,7 @@ use syntax::{algo::SyntaxRewriter, ast, match_ast, AstNode, SyntaxNode, TextRang 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()) } diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs index 7559ddd638..b0511ceb68 100644 --- a/crates/assists/src/utils.rs +++ b/crates/assists/src/utils.rs @@ -16,7 +16,8 @@ use syntax::{ 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) diff --git a/crates/assists/src/utils/insert_use.rs b/crates/assists/src/utils/insert_use.rs index 98553b2e08..6d110aaaf7 100644 --- a/crates/assists/src/utils/insert_use.rs +++ b/crates/assists/src/utils/insert_use.rs @@ -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, diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index 570790384e..3b97e087f8 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -81,7 +81,9 @@ pub use crate::{ }, }; -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, diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 187273bbcb..1a74286f53 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -10,7 +10,10 @@ 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 @@ impl Config { 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 @@ macro_rules! config_data { config_data! { struct ConfigData { + assist_importMergeBehaviour: MergeBehaviourDef = MergeBehaviourDef::None, + callInfo_full: bool = true, cargo_autoreload: bool = true, diff --git a/editors/code/package.json b/editors/code/package.json index 6fc4464dfa..858e4c4f5e 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -626,6 +626,21 @@ }, "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." } } },