refactor: treesitter utils (#91)
* refactor(ts_utils): i dont know why event it was here * fix: typos * fix(struct_tags)!: remove statement that i used for debug * refactor(ts_util): start from scratch * refactor(struct_tags): use new ts_util * fixup! refactor(struct_tags): use new ts_util * test(struct_tags): add support for multiple structs * fix(gotests): use new api * fix(impl): refactor some logic, use new api * docs(ts): add an explanation * refactor(_utils.ts): all public methods are just adapters * fix(comment): now it works * fixup! refactor(_utils.ts): all public methods are just adapters * fixup! fixup! refactor(_utils.ts): all public methods are just adapters * test(comment): e2e * tests(comment): fix * refactor(utils.ts): fix, docs * test(comment): fix tests again * fix(tests/comments): well, now i fell stupid * refactor(ts): add assert just to be sure that all elements are in the result * fix(ts): type annotations * fix(ts): pass bufnr to vim.treesitter.get_node * chore(ci): disable nightly * chore(ci): reorganize
This commit is contained in:
parent
f171953e43
commit
e9f2eef5e7
31 changed files with 346 additions and 409 deletions
|
|
@ -91,7 +91,7 @@ do
|
|||
local log_at_level = function(level_config, message_maker, ...)
|
||||
-- Return early if we're below the current_log_level
|
||||
--
|
||||
-- the log level source get from config directly because otherwise it doesnt work
|
||||
-- the log level source get from config directly because otherwise it doesn't work
|
||||
if level_config.level < c.log_level then
|
||||
return
|
||||
end
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ end
|
|||
---@return string[]|nil
|
||||
function gocmd.run(subcmd, args)
|
||||
if #args == 0 then
|
||||
error "please provice any arguments"
|
||||
error "please provide any arguments"
|
||||
end
|
||||
|
||||
if subcmd == "get" then
|
||||
|
|
@ -45,7 +45,7 @@ function gocmd.run(subcmd, args)
|
|||
if status ~= 0 then
|
||||
error("gocmd failed: " .. data)
|
||||
end
|
||||
u.notify(c.go .. " " .. subcmd .. " successful runned")
|
||||
u.notify(c.go .. " " .. subcmd .. " ran successful")
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
|
|
|||
115
lua/gopher/_utils/ts.lua
Normal file
115
lua/gopher/_utils/ts.lua
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
local ts = {}
|
||||
local queries = {
|
||||
struct = [[
|
||||
(type_spec name: (type_identifier) @_name
|
||||
type: (struct_type))
|
||||
]],
|
||||
func = [[
|
||||
[(function_declaration name: (identifier) @_name)
|
||||
(method_declaration name: (field_identifier) @_name)]
|
||||
]],
|
||||
package = [[
|
||||
(package_identifier) @_name
|
||||
]],
|
||||
interface = [[
|
||||
(type_spec
|
||||
name: (type_identifier) @_name
|
||||
type: (interface_type))
|
||||
]],
|
||||
}
|
||||
|
||||
---@param parent_type string[]
|
||||
---@param node TSNode
|
||||
---@return TSNode?
|
||||
local function get_parrent_node(parent_type, node)
|
||||
---@type TSNode?
|
||||
local current = node
|
||||
while current do
|
||||
if vim.tbl_contains(parent_type, current:type()) then
|
||||
break
|
||||
end
|
||||
|
||||
current = current:parent()
|
||||
if current == nil then
|
||||
return nil
|
||||
end
|
||||
end
|
||||
return current
|
||||
end
|
||||
|
||||
---@param query vim.treesitter.Query
|
||||
---@param node TSNode
|
||||
---@param bufnr integer
|
||||
---@return {name:string}
|
||||
local function get_captures(query, node, bufnr)
|
||||
local res = {}
|
||||
for _, match, _ in query:iter_matches(node, bufnr) do
|
||||
for capture_id, captured_node in pairs(match) do
|
||||
local capture_name = query.captures[capture_id]
|
||||
if capture_name == "_name" then
|
||||
res["name"] = vim.treesitter.get_node_text(captured_node, bufnr)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return res
|
||||
end
|
||||
|
||||
---@class gopher.TsResult
|
||||
---@field name string
|
||||
---@field start_line integer
|
||||
---@field end_line integer
|
||||
|
||||
---@param bufnr integer
|
||||
---@param parent_type string[]
|
||||
---@param query string
|
||||
---@return gopher.TsResult
|
||||
local function do_stuff(bufnr, parent_type, query)
|
||||
local node = vim.treesitter.get_node {
|
||||
bufnr = bufnr,
|
||||
}
|
||||
if not node then
|
||||
error "No nodes found under cursor"
|
||||
end
|
||||
|
||||
local parent_node = get_parrent_node(parent_type, node)
|
||||
if not parent_node then
|
||||
error "No parent node found under cursor"
|
||||
end
|
||||
|
||||
local q = vim.treesitter.query.parse("go", query)
|
||||
local res = get_captures(q, parent_node, bufnr)
|
||||
assert(res.name ~= nil, "No capture name found")
|
||||
|
||||
local start_row, _, end_row, _ = parent_node:range()
|
||||
res["start_line"] = start_row + 1
|
||||
res["end_line"] = end_row + 1
|
||||
|
||||
return res
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
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)
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
function ts.get_func_under_cursor(bufnr)
|
||||
--- since this handles both and funcs and methods we should check for both parent nodes
|
||||
return do_stuff(bufnr, { "function_declaration", "method_declaration" }, queries.func)
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
function ts.get_package_under_cursor(bufnr)
|
||||
return do_stuff(bufnr, { "package_clause" }, queries.package)
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
function ts.get_interface_under_cursor(bufnr)
|
||||
return do_stuff(bufnr, { "type_declaration" }, queries.interface)
|
||||
end
|
||||
|
||||
return ts
|
||||
|
|
@ -1,104 +0,0 @@
|
|||
---@diagnostic disable: param-type-mismatch
|
||||
local nodes = require "gopher._utils.ts.nodes"
|
||||
local u = require "gopher._utils"
|
||||
local ts = {
|
||||
querys = {
|
||||
struct_block = [[((type_declaration (type_spec name:(type_identifier) @struct.name type: (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)]],
|
||||
method_name = [[((method_declaration receiver: (parameter_list)@method.receiver name: (field_identifier)@method.name body:(block))@method.declaration)]],
|
||||
func = [[((function_declaration name: (identifier)@function.name) @function.declaration)]],
|
||||
},
|
||||
}
|
||||
|
||||
---@return table
|
||||
local function get_name_defaults()
|
||||
return {
|
||||
["func"] = "function",
|
||||
["if"] = "if",
|
||||
["else"] = "else",
|
||||
["for"] = "for",
|
||||
}
|
||||
end
|
||||
|
||||
---@param row string
|
||||
---@param col string
|
||||
---@param bufnr string|nil
|
||||
---@param do_notify boolean|nil
|
||||
---@return table|nil
|
||||
function ts.get_struct_node_at_pos(row, col, bufnr, do_notify)
|
||||
local notify = do_notify or true
|
||||
local query = ts.querys.struct_block .. " " .. ts.querys.em_struct_block
|
||||
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
|
||||
if notify then
|
||||
u.deferred_notify("struct not found", vim.log.levels.WARN)
|
||||
end
|
||||
else
|
||||
return ns[#ns]
|
||||
end
|
||||
end
|
||||
|
||||
---@param row string
|
||||
---@param col string
|
||||
---@param bufnr string|nil
|
||||
---@param do_notify boolean|nil
|
||||
---@return table|nil
|
||||
function ts.get_func_method_node_at_pos(row, col, bufnr, do_notify)
|
||||
local notify = do_notify or true
|
||||
local query = ts.querys.func .. " " .. ts.querys.method_name
|
||||
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
|
||||
if notify then
|
||||
u.deferred_notify("function not found", vim.log.levels.WARN)
|
||||
end
|
||||
else
|
||||
return ns[#ns]
|
||||
end
|
||||
end
|
||||
|
||||
---@param row string
|
||||
---@param col string
|
||||
---@param bufnr string|nil
|
||||
---@param do_notify boolean|nil
|
||||
---@return table|nil
|
||||
function ts.get_package_node_at_pos(row, col, bufnr, do_notify)
|
||||
local notify = do_notify or true
|
||||
-- stylua: ignore
|
||||
if row > 10 then return end
|
||||
local query = ts.querys.package
|
||||
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
|
||||
if notify then
|
||||
u.deferred_notify("package not found", vim.log.levels.WARN)
|
||||
return nil
|
||||
end
|
||||
else
|
||||
return ns[#ns]
|
||||
end
|
||||
end
|
||||
|
||||
---@param row string
|
||||
---@param col string
|
||||
---@param bufnr string|nil
|
||||
---@param do_notify boolean|nil
|
||||
---@return table|nil
|
||||
function ts.get_interface_node_at_pos(row, col, bufnr, do_notify)
|
||||
local notify = do_notify or true
|
||||
local query = ts.querys.interface
|
||||
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
|
||||
if notify then
|
||||
u.deferred_notify("interface not found", vim.log.levels.WARN)
|
||||
end
|
||||
else
|
||||
return ns[#ns]
|
||||
end
|
||||
end
|
||||
|
||||
return ts
|
||||
|
|
@ -1,143 +0,0 @@
|
|||
local ts_query = require "nvim-treesitter.query"
|
||||
local parsers = require "nvim-treesitter.parsers"
|
||||
local locals = require "nvim-treesitter.locals"
|
||||
local u = require "gopher._utils"
|
||||
local M = {}
|
||||
|
||||
local function intersects(row, col, sRow, sCol, eRow, eCol)
|
||||
if sRow > row or eRow < row then
|
||||
return false
|
||||
end
|
||||
|
||||
if sRow == row and sCol > col then
|
||||
return false
|
||||
end
|
||||
|
||||
if eRow == row and eCol < col then
|
||||
return false
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
---@param nodes table
|
||||
---@param row string
|
||||
---@param col string
|
||||
---@return table
|
||||
function M.intersect_nodes(nodes, row, col)
|
||||
local found = {}
|
||||
for idx = 1, #nodes do
|
||||
local node = nodes[idx]
|
||||
local sRow = node.dim.s.r
|
||||
local sCol = node.dim.s.c
|
||||
local eRow = node.dim.e.r
|
||||
local eCol = node.dim.e.c
|
||||
|
||||
if intersects(row, col, sRow, sCol, eRow, eCol) then
|
||||
table.insert(found, node)
|
||||
end
|
||||
end
|
||||
|
||||
return found
|
||||
end
|
||||
|
||||
---@param nodes table
|
||||
---@return table
|
||||
function M.sort_nodes(nodes)
|
||||
table.sort(nodes, function(a, b)
|
||||
return M.count_parents(a) < M.count_parents(b)
|
||||
end)
|
||||
|
||||
return nodes
|
||||
end
|
||||
|
||||
---@param query string
|
||||
---@param lang string
|
||||
---@param bufnr integer
|
||||
---@param pos_row string
|
||||
---@return string
|
||||
function M.get_all_nodes(query, lang, _, bufnr, pos_row, _)
|
||||
bufnr = bufnr or 0
|
||||
pos_row = pos_row or 30000
|
||||
|
||||
local ok, parsed_query = pcall(function()
|
||||
return vim.treesitter.query.parse(lang, query)
|
||||
end)
|
||||
if not ok then
|
||||
return nil
|
||||
end
|
||||
|
||||
local parser = parsers.get_parser(bufnr, lang)
|
||||
local root = parser:parse()[1]:root()
|
||||
local start_row, _, end_row, _ = root:range()
|
||||
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
|
||||
local type, name, op = "", "", ""
|
||||
locals.recurse_local_nodes(match, function(_, node, path)
|
||||
local idx = string.find(path, ".[^.]*$")
|
||||
op = string.sub(path, idx + 1, #path)
|
||||
type = string.sub(path, 1, idx - 1)
|
||||
|
||||
if op == "name" then
|
||||
name = vim.treesitter.get_node_text(node, bufnr)
|
||||
elseif op == "declaration" or op == "clause" then
|
||||
declaration_node = node
|
||||
sRow, sCol, eRow, eCol = node:range()
|
||||
sRow = sRow + 1
|
||||
eRow = eRow + 1
|
||||
sCol = sCol + 1
|
||||
eCol = eCol + 1
|
||||
end
|
||||
end)
|
||||
|
||||
if declaration_node ~= nil then
|
||||
table.insert(results, {
|
||||
declaring_node = declaration_node,
|
||||
dim = { s = { r = sRow, c = sCol }, e = { r = eRow, c = eCol } },
|
||||
name = name,
|
||||
operator = op,
|
||||
type = type,
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
return results
|
||||
end
|
||||
|
||||
---@param query string
|
||||
---@param default string
|
||||
---@param bufnr string
|
||||
---@param row string
|
||||
---@param col string
|
||||
---@return table
|
||||
function M.nodes_at_cursor(query, default, bufnr, row, col)
|
||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
||||
local ft = vim.api.nvim_buf_get_option(bufnr, "ft")
|
||||
if row == nil or col == nil then
|
||||
row, col = unpack(vim.api.nvim_win_get_cursor(0))
|
||||
end
|
||||
|
||||
local nodes = M.get_all_nodes(query, ft, default, bufnr, row, col)
|
||||
if nodes == nil then
|
||||
u.deferred_notify(
|
||||
"Unable to find any nodes. Place your cursor on a go symbol and try again",
|
||||
vim.log.levels.DEBUG
|
||||
)
|
||||
return nil
|
||||
end
|
||||
|
||||
nodes = M.sort_nodes(M.intersect_nodes(nodes, row, col))
|
||||
if nodes == nil or #nodes == 0 then
|
||||
u.deferred_notify(
|
||||
"Unable to find any nodes at pos. " .. tostring(row) .. ":" .. tostring(col),
|
||||
vim.log.levels.DEBUG
|
||||
)
|
||||
return nil
|
||||
end
|
||||
|
||||
return nodes
|
||||
end
|
||||
|
||||
return M
|
||||
Loading…
Add table
Add a link
Reference in a new issue