Skip to content

Commit

Permalink
add function declaration to arrow action
Browse files Browse the repository at this point in the history
  • Loading branch information
ianjkaplan committed Mar 2, 2024
1 parent 6d8d7e3 commit f6d182f
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ _you can provide custom key bindings by passing your own keys to the `keys` fiel
| ------------------- | ----------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **`<leader>jer`** | `lua require("shrub").statement_block_surround` | When the cursor in the body of a function or conditional that doesnt have a wrapping `{}` will add a surrounding statement block |
| **`<leader>jir`** | `lua require("shrub").statement_block_remove` | If the cursor is in the body of an arrow function, will remove surrounding `{}` and will move everything following the `return` statement to an implicit return (e.g `const myFn => "result"`). If the cursor is in a conditional everything will produce a one line early return e.g `if (true) return "result"` |
| **`<leader>jfa`** | `lua require("shrub").fun_declaration_to_arrow` | converts a named function declaraton to an arrow function assigning the function name to a `const` declaration |

**shrub.nvim** only provides a few key bindings to perform common text manipulations. It does not format or lint your code. It is recommended to have prettier and eslint set up in your Javascript/Typescript to format and lint your code

Expand Down
23 changes: 21 additions & 2 deletions lua/shrub/actions/fun_declaration_to_arrow.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,29 @@ local M = {
name: (_) @name
parameters: (_) @params
body: (_) @body
)
) @function
]],
root_node_type = { "function_declaration" },
handle_captures = function(captures, bufnr) end,
handle_captures = function(captures, bufnr)
utils.replace_ts_node_text(bufnr, captures["function"], 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["params"]),
" "
),
table.concat(
utils.get_ts_node_text(bufnr, captures["body"]),
" "
)
)
end)
end,
}

return M
10 changes: 10 additions & 0 deletions lua/shrub/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ M.keys = {
desc = "intelligent remove surrounding statement block",
},
},
{
"n",
"<leader>jfa",
'<cmd>lua require"shrub".fun_declaration_to_arrow()<cr>',
{
noremap = true,
silent = true,
desc = "convert a function declaration to an arrow function",
},
},
}

return M
9 changes: 9 additions & 0 deletions lua/shrub/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local actions = require("shrub.actions")
local default_config = require("shrub.config")
local M = {}

---@param action ShrubAction
M._run_action = function(action)
actions.run(
action,
Expand All @@ -18,6 +19,14 @@ M.statement_block_remove = function()
M._run_action("statement_block_remove")
end

M.fun_declaration_to_arrow = function()
M._run_action("fun_declaration_to_arrow")
end

M.arrow_fun_to_declaration = function()
M._run_action("arrow_fun_to_declaration")
end

--- accepts a config table with a keys field to override default keybindings
--- @param config table | nil
M.setup = function(config)
Expand Down
12 changes: 12 additions & 0 deletions lua/shrub/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ M.find_nearest_parent_ts_node = function(node, types)
return nil
end

M.get_ts_node_text = function(bufnr, node)
local start_row, start_col, end_row, end_col = node:range()
return vim.api.nvim_buf_get_text(
bufnr,
start_row,
start_col,
end_row,
end_col,
{}
)
end

---@param bufnr integer
---@param node TSNode
---@param callback function accepts a string[] and returns a string to replace the node text with
Expand Down
7 changes: 7 additions & 0 deletions tests/actions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ describe("Actions:", function()
after = [[if (true) return "something";]],
pos = { 0, 20 },
},
{
action = "fun_declaration_to_arrow",
description = "chould convert a function declaration to an arrow function",
before = [[function myFn() { return "success"; }]],
after = [[const myFn = () => { return "success"; }]],
pos = { 0, 20 },
},
}

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

0 comments on commit f6d182f

Please sign in to comment.