From c4bdb8e516e9f927f37a324185905fb9be387f57 Mon Sep 17 00:00:00 2001 From: feniljain Date: Fri, 7 Oct 2022 00:11:02 +0530 Subject: [PATCH 1/3] feat: add config for inserting must_use in `generate_enum_as_method` --- crates/ide-assists/src/assist_config.rs | 1 + .../generate_enum_projection_method.rs | 27 +++++++++++++++---- crates/ide-assists/src/tests.rs | 1 + crates/rust-analyzer/src/config.rs | 4 +++ docs/user/generated_config.adoc | 6 +++++ editors/code/package.json | 5 ++++ 6 files changed, 39 insertions(+), 5 deletions(-) diff --git a/crates/ide-assists/src/assist_config.rs b/crates/ide-assists/src/assist_config.rs index 60d1588a44..b273ebc85a 100644 --- a/crates/ide-assists/src/assist_config.rs +++ b/crates/ide-assists/src/assist_config.rs @@ -14,4 +14,5 @@ pub struct AssistConfig { pub allowed: Option>, pub insert_use: InsertUseConfig, pub prefer_no_std: bool, + pub assist_emit_must_use: bool, } diff --git a/crates/ide-assists/src/handlers/generate_enum_projection_method.rs b/crates/ide-assists/src/handlers/generate_enum_projection_method.rs index 402ab1ee79..732ee49f66 100644 --- a/crates/ide-assists/src/handlers/generate_enum_projection_method.rs +++ b/crates/ide-assists/src/handlers/generate_enum_projection_method.rs @@ -124,6 +124,7 @@ fn generate_enum_projection_method( happy_case, sad_case, } = props; + let variant = ctx.find_node_at_offset::()?; let variant_name = variant.name()?; let parent_enum = ast::Adt::Enum(variant.parent_enum()); @@ -144,7 +145,7 @@ fn generate_enum_projection_method( ast::StructKind::Unit => return None, }; - let fn_name = format!("{}_{}", fn_name_prefix, &to_lower_snake_case(&variant_name.text())); + let fn_name = format!("{fn_name_prefix}_{}", &to_lower_snake_case(&variant_name.text())); // Return early if we've found an existing new fn let impl_def = find_struct_impl(ctx, &parent_enum, &fn_name)?; @@ -156,15 +157,31 @@ fn generate_enum_projection_method( assist_description, target, |builder| { - let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{v} ")); - let method = format!( - " {vis}fn {fn_name}({self_param}) -> {return_prefix}{field_type}{return_suffix} {{ + let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v)); + + let field_type_syntax = field_type.syntax(); + + let method = if ctx.config.assist_emit_must_use + { + format!( + " #[must_use] + {vis}fn {fn_name}({self_param}) -> {return_prefix}{field_type_syntax}{return_suffix} {{ if let Self::{variant_name}{pattern_suffix} = self {{ {happy_case}({bound_name}) }} else {{ {sad_case} }} - }}"); + }}") + } else { + format!( + " {vis}fn {fn_name}({self_param}) -> {return_prefix}{field_type_syntax}{return_suffix} {{ + if let Self::{variant_name}{pattern_suffix} = self {{ + {happy_case}({bound_name}) + }} else {{ + {sad_case} + }} + }}") + }; add_method_to_adt(builder, &parent_enum, impl_def, &method); }, diff --git a/crates/ide-assists/src/tests.rs b/crates/ide-assists/src/tests.rs index f7f2417d07..92ced27c78 100644 --- a/crates/ide-assists/src/tests.rs +++ b/crates/ide-assists/src/tests.rs @@ -30,6 +30,7 @@ pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig { skip_glob_imports: true, }, prefer_no_std: false, + assist_emit_must_use: false, }; pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 577a8640a4..21d7538fdc 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -56,6 +56,9 @@ mod patch_old_style; // parsing the old name. config_data! { struct ConfigData { + /// Whether to insert must_use derive macro while generating `as_` methods + /// for enum variants. + assist_emitMustUse: bool = "false", /// Placeholder expression to use for missing expressions in assists. assist_expressionFillDefault: ExprFillDefaultDef = "\"todo\"", @@ -1227,6 +1230,7 @@ impl Config { allowed: None, insert_use: self.insert_use_config(), prefer_no_std: self.data.imports_prefer_no_std, + assist_emit_must_use: self.data.assist_emitMustUse, } } diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc index acf0aaea85..82ec1d56f2 100644 --- a/docs/user/generated_config.adoc +++ b/docs/user/generated_config.adoc @@ -1,3 +1,9 @@ +[[rust-analyzer.assist.emitMustUse]]rust-analyzer.assist.emitMustUse (default: `false`):: ++ +-- +Whether to insert must_use derive macro while generating `as_` methods +for enum variants. +-- [[rust-analyzer.assist.expressionFillDefault]]rust-analyzer.assist.expressionFillDefault (default: `"todo"`):: + -- diff --git a/editors/code/package.json b/editors/code/package.json index 1afe2087c7..1446a60377 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -397,6 +397,11 @@ "type": "boolean" }, "$generated-start": {}, + "rust-analyzer.assist.emitMustUse": { + "markdownDescription": "Whether to insert must_use derive macro while generating `as_` methods\nfor enum variants.", + "default": false, + "type": "boolean" + }, "rust-analyzer.assist.expressionFillDefault": { "markdownDescription": "Placeholder expression to use for missing expressions in assists.", "default": "todo", From 4bf9b9b0031f64755bfa102a3cf9363aadea98c5 Mon Sep 17 00:00:00 2001 From: feniljain Date: Mon, 24 Oct 2022 21:06:32 +0530 Subject: [PATCH 2/3] refactor: remove repetitive string interpolation and doc changes --- .../generate_enum_projection_method.rs | 24 +++++++------------ crates/rust-analyzer/src/config.rs | 2 +- docs/user/generated_config.adoc | 2 +- editors/code/package.json | 2 +- 4 files changed, 12 insertions(+), 18 deletions(-) diff --git a/crates/ide-assists/src/handlers/generate_enum_projection_method.rs b/crates/ide-assists/src/handlers/generate_enum_projection_method.rs index 732ee49f66..ad88a04ce8 100644 --- a/crates/ide-assists/src/handlers/generate_enum_projection_method.rs +++ b/crates/ide-assists/src/handlers/generate_enum_projection_method.rs @@ -161,27 +161,21 @@ fn generate_enum_projection_method( let field_type_syntax = field_type.syntax(); - let method = if ctx.config.assist_emit_must_use - { - format!( - " #[must_use] - {vis}fn {fn_name}({self_param}) -> {return_prefix}{field_type_syntax}{return_suffix} {{ - if let Self::{variant_name}{pattern_suffix} = self {{ - {happy_case}({bound_name}) - }} else {{ - {sad_case} - }} - }}") + let must_use = if ctx.config.assist_emit_must_use { + "#[must_use]\n" } else { - format!( - " {vis}fn {fn_name}({self_param}) -> {return_prefix}{field_type_syntax}{return_suffix} {{ + "" + }; + + let method = format!( + " {must_use}{vis}fn {fn_name}({self_param}) -> {return_prefix}{field_type_syntax}{return_suffix} {{ if let Self::{variant_name}{pattern_suffix} = self {{ {happy_case}({bound_name}) }} else {{ {sad_case} }} - }}") - }; + }}" + ); add_method_to_adt(builder, &parent_enum, impl_def, &method); }, diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 21d7538fdc..9bd0e22d8f 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -56,7 +56,7 @@ mod patch_old_style; // parsing the old name. config_data! { struct ConfigData { - /// Whether to insert must_use derive macro while generating `as_` methods + /// Whether to insert #[must_use] when generating `as_` methods /// for enum variants. assist_emitMustUse: bool = "false", /// Placeholder expression to use for missing expressions in assists. diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc index 82ec1d56f2..152b7c5d81 100644 --- a/docs/user/generated_config.adoc +++ b/docs/user/generated_config.adoc @@ -1,7 +1,7 @@ [[rust-analyzer.assist.emitMustUse]]rust-analyzer.assist.emitMustUse (default: `false`):: + -- -Whether to insert must_use derive macro while generating `as_` methods +Whether to insert #[must_use] when generating `as_` methods for enum variants. -- [[rust-analyzer.assist.expressionFillDefault]]rust-analyzer.assist.expressionFillDefault (default: `"todo"`):: diff --git a/editors/code/package.json b/editors/code/package.json index 1446a60377..a25e431384 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -398,7 +398,7 @@ }, "$generated-start": {}, "rust-analyzer.assist.emitMustUse": { - "markdownDescription": "Whether to insert must_use derive macro while generating `as_` methods\nfor enum variants.", + "markdownDescription": "Whether to insert #[must_use] when generating `as_` methods\nfor enum variants.", "default": false, "type": "boolean" }, From 691ce306df39b4c99f7f5b2a366b603532a3b063 Mon Sep 17 00:00:00 2001 From: feniljain <49019259+feniljain@users.noreply.github.com> Date: Wed, 2 Nov 2022 16:09:12 +0530 Subject: [PATCH 3/3] fix: indentation after inserting `#must_use` Co-authored-by: Lukas Wirth --- .../ide-assists/src/handlers/generate_enum_projection_method.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ide-assists/src/handlers/generate_enum_projection_method.rs b/crates/ide-assists/src/handlers/generate_enum_projection_method.rs index ad88a04ce8..ad0cc6d836 100644 --- a/crates/ide-assists/src/handlers/generate_enum_projection_method.rs +++ b/crates/ide-assists/src/handlers/generate_enum_projection_method.rs @@ -162,7 +162,7 @@ fn generate_enum_projection_method( let field_type_syntax = field_type.syntax(); let must_use = if ctx.config.assist_emit_must_use { - "#[must_use]\n" + "#[must_use]\n " } else { "" };