summaryrefslogtreecommitdiff
path: root/lua/nvim-treesitter/refactor
diff options
context:
space:
mode:
authorSteven Sojka <Steven.Sojka@tdameritrade.com>2020-06-26 13:57:23 -0500
committerKiyan Yazdani <yazdani.kiyan@protonmail.com>2020-06-30 08:21:01 +0200
commit6f8e4c97a4f99b1a04cca5c41c333ffb5337d84a (patch)
tree8b49bdf8651b54b0ff398af254c026cb303347fc /lua/nvim-treesitter/refactor
parent64838e51c0fcb9def4be912391a1544b4d9a9d27 (diff)
feat(refactor): add definition navigation module
Diffstat (limited to 'lua/nvim-treesitter/refactor')
-rw-r--r--lua/nvim-treesitter/refactor/navigation.lua87
1 files changed, 87 insertions, 0 deletions
diff --git a/lua/nvim-treesitter/refactor/navigation.lua b/lua/nvim-treesitter/refactor/navigation.lua
new file mode 100644
index 00000000..9c439179
--- /dev/null
+++ b/lua/nvim-treesitter/refactor/navigation.lua
@@ -0,0 +1,87 @@
+-- Definition based navigation module
+
+local ts_utils = require'nvim-treesitter.ts_utils'
+local locals = require'nvim-treesitter.locals'
+local configs = require'nvim-treesitter.configs'
+local api = vim.api
+
+local M = {}
+
+local function node_to_qf(node, kind)
+ local lnum, col, _ = def.node:start()
+
+ return {
+ bufnr = bufnr,
+ lnum = lnum + 1,
+ col = col + 1,
+ text = ts_utils.get_node_text(def.node)[1] or '',
+ kind = kind
+ }
+end
+
+function M.goto_definition(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 return end
+
+ local definition, _ = ts_utils.find_definition(node_at_point, bufnr)
+
+ if not definition then
+ print('No definition found')
+ return
+ end
+
+ local start_row, start_col, _ = definition:start()
+
+ api.nvim_win_set_cursor(0, { start_row + 1, start_col })
+end
+
+function M.list_definitions(bufnr)
+ local bufnr = bufnr or api.nvim_get_current_buf()
+ local definitions = locals.get_definitions(bufnr)
+
+ if #definitions < 1 then return end
+
+ local qf_list = {}
+
+ for _, def in ipairs(definitions) do
+ ts_utils.recurse_local_nodes(def, function(_, node, _, match)
+ local lnum, col, _ = node:start()
+
+ table.insert(qf_list, {
+ bufnr = bufnr,
+ lnum = lnum + 1,
+ col = col + 1,
+ text = ts_utils.get_node_text(node)[1] or "",
+ kind = match and match:sub(1, 1) or ""
+ })
+ end)
+ end
+
+ vim.fn.setqflist(qf_list, 'r')
+ api.nvim_command('copen')
+end
+
+function M.attach(bufnr)
+ local bufnr = bufnr or api.nvim_get_current_buf()
+
+ local config = configs.get_module('refactor.navigation')
+
+ for fn_name, mapping in pairs(config.keymaps) do
+ local cmd = string.format([[:lua require'nvim-treesitter.refactor.navigation'.%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.navigation')
+
+ for fn_name, mapping in pairs(config.keymaps) do
+ api.nvim_buf_del_keymap(bufnr, 'n', mapping)
+ end
+end
+
+return M