From df6fa50f92af1232df5c044b8fe223817588ec8b Mon Sep 17 00:00:00 2001 From: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> Date: Fri, 31 Dec 2021 16:11:17 +0100 Subject: [PATCH] feat(diagnostics): add new config to fill default expression Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> --- crates/ide/src/lib.rs | 2 +- .../src/handlers/missing_fields.rs | 21 ++++++++------- crates/ide_diagnostics/src/lib.rs | 12 +++++++++ crates/rust-analyzer/src/config.rs | 27 +++++++++++++++++-- docs/user/generated_config.adoc | 5 ++++ editors/code/package.json | 15 ++++++++++- 6 files changed, 69 insertions(+), 13 deletions(-) diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index 16a2b4a113..b8482d0222 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -118,7 +118,7 @@ pub use ide_db::{ symbol_index::Query, RootDatabase, SymbolKind, }; -pub use ide_diagnostics::{Diagnostic, DiagnosticsConfig, Severity}; +pub use ide_diagnostics::{Diagnostic, DiagnosticsConfig, ExprFillDefaultMode, Severity}; pub use ide_ssr::SsrError; pub use syntax::{TextRange, TextSize}; pub use text_edit::{Indel, TextEdit}; diff --git a/crates/ide_diagnostics/src/handlers/missing_fields.rs b/crates/ide_diagnostics/src/handlers/missing_fields.rs index 715ac8f974..b5860fb858 100644 --- a/crates/ide_diagnostics/src/handlers/missing_fields.rs +++ b/crates/ide_diagnostics/src/handlers/missing_fields.rs @@ -64,15 +64,18 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option Some(make::ext::expr_todo()), + crate::ExprFillDefaultMode::DefaultImpl => { + let krate = ctx.sema.to_module_def(d.file.original_file(ctx.sema.db))?.krate(); + let default_trait = FamousDefs(&ctx.sema, Some(krate)).core_default_Default(); - match default_trait { - Some(default_trait) if ty.impls_trait(ctx.sema.db, default_trait, &[]) => { - Some(make::ext::expr_default()) + match default_trait { + Some(default_trait) if ty.impls_trait(ctx.sema.db, default_trait, &[]) => { + Some(make::ext::expr_default()) + } + _ => Some(make::ext::expr_todo()), } - _ => Some(make::ext::expr_todo()), } }; @@ -83,10 +86,10 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option Self { + Self::Todo + } +} + #[derive(Default, Debug, Clone)] pub struct DiagnosticsConfig { pub disable_experimental: bool, pub disabled: FxHashSet, + pub expr_fill_default: ExprFillDefaultMode, } struct DiagnosticsContext<'a> { diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 867f2bd1f0..9cef558457 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -11,8 +11,8 @@ use std::{ffi::OsString, iter, path::PathBuf}; use flycheck::FlycheckConfig; use ide::{ - AssistConfig, CompletionConfig, DiagnosticsConfig, HighlightRelatedConfig, HoverConfig, - HoverDocFormat, InlayHintsConfig, JoinLinesConfig, Snippet, SnippetScope, + AssistConfig, CompletionConfig, DiagnosticsConfig, ExprFillDefaultMode, HighlightRelatedConfig, + HoverConfig, HoverDocFormat, InlayHintsConfig, JoinLinesConfig, Snippet, SnippetScope, }; use ide_db::helpers::{ insert_use::{ImportGranularity, InsertUseConfig, PrefixKind}, @@ -45,6 +45,8 @@ use crate::{ // parsing the old name. config_data! { struct ConfigData { + /// How assists will fill missing elements in an expression. + assist_exprFillDefault: ExprFillDefaultDef = "\"todo\"", /// How imports should be grouped into use statements. assist_importGranularity | assist_importMergeBehavior | @@ -694,6 +696,10 @@ impl Config { DiagnosticsConfig { disable_experimental: !self.data.diagnostics_enableExperimental, disabled: self.data.diagnostics_disabled.clone(), + expr_fill_default: match self.data.assist_exprFillDefault { + ExprFillDefaultDef::Todo => ExprFillDefaultMode::Todo, + ExprFillDefaultDef::DefaultImpl => ExprFillDefaultMode::DefaultImpl, + }, } } pub fn diagnostics_map(&self) -> DiagnosticsMapConfig { @@ -1059,6 +1065,15 @@ enum ManifestOrProjectJson { ProjectJson(ProjectJsonData), } +#[derive(Deserialize, Debug, Clone)] +#[serde(rename_all = "snake_case")] +pub enum ExprFillDefaultDef { + #[serde(alias = "todo")] + Todo, + #[serde(alias = "defaultImpl")] + DefaultImpl, +} + #[derive(Deserialize, Debug, Clone)] #[serde(rename_all = "snake_case")] enum ImportGranularityDef { @@ -1253,6 +1268,14 @@ fn field_props(field: &str, ty: &str, doc: &[&str], default: &str) -> serde_json "Merge imports from the same module into a single `use` statement." ], }, + "ExprFillDefaultDef" => set! { + "type": "string", + "enum": ["todo", "defaultImpl"], + "enumDescriptions": [ + "Fill missing elements with 'todo' macro", + "Fill missing elements with Default::default()" + ], + }, "ImportGranularityDef" => set! { "type": "string", "enum": ["preserve", "crate", "module", "item"], diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc index 494051b306..28350374d1 100644 --- a/docs/user/generated_config.adoc +++ b/docs/user/generated_config.adoc @@ -1,3 +1,8 @@ +[[rust-analyzer.assist.exprFillDefault]]rust-analyzer.assist.exprFillDefault (default: `"todo"`):: ++ +-- +How assists will fill missing elements in an expression. +-- [[rust-analyzer.assist.importGranularity]]rust-analyzer.assist.importGranularity (default: `"crate"`):: + -- diff --git a/editors/code/package.json b/editors/code/package.json index 550b28e1cd..cbe9bf5442 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -378,6 +378,19 @@ "markdownDescription": "Optional settings passed to the debug engine. Example: `{ \"lldb\": { \"terminal\":\"external\"} }`" }, "$generated-start": {}, + "rust-analyzer.assist.exprFillDefault": { + "markdownDescription": "How assists will fill missing elements in an expression.", + "default": "todo", + "type": "string", + "enum": [ + "todo", + "defaultImpl" + ], + "enumDescriptions": [ + "Fill missing elements with 'todo' macro", + "Fill missing elements with Default::default()" + ] + }, "rust-analyzer.assist.importGranularity": { "markdownDescription": "How imports should be grouped into use statements.", "default": "crate", @@ -1494,4 +1507,4 @@ ] } } -} +} \ No newline at end of file