feat(treesitter): upstream get_node_at_cursor()

Util from the nvim-treesitter project.
This commit is contained in:
Quentin Rasmont
2022-04-30 11:51:14 +02:00
committed by Christian Clason
parent 5b8d6e0b32
commit ffe98531b9

View File

@@ -81,7 +81,7 @@ end
--- If needed this will create the parser.
--- Unconditionally attach the provided callback
---
---@param bufnr number|nil Buffer the parser should be tied to: (default current buffer)
---@param bufnr number|nil Buffer the parser should be tied to (default: current buffer)
---@param lang string |nil Filetype of this parser (default: buffer filetype)
---@param opts table|nil Options to pass to the created language tree
---
@@ -245,6 +245,27 @@ function M.get_captures_at_position(bufnr, row, col)
return matches
end
--- Gets the smallest named node under the cursor
---
---@param winnr number Window handle or 0 for current window
---@param opts table Options table
---@param opts.ignore_injections boolean (default true) Ignore injected languages.
---
---@returns (table) The named node under the cursor
function M.get_node_at_cursor(winnr, opts)
winnr = winnr or 0
local cursor = a.nvim_win_get_cursor(winnr)
local ts_cursor_range = { cursor[1] - 1, cursor[2], cursor[1] - 1, cursor[2] }
local buf = a.nvim_win_get_buf(winnr)
local root_lang_tree = M.get_parser(buf)
if not root_lang_tree then
return
end
return root_lang_tree:named_node_for_range(ts_cursor_range, opts)
end
--- Start treesitter highlighting for a buffer
---
--- Can be used in an ftplugin or FileType autocommand