aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/treesitter.lua
diff options
context:
space:
mode:
authorQuentin Rasmont <qrasmont@gmail.com>2022-04-30 11:51:14 +0200
committerChristian Clason <c.clason@uni-graz.at>2022-09-06 10:14:05 +0200
commitffe98531b9a6a90a7f4a7ae2105b3c50ad9332fd (patch)
treeed0dfff68cccf735300e2da1ae43248d34c54035 /runtime/lua/vim/treesitter.lua
parent5b8d6e0b3200c5cb9d98cbdb4ed0afe2b4edd38d (diff)
downloadrneovim-ffe98531b9a6a90a7f4a7ae2105b3c50ad9332fd.tar.gz
rneovim-ffe98531b9a6a90a7f4a7ae2105b3c50ad9332fd.tar.bz2
rneovim-ffe98531b9a6a90a7f4a7ae2105b3c50ad9332fd.zip
feat(treesitter): upstream get_node_at_cursor()
Util from the nvim-treesitter project.
Diffstat (limited to 'runtime/lua/vim/treesitter.lua')
-rw-r--r--runtime/lua/vim/treesitter.lua23
1 files changed, 22 insertions, 1 deletions
diff --git a/runtime/lua/vim/treesitter.lua b/runtime/lua/vim/treesitter.lua
index 9c43811e03..d71f8611c1 100644
--- a/runtime/lua/vim/treesitter.lua
+++ b/runtime/lua/vim/treesitter.lua
@@ -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