Skip to content

The Refactoring library based off the Refactoring book by Martin Fowler

License

Notifications You must be signed in to change notification settings

captainko/refactoring.nvim

 
 

Repository files navigation

refactoring.nvim

The Refactoring library based off the Refactoring book by Martin Fowler

Lua Neovim

WIP

Although this plugin is usable, it is very much a work in progress, meaning that the API will inevitably change very rapidly over time.

Table of Contents

Installation

Requirements

  • Neovim Nightly
  • Treesitter
  • Plenary

Setup Using Packer

use {
    "ThePrimeagen/refactoring.nvim",
    requires = {
        {"nvim-lua/plenary.nvim"},
        {"nvim-treesitter/nvim-treesitter"}
    }
}

Features

Supported Languages

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

Refactoring Features

  • Support for various common refactoring operations
    • 106: Extract Function
      • Also possible to extract to file
    • 119: Extract Variable
    • 123: Inline Variable

Debug Features

  • 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

Configuration

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({})

Configuration for Refactoring Operations

Using Direct Remaps

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.

Using Telescope

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 }
)

Configuration for Debug Operations

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 })

Configuration for Prompt type Operations

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
    },
})

About

The Refactoring library based off the Refactoring book by Martin Fowler

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Lua 80.6%
  • Go 4.1%
  • TypeScript 3.7%
  • C 3.2%
  • C++ 2.6%
  • Scheme 2.1%
  • Other 3.7%