mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-01 19:17:02 -04:00
81 lines
2.1 KiB
Lua
81 lines
2.1 KiB
Lua
-- 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)
|
|
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
|