From ffe98531b9a6a90a7f4a7ae2105b3c50ad9332fd Mon Sep 17 00:00:00 2001 From: Quentin Rasmont Date: Sat, 30 Apr 2022 11:51:14 +0200 Subject: feat(treesitter): upstream get_node_at_cursor() Util from the nvim-treesitter project. --- runtime/lua/vim/treesitter.lua | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'runtime/lua/vim') 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 -- cgit From 95fd1ad83e24bbb14cc084fb001251939de6c0a9 Mon Sep 17 00:00:00 2001 From: Christian Clason Date: Tue, 6 Sep 2022 08:50:06 +0200 Subject: refactor(treesitter): get_{nodes,captures}_at_{position,cursor} --- runtime/lua/vim/treesitter.lua | 65 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 12 deletions(-) (limited to 'runtime/lua/vim') diff --git a/runtime/lua/vim/treesitter.lua b/runtime/lua/vim/treesitter.lua index d71f8611c1..8a540b9012 100644 --- a/runtime/lua/vim/treesitter.lua +++ b/runtime/lua/vim/treesitter.lua @@ -147,7 +147,7 @@ end --- ---@param node_or_range table --- ----@returns start_row, start_col, end_row, end_col +---@returns table start_row, start_col, end_row, end_col function M.get_node_range(node_or_range) if type(node_or_range) == 'table' then return unpack(node_or_range) @@ -198,7 +198,11 @@ end ---@param row number Position row ---@param col number Position column --- ----@returns (table) A table of captures +---@param bufnr number Buffer number (0 for current buffer) +---@param row number Position row +---@param col number Position column +--- +---@returns (table) Table of captures function M.get_captures_at_position(bufnr, row, col) if bufnr == 0 then bufnr = a.nvim_get_current_buf() @@ -245,25 +249,62 @@ function M.get_captures_at_position(bufnr, row, col) return matches end ---- Gets the smallest named node under the cursor +---Gets a list of captures 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. +---@param winnr number|nil Window handle or 0 for current window (default) --- ----@returns (table) The named node under the cursor -function M.get_node_at_cursor(winnr, opts) +---@returns (table) Named node under the cursor +function M.get_captures_at_cursor(winnr) winnr = winnr or 0 + local bufnr = a.nvim_win_get_buf(winnr) 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) + local data = M.get_captures_at_position(bufnr, cursor[1] - 1, cursor[2]) + + local captures = {} + + for _, capture in ipairs(data) do + table.insert(captures, capture.capture) + end + + return captures +end + +--- Gets the smallest named node at position +--- +---@param bufnr number Buffer number (0 for current buffer) +---@param row number Position row +---@param col number Position column +---@param opts table Optional keyword arguments: +--- - ignore_injections boolean Ignore injected languages (default true) +--- +---@returns (table) Named node under the cursor +function M.get_node_at_position(bufnr, row, col, opts) + if bufnr == 0 then + bufnr = a.nvim_get_current_buf() + end + local ts_range = { row, col, row, col } + + local root_lang_tree = M.get_parser(bufnr) if not root_lang_tree then return end - return root_lang_tree:named_node_for_range(ts_cursor_range, opts) + return root_lang_tree:named_node_for_range(ts_range, opts) +end + +--- Gets the smallest named node under the cursor +--- +---@param winnr number|nil Window handle or 0 for current window (default) +--- +---@returns (string) Named node under the cursor +function M.get_node_at_cursor(winnr) + winnr = winnr or 0 + local bufnr = a.nvim_win_get_buf(winnr) + local cursor = a.nvim_win_get_cursor(winnr) + + return M.get_node_at_position(bufnr, cursor[1] - 1, cursor[2], { ignore_injections = false }) + :type() end --- Start treesitter highlighting for a buffer -- cgit