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:
Smirnov Oleksandr 2022-06-30 12:00:13 +03:00 committed by GitHub
parent 40a2839eab
commit 0fcdceeb89
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 97 additions and 2 deletions

50
lua/gopher/comment.lua Normal file
View 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