feat(comment): add support for: interface methods, struct fields, variables (#123)
* refactor(comment): dont use unnecessary function * chore: quick way to open vim in dev mode * feat(comment): add comment on on interface method * feat(comment): add comment on a struct field * feat(comment): add comment on a variable * docs: add note about the generate function * docs: gopher.TsResult * fix(utils): handle case when indentation is wrong
This commit is contained in:
parent
295e21e637
commit
1e7af1b212
20 changed files with 250 additions and 33 deletions
|
|
@ -3,7 +3,7 @@ local log = require "gopher._utils.log"
|
|||
local utils = {}
|
||||
|
||||
---@param msg string
|
||||
---@param lvl? number by default `vim.log.levels.INFO`
|
||||
---@param lvl? integer by default `vim.log.levels.INFO`
|
||||
function utils.notify(msg, lvl)
|
||||
lvl = lvl or vim.log.levels.INFO
|
||||
vim.notify(msg, lvl, {
|
||||
|
|
@ -38,4 +38,16 @@ function utils.trimend(s)
|
|||
return r
|
||||
end
|
||||
|
||||
-- Since indentation can be spaces or tabs, that's my hack around it
|
||||
---@param line string
|
||||
---@param indent integer
|
||||
---@return string
|
||||
function utils.indent(line, indent)
|
||||
local char = string.sub(line, 1, 1)
|
||||
if char ~= " " and char ~= "\t" then
|
||||
char = " "
|
||||
end
|
||||
return string.rep(char, indent)
|
||||
end
|
||||
|
||||
return utils
|
||||
|
|
|
|||
|
|
@ -11,9 +11,13 @@ local queries = {
|
|||
right: (expression_list (composite_literal
|
||||
type: (struct_type))))]
|
||||
]],
|
||||
struct_field = [[
|
||||
(field_declaration name: (field_identifier) @_name)
|
||||
]],
|
||||
func = [[
|
||||
[(function_declaration name: (identifier) @_name)
|
||||
(method_declaration name: (field_identifier) @_name)]
|
||||
(method_declaration name: (field_identifier) @_name)
|
||||
(method_elem name: (field_identifier) @_name)]
|
||||
]],
|
||||
package = [[
|
||||
(package_identifier) @_name
|
||||
|
|
@ -23,12 +27,17 @@ local queries = {
|
|||
name: (type_identifier) @_name
|
||||
type: (interface_type))
|
||||
]],
|
||||
var = [[
|
||||
[(var_declaration (var_spec name: (identifier) @_name))
|
||||
(short_var_declaration
|
||||
left: (expression_list (identifier) @_name @_var))]
|
||||
]],
|
||||
}
|
||||
|
||||
---@param parent_type string[]
|
||||
---@param node TSNode
|
||||
---@return TSNode?
|
||||
local function get_parrent_node(parent_type, node)
|
||||
local function get_parent_node(parent_type, node)
|
||||
---@type TSNode?
|
||||
local current = node
|
||||
while current do
|
||||
|
|
@ -64,10 +73,11 @@ local function get_captures(query, node, bufnr)
|
|||
end
|
||||
|
||||
---@class gopher.TsResult
|
||||
---@field name string
|
||||
---@field start integer
|
||||
---@field end_ integer
|
||||
---@field is_varstruct boolean
|
||||
---@field name string Name of the struct, function, etc
|
||||
---@field start integer Line number where the declaration starts
|
||||
---@field end_ integer Line number where the declaration ends
|
||||
---@field indent integer Number of spaces/tabs in the current cursor line
|
||||
---@field is_varstruct boolean Is struct declared as `var S struct{}` or `s := struct{}{}`
|
||||
|
||||
---@param bufnr integer
|
||||
---@param parent_type string[]
|
||||
|
|
@ -78,23 +88,22 @@ local function do_stuff(bufnr, parent_type, query)
|
|||
error "No treesitter parser found for go"
|
||||
end
|
||||
|
||||
local node = vim.treesitter.get_node {
|
||||
bufnr = bufnr,
|
||||
}
|
||||
local node = vim.treesitter.get_node { bufnr = bufnr }
|
||||
if not node then
|
||||
error "No nodes found under cursor"
|
||||
error "No nodes found under the cursor"
|
||||
end
|
||||
|
||||
local parent_node = get_parrent_node(parent_type, node)
|
||||
local parent_node = get_parent_node(parent_type, node)
|
||||
if not parent_node then
|
||||
error "No parent node found under cursor"
|
||||
error "No parent node found under the 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()
|
||||
local start_row, start_col, end_row, _ = parent_node:range()
|
||||
res["indent"] = start_col
|
||||
res["start"] = start_row + 1
|
||||
res["end_"] = end_row + 1
|
||||
|
||||
|
|
@ -104,11 +113,12 @@ 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
|
||||
--- because in cases like `type ( T struct{}, U struct{} )`
|
||||
---
|
||||
--- var_declaration is for cases like `var x struct{}`
|
||||
--- short_var_declaration is for cases like `x := struct{}{}`
|
||||
---
|
||||
--- it always chooses last struct type in the list
|
||||
return do_stuff(bufnr, {
|
||||
"type_spec",
|
||||
"type_declaration",
|
||||
|
|
@ -117,10 +127,19 @@ function ts.get_struct_under_cursor(bufnr)
|
|||
}, queries.struct)
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
function ts.get_struct_field_under_cursor(bufnr)
|
||||
return do_stuff(bufnr, { "field_declaration" }, queries.struct_field)
|
||||
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)
|
||||
return do_stuff(bufnr, {
|
||||
"method_elem",
|
||||
"function_declaration",
|
||||
"method_declaration",
|
||||
}, queries.func)
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
|
|
@ -133,4 +152,9 @@ function ts.get_interface_under_cursor(bufnr)
|
|||
return do_stuff(bufnr, { "type_declaration" }, queries.interface)
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
function ts.get_variable_under_cursor(bufnr)
|
||||
return do_stuff(bufnr, { "var_declaration", "short_var_declaration" }, queries.var)
|
||||
end
|
||||
|
||||
return ts
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue