diff --git a/lua/go/format.lua b/lua/go/format.lua index 618b7e6..14e322d 100644 --- a/lua/go/format.lua +++ b/lua/go/format.lua @@ -24,12 +24,6 @@ local goimport_args = _GO_NVIM_CFG.goimport_args '--base-formatter=goimports', } -if vim.lsp.buf.format == nil then - -- vim.notify('the vim.lsp.buf.format is not available, some feature is missing if you are running old version of neovim (<0.8.0)', vim.log.levels.DEBUG) - -- neovim < 0.7 only - require('go.lsp') -- this set default value of format -end - local M = {} M.lsp_format = function() vim.lsp.buf.format({ @@ -49,6 +43,7 @@ end local run = function(fmtargs, bufnr, cmd) bufnr = bufnr or vim.api.nvim_get_current_buf() log(fmtargs, bufnr, cmd) + cmd = cmd or _GO_NVIM_CFG.gofmt or 'gofumpt' if vim.o.mod == true then vim.cmd('noautocmd write') end @@ -65,11 +60,7 @@ local run = function(fmtargs, bufnr, cmd) log('formatting buffer... ' .. vim.inspect(args), vim.log.levels.DEBUG) local old_lines = api.nvim_buf_get_lines(0, 0, -1, true) - if cmd then - table.insert(args, 1, cmd) - else - table.insert(args, 1, 'golines') - end + table.insert(args, 1, cmd) log('fmt cmd:', args) local j = vfn.jobstart(args, { @@ -101,6 +92,9 @@ local run = function(fmtargs, bufnr, cmd) end old_lines = nil vim.defer_fn(function() + if cmd == 'goimport' then + return M.lsp_format() + end if vfn.getbufinfo('%')[1].changed == 1 then vim.cmd('noautocmd write') end @@ -159,9 +153,7 @@ M.gofmt = function(...) end M.org_imports = function() - require('go.lsp').codeaction('', 'source.organizeImports', function() - M.gofmt() - end) + require('go.lsp').codeaction('', 'source.organizeImports', M.gofmt) end M.goimport = function(...) diff --git a/lua/go/lsp.lua b/lua/go/lsp.lua index 8bdb663..5b227d5 100644 --- a/lua/go/lsp.lua +++ b/lua/go/lsp.lua @@ -82,7 +82,7 @@ local on_attach = function(client, bufnr) table.insert(keymaps, { key = 'ff', func = function() - vim.lsp.buf.format({ async = true }) + vim.lsp.buf.format({ async = _GO_NVIM_CFG.lsp_format_async }) end, desc = 'format', }) @@ -257,6 +257,7 @@ write", "source", "source.organizeImports" } -- action / fix to take -- only gopls M.codeaction = function(gopls_cmd, only, hdlr) + hdlr = hdlr or function () end local params = vim.lsp.util.make_range_params() if not gopls_cmd:find('gopls') then gopls_cmd = 'gopls.' .. gopls_cmd @@ -277,7 +278,7 @@ M.codeaction = function(gopls_cmd, only, hdlr) end if gopls == nil then log('gopls not found') - return + return hdlr() end local ctx = { bufnr = bufnr, client_id = gopls.id } @@ -294,6 +295,7 @@ M.codeaction = function(gopls_cmd, only, hdlr) if fn then local enriched_ctx = vim.deepcopy(ctx) fn(command, enriched_ctx) + hdlr() else gopls.request('workspace/executeCommand', { command = command.command, @@ -303,8 +305,12 @@ M.codeaction = function(gopls_cmd, only, hdlr) if _err then log('error', _err) end + log('workspace/executeCommand', command.command, r) + hdlr() end, bufnr) end + else + hdlr() end end local function ca_hdlr(err, result) @@ -314,7 +320,7 @@ M.codeaction = function(gopls_cmd, only, hdlr) log('gocodeaction', err, result) if not result or next(result) == nil then log('nil result for codeaction with parameters', gopls_cmd, only, bufnr, params) - return + return hdlr() end local actions = {} for _, res in pairs(result) do @@ -326,7 +332,7 @@ M.codeaction = function(gopls_cmd, only, hdlr) if #actions == 0 then log('no code actions available') vim.notify('No code actions available', vim.log.levels.INFO) - return + return hdlr() end local action = actions[1] @@ -339,6 +345,7 @@ M.codeaction = function(gopls_cmd, only, hdlr) else log('resolved', resolved_acrtion) vim.notify('No code actions available', vim.log.levels.INFO) + hdlr() end else apply_action(resolved_acrtion)