Although this plugin is usable, it is very much a work in progress, meaning that the API will inevitably change very rapidly over time.
- Neovim Nightly
- Treesitter
- Plenary
use {
"ThePrimeagen/refactoring.nvim",
requires = {
{"nvim-lua/plenary.nvim"},
{"nvim-treesitter/nvim-treesitter"}
}
}
Given that this is a work in progress, the languages supported for the operations listed below is constantly changing. As of now, these languages are supported (with individual support for each function varying):
- TypeScript
- JavaScript
- Lua
- C/C++
- Golang
- Python
- Support for various common refactoring operations
- 106: Extract Function
- Also possible to extract to file
- 119: Extract Variable
- 123: Inline Variable
- 106: Extract Function
- Also comes with various useful features for debugging
- Printf: Automated insertion of print statement to mark the calling of a function
- Print var: Automated insertion of print statement to print a variable at a given point in the code
- Cleanup: Automated cleanup of all print statements generated by the plugin
There are many ways to configure this plugin. Below are some example configurations.
Setup Function
No matter which configuration option you use, you must first call the setup function.
require('refactoring').setup({})
If you want to make remaps for a specific refactoring operation, you can do so by configuring the plugin like this:
-- Remaps for each of the four debug operations currently offered by the plugin
vim.api.nvim_set_keymap("v", "<Leader>re", [[ <Esc><Cmd>lua require('refactoring').refactor('Extract Function')<CR>]], {noremap = true, silent = true, expr = false})
vim.api.nvim_set_keymap("v", "<Leader>rf", [[ <Esc><Cmd>lua require('refactoring').refactor('Extract Function To File')<CR>]], {noremap = true, silent = true, expr = false})
vim.api.nvim_set_keymap("v", "<Leader>rv", [[ <Esc><Cmd>lua require('refactoring').refactor('Extract Variable')<CR>]], {noremap = true, silent = true, expr = false})
vim.api.nvim_set_keymap("v", "<Leader>ri", [[ <Esc><Cmd>lua require('refactoring').refactor('Inline Variable')<CR>]], {noremap = true, silent = true, expr = false})
Notice that these maps are visual mode remaps, and that ESC is pressed before executing the command. As of now, these are both necessary for the plugin to work.
If you would prefer to use Telescope to choose a refactor when you're in visual mode, you can do so use using the Telescope extension. Here is an example config for this setup:
-- Remap to open the Telescope refactoring menu in visual mode
require("telescope").load_extension("refactoring")
-- remap to open the Telescope refactoring menu in visual mode
vim.api.nvim_set_keymap(
"v",
"<leader>rr",
"<Esc><cmd>lua require('telescope').extensions.refactoring.refactors()<CR>",
{ noremap = true }
)
Finally, you can configure remaps for the debug operations of this plugin like this:
-- You can also use below = true here to to change the position of the printf
-- statement (or set two remaps for either one). This remap must be made in normal mode.
vim.api.nvim_set_keymap(
"n",
"<leader>rp",
":lua require('refactoring').debug.printf({below = false})<CR>",
{ noremap = true }
)
-- Print var: this remap should be made in visual mode
vim.api.nvim_set_keymap("v", "<leader>rv", ":lua require('refactoring').debug.print_var({})<CR>", { noremap = true })
-- Cleanup function: this remap should be made in normal mode
vim.api.nvim_set_keymap("n", "<leader>rc", ":lua require('refactoring').debug.cleanup({})<CR>", { noremap = true })
For certain languages like Golang, types are required for functions that return an object(s) and parameters of functions. Unfortunately, for some parameters and functions there is no way to automatically find their type. In those instances, we want to provide a way to input a type instead of insertting a placeholder value.
By default all prompts are turned off. The configuration below shows how to enable both for Golang. To enable it for your specific language, input something similar to below into the correct map.
require('refactoring').setup({
prompt_func_return_type = {
go = true
},
prompt_func_param_type = {
go = true
},
})