[mirror] Make your go dev experience better
github.com/olexsmir/gopher.nvim
neovim
golang
1---@toc_entry Generate comments
2---@tag gopher.nvim-comments
3---@usage Execute `:GoCmt` to generate a comment for the current function/method/struct/etc on this line.
4---@text This module provides a way to generate comments for Go code.
5
6local log = require "gopher._utils.log"
7
8local function generate(row, col)
9 local ts_utils = require "gopher._utils.ts"
10 local comment, ns = nil, nil
11
12 ns = ts_utils.get_package_node_at_pos(row, col, nil, false)
13 if ns ~= nil then
14 comment = "// Package " .. ns.name .. " provides " .. ns.name
15 return comment, ns
16 end
17
18 ns = ts_utils.get_struct_node_at_pos(row, col, nil, false)
19 if ns ~= nil then
20 comment = "// " .. ns.name .. " " .. ns.type .. " "
21 return comment, ns
22 end
23
24 ns = ts_utils.get_func_method_node_at_pos(row, col, nil, false)
25 if ns ~= nil then
26 comment = "// " .. ns.name .. " " .. ns.type .. " "
27 return comment, ns
28 end
29
30 ns = ts_utils.get_interface_node_at_pos(row, col, nil, false)
31 if ns ~= nil then
32 comment = "// " .. ns.name .. " " .. ns.type .. " "
33 return comment, ns
34 end
35
36 return "// ", {}
37end
38
39return function()
40 local row, col = unpack(vim.api.nvim_win_get_cursor(0))
41 local comment, ns = generate(row + 1, col + 1)
42
43 log.debug("generated comment: " .. comment)
44
45 vim.api.nvim_win_set_cursor(0, {
46 ns.dim.s.r,
47 ns.dim.s.c,
48 })
49
50 ---@diagnostic disable-next-line: param-type-mismatch
51 vim.fn.append(row - 1, comment)
52
53 vim.api.nvim_win_set_cursor(0, {
54 ns.dim.s.r,
55 #comment + 1,
56 })
57
58 vim.cmd [[startinsert!]]
59end