|
|
|
@ -1,5 +1,5 @@
|
|
|
|
|
local golist = require("go.list").list
|
|
|
|
|
local util = require("go.utils")
|
|
|
|
|
local golist = require('go.list').list
|
|
|
|
|
local util = require('go.utils')
|
|
|
|
|
local log = util.log
|
|
|
|
|
local vfn = vim.fn
|
|
|
|
|
local api = vim.api
|
|
|
|
@ -8,13 +8,13 @@ local path_to_pkg = {}
|
|
|
|
|
local complete = function()
|
|
|
|
|
local ok, l = golist(false, { util.all_pkgs() })
|
|
|
|
|
if not ok then
|
|
|
|
|
log("Failed to find all packages for current module/project.")
|
|
|
|
|
log('Failed to find all packages for current module/project.')
|
|
|
|
|
end
|
|
|
|
|
local curpkgmatch = false
|
|
|
|
|
local curpkg = vfn.fnamemodify(vfn.expand("%"), ":h:.")
|
|
|
|
|
local curpkg = vfn.fnamemodify(vfn.expand('%'), ':h:.')
|
|
|
|
|
local pkgs = {}
|
|
|
|
|
for _, p in ipairs(l) do
|
|
|
|
|
local d = vfn.fnamemodify(p.Dir, ":.")
|
|
|
|
|
local d = vfn.fnamemodify(p.Dir, ':.')
|
|
|
|
|
if curpkg ~= d then
|
|
|
|
|
if d ~= vfn.getcwd() then
|
|
|
|
|
table.insert(pkgs, util.relative_to_cwd(d))
|
|
|
|
@ -25,23 +25,23 @@ local complete = function()
|
|
|
|
|
end
|
|
|
|
|
table.sort(pkgs)
|
|
|
|
|
table.insert(pkgs, util.all_pkgs())
|
|
|
|
|
table.insert(pkgs, ".")
|
|
|
|
|
table.insert(pkgs, '.')
|
|
|
|
|
if curpkgmatch then
|
|
|
|
|
table.insert(pkgs, util.relative_to_cwd(curpkg))
|
|
|
|
|
end
|
|
|
|
|
return table.concat(pkgs, "\n")
|
|
|
|
|
return table.concat(pkgs, '\n')
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local all_pkgs = function()
|
|
|
|
|
local ok, l = golist(false, { util.all_pkgs() })
|
|
|
|
|
if not ok then
|
|
|
|
|
log("Failed to find all packages for current module/project.")
|
|
|
|
|
log('Failed to find all packages for current module/project.')
|
|
|
|
|
end
|
|
|
|
|
return l
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local pkg_from_path = function(pkg, bufnr)
|
|
|
|
|
local cmd = { "go", "list" }
|
|
|
|
|
local cmd = { 'go', 'list' }
|
|
|
|
|
if pkg ~= nil then
|
|
|
|
|
table.insert(cmd, pkg)
|
|
|
|
|
end
|
|
|
|
@ -50,41 +50,41 @@ local pkg_from_path = function(pkg, bufnr)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local show_float = function(result)
|
|
|
|
|
local textview = util.load_plugin("guihua.lua", "guihua.textview")
|
|
|
|
|
local textview = util.load_plugin('guihua.lua', 'guihua.textview')
|
|
|
|
|
if not textview then
|
|
|
|
|
util.log("Failed to load guihua.textview")
|
|
|
|
|
util.log('Failed to load guihua.textview')
|
|
|
|
|
|
|
|
|
|
vim.fn.setloclist(0, {}, " ", {
|
|
|
|
|
title = "go package outline",
|
|
|
|
|
vim.fn.setloclist(0, {}, ' ', {
|
|
|
|
|
title = 'go package outline',
|
|
|
|
|
lines = result,
|
|
|
|
|
})
|
|
|
|
|
util.quickfix("lopen")
|
|
|
|
|
util.quickfix('lopen')
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
local win = textview:new({
|
|
|
|
|
relative = "cursor",
|
|
|
|
|
syntax = "lua",
|
|
|
|
|
relative = 'cursor',
|
|
|
|
|
syntax = 'lua',
|
|
|
|
|
rect = { height = math.min(40, #result), pos_x = 0, pos_y = 10 },
|
|
|
|
|
data = result,
|
|
|
|
|
})
|
|
|
|
|
log("draw data", result)
|
|
|
|
|
vim.api.nvim_buf_set_option(win.buf, "filetype", "go")
|
|
|
|
|
log('draw data', result)
|
|
|
|
|
vim.api.nvim_buf_set_option(win.buf, 'filetype', 'go')
|
|
|
|
|
return win:on_draw(result)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local defs
|
|
|
|
|
local render_outline = function(result)
|
|
|
|
|
if not result then
|
|
|
|
|
log("result nil", debug.traceback())
|
|
|
|
|
log('result nil', debug.traceback())
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
local fname = vim.fn.tempname() .. "._go" -- avoid lsp activation
|
|
|
|
|
log("tmp:" .. fname)
|
|
|
|
|
local fname = vim.fn.tempname() .. '._go' -- avoid lsp activation
|
|
|
|
|
log('tmp:' .. fname)
|
|
|
|
|
local uri = vim.uri_from_fname(fname)
|
|
|
|
|
local bufnr = vim.uri_to_bufnr(uri)
|
|
|
|
|
vim.fn.writefile(result, fname)
|
|
|
|
|
vfn.bufload(bufnr)
|
|
|
|
|
defs = require("go.ts.utils").list_definitions_toc(bufnr)
|
|
|
|
|
defs = require('go.ts.utils').list_definitions_toc(bufnr)
|
|
|
|
|
return bufnr, fname
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
@ -96,26 +96,26 @@ local show_panel = function(result, pkg, rerender)
|
|
|
|
|
return true -- just re-gen the outline
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
log("defs 1", defs[1])
|
|
|
|
|
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")
|
|
|
|
|
log('defs 1', defs[1])
|
|
|
|
|
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')
|
|
|
|
|
if panel then
|
|
|
|
|
local p = panel:new({
|
|
|
|
|
header = " " .. pkg_name .. " ",
|
|
|
|
|
header = ' ' .. pkg_name .. ' ',
|
|
|
|
|
render = function(b)
|
|
|
|
|
log("render for ", bufnr, b)
|
|
|
|
|
log('render for ', bufnr, b)
|
|
|
|
|
log(debug.traceback())
|
|
|
|
|
-- outline("-r")
|
|
|
|
|
render()
|
|
|
|
|
return defs
|
|
|
|
|
end,
|
|
|
|
|
on_confirm = function(n)
|
|
|
|
|
log("on_confirm symbol ", n)
|
|
|
|
|
log('on_confirm symbol ', n)
|
|
|
|
|
if not n or not n.symbol then
|
|
|
|
|
log("info missing: symbol ", n)
|
|
|
|
|
log('info missing: symbol ', n)
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
-- need to change to main window first to enable gopls
|
|
|
|
@ -133,7 +133,7 @@ local show_panel = function(result, pkg, rerender)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
vim.lsp.buf_request(0, "workspace/symbol", { query = "'" .. n.symbol }, function(e, lsp_result, ctx)
|
|
|
|
|
vim.lsp.buf_request(0, 'workspace/symbol', { query = "'" .. n.symbol }, function(e, lsp_result, ctx)
|
|
|
|
|
local filtered = {}
|
|
|
|
|
for _, r in pairs(lsp_result) do
|
|
|
|
|
local container = r.containerName
|
|
|
|
@ -141,15 +141,15 @@ local show_panel = function(result, pkg, rerender)
|
|
|
|
|
table.insert(filtered, r)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
log("filtered", filtered)
|
|
|
|
|
log('filtered', filtered)
|
|
|
|
|
if #filtered == 0 then
|
|
|
|
|
log("nothing found fallback to result", pkg, n.symbol)
|
|
|
|
|
log('nothing found fallback to result', pkg, n.symbol)
|
|
|
|
|
filtered = lsp_result
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if vfn.empty(filtered) == 1 then
|
|
|
|
|
log(e, lsp_result, ctx)
|
|
|
|
|
vim.notify("no symbol found for " .. vim.inspect(pkg))
|
|
|
|
|
vim.notify('no symbol found for ' .. vim.inspect(pkg))
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
if #filtered == 1 then
|
|
|
|
@ -163,7 +163,7 @@ local show_panel = function(result, pkg, rerender)
|
|
|
|
|
api.nvim_win_set_cursor(cur_win, { loc.range.start.line + 1, loc.range.start.character })
|
|
|
|
|
else
|
|
|
|
|
-- lets just call workspace/symbol handler
|
|
|
|
|
vim.lsp.handlers["workspace/symbol"](e, filtered, ctx)
|
|
|
|
|
vim.lsp.handlers['workspace/symbol'](e, filtered, ctx)
|
|
|
|
|
end
|
|
|
|
|
end)
|
|
|
|
|
-- vim.lsp.buf.workspace_symbol("'" .. n.symbol)
|
|
|
|
@ -172,13 +172,13 @@ local show_panel = function(result, pkg, rerender)
|
|
|
|
|
})
|
|
|
|
|
p:open(true)
|
|
|
|
|
else
|
|
|
|
|
vim.fn.setloclist(0, {}, " ", {
|
|
|
|
|
title = "go package outline",
|
|
|
|
|
vim.fn.setloclist(0, {}, ' ', {
|
|
|
|
|
title = 'go package outline',
|
|
|
|
|
lines = defs,
|
|
|
|
|
})
|
|
|
|
|
util.quickfix("lopen")
|
|
|
|
|
util.quickfix('lopen')
|
|
|
|
|
end
|
|
|
|
|
log("cleanup")
|
|
|
|
|
log('cleanup')
|
|
|
|
|
vim.api.nvim_buf_delete(bufnr, { unload = true })
|
|
|
|
|
os.remove(fname)
|
|
|
|
|
end
|
|
|
|
@ -191,31 +191,31 @@ local function handle_data_out(_, data, ev)
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
pkg_info = {}
|
|
|
|
|
local types = { "CONSTANTS", "FUNCTIONS", "TYPES", "VARIABLES" }
|
|
|
|
|
local types = { 'CONSTANTS', 'FUNCTIONS', 'TYPES', 'VARIABLES' }
|
|
|
|
|
for i, val in ipairs(data) do
|
|
|
|
|
-- first strip the filename
|
|
|
|
|
if vim.tbl_contains(types, val) then
|
|
|
|
|
val = "//" .. val
|
|
|
|
|
val = '//' .. val
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local sp = string.match(val, "^(%s*)")
|
|
|
|
|
local sp = string.match(val, '^(%s*)')
|
|
|
|
|
if sp and #sp == 4 then
|
|
|
|
|
val = "//" .. val
|
|
|
|
|
val = '//' .. val
|
|
|
|
|
end
|
|
|
|
|
local f = string.match(val, "^func ")
|
|
|
|
|
local f = string.match(val, '^func ')
|
|
|
|
|
if f then
|
|
|
|
|
-- incase the func def is mulilines
|
|
|
|
|
local next_line = data[i + 1]
|
|
|
|
|
if next_line then
|
|
|
|
|
local next_sp = string.match(next_line, "^(%s*)") -- one tab in front
|
|
|
|
|
local next_sp = string.match(next_line, '^(%s*)') -- one tab in front
|
|
|
|
|
if next_sp and #next_sp == 1 then -- tab size 1
|
|
|
|
|
next_line = next_line .. "{}"
|
|
|
|
|
next_line = next_line .. '{}'
|
|
|
|
|
data[i + 1] = next_line
|
|
|
|
|
else
|
|
|
|
|
val = val .. "{}"
|
|
|
|
|
val = val .. '{}'
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
val = val .. "{}"
|
|
|
|
|
val = val .. '{}'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
-- log(val)
|
|
|
|
@ -224,15 +224,15 @@ local function handle_data_out(_, data, ev)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local gen_pkg_info = function(cmd, pkg, arg, rerender)
|
|
|
|
|
log("gen_pkg_info", cmd, pkg, rerender)
|
|
|
|
|
log('gen_pkg_info', cmd, pkg, rerender)
|
|
|
|
|
vfn.jobstart(cmd, {
|
|
|
|
|
on_stdout = handle_data_out,
|
|
|
|
|
on_exit = function(e, data, _)
|
|
|
|
|
if data ~= 0 then
|
|
|
|
|
log("no packege info data " .. e .. tostring(data))
|
|
|
|
|
log('no packege info data ' .. e .. tostring(data))
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
if arg == "-f" then
|
|
|
|
|
if arg == '-f' then
|
|
|
|
|
return show_float(pkg_info)
|
|
|
|
|
end
|
|
|
|
|
show_panel(pkg_info, pkg[1], rerender)
|
|
|
|
@ -243,16 +243,16 @@ end
|
|
|
|
|
outline = function(...)
|
|
|
|
|
log(debug.traceback())
|
|
|
|
|
local arg = select(1, ...)
|
|
|
|
|
local path = vim.fn.expand("%:p:h")
|
|
|
|
|
path = vfn.fnamemodify(path, ":p")
|
|
|
|
|
local path = vim.fn.expand('%:p:h')
|
|
|
|
|
path = vfn.fnamemodify(path, ':p')
|
|
|
|
|
|
|
|
|
|
if arg == "-p" then
|
|
|
|
|
if arg == '-p' then
|
|
|
|
|
path = select(2, ...)
|
|
|
|
|
else
|
|
|
|
|
path = "." .. util.sep() .. "..." -- how about window?
|
|
|
|
|
path = '.' .. util.sep() .. '...' -- how about window?
|
|
|
|
|
end
|
|
|
|
|
local re_render = false
|
|
|
|
|
if arg == "-r" then
|
|
|
|
|
if arg == '-r' then
|
|
|
|
|
re_render = true
|
|
|
|
|
end
|
|
|
|
|
local pkg = path_to_pkg[path]
|
|
|
|
@ -261,32 +261,32 @@ outline = function(...)
|
|
|
|
|
path_to_pkg[path] = pkg
|
|
|
|
|
end
|
|
|
|
|
if vfn.empty(pkg) == 1 then
|
|
|
|
|
util.log("No package found in current directory.")
|
|
|
|
|
util.log('No package found in current directory.')
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
local setup = { "go", "doc", "-all", "-u", "-cmd", pkg[1] }
|
|
|
|
|
local setup = { 'go', 'doc', '-all', '-u', '-cmd', pkg[1] }
|
|
|
|
|
gen_pkg_info(setup, pkg, arg, re_render)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
render = function(bufnr)
|
|
|
|
|
log(debug.traceback())
|
|
|
|
|
local fpath = vfn.fnamemodify(vfn.bufname(bufnr or 0), ":p")
|
|
|
|
|
local fpath = vfn.fnamemodify(vfn.bufname(bufnr or 0), ':p')
|
|
|
|
|
local pkg = path_to_pkg[fpath]
|
|
|
|
|
if not pkg then
|
|
|
|
|
pkg = pkg_from_path("." .. util.sep() .. "...", bufnr) -- return list of all packages only check first one
|
|
|
|
|
pkg = pkg_from_path('.' .. util.sep() .. '...', bufnr) -- return list of all packages only check first one
|
|
|
|
|
path_to_pkg[fpath] = pkg
|
|
|
|
|
end
|
|
|
|
|
if vfn.empty(pkg) == 1 then
|
|
|
|
|
util.log("No package found in current directory.")
|
|
|
|
|
util.log('No package found in current directory.')
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
local cmd = { "go", "doc", "-all", "-u", "-cmd", pkg[1] }
|
|
|
|
|
log("gen_pkg_info", cmd, pkg)
|
|
|
|
|
local cmd = { 'go', 'doc', '-all', '-u', '-cmd', pkg[1] }
|
|
|
|
|
log('gen_pkg_info', cmd, pkg)
|
|
|
|
|
vfn.jobstart(cmd, {
|
|
|
|
|
on_stdout = handle_data_out,
|
|
|
|
|
on_exit = function(e, data, _)
|
|
|
|
|
if data ~= 0 then
|
|
|
|
|
log("no packege info data " .. e .. tostring(data))
|
|
|
|
|
log('no packege info data ' .. e .. tostring(data))
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
local buf, fname = render_outline()
|
|
|
|
|