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
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue