diff --git a/lua/go/format.lua b/lua/go/format.lua index a84b438..c622ac6 100644 --- a/lua/go/format.lua +++ b/lua/go/format.lua @@ -30,6 +30,22 @@ if vim.lsp.buf.format == nil then require('go.lsp') -- this set default value of format end +local M = {} +M.lsp_format = function() + vim.lsp.buf.format({ + async = _GO_NVIM_CFG.lsp_fmt_async, + bufnr = vim.api.nvim_get_current_buf(), + name = 'gopls', + }) + if not _GO_NVIM_CFG.lsp_fmt_async then + vim.defer_fn(function() + if vfn.getbufinfo('%')[1].changed == 1 then + vim.cmd('noautocmd write') + end + end, write_delay) + end +end + local run = function(fmtargs, bufnr, cmd) bufnr = bufnr or vim.api.nvim_get_current_buf() log(fmtargs, bufnr, cmd) @@ -41,18 +57,7 @@ local run = function(fmtargs, bufnr, cmd) vfn.bufload(bufnr) end -- log gopls format - vim.lsp.buf.format({ - async = _GO_NVIM_CFG.lsp_fmt_async, - bufnr = bufnr, - name = 'gopls', - }) - if not _GO_NVIM_CFG.lsp_fmt_async then - vim.defer_fn(function() - if vfn.getbufinfo('%')[1].changed == 1 then - vim.cmd('noautocmd write') - end - end, write_delay) - end + return M.lsp_format() end local args = vim.deepcopy(fmtargs) @@ -108,7 +113,6 @@ local run = function(fmtargs, bufnr, cmd) vfn.chanclose(j, 'stdin') end -local M = {} M.gofmt = function(...) local long_opts = { all = 'a', @@ -119,7 +123,7 @@ M.gofmt = function(...) local getopt = require('go.alt_getopt') local optarg = getopt.get_opts(args, short_opts, long_opts) - log(optarg) + log('formatting', optarg) local all_buf = false if optarg['a'] then @@ -156,18 +160,7 @@ end M.org_imports = function() require('go.lsp').codeaction('', 'source.organizeImports', function() - vim.lsp.buf.format({ - async = _GO_NVIM_CFG.lsp_fmt_async, - bufnr = vim.api.nvim_get_current_buf(), - name = 'gopls', - }) - if not _GO_NVIM_CFG.lsp_fmt_async then - vim.defer_fn(function() - if vfn.getbufinfo('%')[1].changed == 1 then - vim.cmd('noautocmd write') - end - end, write_delay) - end + M.gofmt() end) end diff --git a/lua/go/gopls.lua b/lua/go/gopls.lua index 2def9b3..58754e5 100644 --- a/lua/go/gopls.lua +++ b/lua/go/gopls.lua @@ -41,7 +41,7 @@ local function check_for_error(msg) if msg ~= nil and type(msg[1]) == 'table' then for k, v in pairs(msg[1]) do if k == 'error' then - log('LSP', v.message) + log('LSP error:', v.message) break end end @@ -50,7 +50,7 @@ end for _, value in ipairs(gopls_cmds) do local fname = string.sub(value, #'gopls.' + 1) - cmds[fname] = function(arg) + cmds[fname] = function(arg, callback) local b = vim.api.nvim_get_current_buf() local uri = vim.uri_from_bufnr(b) local arguments = { { URI = uri } } @@ -74,12 +74,16 @@ for _, value in ipairs(gopls_cmds) do end vim.schedule(function() + -- it likely to be a edit command local resp = vim.lsp.buf.execute_command({ command = value, arguments = arguments, }) check_for_error(resp) log(resp) + if callback then + callback(resp) + end end) end end @@ -87,7 +91,7 @@ M.cmds = cmds M.import = function(path) cmds.add_import({ ImportPath = path, - }) + }, require('go.format').gofmt) end M.list_imports = function(path)