summaryrefslogtreecommitdiff
path: root/lua/nvim-treesitter/refactor
diff options
context:
space:
mode:
authorSteven Sojka <Steven.Sojka@tdameritrade.com>2020-06-26 11:11:21 -0500
committerKiyan Yazdani <yazdani.kiyan@protonmail.com>2020-06-30 08:21:01 +0200
commit64838e51c0fcb9def4be912391a1544b4d9a9d27 (patch)
treee2a66a26be775d27a74e916dabfc66664a99644b /lua/nvim-treesitter/refactor
parent058e8d2296515041be982c6f23c119ec6b6d1ba9 (diff)
feat(refactor): add smart rename module
Diffstat (limited to 'lua/nvim-treesitter/refactor')
-rw-r--r--lua/nvim-treesitter/refactor/highlight_definitions.lua20
-rw-r--r--lua/nvim-treesitter/refactor/smart_rename.lua75
2 files changed, 76 insertions, 19 deletions
diff --git a/lua/nvim-treesitter/refactor/highlight_definitions.lua b/lua/nvim-treesitter/refactor/highlight_definitions.lua
index ef415bb7..6279e470 100644
--- a/lua/nvim-treesitter/refactor/highlight_definitions.lua
+++ b/lua/nvim-treesitter/refactor/highlight_definitions.lua
@@ -11,24 +11,6 @@ local M = {}
local usage_namespace = api.nvim_create_namespace('nvim-treesitter-usages')
-local function find_usages(node, scope_node)
- local usages = {}
- local node_text = ts_utils.get_node_text(node)[1]
-
- if not node_text or #node_text < 1 then return end
-
- for _, def in ipairs(locals.collect_locals(bufnr, scope_node)) do
- if def.reference
- and def.reference.node
- and ts_utils.get_node_text(def.reference.node)[1] == node_text then
-
- table.insert(usages, def.reference.node)
- end
- end
-
- return usages
-end
-
function M.highlight_usages(bufnr)
M.clear_usage_highlights(bufnr)
@@ -37,7 +19,7 @@ function M.highlight_usages(bufnr)
if not node_at_point then return end
local def_node, scope = ts_utils.find_definition(node_at_point, bufnr)
- local usages = find_usages(node_at_point, scope)
+ local usages = ts_utils.find_usages(node_at_point, scope)
for _, usage_node in ipairs(usages) do
local start_row, start_col, _, end_col = usage_node:range()
diff --git a/lua/nvim-treesitter/refactor/smart_rename.lua b/lua/nvim-treesitter/refactor/smart_rename.lua
new file mode 100644
index 00000000..5b7562eb
--- /dev/null
+++ b/lua/nvim-treesitter/refactor/smart_rename.lua
@@ -0,0 +1,75 @@
+-- Binds a keybinding to smart rename definitions and usages.
+-- Can be used directly using the `smart_rename` function.
+
+local ts_utils = require'nvim-treesitter.ts_utils'
+local configs = require'nvim-treesitter.configs'
+local api = vim.api
+
+local M = {}
+
+function M.smart_rename(bufnr)
+ local bufnr = bufnr or api.nvim_get_current_buf()
+ local node_at_point = ts_utils.get_node_at_cursor()
+
+ if not node_at_point then
+ print('No node to rename!')
+ return
+ end
+
+ local node_text = ts_utils.get_node_text(node_at_point)[1]
+ local new_name = vim.fn.input('New name: ', node_text or '')
+
+ -- Empty name cancels the interaction or ESC
+ if not new_name or #new_name < 1 then return end
+
+ local definition, scope = ts_utils.find_definition(node_at_point, bufnr)
+ local nodes_to_rename = ts_utils.find_usages(node_at_point, scope)
+
+ if not vim.tbl_contains(nodes_to_rename, node_at_point) then
+ table.insert(nodes_to_rename, node_at_point)
+ end
+
+ if definition and not vim.tbl_contains(nodes_to_rename, definition) then
+ table.insert(nodes_to_rename, definition)
+ end
+
+ if #nodes_to_rename < 1 then
+ print('No nodes to rename!')
+ return
+ end
+
+ for _, node in ipairs(nodes_to_rename) do
+ local start_row, start_col, end_row, end_col = node:range()
+
+ local line = api.nvim_buf_get_lines(bufnr, start_row, start_row + 1, false)[1]
+
+ if line then
+ local new_line = line:sub(1, start_col) .. new_name .. line:sub(end_col + 1, -1)
+
+ api.nvim_buf_set_lines(bufnr, start_row, start_row + 1, false, { new_line })
+ end
+ end
+end
+
+function M.attach(bufnr)
+ local bufnr = bufnr or api.nvim_get_current_buf()
+
+ local config = configs.get_module('refactor.smart_rename')
+
+ for fn_name, mapping in pairs(config.keymaps) do
+ local cmd = string.format([[:lua require'nvim-treesitter.refactor.smart_rename'.%s(%d)<CR>]], fn_name, bufnr)
+
+ api.nvim_buf_set_keymap(bufnr, 'n', mapping, cmd, { silent = true })
+ end
+end
+
+function M.detach(bufnr)
+ local buf = bufnr or api.nvim_get_current_buf()
+ local config = configs.get_module('refactor.smart_rename')
+
+ for fn_name, mapping in pairs(config.keymaps) do
+ api.nvim_buf_del_keymap(bufnr, 'n', mapping)
+ end
+end
+
+return M