Add comment generator (#10)
* feat(_utils.ts): get package * feat(_utils.ts): get interface * feat(_utils.ts): update type annotations * feat(comments): first naive implementation * docs: add comment [skip ci]
This commit is contained in:
parent
40a2839eab
commit
0fcdceeb89
5 changed files with 97 additions and 2 deletions
|
|
@ -4,6 +4,8 @@ local M = {
|
|||
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)]],
|
||||
},
|
||||
|
|
@ -21,7 +23,7 @@ end
|
|||
|
||||
---@param row string
|
||||
---@param col string
|
||||
---@param bufnr string
|
||||
---@param bufnr string|nil
|
||||
---@return table|nil
|
||||
function M.get_struct_node_at_pos(row, col, bufnr)
|
||||
local query = M.querys.struct_block .. " " .. M.querys.em_struct_block
|
||||
|
|
@ -36,7 +38,7 @@ end
|
|||
|
||||
---@param row string
|
||||
---@param col string
|
||||
---@param bufnr string
|
||||
---@param bufnr string|nil
|
||||
---@return table|nil
|
||||
function M.get_func_method_node_at_pos(row, col, bufnr)
|
||||
local query = M.querys.func .. " " .. M.querys.method_name
|
||||
|
|
@ -49,4 +51,37 @@ function M.get_func_method_node_at_pos(row, col, bufnr)
|
|||
end
|
||||
end
|
||||
|
||||
---@param row string
|
||||
---@param col string
|
||||
---@param bufnr string|nil
|
||||
---@return table|nil
|
||||
function M.get_package_node_at_pos(row, col, bufnr)
|
||||
-- stylua: ignore
|
||||
if row > 10 then return end
|
||||
local query = M.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
|
||||
u.notify("package not found", "warn")
|
||||
return nil
|
||||
else
|
||||
return ns[#ns]
|
||||
end
|
||||
end
|
||||
|
||||
---@param row string
|
||||
---@param col string
|
||||
---@param bufnr string|nil
|
||||
---@return table|nil
|
||||
function M.get_interface_node_at_pos(row, col, bufnr)
|
||||
local query = M.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
|
||||
u.notify("interface not found", "warn")
|
||||
else
|
||||
return ns[#ns]
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
|
|||
50
lua/gopher/comment.lua
Normal file
50
lua/gopher/comment.lua
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
local ts_utils = require "gopher._utils.ts"
|
||||
|
||||
local function generate(row, col)
|
||||
local comment, ns = nil, nil
|
||||
|
||||
ns = ts_utils.get_package_node_at_pos(row, col)
|
||||
if ns ~= nil and ns ~= {} then
|
||||
comment = "// Package " .. ns.name .. " provides " .. ns.name
|
||||
return comment, ns
|
||||
end
|
||||
|
||||
ns = ts_utils.get_struct_node_at_pos(row, col)
|
||||
if ns ~= nil and ns ~= {} then
|
||||
comment = "// " .. ns.name .. " " .. ns.type .. " "
|
||||
return comment, ns
|
||||
end
|
||||
|
||||
ns = ts_utils.get_func_method_node_at_pos(row, col)
|
||||
if ns ~= nil and ns ~= {} then
|
||||
comment = "// " .. ns.name .. " " .. ns.type .. " "
|
||||
return comment, ns
|
||||
end
|
||||
|
||||
ns = ts_utils.get_interface_node_at_pos(row, col)
|
||||
if ns ~= nil and ns ~= {} then
|
||||
comment = "// " .. ns.name .. " " .. ns.type .. " "
|
||||
return comment, ns
|
||||
end
|
||||
|
||||
return "// ", {}
|
||||
end
|
||||
|
||||
return function()
|
||||
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
|
||||
local comment, ns = generate(row + 1, col + 1)
|
||||
|
||||
vim.api.nvim_win_set_cursor(0, {
|
||||
ns.dim.s.r,
|
||||
ns.dim.s.c,
|
||||
})
|
||||
|
||||
vim.fn.append(row - 1, comment)
|
||||
|
||||
vim.api.nvim_win_set_cursor(0, {
|
||||
ns.dim.s.r,
|
||||
#comment + 1,
|
||||
})
|
||||
|
||||
vim.cmd [[startinsert!]]
|
||||
end
|
||||
|
|
@ -9,6 +9,7 @@ gopher.mod = require "gopher.gomod"
|
|||
gopher.get = require "gopher.goget"
|
||||
gopher.impl = require "gopher.impl"
|
||||
gopher.generate = require "gopher.gogenerate"
|
||||
gopher.comment = require "gopher.comment"
|
||||
gopher.test_add = gotests.func_test
|
||||
gopher.test_exported = gotests.all_exported_tests
|
||||
gopher.tests_all = gotests.all_tests
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue