Skip to content

Commit adc4c6b

Browse files
committed
Make MergeBehaviour configurable
1 parent c862346 commit adc4c6b

File tree

9 files changed

+65
-17
lines changed

9 files changed

+65
-17
lines changed

crates/assists/src/assist_config.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
//! module, and we use to statically check that we only produce snippet
55
//! assists if we are allowed to.
66
7-
use crate::AssistKind;
7+
use crate::{utils::MergeBehaviour, AssistKind};
88

99
#[derive(Clone, Debug, PartialEq, Eq)]
1010
pub struct AssistConfig {
1111
pub snippet_cap: Option<SnippetCap>,
1212
pub allowed: Option<Vec<AssistKind>>,
13+
pub insert_use: InsertUseConfig,
1314
}
1415

1516
impl AssistConfig {
@@ -25,6 +26,21 @@ pub struct SnippetCap {
2526

2627
impl Default for AssistConfig {
2728
fn default() -> Self {
28-
AssistConfig { snippet_cap: Some(SnippetCap { _private: () }), allowed: None }
29+
AssistConfig {
30+
snippet_cap: Some(SnippetCap { _private: () }),
31+
allowed: None,
32+
insert_use: InsertUseConfig::default(),
33+
}
34+
}
35+
}
36+
37+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
38+
pub struct InsertUseConfig {
39+
pub merge: Option<MergeBehaviour>,
40+
}
41+
42+
impl Default for InsertUseConfig {
43+
fn default() -> Self {
44+
InsertUseConfig { merge: Some(MergeBehaviour::Full) }
2945
}
3046
}

crates/assists/src/handlers/auto_import.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ use syntax::{
1414
SyntaxNode,
1515
};
1616

17-
use crate::{
18-
utils::{insert_use, MergeBehaviour},
19-
AssistContext, AssistId, AssistKind, Assists, GroupLabel,
20-
};
17+
use crate::{utils::insert_use, AssistContext, AssistId, AssistKind, Assists, GroupLabel};
2118

2219
// Assist: auto_import
2320
//
@@ -60,7 +57,7 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
6057
let new_syntax = insert_use(
6158
&scope,
6259
make::path_from_text(&import.to_string()),
63-
Some(MergeBehaviour::Full),
60+
ctx.config.insert_use.merge,
6461
);
6562
builder.replace(syntax.text_range(), new_syntax.to_string())
6663
},

crates/assists/src/handlers/extract_struct_from_enum_variant.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ use syntax::{
1010
};
1111

1212
use crate::{
13-
assist_context::AssistBuilder,
14-
utils::{insert_use, MergeBehaviour},
15-
AssistContext, AssistId, AssistKind, Assists,
13+
assist_context::AssistBuilder, utils::insert_use, AssistContext, AssistId, AssistKind, Assists,
1614
};
1715
use ast::make;
1816
use insert_use::ImportScope;
@@ -117,7 +115,7 @@ fn insert_import(
117115
let new_syntax = insert_use(
118116
&scope,
119117
make::path_from_text(&mod_path.to_string()),
120-
Some(MergeBehaviour::Full),
118+
ctx.config.insert_use.merge,
121119
);
122120
// FIXME: this will currently panic as multiple imports will have overlapping text ranges
123121
builder.replace(syntax.text_range(), new_syntax.to_string())

crates/assists/src/handlers/replace_qualified_name_with_use.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use syntax::{algo::SyntaxRewriter, ast, match_ast, AstNode, SyntaxNode, TextRang
22
use test_utils::mark;
33

44
use crate::{
5-
utils::{insert_use, ImportScope, MergeBehaviour},
5+
utils::{insert_use, ImportScope},
66
AssistContext, AssistId, AssistKind, Assists,
77
};
88
use ast::make;
@@ -60,7 +60,7 @@ pub(crate) fn replace_qualified_name_with_use(
6060
let new_syntax = insert_use(
6161
import_scope,
6262
make::path_from_text(path_to_import),
63-
Some(MergeBehaviour::Full),
63+
ctx.config.insert_use.merge,
6464
);
6565
builder.replace(syntax.text_range(), new_syntax.to_string())
6666
}

crates/assists/src/utils.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ use syntax::{
1616

1717
use crate::assist_config::SnippetCap;
1818

19-
pub(crate) use insert_use::{insert_use, ImportScope, MergeBehaviour};
19+
pub use insert_use::MergeBehaviour;
20+
pub(crate) use insert_use::{insert_use, ImportScope};
2021

2122
pub(crate) fn unwrap_trivial_block(block: ast::BlockExpr) -> ast::Expr {
2223
extract_trivial_expression(&block)

crates/assists/src/utils/insert_use.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ fn common_prefix(lhs: &ast::Path, rhs: &ast::Path) -> Option<(ast::Path, ast::Pa
236236
}
237237

238238
/// What type of merges are allowed.
239-
#[derive(Copy, Clone, PartialEq, Eq)]
239+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
240240
pub enum MergeBehaviour {
241241
/// Merge everything together creating deeply nested imports.
242242
Full,

crates/ide/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ pub use crate::{
8181
},
8282
};
8383

84-
pub use assists::{Assist, AssistConfig, AssistId, AssistKind, ResolvedAssist};
84+
pub use assists::{
85+
utils::MergeBehaviour, Assist, AssistConfig, AssistId, AssistKind, ResolvedAssist,
86+
};
8587
pub use base_db::{
8688
Canceled, CrateGraph, CrateId, Edition, FileId, FilePosition, FileRange, SourceRoot,
8789
SourceRootId,

crates/rust-analyzer/src/config.rs

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

1212
use flycheck::FlycheckConfig;
13-
use ide::{AssistConfig, CompletionConfig, DiagnosticsConfig, HoverConfig, InlayHintsConfig};
13+
use ide::{
14+
AssistConfig, CompletionConfig, DiagnosticsConfig, HoverConfig, InlayHintsConfig,
15+
MergeBehaviour,
16+
};
1417
use lsp_types::ClientCapabilities;
1518
use project_model::{CargoConfig, ProjectJson, ProjectJsonData, ProjectManifest};
1619
use rustc_hash::FxHashSet;
@@ -263,6 +266,12 @@ impl Config {
263266
self.completion.add_call_parenthesis = data.completion_addCallParenthesis;
264267
self.completion.add_call_argument_snippets = data.completion_addCallArgumentSnippets;
265268

269+
self.assist.insert_use.merge = match data.assist_importMergeBehaviour {
270+
MergeBehaviourDef::None => None,
271+
MergeBehaviourDef::Full => Some(MergeBehaviour::Full),
272+
MergeBehaviourDef::Last => Some(MergeBehaviour::Last),
273+
};
274+
266275
self.call_info_full = data.callInfo_full;
267276

268277
self.lens = LensConfig {
@@ -370,6 +379,14 @@ enum ManifestOrProjectJson {
370379
ProjectJson(ProjectJsonData),
371380
}
372381

382+
#[derive(Deserialize)]
383+
#[serde(rename_all = "lowercase")]
384+
enum MergeBehaviourDef {
385+
None,
386+
Full,
387+
Last,
388+
}
389+
373390
macro_rules! config_data {
374391
(struct $name:ident { $($field:ident: $ty:ty = $default:expr,)*}) => {
375392
#[allow(non_snake_case)]
@@ -393,6 +410,8 @@ macro_rules! config_data {
393410

394411
config_data! {
395412
struct ConfigData {
413+
assist_importMergeBehaviour: MergeBehaviourDef = MergeBehaviourDef::None,
414+
396415
callInfo_full: bool = true,
397416

398417
cargo_autoreload: bool = true,

editors/code/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,21 @@
626626
},
627627
"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.",
628628
"default": []
629+
},
630+
"rust-analyzer.assist.importMergeBehaviour": {
631+
"type": "string",
632+
"enum": [
633+
"none",
634+
"full",
635+
"last"
636+
],
637+
"enumDescriptions": [
638+
"No merging",
639+
"Merge all layers of the import trees",
640+
"Only merge the last layer of the import trees"
641+
],
642+
"default": "full",
643+
"description": "The strategy to use when inserting new imports or merging imports."
629644
}
630645
}
631646
},

0 commit comments

Comments
 (0)