7 files changed,
96 insertions(+),
6 deletions(-)
Author:
Smirnov Oleksandr
ss2316544@gmail.com
Committed by:
Oleksandr Smirnov
olexsmir@gmail.com
Committed at:
2025-03-22 21:19:59 +0200
Parent:
a993ece
M
lua/gopher/_utils/ts.lua
@@ -1,8 +1,15 @@
local ts = {} local queries = { struct = [[ - (type_spec name: (type_identifier) @_name - type: (struct_type)) + [(type_spec name: (type_identifier) @_name + type: (struct_type)) + (var_declaration (var_spec + name: (identifier) @_name @_var + type: (struct_type))) + (short_var_declaration + left: (expression_list (identifier) @_name @_var) + right: (expression_list (composite_literal + type: (struct_type))))] ]], func = [[ [(function_declaration name: (identifier) @_name)@@ -40,7 +47,7 @@
---@param query vim.treesitter.Query ---@param node TSNode ---@param bufnr integer ----@return {name:string} +---@return {name:string, is_varstruct:boolean} local function get_captures(query, node, bufnr) local res = {} for _, match, _ in query:iter_matches(node, bufnr) do@@ -49,6 +56,10 @@ local capture_name = query.captures[capture_id]
if capture_name == "_name" then res["name"] = vim.treesitter.get_node_text(captured_node, bufnr) end + + if capture_name == "_var" then + res["is_varstruct"] = true + end end end@@ -59,6 +70,7 @@ ---@class gopher.TsResult
---@field name string ---@field start_line integer ---@field end_line integer +---@field is_varstruct boolean ---@param bufnr integer ---@param parent_type string[]@@ -93,7 +105,15 @@ function ts.get_struct_under_cursor(bufnr)
--- should be both type_spec and type_declaration --- because in cases like `type ( T struct{}, U strict{} )` --- i will be choosing always last struct in the list - return do_stuff(bufnr, { "type_spec", "type_declaration" }, queries.struct) + --- + --- var_declaration is for cases like `var x struct{}` + --- short_var_declaration is for cases like `x := struct{}{}` + return do_stuff(bufnr, { + "type_spec", + "type_declaration", + "var_declaration", + "short_var_declaration", + }, queries.struct) end ---@param bufnr integer