go.nvim/lua/go/godoc.lua

142 lines
3.7 KiB
Lua
Raw Normal View History

2021-09-18 11:14:02 +00:00
local utils = require("go.utils")
2021-11-13 03:29:42 +00:00
local log = utils.log
local gopls = require("go.gopls")
2021-11-13 03:29:42 +00:00
local help_items = {}
2022-06-01 11:29:13 +00:00
local vfn = vim.fn
2021-11-13 03:29:42 +00:00
local m = {}
2022-06-01 11:29:13 +00:00
function m.help_complete(_, _, _)
2021-11-13 03:29:42 +00:00
if #help_items < 1 then
2022-06-01 11:29:13 +00:00
local doc = vfn.systemlist("go help")
2021-11-13 03:29:42 +00:00
if vim.v.shell_error ~= 0 then
vim.notify(string.format("failed to run go help %d", vim.v.shell_error), vim.lsp.log_levels.ERROR)
2021-11-13 03:29:42 +00:00
return
end
for _, line in ipairs(doc) do
2022-06-01 11:29:13 +00:00
local m1 = string.match(line, "^%s+([%w-]+)")
if m1 ~= nil and m1 ~= "go" then
table.insert(help_items, m1)
2021-11-13 03:29:42 +00:00
end
end
table.sort(help_items)
end
return table.concat(help_items, "\n")
2021-11-13 03:29:42 +00:00
end
local function match_doc_flag(lead)
local doc_flags = { "-all", "-c", "-cmd", "-short", "-src", "-u" }
2021-11-13 03:29:42 +00:00
local items = {}
local p = string.format("^%s", lead)
2021-11-13 03:29:42 +00:00
for _, f in ipairs(doc_flags) do
local k = string.match(f, p)
log(k, f, p)
if k then
table.insert(items, f)
end
end
table.sort(items)
log(items)
return table.concat(items or {}, "\n")
2021-11-13 03:29:42 +00:00
end
local function match_partial_item_name(pkg, pattern)
local cmd = string.format("go doc %s", pkg)
2022-06-01 11:29:13 +00:00
local doc = vfn.systemlist(cmd)
2021-11-13 03:29:42 +00:00
if vim.v.shell_error ~= 0 then
utils.warn("go doc failed", vim.inspect(doc))
2021-11-13 03:29:42 +00:00
return
end
local items = {}
for _, _type in ipairs({ "var", "const", "func", "type" }) do
2021-11-13 03:29:42 +00:00
local patterns = {
string.format("^%%s*%s (%s%%w+)", _type, pattern),
string.format("^%%s*%s %%(.-%%) (%s%%w+)", _type, pattern),
2021-11-13 03:29:42 +00:00
}
log(patterns)
for _, line in ipairs(doc) do
local k
for _, pat in ipairs(patterns) do
k = string.match(line, pat)
if k then
log(k)
table.insert(items, k)
break
end
end
end
end
table.sort(items)
log(items)
return items
end
2022-06-01 11:29:13 +00:00
function m.doc_complete(_, cmdline, _)
local words = vim.split(cmdline, "%s+")
if string.match(words[#words], "^-") then
2021-11-13 03:29:42 +00:00
log(words)
return match_doc_flag(words[#words])
end
if #words > 2 and string.match(words[#words - 1], "^-") == nil then
2021-11-13 03:29:42 +00:00
local pkg = words[#words - 1]
local item = words[#words]
return table.concat(match_partial_item_name(pkg, item), "\n")
elseif #words > 1 and string.match(words[#words], "^[^-].+%..*") ~= nil then
local pkg, item, method = unpack(vim.split(words[#words], "%."))
2021-11-13 03:29:42 +00:00
if method then
pkg = string.format("%s.%s", pkg, item)
2021-11-13 03:29:42 +00:00
item = method
end
local comps = match_partial_item_name(pkg, item)
for i, comp in ipairs(comps or {}) do
comps[i] = string.format("%s.%s", pkg, comp)
2021-11-13 03:29:42 +00:00
end
return table.concat(comps or {}, "\n")
elseif #words >= 1 and not string.match(words[#words], "^-") then
2021-11-13 03:29:42 +00:00
local pkgs = gopls.list_pkgs()
if pkgs then
local match = {}
if #words > 1 and #words[#words] > 0 then
for _, value in ipairs(pkgs) do
if string.match(value, words[#words]) then
table.insert(match, value)
end
end
else
match = pkgs
end
2022-04-18 12:31:17 +00:00
log(match)
return table.concat(match or {}, "\n")
2021-11-13 03:29:42 +00:00
end
end
return ""
2021-11-13 03:29:42 +00:00
end
2021-09-18 11:14:02 +00:00
2022-06-01 11:29:13 +00:00
m.run = function(kind, func)
2021-11-13 03:29:42 +00:00
log(func)
2021-09-18 11:14:02 +00:00
if func == nil or next(func) == nil then
return vim.lsp.buf.hover()
end
-- vim.validate({ func = { func, "table" } })
2021-09-18 11:14:02 +00:00
local setup = { "go", kind, unpack(func) }
2021-09-18 11:14:02 +00:00
--
2022-06-01 11:29:13 +00:00
vfn.jobstart(setup, {
on_stdout = function(_, data, _)
2021-09-18 11:14:02 +00:00
data = utils.handle_job_data(data)
if not data then
return
end
2021-11-11 02:01:14 +00:00
local close_events = { "CursorMoved", "CursorMovedI", "BufHidden", "InsertCharPre" }
local config = { close_events = close_events, focusable = true, border = "single" }
vim.lsp.util.open_floating_preview(data, "go", config)
end,
2021-09-18 11:14:02 +00:00
})
end
2021-11-13 03:29:42 +00:00
return m