From abc15511a2d185b6a72cb0da96c15f2011ad9546 Mon Sep 17 00:00:00 2001 From: Henry Date: Fri, 19 Apr 2024 23:11:29 -0400 Subject: [PATCH] Fix function documentation and improve struct_query Add identifiers for type_parameters and fields in struct query Add 'struct_properties' field to response of `get_all_nodes` function, this field will be present only for struct queries. It will contains `parameters_node` and `fields_node` fields. --- lua/gopher/_utils/ts/init.lua | 8 ++++---- lua/gopher/_utils/ts/nodes.lua | 35 ++++++++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/lua/gopher/_utils/ts/init.lua b/lua/gopher/_utils/ts/init.lua index ee24f49..c6d0310 100644 --- a/lua/gopher/_utils/ts/init.lua +++ b/lua/gopher/_utils/ts/init.lua @@ -3,7 +3,7 @@ local nodes = require "gopher._utils.ts.nodes" local u = require "gopher._utils" local M = { querys = { - struct_block = [[((type_declaration (type_spec name:(type_identifier) @struct.name type: (struct_type)))@struct.declaration)]], + struct_block = [[((type_declaration (type_spec name:(type_identifier) @struct.name type_parameters: (type_parameter_list)? @struct.parameters type: (struct_type ( field_declaration_list) @struct.fields) @struct.type))@struct.declaration)]], em_struct_block = [[(field_declaration name:(field_identifier)@struct.name type: (struct_type)) @struct.declaration]], package = [[(package_clause (package_identifier)@package.name)@package.clause]], interface = [[((type_declaration (type_spec name:(type_identifier) @interface.name type:(interface_type)))@interface.declaration)]], @@ -22,11 +22,11 @@ local function get_name_defaults() } end ----@param row string +---@param row integer ---@param col string ---@param bufnr string|nil ---@param do_notify boolean|nil ----@return table|nil +---@return table? function M.get_struct_node_at_pos(row, col, bufnr, do_notify) local notify = do_notify or true local query = M.querys.struct_block .. " " .. M.querys.em_struct_block @@ -41,7 +41,7 @@ function M.get_struct_node_at_pos(row, col, bufnr, do_notify) end end ----@param row string +---@param row integer ---@param col string ---@param bufnr string|nil ---@param do_notify boolean|nil diff --git a/lua/gopher/_utils/ts/nodes.lua b/lua/gopher/_utils/ts/nodes.lua index 87286a2..f6c467f 100644 --- a/lua/gopher/_utils/ts/nodes.lua +++ b/lua/gopher/_utils/ts/nodes.lua @@ -50,8 +50,8 @@ end ---@param query string ---@param lang string ---@param bufnr integer ----@param pos_row string ----@return string +---@param pos_row integer +---@return table? | string? function M.get_all_nodes(query, lang, _, bufnr, pos_row, _) local ts_query = require "nvim-treesitter.query" local parsers = require "nvim-treesitter.parsers" @@ -73,8 +73,19 @@ function M.get_all_nodes(query, lang, _, bufnr, pos_row, _) local results = {} for match in ts_query.iter_prepared_matches(parsed_query, root, bufnr, start_row, end_row) do - local sRow, sCol, eRow, eCol, declaration_node + -- 'type' will be the left side of a query identifier, e.g. @struct.name => struct + -- 'name' will be the name of the node + -- 'op' will be the right side of a query identifier, e.g. @struct.name => name local type, name, op = "", "", "" + + -- sRow and sCol are the column where the node start + -- eRow and eCol are the column where the node end + -- 'declaration_node' is the node that declares the struct + local sRow, sCol, eRow, eCol, declaration_node + + -- parameters and fields are properties of 'struct' nodes + local parameters_node, fields_node + locals.recurse_local_nodes(match, function(_, node, path) local idx = string.find(path, ".[^.]*$") op = string.sub(path, idx + 1, #path) @@ -89,6 +100,11 @@ function M.get_all_nodes(query, lang, _, bufnr, pos_row, _) eRow = eRow + 1 sCol = sCol + 1 eCol = eCol + 1 + elseif op == "parameters" then + print(node) + parameters_node = node + elseif op == "fields" then + fields_node = node end end) @@ -99,6 +115,13 @@ function M.get_all_nodes(query, lang, _, bufnr, pos_row, _) name = name, operator = op, type = type, + + + -- struct_properties only will be added for struct nodes + struct_properties = { + parameters_node = parameters_node, + fields_node = fields_node, + }, }) end end @@ -109,14 +132,14 @@ end ---@param query string ---@param default string ---@param bufnr string ----@param row string +---@param row integer ---@param col string ----@return table +---@return table? function M.nodes_at_cursor(query, default, bufnr, row, col) local u = require "gopher._utils" bufnr = bufnr or vim.api.nvim_get_current_buf() - local ft = vim.api.nvim_buf_get_option(bufnr, "ft") + local ft = vim.api.nvim_get_option_value("ft",{buf = bufnr}) if row == nil or col == nil then row, col = unpack(vim.api.nvim_win_get_cursor(0)) end