go.nvim/lua/go/godoc.lua

143 lines
3.6 KiB
Lua
Raw Normal View History

2023-02-10 21:02:03 +00:00
local utils = require('go.utils')
2021-11-13 03:29:42 +00:00
local log = utils.log
2023-02-10 21:02:03 +00:00
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
2023-02-10 21:02:03 +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.log.levels.ERROR)
2021-11-13 03:29:42 +00:00
return
end
for _, line in ipairs(doc) do
2023-02-10 21:02:03 +00:00
local m1 = string.match(line, '^%s+([%w-]+)')
if m1 ~= nil and m1 ~= 'go' then
2022-06-01 11:29:13 +00:00
table.insert(help_items, m1)
2021-11-13 03:29:42 +00:00
end
end
table.sort(help_items)
end
return help_items
2021-11-13 03:29:42 +00:00
end
local function match_doc_flag(lead)
2023-02-10 21:02:03 +00:00
local doc_flags = { '-all', '-c', '-cmd', '-short', '-src', '-u' }
2021-11-13 03:29:42 +00:00
local items = {}
2023-02-10 21:02:03 +00:00
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 items or {}
2021-11-13 03:29:42 +00:00
end
local function match_partial_item_name(pkg, pattern)
2023-02-10 21:02:03 +00:00
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
2023-02-10 21:02:03 +00:00
utils.warn('go doc failed', vim.inspect(doc))
2021-11-13 03:29:42 +00:00
return
end
local items = {}
2023-02-10 21:02:03 +00:00
for _, _type in ipairs({ 'var', 'const', 'func', 'type' }) do
2021-11-13 03:29:42 +00:00
local patterns = {
2023-02-10 21:02:03 +00:00
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, _)
2023-02-10 21:02:03 +00:00
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
2023-02-10 21:02:03 +00:00
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 match_partial_item_name(pkg, item)
2023-02-10 21:02:03 +00:00
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
2023-02-10 21:02:03 +00:00
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
2023-02-10 21:02:03 +00:00
comps[i] = string.format('%s.%s', pkg, comp)
2021-11-13 03:29:42 +00:00
end
return comps or {}
2023-02-10 21:02:03 +00:00
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 match or {}
2021-11-13 03:29:42 +00:00
end
end
2023-02-10 21:02:03 +00:00
return ''
2021-11-13 03:29:42 +00:00
end
2021-09-18 11:14:02 +00:00
m.run = function(fargs)
log(fargs)
2021-09-18 11:14:02 +00:00
if vim.fn.empty(fargs) == 1 then
return vim.lsp.buf.hover()
end
2021-09-18 11:14:02 +00:00
local setup = { 'go', 'doc', unpack(fargs or {}) }
2021-09-18 11:14:02 +00:00
--
2023-02-10 21:02:03 +00:00
return vfn.jobstart(setup, {
2022-06-01 11:29:13 +00:00
on_stdout = function(_, data, _)
2023-02-10 21:02:03 +00:00
log(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
2023-02-10 21:02:03 +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,
2023-02-10 21:02:03 +00:00
}),
setup
2021-09-18 11:14:02 +00:00
end
2021-11-13 03:29:42 +00:00
return m