gomod file reload

pull/234/head
ray-x 2 years ago
parent 825f20b036
commit fabe62defc

@ -0,0 +1,13 @@
" don't spam the user when Vim is started in Vi compatibility mode
let s:cpo_save = &cpo
set cpo&vim
setlocal formatoptions-=t
setlocal comments=://
setlocal commentstring=//\ %s
" restore Vi compatibility settings
let &cpo = s:cpo_save
unlet s:cpo_save
" vim: sw=2 ts=2 et

@ -1,5 +1,6 @@
local runner = require("go.runner")
local utils = require("go.utils")
local vfn = vim.fn
local M = {}
function M.run(args)
@ -26,11 +27,16 @@ function M.run(args)
table.insert(cmd, "./...")
end
end
local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vfn.getcwd()
local modfile = workfolder .. utils.sep() .. "go.mod"
local opts = {
update_buffer = true,
on_exit = function()
vim.schedule(function()
utils.restart()
-- utils.restart()
require('go.lsp').watchFileChanged(modfile)
end)
end,
}

@ -1,6 +1,7 @@
local vim, api = vim, vim.api
local utils = require('go.utils')
local log = utils.log
local trace = utils.trace
local diagnostic_map = function(bufnr)
local opts = { noremap = true, silent = true }
api.nvim_buf_set_keymap(bufnr, 'n', ']O', ':lua vim.lsp.diagnostic.set_loclist()<CR>', opts)
@ -371,4 +372,28 @@ function M.hover_returns()
end)
end
local change_type = {
Created = 1,
Changed = 2,
Deleted = 3,
}
function M.watchFileChanged(fname, params)
params = params or vim.lsp.util.make_workspace_params()
fname = fname or vim.api.nvim_buf_get_name(0)
-- \ 'method': 'workspace/didChangeWatchedFiles',
params.changes = params.changes or {{uri = params.uri or vim.uri_from_fname(fname), type = params.type or change_type.Changed}}
vim.lsp.buf_request(vim.api.nvim_get_current_buf(), 'workspace/didChangeWatchedFiles', params, function(err, result, ctx)
vim.defer_fn(function()
-- log(err, result, ctx)
if err then
-- the request was send to all clients and some may not support
log('failed to workspace reloaded:' .. vim.inspect(err) .. vim.inspect(ctx).. vim.inspect(result))
else
vim.notify('workspace reloaded')
end
end, 200)
end)
end
return M

@ -18,4 +18,19 @@ function M.run(...)
runner.run(cmd, opts)
end
function M.setup()
local aug = vim.api.nvim_create_augroup('gomod_save', {})
local pat = { '*.mod' }
vim.api.nvim_create_autocmd({ 'BufWritePost' }, {
group = aug,
pattern = pat,
callback = function()
require('go.lsp').watchFileChanged()
end,
})
end
return M

@ -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()

Loading…
Cancel
Save