2022-11-03 01:53:45 +00:00
|
|
|
local golist = require('go.list').list
|
|
|
|
local util = require('go.utils')
|
2021-11-13 03:29:42 +00:00
|
|
|
local log = util.log
|
2022-06-01 11:29:13 +00:00
|
|
|
local vfn = vim.fn
|
2022-06-26 22:12:49 +00:00
|
|
|
local api = vim.api
|
|
|
|
|
|
|
|
local path_to_pkg = {}
|
2022-12-25 04:20:09 +00:00
|
|
|
local complete = function(sep)
|
|
|
|
sep = sep or "\n"
|
2022-06-18 05:43:07 +00:00
|
|
|
local ok, l = golist(false, { util.all_pkgs() })
|
|
|
|
if not ok then
|
2022-11-03 01:53:45 +00:00
|
|
|
log('Failed to find all packages for current module/project.')
|
2022-06-18 05:43:07 +00:00
|
|
|
end
|
|
|
|
local curpkgmatch = false
|
2022-11-03 01:53:45 +00:00
|
|
|
local curpkg = vfn.fnamemodify(vfn.expand('%'), ':h:.')
|
2022-06-18 05:43:07 +00:00
|
|
|
local pkgs = {}
|
|
|
|
for _, p in ipairs(l) do
|
2022-11-03 01:53:45 +00:00
|
|
|
local d = vfn.fnamemodify(p.Dir, ':.')
|
2022-06-18 05:43:07 +00:00
|
|
|
if curpkg ~= d then
|
|
|
|
if d ~= vfn.getcwd() then
|
|
|
|
table.insert(pkgs, util.relative_to_cwd(d))
|
2021-11-13 03:29:42 +00:00
|
|
|
end
|
2022-06-18 05:43:07 +00:00
|
|
|
else
|
|
|
|
curpkgmatch = true
|
2021-11-13 03:29:42 +00:00
|
|
|
end
|
|
|
|
end
|
2022-06-18 05:43:07 +00:00
|
|
|
table.sort(pkgs)
|
|
|
|
table.insert(pkgs, util.all_pkgs())
|
2022-11-03 01:53:45 +00:00
|
|
|
table.insert(pkgs, '.')
|
2022-06-18 05:43:07 +00:00
|
|
|
if curpkgmatch then
|
|
|
|
table.insert(pkgs, util.relative_to_cwd(curpkg))
|
|
|
|
end
|
2022-12-25 04:20:09 +00:00
|
|
|
return table.concat(pkgs, sep)
|
2022-06-18 05:43:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local all_pkgs = function()
|
|
|
|
local ok, l = golist(false, { util.all_pkgs() })
|
|
|
|
if not ok then
|
2022-11-03 01:53:45 +00:00
|
|
|
log('Failed to find all packages for current module/project.')
|
2022-06-18 05:43:07 +00:00
|
|
|
end
|
|
|
|
return l
|
|
|
|
end
|
2021-11-23 23:13:40 +00:00
|
|
|
|
2022-12-25 04:20:09 +00:00
|
|
|
-- short form of go list
|
|
|
|
local all_pkgs2 = function()
|
|
|
|
local l = require('go.list').list_pkgs()
|
|
|
|
if not l then
|
|
|
|
log('Failed to find all packages for current module/project.')
|
|
|
|
end
|
|
|
|
return l
|
|
|
|
end
|
|
|
|
|
2022-06-26 22:12:49 +00:00
|
|
|
local pkg_from_path = function(pkg, bufnr)
|
2022-11-03 01:53:45 +00:00
|
|
|
local cmd = { 'go', 'list' }
|
2022-06-26 22:12:49 +00:00
|
|
|
if pkg ~= nil then
|
|
|
|
table.insert(cmd, pkg)
|
|
|
|
end
|
|
|
|
log(cmd)
|
|
|
|
return util.exec_in_path(cmd, bufnr)
|
|
|
|
end
|
|
|
|
|
|
|
|
local show_float = function(result)
|
2022-11-03 01:53:45 +00:00
|
|
|
local textview = util.load_plugin('guihua.lua', 'guihua.textview')
|
2022-06-26 22:12:49 +00:00
|
|
|
if not textview then
|
2022-11-03 01:53:45 +00:00
|
|
|
util.log('Failed to load guihua.textview')
|
2022-06-26 22:12:49 +00:00
|
|
|
|
2022-11-03 01:53:45 +00:00
|
|
|
vim.fn.setloclist(0, {}, ' ', {
|
|
|
|
title = 'go package outline',
|
2022-06-26 22:12:49 +00:00
|
|
|
lines = result,
|
|
|
|
})
|
2022-11-03 01:53:45 +00:00
|
|
|
util.quickfix('lopen')
|
2022-06-26 22:12:49 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
local win = textview:new({
|
2022-11-03 01:53:45 +00:00
|
|
|
relative = 'cursor',
|
|
|
|
syntax = 'lua',
|
2022-06-26 22:12:49 +00:00
|
|
|
rect = { height = math.min(40, #result), pos_x = 0, pos_y = 10 },
|
|
|
|
data = result,
|
|
|
|
})
|
2022-11-03 01:53:45 +00:00
|
|
|
log('draw data', result)
|
|
|
|
vim.api.nvim_buf_set_option(win.buf, 'filetype', 'go')
|
2022-06-26 22:12:49 +00:00
|
|
|
return win:on_draw(result)
|
|
|
|
end
|
|
|
|
|
|
|
|
local defs
|
|
|
|
local render_outline = function(result)
|
|
|
|
if not result then
|
2022-11-03 01:53:45 +00:00
|
|
|
log('result nil', debug.traceback())
|
2022-06-26 22:12:49 +00:00
|
|
|
return
|
|
|
|
end
|
2022-11-03 01:53:45 +00:00
|
|
|
local fname = vim.fn.tempname() .. '._go' -- avoid lsp activation
|
2022-11-23 16:10:54 +00:00
|
|
|
log('tmp: ' .. fname)
|
2022-06-26 22:12:49 +00:00
|
|
|
local uri = vim.uri_from_fname(fname)
|
|
|
|
local bufnr = vim.uri_to_bufnr(uri)
|
|
|
|
vim.fn.writefile(result, fname)
|
|
|
|
vfn.bufload(bufnr)
|
2022-11-03 01:53:45 +00:00
|
|
|
defs = require('go.ts.utils').list_definitions_toc(bufnr)
|
2022-11-23 16:17:19 +00:00
|
|
|
if vfn.empty(defs) == 1 then
|
2022-11-23 16:10:54 +00:00
|
|
|
vim.notify('No definitions found in package.')
|
|
|
|
return
|
|
|
|
end
|
2022-06-26 22:12:49 +00:00
|
|
|
return bufnr, fname
|
|
|
|
end
|
|
|
|
|
|
|
|
local outline
|
|
|
|
local render
|
|
|
|
local show_panel = function(result, pkg, rerender)
|
|
|
|
local bufnr, fname = render_outline(result)
|
2022-11-23 02:24:52 +00:00
|
|
|
if rerender or not defs then
|
2022-06-26 22:12:49 +00:00
|
|
|
return true -- just re-gen the outline
|
|
|
|
end
|
|
|
|
|
2022-11-23 02:24:52 +00:00
|
|
|
log('defs 1', defs and defs[1])
|
2022-11-03 01:53:45 +00:00
|
|
|
local panel = util.load_plugin('guihua.lua', 'guihua.panel')
|
|
|
|
local pkg_name = pkg or 'pkg'
|
|
|
|
pkg_name = vfn.split(pkg_name, '/')
|
|
|
|
pkg_name = pkg_name[#pkg_name] or 'pkg'
|
|
|
|
log('create panel')
|
2022-06-26 22:12:49 +00:00
|
|
|
if panel then
|
|
|
|
local p = panel:new({
|
2022-11-03 01:53:45 +00:00
|
|
|
header = ' ' .. pkg_name .. ' ',
|
2022-06-26 22:12:49 +00:00
|
|
|
render = function(b)
|
2022-11-03 01:53:45 +00:00
|
|
|
log('render for ', bufnr, b)
|
2022-11-25 02:16:51 +00:00
|
|
|
-- log(debug.traceback())
|
2022-06-26 22:12:49 +00:00
|
|
|
-- outline("-r")
|
|
|
|
render()
|
|
|
|
return defs
|
|
|
|
end,
|
|
|
|
on_confirm = function(n)
|
2022-11-03 01:53:45 +00:00
|
|
|
log('on_confirm symbol ', n)
|
2022-06-26 22:12:49 +00:00
|
|
|
if not n or not n.symbol then
|
2022-11-03 01:53:45 +00:00
|
|
|
log('info missing: symbol ', n)
|
2022-06-26 22:12:49 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
-- need to change to main window first to enable gopls
|
|
|
|
local wins = api.nvim_list_wins()
|
|
|
|
local panel_win = api.nvim_get_current_win()
|
|
|
|
log(wins, panel_win)
|
|
|
|
local cur_win
|
|
|
|
for _, w in ipairs(wins) do
|
|
|
|
if w ~= panel_win then
|
|
|
|
api.nvim_set_current_win(w)
|
|
|
|
local cur = api.nvim_win_get_cursor(w)
|
|
|
|
api.nvim_win_set_cursor(w, cur)
|
|
|
|
cur_win = w
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-03 01:53:45 +00:00
|
|
|
vim.lsp.buf_request(0, 'workspace/symbol', { query = "'" .. n.symbol }, function(e, lsp_result, ctx)
|
2022-06-26 22:12:49 +00:00
|
|
|
local filtered = {}
|
2022-07-26 16:05:24 +00:00
|
|
|
for _, r in pairs(lsp_result) do
|
2022-06-26 22:12:49 +00:00
|
|
|
local container = r.containerName
|
|
|
|
if pkg == container and r.name == n.symbol then
|
|
|
|
table.insert(filtered, r)
|
|
|
|
end
|
|
|
|
end
|
2022-11-03 01:53:45 +00:00
|
|
|
log('filtered', filtered)
|
2022-06-26 22:12:49 +00:00
|
|
|
if #filtered == 0 then
|
2022-11-03 01:53:45 +00:00
|
|
|
log('nothing found fallback to result', pkg, n.symbol)
|
2022-07-26 16:05:24 +00:00
|
|
|
filtered = lsp_result
|
2022-06-26 22:12:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if vfn.empty(filtered) == 1 then
|
2022-07-26 16:05:24 +00:00
|
|
|
log(e, lsp_result, ctx)
|
2022-11-03 01:53:45 +00:00
|
|
|
vim.notify('no symbol found for ' .. vim.inspect(pkg))
|
2022-06-26 22:12:49 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
if #filtered == 1 then
|
|
|
|
-- jump to pos
|
|
|
|
local loc = filtered[1].location
|
|
|
|
local buf = vim.uri_to_bufnr(loc.uri)
|
|
|
|
vfn.bufload(buf)
|
|
|
|
api.nvim_set_current_win(cur_win)
|
|
|
|
api.nvim_set_current_buf(buf)
|
|
|
|
api.nvim_win_set_buf(cur_win, buf)
|
|
|
|
api.nvim_win_set_cursor(cur_win, { loc.range.start.line + 1, loc.range.start.character })
|
|
|
|
else
|
|
|
|
-- lets just call workspace/symbol handler
|
2022-11-03 01:53:45 +00:00
|
|
|
vim.lsp.handlers['workspace/symbol'](e, filtered, ctx)
|
2022-06-26 22:12:49 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
-- vim.lsp.buf.workspace_symbol("'" .. n.symbol)
|
|
|
|
return n.symbol
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
p:open(true)
|
|
|
|
else
|
2022-11-03 01:53:45 +00:00
|
|
|
vim.fn.setloclist(0, {}, ' ', {
|
|
|
|
title = 'go package outline',
|
2022-06-26 22:12:49 +00:00
|
|
|
lines = defs,
|
|
|
|
})
|
2022-11-03 01:53:45 +00:00
|
|
|
util.quickfix('lopen')
|
2022-06-26 22:12:49 +00:00
|
|
|
end
|
2022-11-03 01:53:45 +00:00
|
|
|
log('cleanup')
|
2022-06-26 22:12:49 +00:00
|
|
|
vim.api.nvim_buf_delete(bufnr, { unload = true })
|
|
|
|
os.remove(fname)
|
|
|
|
end
|
|
|
|
|
|
|
|
local pkg_info = {}
|
|
|
|
-- get package info
|
|
|
|
local function handle_data_out(_, data, ev)
|
|
|
|
data = util.handle_job_data(data)
|
|
|
|
if not data then
|
|
|
|
return
|
|
|
|
end
|
2022-08-07 13:18:26 +00:00
|
|
|
pkg_info = {}
|
2022-11-03 01:53:45 +00:00
|
|
|
local types = { 'CONSTANTS', 'FUNCTIONS', 'TYPES', 'VARIABLES' }
|
2022-06-26 22:12:49 +00:00
|
|
|
for i, val in ipairs(data) do
|
|
|
|
-- first strip the filename
|
|
|
|
if vim.tbl_contains(types, val) then
|
2022-11-03 01:53:45 +00:00
|
|
|
val = '//' .. val
|
2022-06-26 22:12:49 +00:00
|
|
|
end
|
|
|
|
|
2022-11-03 01:53:45 +00:00
|
|
|
local sp = string.match(val, '^(%s*)')
|
2022-06-26 22:12:49 +00:00
|
|
|
if sp and #sp == 4 then
|
2022-11-03 01:53:45 +00:00
|
|
|
val = '//' .. val
|
2022-06-26 22:12:49 +00:00
|
|
|
end
|
2022-11-03 01:53:45 +00:00
|
|
|
local f = string.match(val, '^func ')
|
2022-06-26 22:12:49 +00:00
|
|
|
if f then
|
|
|
|
-- incase the func def is mulilines
|
|
|
|
local next_line = data[i + 1]
|
|
|
|
if next_line then
|
2022-11-03 01:53:45 +00:00
|
|
|
local next_sp = string.match(next_line, '^(%s*)') -- one tab in front
|
2022-06-26 22:12:49 +00:00
|
|
|
if next_sp and #next_sp == 1 then -- tab size 1
|
2022-11-03 01:53:45 +00:00
|
|
|
next_line = next_line .. '{}'
|
2022-06-26 22:12:49 +00:00
|
|
|
data[i + 1] = next_line
|
|
|
|
else
|
2022-11-03 01:53:45 +00:00
|
|
|
val = val .. '{}'
|
2022-06-26 22:12:49 +00:00
|
|
|
end
|
|
|
|
else
|
2022-11-03 01:53:45 +00:00
|
|
|
val = val .. '{}'
|
2022-06-26 22:12:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
-- log(val)
|
|
|
|
table.insert(pkg_info, val)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local gen_pkg_info = function(cmd, pkg, arg, rerender)
|
2022-11-03 01:53:45 +00:00
|
|
|
log('gen_pkg_info', cmd, pkg, rerender)
|
2022-06-26 22:12:49 +00:00
|
|
|
vfn.jobstart(cmd, {
|
|
|
|
on_stdout = handle_data_out,
|
|
|
|
on_exit = function(e, data, _)
|
|
|
|
if data ~= 0 then
|
2022-11-23 16:10:54 +00:00
|
|
|
local info = string.format(
|
|
|
|
'no packege (%s) \n errcode %s \n cmd: %s \n code %s',
|
|
|
|
vim.inspect(pkg),
|
|
|
|
e,
|
|
|
|
vim.inspect(cmd),
|
|
|
|
tostring(data)
|
|
|
|
)
|
2022-11-23 02:34:51 +00:00
|
|
|
vim.notify(info)
|
2022-11-23 16:10:54 +00:00
|
|
|
log(cmd, info, data)
|
2022-06-26 22:12:49 +00:00
|
|
|
return
|
|
|
|
end
|
2022-11-03 01:53:45 +00:00
|
|
|
if arg == '-f' then
|
2022-06-26 22:12:49 +00:00
|
|
|
return show_float(pkg_info)
|
|
|
|
end
|
|
|
|
show_panel(pkg_info, pkg[1], rerender)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
outline = function(...)
|
2022-11-23 16:10:54 +00:00
|
|
|
-- log(debug.traceback())
|
2022-06-26 22:12:49 +00:00
|
|
|
local arg = select(1, ...)
|
2022-11-03 01:53:45 +00:00
|
|
|
local path = vim.fn.expand('%:p:h')
|
|
|
|
path = vfn.fnamemodify(path, ':p')
|
2022-06-26 22:12:49 +00:00
|
|
|
|
2022-11-03 01:53:45 +00:00
|
|
|
if arg == '-p' then
|
2022-11-23 22:54:52 +00:00
|
|
|
local pkg = select(2, ...)
|
|
|
|
if pkg ~= nil then
|
|
|
|
path = pkg
|
|
|
|
else
|
|
|
|
vim.notify('no package provided')
|
|
|
|
end
|
2022-06-26 22:12:49 +00:00
|
|
|
else
|
2022-11-03 01:53:45 +00:00
|
|
|
path = '.' .. util.sep() .. '...' -- how about window?
|
2022-06-26 22:12:49 +00:00
|
|
|
end
|
2022-11-23 16:10:54 +00:00
|
|
|
|
2022-06-26 22:12:49 +00:00
|
|
|
local re_render = false
|
2022-11-03 01:53:45 +00:00
|
|
|
if arg == '-r' then
|
2022-06-26 22:12:49 +00:00
|
|
|
re_render = true
|
|
|
|
end
|
|
|
|
local pkg = path_to_pkg[path]
|
2022-11-23 16:10:54 +00:00
|
|
|
log(path, pkg)
|
2022-06-26 22:12:49 +00:00
|
|
|
if not pkg then
|
|
|
|
pkg = pkg_from_path(path) -- return list of all packages only check first one
|
|
|
|
path_to_pkg[path] = pkg
|
|
|
|
end
|
2022-11-23 16:10:54 +00:00
|
|
|
if pkg and pkg[1] and pkg[1]:find('does not contain') then
|
|
|
|
util.log('no package found for ' .. vim.inspect(path))
|
|
|
|
pkg = { '' }
|
|
|
|
path_to_pkg[path] = pkg
|
|
|
|
end
|
2022-06-26 22:12:49 +00:00
|
|
|
if vfn.empty(pkg) == 1 then
|
2022-11-23 16:10:54 +00:00
|
|
|
vim.notify('no package found ' .. pkg .. ' in path' .. path)
|
2022-11-03 01:53:45 +00:00
|
|
|
util.log('No package found in current directory.')
|
2022-11-23 16:10:54 +00:00
|
|
|
local setup = { 'go', 'doc', '-all', '-u', '-cmd' }
|
|
|
|
gen_pkg_info(setup, pkg, arg, re_render)
|
|
|
|
return
|
2022-06-26 22:12:49 +00:00
|
|
|
end
|
2022-11-03 01:53:45 +00:00
|
|
|
local setup = { 'go', 'doc', '-all', '-u', '-cmd', pkg[1] }
|
2022-06-26 22:12:49 +00:00
|
|
|
gen_pkg_info(setup, pkg, arg, re_render)
|
|
|
|
end
|
|
|
|
|
|
|
|
render = function(bufnr)
|
2022-11-23 16:10:54 +00:00
|
|
|
util.log(debug.traceback())
|
2022-11-03 01:53:45 +00:00
|
|
|
local fpath = vfn.fnamemodify(vfn.bufname(bufnr or 0), ':p')
|
2022-06-26 22:12:49 +00:00
|
|
|
local pkg = path_to_pkg[fpath]
|
|
|
|
if not pkg then
|
2022-11-03 01:53:45 +00:00
|
|
|
pkg = pkg_from_path('.' .. util.sep() .. '...', bufnr) -- return list of all packages only check first one
|
2022-06-26 22:12:49 +00:00
|
|
|
path_to_pkg[fpath] = pkg
|
|
|
|
end
|
|
|
|
if vfn.empty(pkg) == 1 then
|
2022-11-03 01:53:45 +00:00
|
|
|
util.log('No package found in current directory.')
|
2022-06-26 22:12:49 +00:00
|
|
|
return nil
|
|
|
|
end
|
2022-11-03 01:53:45 +00:00
|
|
|
local cmd = { 'go', 'doc', '-all', '-u', '-cmd', pkg[1] }
|
|
|
|
log('gen_pkg_info', cmd, pkg)
|
2022-06-26 22:12:49 +00:00
|
|
|
vfn.jobstart(cmd, {
|
|
|
|
on_stdout = handle_data_out,
|
|
|
|
on_exit = function(e, data, _)
|
|
|
|
if data ~= 0 then
|
2022-11-03 01:53:45 +00:00
|
|
|
log('no packege info data ' .. e .. tostring(data))
|
2022-06-26 22:12:49 +00:00
|
|
|
return
|
|
|
|
end
|
2022-07-26 16:05:24 +00:00
|
|
|
local buf, fname = render_outline()
|
|
|
|
log(buf, fname)
|
2022-06-26 22:12:49 +00:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
return defs
|
2022-06-18 05:43:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
|
|
|
complete = complete,
|
|
|
|
all_pkgs = all_pkgs,
|
2022-12-25 04:20:09 +00:00
|
|
|
all_pkgs2 = all_pkgs2,
|
2022-06-18 05:43:07 +00:00
|
|
|
pkg_from_path = pkg_from_path,
|
2022-06-26 22:12:49 +00:00
|
|
|
outline = outline,
|
2021-11-13 03:29:42 +00:00
|
|
|
}
|
2022-06-26 22:12:49 +00:00
|
|
|
|
|
|
|
--[[
|
|
|
|
result of workspacesymbol
|
|
|
|
{ {
|
|
|
|
containerName = "github.com/vendor/packagename/internal/aws",
|
|
|
|
kind = 12,
|
|
|
|
location = {
|
|
|
|
range = {
|
|
|
|
end = {
|
|
|
|
character = 23,
|
|
|
|
line = 39
|
|
|
|
},
|
|
|
|
start = {
|
|
|
|
character = 5,
|
|
|
|
line = 39
|
|
|
|
}
|
|
|
|
},
|
|
|
|
uri = "file:///go_home/src/vendor/packagename/internal/aws/aws.go"
|
|
|
|
},
|
|
|
|
name = "S3EndpointResolver"
|
|
|
|
} }
|
|
|
|
]]
|