From fd5ba3978b4413e50f433c17f7bd639328c3f0f7 Mon Sep 17 00:00:00 2001 From: Alex Walker Date: Sun, 7 May 2023 23:01:10 -0500 Subject: [PATCH] fix: 3 cases mentioned in #41 --- lua/ts-node-action/filetypes/rust.lua | 42 +++++++++++--------- spec/filetypes/rust_spec.lua | 56 ++++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 20 deletions(-) diff --git a/lua/ts-node-action/filetypes/rust.lua b/lua/ts-node-action/filetypes/rust.lua index e670af3..d503a53 100644 --- a/lua/ts-node-action/filetypes/rust.lua +++ b/lua/ts-node-action/filetypes/rust.lua @@ -37,33 +37,39 @@ local operators = { } local padding = { - ["{"] = "%s ", - ["}"] = { + ["{"] = "%s ", + ["}"] = { " %s", ["{"] = "%s", [","] = "%s", [";"] = "%s", ["prev_nil"] = "%s", }, - ["as"] = " %s ", - ["in"] = { " %s ", ["prev_nil"] = "%s ", }, - [":"] = "%s ", - [";"] = "%s ", - [","] = "%s ", + ["let"] = "%s ", + ["as"] = " %s ", + ["in"] = { " %s ", ["prev_nil"] = "%s ", }, + ["="] = " %s ", + [":"] = "%s ", + [";"] = "%s ", + [","] = "%s ", } -local padding_compact = { +local padding_use_list = { ["{"] = "%s", ["}"] = "%s", [","] = "%s ", } local uncollapsible = { - ["string_literal"] = true, - ["macro_invocation"] = true, - ["macro"] = true, - ["for_expression"] = true, - ["range_expression"] = true, - ["line_comment"] = true, + ["string_literal"] = true, + ["let_declaration"] = true, + ["reference_type"] = true, + ["reference_expression"] = true, + ["type_case_expression"] = true, + ["macro_invocation"] = true, + ["macro"] = true, + ["for_expression"] = true, + ["range_expression"] = true, + ["line_comment"] = true, } return { @@ -71,14 +77,14 @@ return { ["integer_literal"] = actions.toggle_int_readability(), ["binary_expression"] = actions.toggle_operator(operators), ["compound_assignment_expr"] = actions.toggle_operator(operators), - ["arguments"] = actions.toggle_multiline(padding, uncollapsible), - ["parameters"] = actions.toggle_multiline(padding, uncollapsible), + ["use_list"] = actions.toggle_multiline(padding_use_list, uncollapsible), ["block"] = actions.toggle_multiline(padding, uncollapsible), - ["use_list"] = actions.toggle_multiline(padding_compact, uncollapsible), + ["parameters"] = actions.toggle_multiline(padding, uncollapsible), + ["arguments"] = actions.toggle_multiline(padding, uncollapsible), ["array_expression"] = actions.toggle_multiline(padding, uncollapsible), ["tuple_expression"] = actions.toggle_multiline(padding, uncollapsible), ["tuple_pattern"] = actions.toggle_multiline(padding, uncollapsible), + ["enum_variant_list"] = actions.toggle_multiline(padding, uncollapsible), ["field_initializer_list"] = actions.toggle_multiline(padding, uncollapsible), ["field_declaration_list"] = actions.toggle_multiline(padding, uncollapsible), - ["enum_variant_list"] = actions.toggle_multiline(padding, uncollapsible), } diff --git a/spec/filetypes/rust_spec.lua b/spec/filetypes/rust_spec.lua index 2b09f35..70ae282 100644 --- a/spec/filetypes/rust_spec.lua +++ b/spec/filetypes/rust_spec.lua @@ -2,14 +2,14 @@ dofile("spec/spec_helper.lua") local Helper = SpecHelper.new("rust", { shiftwidth = 4 }) -describe("boolean", function() +describe("toggle_boolean", function() it("toggles 'true' and 'false'", function() assert.are.same({ "let i = true;" }, Helper:call({ "let i = false;" }, { 1, 9 })) assert.are.same({ "let i = false;" }, Helper:call({ "let i = true;" }, { 1, 9 })) end) end) -describe("friendly integers", function() +describe("toggle_int_readability", function() it("1 million to friendly", function() assert.are.same({ "x = 1000000" }, Helper:call({ "x = 1_000_000",}, { 1, 5 })) @@ -155,7 +155,9 @@ describe("toggle_multiline", function() { 1, 11 } ) ) + end) + it("block fix #41 - as", function() assert.are.same( { "{", @@ -184,6 +186,56 @@ describe("toggle_multiline", function() ) end) + it("block fix #41 - let", function() + assert.are.same( + { + "fn test() { let foo = stuff(); }" + }, + Helper:call( + { + "fn test() {", + " let foo = stuff();", + "}", + }, + { 1, 11 } + ) + ) + end) + + it("block fix #41 - &mut (reference_type)", function() + assert.are.same( + { + "fn test(", + " foo: &mut Foo", + ") {", + "}" + }, + Helper:call( + { + "fn test(foo: &mut Foo) {", + "}", + }, + { 1, 8 } + ) + ) + end) + + it("block fix #41 - &mut (reference_expression)", function() + assert.are.same( + { + "value.serialize(&mut serializer)" + }, + Helper:call( + { + "value.serialize(", + " &mut serializer", + ")", + }, + { 1, 16 } + ) + ) + end) + it("parameters", function() assert.are.same( {