11145: feat: add config to use reasonable default expression instead of todo! when filling missing fields r=Veykril a=bnjjj

Use `Default::default()` in struct fields when we ask to fill it instead of putting `todo!()` for every fields

before:

```rust
pub enum Other {
    One,
    Two,
}

pub struct Test {
    text: String,
    num: usize,
    other: Other,
}

fn t_test() {
    let test = Test {<|>};
}
``` 

after: 

```rust
pub enum Other {
    One,
    Two,
}

pub struct Test {
    text: String,
    num: usize,
    other: Other,
}

fn t_test() {
    let test = Test {
        text: String::new(),
        num: 0,
        other: todo!(),
    };
}
``` 



Co-authored-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
Co-authored-by: Coenen Benjamin <benjamin.coenen@hotmail.com>
This commit is contained in:
bors[bot] 2022-01-07 14:10:11 +00:00 committed by GitHub
commit 40009e07d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 271 additions and 25 deletions

View file

@ -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 {
/// Placeholder for missing expressions in assists.
assist_exprFillDefault: ExprFillDefaultDef = "\"todo\"",
/// How imports should be grouped into use statements.
assist_importGranularity |
assist_importMergeBehavior |
@ -708,6 +710,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::Default => ExprFillDefaultMode::Default,
},
}
}
pub fn diagnostics_map(&self) -> DiagnosticsMapConfig {
@ -1079,6 +1085,15 @@ enum ManifestOrProjectJson {
ProjectJson(ProjectJsonData),
}
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
pub enum ExprFillDefaultDef {
#[serde(alias = "todo")]
Todo,
#[serde(alias = "default")]
Default,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
enum ImportGranularityDef {
@ -1285,6 +1300,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", "default"],
"enumDescriptions": [
"Fill missing expressions with the `todo` macro",
"Fill missing expressions with reasonable defaults, `new` or `default` constructors."
],
},
"ImportGranularityDef" => set! {
"type": "string",
"enum": ["preserve", "crate", "module", "item"],