Skip to content

Commit b40bd7e

Browse files
committed
LS: include type name in generate variant code action title
1 parent 52ed981 commit b40bd7e

3 files changed

Lines changed: 51 additions & 17 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,10 @@
403403

404404
([Daniel Venable](https://github.com/DanielVenable))
405405

406+
- The "Generate Variant" code action now includes the name of the type to be
407+
generated in its title.
408+
([0xda157](https://github.com/0xda157))
409+
406410
### Formatter
407411

408412
- Performance of the formatter has been improved.

language-server/src/code_action.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6919,6 +6919,7 @@ pub struct GenerateVariant<'a, IO> {
69196919
params: &'a CodeActionParams,
69206920
line_numbers: &'a LineNumbers,
69216921
variant_to_generate: Option<VariantToGenerate<'a>>,
6922+
printer: Printer<'a>,
69226923
}
69236924

69246925
struct VariantToGenerate<'a> {
@@ -6964,6 +6965,9 @@ struct VariantToGenerate<'a> {
69646965
///
69656966
module_name: EcoString,
69666967

6968+
/// The type to add this variant to.
6969+
type_: Arc<Type>,
6970+
69676971
/// The arguments actually supplied as input to the variant, if any.
69686972
/// A variant to generate might as well be just a name passed as an argument
69696973
/// `list.map([1, 2, 3], ToGenerate)` so it's not guaranteed to actually
@@ -7060,6 +7064,7 @@ impl<'a, IO> GenerateVariant<'a, IO> {
70607064
compiler,
70617065
line_numbers,
70627066
variant_to_generate: None,
7067+
printer: Printer::new(&module.ast.names),
70637068
}
70647069
}
70657070

@@ -7068,6 +7073,7 @@ impl<'a, IO> GenerateVariant<'a, IO> {
70687073

70697074
let Some(VariantToGenerate {
70707075
name,
7076+
type_,
70717077
constructors,
70727078
arguments_types,
70737079
given_arguments,
@@ -7126,9 +7132,12 @@ impl<'a, IO> GenerateVariant<'a, IO> {
71267132
);
71277133
}
71287134

7129-
let mut builder = CodeActionBuilder::new("Generate variant")
7130-
.kind(CodeActionKind::QuickFix)
7131-
.preferred(true);
7135+
let mut builder = CodeActionBuilder::new(&format!(
7136+
"Generate `{}` variant",
7137+
self.printer.print_type(&type_)
7138+
))
7139+
.kind(CodeActionKind::QuickFix)
7140+
.preferred(true);
71327141

71337142
match edits {
71347143
GenerateVariantEdits::GenerateInCurrentModule {
@@ -7289,6 +7298,7 @@ impl<'a, IO> GenerateVariant<'a, IO> {
72897298
arguments_types,
72907299
given_arguments,
72917300
module_name,
7301+
type_: custom_type,
72927302
end_position,
72937303
type_braces,
72947304
variant_start: function_name_location.start,

language-server/src/tests/action.rs

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ const INTERPOLATE_STRING: &str = "Interpolate string";
193193
const FILL_UNUSED_FIELDS: &str = "Fill unused fields";
194194
const REMOVE_ALL_ECHOS_FROM_THIS_MODULE: &str = "Remove all `echo`s from this module";
195195
const WRAP_IN_BLOCK: &str = "Wrap in block";
196-
const GENERATE_VARIANT: &str = "Generate variant";
197196
const REMOVE_BLOCK: &str = "Remove block";
198197
const REMOVE_OPAQUE_FROM_PRIVATE_TYPE: &str = "Remove opaque from private type";
199198
const COLLAPSE_NESTED_CASE: &str = "Collapse nested case";
@@ -211,6 +210,10 @@ const ADD_EXTRA_PARENTHESES: &str = "Add extra parentheses";
211210
const CONVERT_TO_DOCUMENTATION_COMMENT: &str = "Convert to documentation comment";
212211
const CONVERT_TO_REGULAR_COMMENT: &str = "Convert to regular comment";
213212

213+
fn generate_variant_message(type_name: &str) -> String {
214+
format!("Generate `{type_name}` variant")
215+
}
216+
214217
macro_rules! assert_code_action {
215218
($title:expr, $code:literal, $range_selector:expr $(,)?) => {
216219
let project = TestProject::for_source($code);
@@ -291,6 +294,23 @@ macro_rules! assert_no_code_actions {
291294
);
292295
assert_eq!(expected, result);
293296
};
297+
298+
($title:expr, $code:literal, $range_selector:expr $(,)?) => {
299+
let project = TestProject::for_source($code);
300+
assert_no_code_actions!($title, project, $range_selector);
301+
};
302+
303+
($title:expr, $project:expr, $range_selector:expr $(,)?) => {
304+
let expected: Vec<lsp_types::CodeAction> = vec![];
305+
let result = actions_with_title(
306+
vec![$title],
307+
&$project,
308+
Origin::Src,
309+
LSP_TEST_ROOT_PACKAGE_NAME,
310+
$range_selector
311+
);
312+
assert_eq!(expected, result);
313+
};
294314
}
295315

296316
#[test]
@@ -322,7 +342,7 @@ pub fn main() {
322342
#[test]
323343
fn generate_variant_with_fields_in_same_module() {
324344
assert_code_action!(
325-
GENERATE_VARIANT,
345+
&generate_variant_message("Wibble"),
326346
r#"
327347
pub type Wibble {
328348
Wibble
@@ -338,7 +358,7 @@ pub fn main() -> Wibble {
338358
#[test]
339359
fn generate_variant_with_no_fields_in_same_module() {
340360
assert_code_action!(
341-
GENERATE_VARIANT,
361+
&generate_variant_message("Wibble"),
342362
r#"
343363
pub type Wibble {
344364
Wibble
@@ -354,7 +374,7 @@ pub fn main() -> Wibble {
354374
#[test]
355375
fn generate_variant_with_labels_in_same_module() {
356376
assert_code_action!(
357-
GENERATE_VARIANT,
377+
&generate_variant_message("Wibble"),
358378
r#"
359379
pub type Wibble {
360380
Wibble
@@ -370,7 +390,7 @@ pub fn main() -> Wibble {
370390
#[test]
371391
fn generate_variant_from_pattern_with_fields() {
372392
assert_code_action!(
373-
GENERATE_VARIANT,
393+
&generate_variant_message("Wibble"),
374394
r#"
375395
pub type Wibble {
376396
Wibble
@@ -390,7 +410,7 @@ pub fn main() -> Wibble {
390410
#[test]
391411
fn generate_variant_from_pattern_with_labelled_fields() {
392412
assert_code_action!(
393-
GENERATE_VARIANT,
413+
&generate_variant_message("Wibble"),
394414
r#"
395415
pub type Wibble {
396416
Wibble
@@ -410,7 +430,7 @@ pub fn main() -> Wibble {
410430
#[test]
411431
fn generate_variant_from_pattern_with_no_fields() {
412432
assert_code_action!(
413-
GENERATE_VARIANT,
433+
&generate_variant_message("Wibble"),
414434
r#"
415435
pub type Wibble {
416436
Wibble
@@ -440,7 +460,7 @@ pub fn new() -> other.Wibble { todo }
440460
"#;
441461

442462
assert_code_action!(
443-
GENERATE_VARIANT,
463+
&generate_variant_message("other.Wibble"),
444464
TestProject::for_source(src).add_module("other", "pub type Wibble"),
445465
find_position_of("Wobble").to_selection()
446466
);
@@ -460,7 +480,7 @@ pub fn new() -> other.Wibble { todo }
460480
"#;
461481

462482
assert_code_action!(
463-
GENERATE_VARIANT,
483+
&generate_variant_message("other.Wibble"),
464484
TestProject::for_source(src).add_module(
465485
"other",
466486
"pub type Wibble {
@@ -485,7 +505,7 @@ pub fn new() -> other.Wibble { todo }
485505
"#;
486506

487507
assert_code_action!(
488-
GENERATE_VARIANT,
508+
&generate_variant_message("other.Wibble"),
489509
TestProject::for_source(src).add_module(
490510
"other",
491511
"pub type Wibble {
@@ -508,7 +528,7 @@ pub fn main() -> other.Wibble {
508528
pub fn new() -> other.Wibble { todo }
509529
"#;
510530
assert_code_action!(
511-
GENERATE_VARIANT,
531+
&generate_variant_message("other.Wibble"),
512532
TestProject::for_source(src).add_module("other", "pub type Wibble"),
513533
find_position_of("Wobble").to_selection()
514534
);
@@ -517,7 +537,7 @@ pub fn new() -> other.Wibble { todo }
517537
#[test]
518538
fn do_not_generate_variant_if_one_with_the_same_name_exists() {
519539
assert_no_code_actions!(
520-
GENERATE_VARIANT,
540+
&generate_variant_message("Wibble"),
521541
r#"
522542
pub fn main() -> Wibble {
523543
let assert Wobble = new()
@@ -545,7 +565,7 @@ pub fn main() -> Wibble {
545565
pub fn new() -> Wibble { todo }
546566
"#;
547567
assert_no_code_actions!(
548-
GENERATE_VARIANT,
568+
&generate_variant_message("Wibble"),
549569
TestProject::for_source(src).add_module("other", "pub type Wibble { Wobble(String) }"),
550570
find_position_of("Wobble").to_selection()
551571
);
@@ -563,7 +583,7 @@ pub fn main() -> Wibble {
563583
pub fn new() -> Wibble { todo }
564584
"#;
565585
assert_no_code_actions!(
566-
GENERATE_VARIANT,
586+
&generate_variant_message("Wibble"),
567587
TestProject::for_source(src).add_module("other", "pub type Wibble { Wobble(String) }"),
568588
find_position_of("Wobble").to_selection()
569589
);

0 commit comments

Comments
 (0)