Skip to content

Commit

Permalink
add arrow function to declaration action
Browse files Browse the repository at this point in the history
  • Loading branch information
ianjkaplan committed Mar 2, 2024
1 parent f6d182f commit 6dda5fa
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 20 deletions.
1 change: 1 addition & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ std = luajit
cache = true
codes = true

ignore = { "631" }
read_globals = { "vim" }
21 changes: 18 additions & 3 deletions lua/shrub/actions/arrow_fun_to_declaration.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,33 @@ local utils = require("shrub.utils")

---@class ShrubActionConfig
local M = {
root_node_type = { "lexical_declaration" },
query = [[
(lexical_declaration
(variable_declarator
name: (_) @name
name: (_) @name
value: (
arrow_function
parameters: (_) @params
body: (_) @body
)
)
)@declaration
]],
root_node_type = { "variable_declarator" },
handle_captures = function(captures, bufnr) end,
handle_captures = function(captures, bufnr)
-- TODO: write multiple statement blocks or add formatting
utils.replace_ts_node_text(bufnr, captures.declaration, function()
return string.format(
"function %s%s %s",
table.concat(utils.get_ts_node_text(bufnr, captures.name), " "),
table.concat(
utils.get_ts_node_text(bufnr, captures.params),
" "
),
table.concat(utils.get_ts_node_text(bufnr, captures.body), " ")
)
end)
end,
}

return M
18 changes: 6 additions & 12 deletions lua/shrub/actions/fun_declaration_to_arrow.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,25 @@ local utils = require("shrub.utils")

---@class ShrubActionConfig
local M = {
root_node_type = { "function_declaration" },
query = [[
(function_declaration
name: (_) @name
parameters: (_) @params
body: (_) @body
) @function
) @func
]],
root_node_type = { "function_declaration" },
handle_captures = function(captures, bufnr)
utils.replace_ts_node_text(bufnr, captures["function"], function()
utils.replace_ts_node_text(bufnr, captures.func, function()
-- TODO: write multiple statement blocks or add formatting
return string.format(
"const %s = %s => %s",
table.concat(utils.get_ts_node_text(bufnr, captures.name), " "),
table.concat(
utils.get_ts_node_text(bufnr, captures["name"]),
utils.get_ts_node_text(bufnr, captures.params),
" "
),
table.concat(
utils.get_ts_node_text(bufnr, captures["params"]),
" "
),
table.concat(
utils.get_ts_node_text(bufnr, captures["body"]),
" "
)
table.concat(utils.get_ts_node_text(bufnr, captures.body), " ")
)
end)
end,
Expand Down
3 changes: 2 additions & 1 deletion lua/shrub/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local M = {
_ft = "typescript",
}

--- register a new action as config
---@type table<ShrubAction, ShrubActionConfig>
local shrub_actions = {
statement_block_surround = require(
Expand All @@ -13,7 +14,7 @@ local shrub_actions = {
fun_declaration_to_arrow = require(
"shrub.actions.fun_declaration_to_arrow"
),
arrow_to_function_declaration = require(
arrow_fun_to_declaration = require(
"shrub.actions.arrow_fun_to_declaration"
),
}
Expand Down
2 changes: 1 addition & 1 deletion lua/shrub/actions/statement_block_surround.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ local M = {
body: (_) @arrow_body)
(if_statement
consequence: (_) @condition_body
)
)
]],
handle_captures = function(captures, bufnr)
if captures["condition_body"] ~= nil then
Expand Down
10 changes: 10 additions & 0 deletions lua/shrub/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ M.keys = {
desc = "convert a function declaration to an arrow function",
},
},
{
"n",
"<leader>jaf",
'<cmd>lua require"shrub".arrow_fun_to_declaration()<cr>',
{
noremap = true,
silent = true,
desc = "convert an arrow function to a function declaration",
},
},
}

return M
3 changes: 1 addition & 2 deletions lua/shrub/types.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---@alias ShrubAction "statement_block_surround" | "statement_block_remove" | "fun_declaration_to_arrow" | "arrow_fun_to_declaration"

---@class ShrubActionConfig
---@field query string treesitter query string
---@field root_node_type string[] nearest node type that will be used as the root of the query
---@field handle_captures fun(captures:table<string,TSNode>, bufnr:integer): any execute an action on table of captured nodes based on the captures where the key is the capture name and the value is the TSNode
---@field handle_captures fun(captures:table<string,TSNode>, bufnr:integer): any execute action on table of captured nodes based on the captures where the key is the capture name and the value is the TSNode
9 changes: 8 additions & 1 deletion tests/actions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,18 @@ describe("Actions:", function()
},
{
action = "fun_declaration_to_arrow",
description = "chould convert a function declaration to an arrow function",
description = "should convert a function declaration to an arrow function",
before = [[function myFn() { return "success"; }]],
after = [[const myFn = () => { return "success"; }]],
pos = { 0, 20 },
},
{
action = "arrow_fun_to_declaration",
description = "should convert an arrow function to a function declaration",
before = [[const myFn = () => { return "success"; }]],
after = [[function myFn() { return "success"; }]],
pos = { 0, 25 },
},
}

for _, test in ipairs(test_table) do
Expand Down

0 comments on commit 6dda5fa

Please sign in to comment.