From db23cd974647fa2e0514336c32a0bd6f0948fcbb Mon Sep 17 00:00:00 2001 From: Alex Walker Date: Sun, 7 May 2023 19:46:55 -0500 Subject: [PATCH 1/5] feat: support for rust --- .github/workflows/spec.yml | 2 +- README.md | 26 +-- lua/ts-node-action/filetypes/rust.lua | 96 ++++++-- spec/filetypes/rust_spec.lua | 325 ++++++++++++++++++++++++++ 4 files changed, 417 insertions(+), 32 deletions(-) create mode 100644 spec/filetypes/rust_spec.lua diff --git a/.github/workflows/spec.yml b/.github/workflows/spec.yml index 80d5e92..6549862 100644 --- a/.github/workflows/spec.yml +++ b/.github/workflows/spec.yml @@ -48,7 +48,7 @@ jobs: - name: Compile parsers run: | - nvim --headless -c "TSInstallSync ruby python lua javascript julia yaml sql r" -c "q" + nvim --headless -c "TSInstallSync ruby python lua javascript julia yaml sql r rust" -c "q" - name: Tests env: ci: "1" diff --git a/README.md b/README.md index cf81b2d..925716b 100644 --- a/README.md +++ b/README.md @@ -256,19 +256,19 @@ NOTE: The order of formats can be important, as some identifiers are the same fo Builtin actions are all higher-order functions so they can easily have options overridden on a per-lang basis. Check out the implementations under `lua/filetypes/` to see how! -| | (*) | Ruby | js/ts/tsx/jsx | Lua | Python | PHP | Rust | JSON | HTML | YAML | R | -|---|---|---|---|---|---|---|---|---|---|---|---| -| `toggle_boolean()` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | | ✅ | ✅ | -| `cycle_case()` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | | | ✅ | ✅ | -| `cycle_quotes()` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | | | | ✅ | -| `toggle_multiline()` | | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | | | -| `toggle_operator()` | | ✅ | ✅ | ✅ | ✅ | ✅ | | | | | ✅ | -| `toggle_int_readability()` | | ✅ | ✅ | | ✅ | ✅ | ✅ | ✅ | | | | -| `toggle_block()` | | ✅ | | | | | | | | | | -| if/else <-> ternery | | ✅ | | | ✅ | | | | | | | -| if block/postfix | | ✅ | | | | | | | | | | -| `toggle_hash_style()` | | ✅ | | | | | | | | | | -| `conceal_string()` | | | ✅ | | | | | | ✅ | | | +| | (*) | Ruby | js/ts/tsx/jsx | Lua | Python | PHP | Rust | JSON | HTML | YAML | R | Rust | +|---|---|---|---|---|---|---|---|---|---|---|---|---| +| `toggle_boolean()` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | | ✅ | ✅ | ✅ | +| `cycle_case()` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | | | ✅ | ✅ | ✅ | +| `cycle_quotes()` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | | | | ✅ | | +| `toggle_multiline()` | | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | | | ✅ | +| `toggle_operator()` | | ✅ | ✅ | ✅ | ✅ | ✅ | | | | | ✅ | ✅ | +| `toggle_int_readability()` | | ✅ | ✅ | | ✅ | ✅ | ✅ | ✅ | | | | ✅ | +| `toggle_block()` | | ✅ | | | | | | | | | | | +| if/else <-> ternery | | ✅ | | | ✅ | | | | | | | | +| if block/postfix | | ✅ | | | | | | | | | | | +| `toggle_hash_style()` | | ✅ | | | | | | | | | | | +| `conceal_string()` | | | ✅ | | | | | | ✅ | | | | ## Testing To run the test suite, clone the repo and run `./run_spec`. It should pull all dependencies into `spec/support/` on diff --git a/lua/ts-node-action/filetypes/rust.lua b/lua/ts-node-action/filetypes/rust.lua index 2c87bf3..4b5050f 100644 --- a/lua/ts-node-action/filetypes/rust.lua +++ b/lua/ts-node-action/filetypes/rust.lua @@ -1,26 +1,86 @@ local actions = require("ts-node-action.actions") +local boolean_override = { + ["true"] = "false", + ["false"] = "true", +} + +local operators = { + -- assignment + ["+="] = "-=", + ["-="] = "+=", + ["%="] = "/=", + ["/="] = "%=", + -- bitwise assignment + ["&="] = "|=", + ["|="] = "^=", + ["^="] = "&=", + -- comparison + ["!="] = "==", + ["=="] = "!=", + [">"] = "<", + ["<"] = ">", + [">="] = "<=", + ["<="] = ">=", + -- shift + ["<<"] = ">>", + [">>"] = "<<", + -- shift assignment + [">>="] = "<<=", + ["<<="] = ">>=", + -- arithmetic + ["+"] = "-", + ["-"] = "+", + ["*"] = "/", + ["/"] = "*", + -- bitwise + ["|"] = "&", + ["&"] = "|", + -- logical + ["||"] = "&&", + ["&&"] = "||", +} + local padding = { - [","] = "%s ", - [":"] = "%s ", ["{"] = "%s ", - ["=>"] = " %s ", - ["="] = " %s ", - ["}"] = " %s", - ["+"] = " %s ", - ["-"] = " %s ", - ["*"] = " %s ", - ["/"] = " %s ", + ["}"] = { + " %s", + ["{"] = "%s", + [","] = "%s", + [";"] = "%s", + ["prev_nil"] = "%s", + }, + ["as"] = " %s ", + ["in"] = { " %s ", ["prev_nil"] = "%s ", }, + [":"] = "%s ", + [";"] = "%s ", + [","] = "%s ", +} +local padding_compact = { + ["{"] = "%s", + ["}"] = "%s", + [","] = "%s ", +} + +local uncollapsible = { + ["string_literal"] = true, + ["macro_invocation"] = true, + ["macro"] = true, + ["for_expression"] = true, + ["range_expression"] = true, + ["line_comment"] = true, } return { - ["field_declaration_list"] = actions.toggle_multiline(padding), - ["parameters"] = actions.toggle_multiline(padding), - ["enum_variant_list"] = actions.toggle_multiline(padding), - ["block"] = actions.toggle_multiline(padding), - ["array_expression"] = actions.toggle_multiline(padding), - ["tuple_expression"] = actions.toggle_multiline(padding), - ["tuple_pattern"] = actions.toggle_multiline(padding), - ["boolean_literal"] = actions.toggle_boolean(), - ["integer_literal"] = actions.toggle_int_readability(), + ["boolean_literal"] = actions.toggle_boolean(boolean_override), + ["integer_literal"] = actions.toggle_int_readability(), + ["binary_expression"] = actions.toggle_operator(operators), + ["compound_assignment_expr"] = actions.toggle_operator(operators), + ["block"] = actions.toggle_multiline(padding, uncollapsible), + ["use_list"] = actions.toggle_multiline(padding_compact, uncollapsible), + ["array_expression"] = actions.toggle_multiline(padding, uncollapsible), + ["tuple_expression"] = actions.toggle_multiline(padding, uncollapsible), + ["arguments"] = actions.toggle_multiline(padding, uncollapsible), + ["field_initializer_list"] = actions.toggle_multiline(padding, uncollapsible), + ["field_declaration_list"] = actions.toggle_multiline(padding, uncollapsible), } diff --git a/spec/filetypes/rust_spec.lua b/spec/filetypes/rust_spec.lua new file mode 100644 index 0000000..bde81aa --- /dev/null +++ b/spec/filetypes/rust_spec.lua @@ -0,0 +1,325 @@ +dofile("spec/spec_helper.lua") + +local Helper = SpecHelper.new("rust", { shiftwidth = 4 }) + +describe("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() + it("1 million to friendly", function() + assert.are.same({ "x = 1000000" }, Helper:call({ + "x = 1_000_000",}, { 1, 5 })) + end) + + it("1 million to unfriendly", function() + assert.are.same({ "x = 1_000_000" }, Helper:call({ + "x = 1000000",}, { 1, 5 })) + end) +end) + +describe("operator", function() + -- assignment + it("toggles '-=' and '+='", function() + assert.are.same({ "i += 8" }, Helper:call({ "i -= 8" }, { 1, 3 })) + assert.are.same({ "i -= 8" }, Helper:call({ "i += 8" }, { 1, 3 })) + end) + + it("toggles '/=' and '%='", function() + assert.are.same({ "i %= 8" }, Helper:call({ "i /= 8" }, { 1, 3 })) + assert.are.same({ "i /= 8" }, Helper:call({ "i %= 8" }, { 1, 3 })) + end) + + -- bitwise assignment + it("toggles '&=' and '|=' and '^='", function() + assert.are.same({ "i |= 8" }, Helper:call({ "i &= 8" }, { 1, 3 })) + assert.are.same({ "i ^= 8" }, Helper:call({ "i |= 8" }, { 1, 3 })) + assert.are.same({ "i &= 8" }, Helper:call({ "i ^= 8" }, { 1, 3 })) + end) + + -- comparison + it("toggles '==' and '!='", function() + assert.are.same({ "i == 8" }, Helper:call({ "i != 8" }, { 1, 3 })) + assert.are.same({ "i != 8" }, Helper:call({ "i == 8" }, { 1, 3 })) + end) + + it("toggles '>' and '<'", function() + assert.are.same({ "i > 8" }, Helper:call({ "i < 8" }, { 1, 3 })) + assert.are.same({ "i < 8" }, Helper:call({ "i > 8" }, { 1, 3 })) + end) + + it("toggles '<= and '>='", function() + assert.are.same({ "i <= 8" }, Helper:call({ "i >= 8" }, { 1, 3 })) + assert.are.same({ "i >= 8" }, Helper:call({ "i <= 8" }, { 1, 3 })) + end) + + -- shift + it("toggles '<<' and '>>'", function() + assert.are.same({ "i << 8" }, Helper:call({ "i >> 8" }, { 1, 3 })) + assert.are.same({ "i >> 8" }, Helper:call({ "i << 8" }, { 1, 3 })) + end) + + -- shift assignment + it("toggles '<<=' and '>>='", function() + assert.are.same({ "i >>= 8" }, Helper:call({ "i <<= 8" }, { 1, 3 })) + assert.are.same({ "i <<= 8" }, Helper:call({ "i >>= 8" }, { 1, 3 })) + end) + + -- arithmetic + it("toggles '+' and '-'", function() + assert.are.same({ "i + 8" }, Helper:call({ "i - 8" }, { 1, 3 })) + assert.are.same({ "i - 8" }, Helper:call({ "i + 8" }, { 1, 3 })) + end) + + it("toggles '*' and '/'", function() + assert.are.same({ "i * 8" }, Helper:call({ "i / 8" }, { 1, 3 })) + assert.are.same({ "i / 8" }, Helper:call({ "i * 8" }, { 1, 3 })) + end) + + -- bitwise + it("toggles '|' and '&'", function() + assert.are.same({ "i | 8" }, Helper:call({ "i & 8" }, { 1, 3 })) + assert.are.same({ "i & 8" }, Helper:call({ "i | 8" }, { 1, 3 })) + end) + + -- logical + it("toggles '||' and '&&'", function() + assert.are.same({ "i || 8" }, Helper:call({ "i && 8" }, { 1, 3 })) + assert.are.same({ "i && 8" }, Helper:call({ "i || 8" }, { 1, 3 })) + end) +end) + +describe("toggle_multiline", function() + it("block", function() + assert.are.same( + { + "fn main() {", + " println!(\"main:\\tmodule '{}', file '{}'\", module_path!(), file!());", + " submod::hi();", + "}" + }, + Helper:call( + { + "fn main() { println!(\"main:\\tmodule '{}', file '{}'\", module_path!(), file!()); submod::hi(); }" + }, + { 1, 11 } + ) + ) + + assert.are.same( + { + "fn main() { println!(\"main:\\tmodule '{}', file '{}'\", module_path!(), file!()); submod::hi(); }" + }, + Helper:call( + { + "fn main() {", + " println!(\"main:\\tmodule '{}', file '{}'\", module_path!(), file!());", + " submod::hi();", + "}" + }, + { 1, 11 } + ) + ) + end) + + it("use_list", function() + assert.are.same( + { + "use std::collections::{", + " HashMap,", + " HashSet", + "};", + }, + Helper:call( + { + "use std::collections::{HashMap, HashSet};", + }, + { 1, 23 } + ) + ) + + assert.are.same( + { + "use std::collections::{HashMap, HashSet};", + }, + Helper:call( + { + "use std::collections::{", + " HashMap,", + " HashSet", + "};", + }, + { 1, 23 } + ) + ) + end) + + it("array_expression", function() + assert.are.same( + { + "let bytes: [u8; 3] = [", + " 1,", + " 2,", + " 3", + "];", + }, + Helper:call( + { + "let bytes: [u8; 3] = [1, 2, 3];" + }, + { 1, 22 } + ) + ) + + assert.are.same( + { + "let bytes: [u8; 3] = [1, 2, 3];", + }, + Helper:call( + { + "let bytes: [u8; 3] = [", + " 1,", + " 2,", + " 3", + "];", + }, + { 1, 22 } + ) + ) + end) + + it("arguments", function() + assert.are.same( + { + "list.insert_at_ith(", + " 0,", + " first_value", + ");" + }, + Helper:call( + { + "list.insert_at_ith(0, first_value);", + }, + { 1, 19 } + ) + ) + + assert.are.same( + { + "list.insert_at_ith(0, first_value);", + }, + Helper:call( + { + "list.insert_at_ith(", + " 0,", + " first_value", + ");", + }, + { 1, 19 } + ) + ) + end) + + it("tuple_expression", function() + assert.are.same( + { + "let foo = (", + " 0.0,", + " 4.5", + ")" + }, + Helper:call( + { + "let foo = (0.0, 4.5)" + }, + { 1, 11 } + ) + ) + + assert.are.same( + { + "let foo = (0.0, 4.5)", + }, + Helper:call( + { + "let foo = (", + " 0.0,", + " 4.5", + ")", + }, + { 1, 11 } + ) + ) + end) + + it("field_initializer_list", function() + assert.are.same( + { + "Foo {", + " a: 1,", + " b: 2,", + " c: 3", + "}", + }, + Helper:call( + { + "Foo { a: 1, b: 2, c: 3 }", + }, + { 1, 5 } + ) + ) + + assert.are.same( + { + "Foo { a: 1, b: 2, c: 3 }", + }, + Helper:call( + { + "Foo {", + " a: 1,", + " b: 2,", + " c: 3", + "}", + }, + { 1, 5 } + ) + ) + end) + + it("field_declaration_list", function() + assert.are.same( + { + "struct Foo {", + " a: i32,", + " b: i32,", + " c: i32", + "}", + }, + Helper:call( + { + "struct Foo { a: i32, b: i32, c: i32 }", + }, + { 1, 12 } + ) + ) + + assert.are.same( + { + "struct Foo { a: i32, b: i32, c: i32 }", + }, + Helper:call( + { + "struct Foo {", + " a: i32,", + " b: i32,", + " c: i32", + "}", + }, + { 1, 12 } + ) + ) + end) +end) From ba0277b5833b90e5e9ef08b196b3c7f483d019af Mon Sep 17 00:00:00 2001 From: Alex Walker Date: Sun, 7 May 2023 20:14:32 -0500 Subject: [PATCH 2/5] feat: spec/support for parameters, tuple_pattern, enum_variant_list --- lua/ts-node-action/filetypes/rust.lua | 12 +- spec/filetypes/rust_spec.lua | 184 ++++++++++++++++++++------ 2 files changed, 150 insertions(+), 46 deletions(-) diff --git a/lua/ts-node-action/filetypes/rust.lua b/lua/ts-node-action/filetypes/rust.lua index 4b5050f..e670af3 100644 --- a/lua/ts-node-action/filetypes/rust.lua +++ b/lua/ts-node-action/filetypes/rust.lua @@ -1,10 +1,5 @@ local actions = require("ts-node-action.actions") -local boolean_override = { - ["true"] = "false", - ["false"] = "true", -} - local operators = { -- assignment ["+="] = "-=", @@ -72,15 +67,18 @@ local uncollapsible = { } return { - ["boolean_literal"] = actions.toggle_boolean(boolean_override), + ["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), ["block"] = actions.toggle_multiline(padding, uncollapsible), ["use_list"] = actions.toggle_multiline(padding_compact, uncollapsible), ["array_expression"] = actions.toggle_multiline(padding, uncollapsible), ["tuple_expression"] = actions.toggle_multiline(padding, uncollapsible), - ["arguments"] = actions.toggle_multiline(padding, uncollapsible), + ["tuple_pattern"] = 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 bde81aa..191a538 100644 --- a/spec/filetypes/rust_spec.lua +++ b/spec/filetypes/rust_spec.lua @@ -93,6 +93,38 @@ describe("operator", function() end) describe("toggle_multiline", function() + it("use_list", function() + assert.are.same( + { + "use std::collections::{", + " HashMap,", + " HashSet", + "};", + }, + Helper:call( + { + "use std::collections::{HashMap, HashSet};", + }, + { 1, 23 } + ) + ) + + assert.are.same( + { + "use std::collections::{HashMap, HashSet};", + }, + Helper:call( + { + "use std::collections::{", + " HashMap,", + " HashSet", + "};", + }, + { 1, 23 } + ) + ) + end) + it("block", function() assert.are.same( { @@ -125,34 +157,74 @@ describe("toggle_multiline", function() ) end) - it("use_list", function() + it("parameters", function() assert.are.same( { - "use std::collections::{", - " HashMap,", - " HashSet", - "};", + "fn main(", + " first: u8,", + " second: u8", + ") {", + " submod::hi();", + "}" }, Helper:call( { - "use std::collections::{HashMap, HashSet};", + "fn main(first: u8, second: u8) {", + " submod::hi();", + "}" }, - { 1, 23 } + { 1, 8 } ) ) assert.are.same( { - "use std::collections::{HashMap, HashSet};", + "fn main(first: u8, second: u8) {", + " submod::hi();", + "}" }, Helper:call( { - "use std::collections::{", - " HashMap,", - " HashSet", - "};", + "fn main(", + " first: u8,", + " second: u8", + ") {", + " submod::hi();", + "}" }, - { 1, 23 } + { 1, 8 } + ) + ) + end) + + it("arguments", function() + assert.are.same( + { + "list.insert_at_ith(", + " 0,", + " first_value", + ");" + }, + Helper:call( + { + "list.insert_at_ith(0, first_value);", + }, + { 1, 19 } + ) + ) + + assert.are.same( + { + "list.insert_at_ith(0, first_value);", + }, + Helper:call( + { + "list.insert_at_ith(", + " 0,", + " first_value", + ");", + }, + { 1, 19 } ) ) end) @@ -191,66 +263,100 @@ describe("toggle_multiline", function() ) end) - it("arguments", function() + it("tuple_expression", function() assert.are.same( { - "list.insert_at_ith(", - " 0,", - " first_value", - ");" + "let foo = (", + " 0.0,", + " 4.5", + ")" }, Helper:call( { - "list.insert_at_ith(0, first_value);", + "let foo = (0.0, 4.5)" }, - { 1, 19 } + { 1, 11 } ) ) assert.are.same( { - "list.insert_at_ith(0, first_value);", + "let foo = (0.0, 4.5)", }, Helper:call( { - "list.insert_at_ith(", - " 0,", - " first_value", - ");", + "let foo = (", + " 0.0,", + " 4.5", + ")", }, - { 1, 19 } + { 1, 11 } ) ) end) - it("tuple_expression", function() + it("tuple_pattern", function() assert.are.same( { - "let foo = (", - " 0.0,", - " 4.5", - ")" + "let (", + "a,", + "b", + ") = foo;" }, Helper:call( { - "let foo = (0.0, 4.5)" + "let (a, b) = foo;" }, - { 1, 11 } + { 1, 5 } ) ) assert.are.same( { - "let foo = (0.0, 4.5)", + "let (a, b) = foo;", }, Helper:call( { - "let foo = (", - " 0.0,", - " 4.5", - ")", + "let (", + " a,", + " b", + ") = foo;", }, - { 1, 11 } + { 1, 5 } + ) + ) + end) + + it("enum_variant_list", function() + assert.are.same( + { + "enum Foo {", + " A,", + " B,", + " C", + "}", + }, + Helper:call( + { + "enum Foo { A, B, C }", + }, + { 1, 10 } + ) + ) + + assert.are.same( + { + "enum Foo { A, B, C }", + }, + Helper:call( + { + "enum Foo {", + " A,", + " B,", + " C", + "}", + }, + { 1, 10 } ) ) end) From b10dff4b2d6c918ede8004ed2e663fc2df048ad5 Mon Sep 17 00:00:00 2001 From: Alex Walker Date: Sun, 7 May 2023 20:20:02 -0500 Subject: [PATCH 3/5] feat: add spec for #41 case --- spec/filetypes/rust_spec.lua | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/spec/filetypes/rust_spec.lua b/spec/filetypes/rust_spec.lua index 191a538..2b09f35 100644 --- a/spec/filetypes/rust_spec.lua +++ b/spec/filetypes/rust_spec.lua @@ -155,6 +155,33 @@ describe("toggle_multiline", function() { 1, 11 } ) ) + + assert.are.same( + { + "{", + " visitor.visit_char(self.parse_u8()? as char)", + "}", + }, + Helper:call( + { + "{ visitor.visit_char(self.parse_u8()? as char) }", + }, + { 1, 1 } + ) + ) + assert.are.same( + { + "{ visitor.visit_char(self.parse_u8()? as char) }", + }, + Helper:call( + { + "{", + " visitor.visit_char(self.parse_u8()? as char)", + "}", + }, + { 1, 1 } + ) + ) end) it("parameters", function() From 4bd056e7cc84dcfefd14ed1453dd77dc6674498a Mon Sep 17 00:00:00 2001 From: Alex Walker Date: Sun, 7 May 2023 21:05:48 -0500 Subject: [PATCH 4/5] fix: remove dupe Rust entry -- oops, must be tired --- README.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 925716b..917baf2 100644 --- a/README.md +++ b/README.md @@ -256,19 +256,19 @@ NOTE: The order of formats can be important, as some identifiers are the same fo Builtin actions are all higher-order functions so they can easily have options overridden on a per-lang basis. Check out the implementations under `lua/filetypes/` to see how! -| | (*) | Ruby | js/ts/tsx/jsx | Lua | Python | PHP | Rust | JSON | HTML | YAML | R | Rust | -|---|---|---|---|---|---|---|---|---|---|---|---|---| -| `toggle_boolean()` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | | ✅ | ✅ | ✅ | -| `cycle_case()` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | | | ✅ | ✅ | ✅ | -| `cycle_quotes()` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | | | | ✅ | | -| `toggle_multiline()` | | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | | | ✅ | -| `toggle_operator()` | | ✅ | ✅ | ✅ | ✅ | ✅ | | | | | ✅ | ✅ | -| `toggle_int_readability()` | | ✅ | ✅ | | ✅ | ✅ | ✅ | ✅ | | | | ✅ | -| `toggle_block()` | | ✅ | | | | | | | | | | | -| if/else <-> ternery | | ✅ | | | ✅ | | | | | | | | -| if block/postfix | | ✅ | | | | | | | | | | | -| `toggle_hash_style()` | | ✅ | | | | | | | | | | | -| `conceal_string()` | | | ✅ | | | | | | ✅ | | | | +| | (*) | Ruby | js/ts/tsx/jsx | Lua | Python | PHP | Rust | JSON | HTML | YAML | R | +|---|---|---|---|---|---|---|---|---|---|---|---| +| `toggle_boolean()` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | | ✅ | ✅ | +| `cycle_case()` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | | | ✅ | ✅ | +| `cycle_quotes()` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | | | | ✅ | +| `toggle_multiline()` | | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | | | +| `toggle_operator()` | | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | | | ✅ | +| `toggle_int_readability()` | | ✅ | ✅ | | ✅ | ✅ | ✅ | ✅ | | | | +| `toggle_block()` | | ✅ | | | | | | | | | | +| if/else <-> ternery | | ✅ | | | ✅ | | | | | | | +| if block/postfix | | ✅ | | | | | | | | | | +| `toggle_hash_style()` | | ✅ | | | | | | | | | | +| `conceal_string()` | | | ✅ | | | | | | ✅ | | | ## Testing To run the test suite, clone the repo and run `./run_spec`. It should pull all dependencies into `spec/support/` on From e080bbfe3ef4ae2b7800ceb1997f8130756f4def Mon Sep 17 00:00:00 2001 From: Alex Walker Date: Sun, 7 May 2023 23:01:10 -0500 Subject: [PATCH 5/5] 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( {