go.nvim/lua/go/ts/go.lua

194 lines
7.0 KiB
Lua
Raw Normal View History

2021-04-19 06:09:39 +00:00
local nodes = require("go.ts.nodes")
2021-07-16 15:58:44 +00:00
local log = require("go.utils").log
-- local warn = require("go.utils").warn
-- local info = require("go.utils").info
local debug = require("go.utils").debug
2021-07-16 15:58:44 +00:00
2022-04-18 14:13:51 +00:00
local M = {
query_struct = "(type_spec name:(type_identifier) @definition.struct type: (struct_type))",
2021-03-10 12:15:06 +00:00
query_package = "(package_clause (package_identifier)@package.name)@package.clause",
query_struct_id = "(type_spec name:(type_identifier) @definition.struct (struct_type))",
query_em_struct_id = "(field_declaration name:(field_identifier) @definition.struct (struct_type))",
2022-07-06 17:12:24 +00:00
query_struct_block = [[((type_declaration (type_spec name:(type_identifier) @struct.name type: (struct_type)))@struct.declaration)]],
query_type_declaration = [[((type_declaration (type_spec name:(type_identifier)@type.name)))]],
2021-04-19 06:09:39 +00:00
query_em_struct_block = [[(field_declaration name:(field_identifier)@struct.name type: (struct_type)) @struct.declaration]],
2021-03-12 02:54:08 +00:00
query_struct_block_from_id = [[(((type_spec name:(type_identifier) type: (struct_type)))@block.struct_from_id)]],
2021-07-16 15:58:44 +00:00
-- query_em_struct = "(field_declaration name:(field_identifier) @definition.struct type: (struct_type))",
2021-03-12 02:54:08 +00:00
query_interface_id = [[((type_declaration (type_spec name:(type_identifier) @interface.name type:(interface_type)))@interface.declaration)]],
query_interface_method = [[((method_spec name: (field_identifier)@method.name)@interface.method.declaration)]],
2021-03-10 12:15:06 +00:00
query_func = "((function_declaration name: (identifier)@function.name) @function.declaration)",
query_method = "(method_declaration receiver: (parameter_list (parameter_declaration name:(identifier)@method.receiver.name type:(type_identifier)@method.receiver.type)) name:(field_identifier)@method.name)@method.declaration",
2021-03-12 02:54:08 +00:00
query_method_name = [[((method_declaration
2021-03-10 12:15:06 +00:00
receiver: (parameter_list)@method.receiver
name: (field_identifier)@method.name
2021-03-12 02:54:08 +00:00
body:(block))@method.declaration)]],
query_method_void = [[((method_declaration
2021-03-10 12:15:06 +00:00
receiver: (parameter_list
(parameter_declaration
name: (identifier)@method.receiver.name
type: (pointer_type)@method.receiver.type)
)
name: (field_identifier)@method.name
parameters: (parameter_list)@method.parameter
body:(block)
2021-03-12 02:54:08 +00:00
)@method.declaration)]],
2021-03-10 12:15:06 +00:00
query_method_multi_ret = [[(method_declaration
receiver: (parameter_list
(parameter_declaration
name: (identifier)@method.receiver.name
type: (pointer_type)@method.receiver.type)
)
name: (field_identifier)@method.name
parameters: (parameter_list)@method.parameter
result: (parameter_list)@method.result
body:(block)
)@method.declaration]],
2021-03-12 02:54:08 +00:00
query_method_single_ret = [[((method_declaration
2021-03-10 12:15:06 +00:00
receiver: (parameter_list
(parameter_declaration
name: (identifier)@method.receiver.name
type: (pointer_type)@method.receiver.type)
)
name: (field_identifier)@method.name
parameters: (parameter_list)@method.parameter
result: (type_identifier)@method.result
body:(block)
2021-03-12 02:54:08 +00:00
)@method.declaration)]],
query_tr_method_void = [[((method_declaration
2021-03-10 12:15:06 +00:00
receiver: (parameter_list
(parameter_declaration
name: (identifier)@method.receiver.name
type: (type_identifier)@method.receiver.type)
)
name: (field_identifier)@method.name
parameters: (parameter_list)@method.parameter
body:(block)
2021-03-12 02:54:08 +00:00
)@method.declaration)]],
query_tr_method_multi_ret = [[((method_declaration
2021-03-10 12:15:06 +00:00
receiver: (parameter_list
(parameter_declaration
name: (identifier)@method.receiver.name
type: (type_identifier)@method.receiver.type)
)
name: (field_identifier)@method.name
parameters: (parameter_list)@method.parameter
result: (parameter_list)@method.result
body:(block)
2021-03-12 02:54:08 +00:00
)@method.declaration)]],
query_tr_method_single_ret = [[((method_declaration
2021-03-10 12:15:06 +00:00
receiver: (parameter_list
(parameter_declaration
name: (identifier)@method.receiver.name
type: (type_identifier)@method.receiver.type)
)
name: (field_identifier)@method.name
parameters: (parameter_list)@method.parameter
result: (type_identifier)@method.result
body:(block)
2022-04-18 12:31:17 +00:00
)@method.declaration)]],
2022-04-18 14:13:51 +00:00
query_test_func = [[((function_declaration name: (identifier) @test_name
2022-04-18 12:31:17 +00:00
parameters: (parameter_list
(parameter_declaration
name: (identifier)
type: (pointer_type
(qualified_type
package: (package_identifier) @_param_package
name: (type_identifier) @_param_name))))
) @testfunc
(#contains? @test_name "Test")
(#match? @_param_package "testing")
(#match? @_param_name "T"))]],
2021-03-10 12:15:06 +00:00
}
2022-04-18 14:13:51 +00:00
2021-09-18 23:58:04 +00:00
local function get_name_defaults()
2022-04-18 12:31:17 +00:00
return { ["func"] = "function", ["if"] = "if", ["else"] = "else", ["for"] = "for" }
2021-03-10 12:15:06 +00:00
end
2021-09-18 23:58:04 +00:00
M.get_struct_node_at_pos = function(row, col, bufnr)
2021-04-19 06:09:39 +00:00
local query = M.query_struct_block .. " " .. M.query_em_struct_block
2021-09-18 23:58:04 +00:00
local bufn = bufnr or vim.api.nvim_get_current_buf()
2021-03-10 12:15:06 +00:00
local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col)
2021-04-19 06:09:39 +00:00
if ns == nil then
debug("struct not found")
2021-04-19 06:09:39 +00:00
else
2022-04-18 12:31:17 +00:00
log("struct node", ns)
2021-04-19 06:09:39 +00:00
return ns[#ns]
end
2021-03-10 12:15:06 +00:00
end
M.get_type_node_at_pos = function(row, col, bufnr)
local query = M.query_type_declaration
local bufn = bufnr or vim.api.nvim_get_current_buf()
local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col)
if ns == nil then
debug("type not found")
else
log("type node", ns)
return ns[#ns]
end
end
2021-09-18 23:58:04 +00:00
M.get_interface_node_at_pos = function(row, col, bufnr)
2021-04-19 06:09:39 +00:00
local query = M.query_interface_id
2021-03-10 12:15:06 +00:00
2021-09-18 23:58:04 +00:00
local bufn = bufnr or vim.api.nvim_get_current_buf()
2021-03-10 12:15:06 +00:00
local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col)
2021-04-19 06:09:39 +00:00
if ns == nil then
debug("interface not found")
2021-04-19 06:09:39 +00:00
else
return ns[#ns]
end
2021-03-10 12:15:06 +00:00
end
2021-09-18 23:58:04 +00:00
M.get_interface_method_node_at_pos = function(row, col, bufnr)
2021-04-19 06:09:39 +00:00
local query = M.query_interface_method
2021-09-18 23:58:04 +00:00
local bufn = bufnr or vim.api.nvim_get_current_buf()
2021-03-10 12:15:06 +00:00
local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col)
2021-04-19 06:09:39 +00:00
if ns == nil then
2021-11-16 22:22:16 +00:00
warn("interface method not found")
2021-04-19 06:09:39 +00:00
else
return ns[#ns]
end
2021-03-10 12:15:06 +00:00
end
2021-09-18 23:58:04 +00:00
M.get_func_method_node_at_pos = function(row, col, bufnr)
2021-04-19 06:09:39 +00:00
local query = M.query_func .. " " .. M.query_method_name
2021-03-10 12:15:06 +00:00
-- local query = require("go.ts.go").query_method_name
2021-09-18 23:58:04 +00:00
local bufn = bufnr or vim.api.nvim_get_current_buf()
2021-03-10 12:15:06 +00:00
local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col)
2021-04-19 06:09:39 +00:00
if ns == nil then
return nil
end
if ns == nil then
2021-11-16 22:22:16 +00:00
warn("function not found")
2021-04-19 06:09:39 +00:00
else
return ns[#ns]
end
2021-03-10 12:15:06 +00:00
end
2021-09-18 23:58:04 +00:00
M.get_package_node_at_pos = function(row, col, bufnr)
2021-04-19 06:09:39 +00:00
if row > 10 then
return
end
local query = M.query_package
2021-03-10 12:15:06 +00:00
-- local query = require("go.ts.go").query_method_name
2021-09-18 23:58:04 +00:00
local bufn = bufnr or vim.api.nvim_get_current_buf()
2021-03-10 12:15:06 +00:00
local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col)
2021-04-19 06:09:39 +00:00
if ns == nil then
return nil
end
if ns == nil then
2021-11-16 22:22:16 +00:00
warn("package not found")
2021-04-19 06:09:39 +00:00
else
return ns[#ns]
end
2021-03-10 12:15:06 +00:00
end
return M