Make MergeBehaviour configurable

This commit is contained in:
Lukas Wirth 2020-09-12 11:55:01 +02:00
parent c8623461a5
commit adc4c6b9d7
9 changed files with 65 additions and 17 deletions

View file

@ -4,12 +4,13 @@
//! module, and we use to statically check that we only produce snippet //! module, and we use to statically check that we only produce snippet
//! assists if we are allowed to. //! assists if we are allowed to.
use crate::AssistKind; use crate::{utils::MergeBehaviour, AssistKind};
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct AssistConfig { pub struct AssistConfig {
pub snippet_cap: Option<SnippetCap>, pub snippet_cap: Option<SnippetCap>,
pub allowed: Option<Vec<AssistKind>>, pub allowed: Option<Vec<AssistKind>>,
pub insert_use: InsertUseConfig,
} }
impl AssistConfig { impl AssistConfig {
@ -25,6 +26,21 @@ pub struct SnippetCap {
impl Default for AssistConfig { impl Default for AssistConfig {
fn default() -> Self { 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) }
} }
} }

View file

@ -14,10 +14,7 @@ use syntax::{
SyntaxNode, SyntaxNode,
}; };
use crate::{ use crate::{utils::insert_use, AssistContext, AssistId, AssistKind, Assists, GroupLabel};
utils::{insert_use, MergeBehaviour},
AssistContext, AssistId, AssistKind, Assists, GroupLabel,
};
// Assist: auto_import // Assist: auto_import
// //
@ -60,7 +57,7 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
let new_syntax = insert_use( let new_syntax = insert_use(
&scope, &scope,
make::path_from_text(&import.to_string()), make::path_from_text(&import.to_string()),
Some(MergeBehaviour::Full), ctx.config.insert_use.merge,
); );
builder.replace(syntax.text_range(), new_syntax.to_string()) builder.replace(syntax.text_range(), new_syntax.to_string())
}, },

View file

@ -10,9 +10,7 @@ use syntax::{
}; };
use crate::{ use crate::{
assist_context::AssistBuilder, assist_context::AssistBuilder, utils::insert_use, AssistContext, AssistId, AssistKind, Assists,
utils::{insert_use, MergeBehaviour},
AssistContext, AssistId, AssistKind, Assists,
}; };
use ast::make; use ast::make;
use insert_use::ImportScope; use insert_use::ImportScope;
@ -117,7 +115,7 @@ fn insert_import(
let new_syntax = insert_use( let new_syntax = insert_use(
&scope, &scope,
make::path_from_text(&mod_path.to_string()), 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 // FIXME: this will currently panic as multiple imports will have overlapping text ranges
builder.replace(syntax.text_range(), new_syntax.to_string()) builder.replace(syntax.text_range(), new_syntax.to_string())

View file

@ -2,7 +2,7 @@ use syntax::{algo::SyntaxRewriter, ast, match_ast, AstNode, SyntaxNode, TextRang
use test_utils::mark; use test_utils::mark;
use crate::{ use crate::{
utils::{insert_use, ImportScope, MergeBehaviour}, utils::{insert_use, ImportScope},
AssistContext, AssistId, AssistKind, Assists, AssistContext, AssistId, AssistKind, Assists,
}; };
use ast::make; use ast::make;
@ -60,7 +60,7 @@ pub(crate) fn replace_qualified_name_with_use(
let new_syntax = insert_use( let new_syntax = insert_use(
import_scope, import_scope,
make::path_from_text(path_to_import), make::path_from_text(path_to_import),
Some(MergeBehaviour::Full), ctx.config.insert_use.merge,
); );
builder.replace(syntax.text_range(), new_syntax.to_string()) builder.replace(syntax.text_range(), new_syntax.to_string())
} }

View file

@ -16,7 +16,8 @@ use syntax::{
use crate::assist_config::SnippetCap; 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 { pub(crate) fn unwrap_trivial_block(block: ast::BlockExpr) -> ast::Expr {
extract_trivial_expression(&block) extract_trivial_expression(&block)

View 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. /// What type of merges are allowed.
#[derive(Copy, Clone, PartialEq, Eq)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum MergeBehaviour { pub enum MergeBehaviour {
/// Merge everything together creating deeply nested imports. /// Merge everything together creating deeply nested imports.
Full, Full,

View file

@ -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::{ pub use base_db::{
Canceled, CrateGraph, CrateId, Edition, FileId, FilePosition, FileRange, SourceRoot, Canceled, CrateGraph, CrateId, Edition, FileId, FilePosition, FileRange, SourceRoot,
SourceRootId, SourceRootId,

View file

@ -10,7 +10,10 @@
use std::{ffi::OsString, path::PathBuf}; use std::{ffi::OsString, path::PathBuf};
use flycheck::FlycheckConfig; use flycheck::FlycheckConfig;
use ide::{AssistConfig, CompletionConfig, DiagnosticsConfig, HoverConfig, InlayHintsConfig}; use ide::{
AssistConfig, CompletionConfig, DiagnosticsConfig, HoverConfig, InlayHintsConfig,
MergeBehaviour,
};
use lsp_types::ClientCapabilities; use lsp_types::ClientCapabilities;
use project_model::{CargoConfig, ProjectJson, ProjectJsonData, ProjectManifest}; use project_model::{CargoConfig, ProjectJson, ProjectJsonData, ProjectManifest};
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
@ -263,6 +266,12 @@ impl Config {
self.completion.add_call_parenthesis = data.completion_addCallParenthesis; self.completion.add_call_parenthesis = data.completion_addCallParenthesis;
self.completion.add_call_argument_snippets = data.completion_addCallArgumentSnippets; 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.call_info_full = data.callInfo_full;
self.lens = LensConfig { self.lens = LensConfig {
@ -370,6 +379,14 @@ enum ManifestOrProjectJson {
ProjectJson(ProjectJsonData), ProjectJson(ProjectJsonData),
} }
#[derive(Deserialize)]
#[serde(rename_all = "lowercase")]
enum MergeBehaviourDef {
None,
Full,
Last,
}
macro_rules! config_data { macro_rules! config_data {
(struct $name:ident { $($field:ident: $ty:ty = $default:expr,)*}) => { (struct $name:ident { $($field:ident: $ty:ty = $default:expr,)*}) => {
#[allow(non_snake_case)] #[allow(non_snake_case)]
@ -393,6 +410,8 @@ macro_rules! config_data {
config_data! { config_data! {
struct ConfigData { struct ConfigData {
assist_importMergeBehaviour: MergeBehaviourDef = MergeBehaviourDef::None,
callInfo_full: bool = true, callInfo_full: bool = true,
cargo_autoreload: bool = true, cargo_autoreload: bool = true,

View file

@ -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.", "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": [] "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."
} }
} }
}, },