mirror of
https://github.com/neovim/neovim.git
synced 2025-10-22 17:11:49 +00:00
feat(treesitter): upstream get_node_at_cursor()
Util from the nvim-treesitter project.
This commit is contained in:

committed by
Christian Clason

parent
5b8d6e0b32
commit
ffe98531b9
@@ -81,7 +81,7 @@ end
|
|||||||
--- If needed this will create the parser.
|
--- If needed this will create the parser.
|
||||||
--- Unconditionally attach the provided callback
|
--- 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 lang string |nil Filetype of this parser (default: buffer filetype)
|
||||||
---@param opts table|nil Options to pass to the created language tree
|
---@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
|
return matches
|
||||||
end
|
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
|
--- Start treesitter highlighting for a buffer
|
||||||
---
|
---
|
||||||
--- Can be used in an ftplugin or FileType autocommand
|
--- Can be used in an ftplugin or FileType autocommand
|
||||||
|
Reference in New Issue
Block a user