go.nvim/lua/go/comment.lua

85 lines
2.4 KiB
Lua
Raw Normal View History

2021-03-10 12:15:06 +00:00
-- todo
-- for func name(args) rets {}
-- add cmts // name : rets
local comment = {}
2021-07-10 11:04:24 +00:00
local placeholder = _GO_NVIM_CFG.comment_placeholder or ""
local ulog = require("go.utils").log
local api = vim.api
local gen_comment = function()
2021-03-10 12:15:06 +00:00
local comments = nil
local ns = require("go.ts.go").get_package_node_at_pos()
2021-03-10 12:15:06 +00:00
if ns ~= nil and ns ~= {} then
-- ulog("parnode" .. vim.inspect(ns))
2021-03-10 12:15:06 +00:00
comments = "// Package " .. ns.name .. " provides " .. ns.name
return comments, ns
end
ns = require("go.ts.go").get_func_method_node_at_pos()
2021-03-10 12:15:06 +00:00
if ns ~= nil and ns ~= {} then
-- ulog("parnode" .. vim.inspect(ns))
2021-03-10 12:15:06 +00:00
comments = "// " .. ns.name .. " " .. ns.type
return comments, ns
end
ns = require("go.ts.go").get_struct_node_at_pos()
2021-03-10 12:15:06 +00:00
if ns ~= nil and ns ~= {} then
comments = "// " .. ns.name .. " " .. ns.type
return comments, ns
end
ns = require("go.ts.go").get_interface_node_at_pos()
2021-03-10 12:15:06 +00:00
if ns ~= nil and ns ~= {} then
-- ulog("parnode" .. vim.inspect(ns))
2021-03-10 12:15:06 +00:00
comments = "// " .. ns.name .. " " .. ns.type
return comments, ns
end
2022-07-06 04:30:13 +00:00
ns = require("go.ts.go").get_type_node_at_pos()
2022-07-06 04:30:13 +00:00
if ns ~= nil and ns ~= {} then
-- ulog("parnode" .. vim.inspect(ns))
2022-07-06 04:30:13 +00:00
comments = "// " .. ns.name .. " " .. ns.type
return comments, ns
end
2021-03-10 12:15:06 +00:00
return ""
end
local wrap_comment = function(comment_line, ns)
if string.len(comment_line) > 0 and placeholder ~= nil and string.len(placeholder) > 0 then
2021-03-10 12:15:06 +00:00
return comment_line .. " " .. placeholder, ns
end
return comment_line, ns
end
comment.gen = function()
local row, col = unpack(api.nvim_win_get_cursor(0))
row, col = row, col + 1
local c, ns = wrap_comment(gen_comment())
local bufnr = api.nvim_get_current_buf()
if ns == nil then
-- nothing found
local ts_utils = require("nvim-treesitter.ts_utils")
ns = ts_utils.get_node_at_cursor()
2023-04-04 11:55:20 +00:00
local node_text = require("go.utils").get_node_text(ns, bufnr)
local line = api.nvim_get_current_line()
local regex = "^(%s+)"
local q = line:match(regex)
c = (q or "") .. "// " .. node_text
c, _ = wrap_comment(c, {})
vim.fn.append(row - 1, c)
vim.fn.cursor(row, #c + 1)
return
2021-03-10 12:15:06 +00:00
end
ulog(vim.inspect(ns))
2021-03-10 12:15:06 +00:00
row, col = ns.dim.s.r, ns.dim.s.c
ulog("set cursor " .. tostring(row))
api.nvim_win_set_cursor(0, { row, col })
2021-03-10 12:15:06 +00:00
-- insert doc
vim.fn.append(row - 1, c)
-- set curosr
vim.fn.cursor(row, #c + 1)
2021-03-10 12:15:06 +00:00
-- enter into insert mode
api.nvim_command("startinsert!")
2021-03-10 12:15:06 +00:00
return c
end
return comment