Skip to content

Commit

Permalink
fix: 3 cases mentioned in CKolkey#41
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpw authored and bo5o committed Jul 31, 2023
1 parent 9aaa4b6 commit fd5ba39
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 20 deletions.
42 changes: 24 additions & 18 deletions lua/ts-node-action/filetypes/rust.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,48 +37,54 @@ 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 {
["boolean_literal"] = actions.toggle_boolean(),
["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),
}
56 changes: 54 additions & 2 deletions spec/filetypes/rust_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 }))
Expand Down Expand Up @@ -155,7 +155,9 @@ describe("toggle_multiline", function()
{ 1, 11 }
)
)
end)

it("block fix #41 - as", function()
assert.are.same(
{
"{",
Expand Down Expand Up @@ -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(
{
Expand Down

0 comments on commit fd5ba39

Please sign in to comment.