cleanup after format save logic

pull/429/merge
ray-x 2 months ago
parent 29a967457a
commit dadedaeda2

@ -2,20 +2,9 @@ local api = vim.api
local utils = require('go.utils') local utils = require('go.utils')
local log = utils.log local log = utils.log
local max_len = _GO_NVIM_CFG.max_line_len or 128 local max_len = _GO_NVIM_CFG.max_line_len or 128
local gofmt = _GO_NVIM_CFG.gofmt or 'gofumpt'
local vfn = vim.fn local vfn = vim.fn
local write_delay = 10
if _GO_NVIM_CFG.lsp_fmt_async then
write_delay = 200
end
local install = require('go.install').install local install = require('go.install').install
local gofmt_args = _GO_NVIM_CFG.gofmt_args
or gofmt == 'golines' and {
'--max-len=' .. tostring(max_len),
'--base-formatter=gofumpt',
}
or {}
local goimport_args = _GO_NVIM_CFG.goimport_args local goimport_args = _GO_NVIM_CFG.goimport_args
or { or {
@ -24,28 +13,26 @@ local goimport_args = _GO_NVIM_CFG.goimport_args
} }
local M = {} local M = {}
M.lsp_format = function() M.lsp_format = function()
-- vim.lsp.buf.format({
vim.lsp.buf.format({ vim.lsp.buf.format({
async = _GO_NVIM_CFG.lsp_fmt_async, async = _GO_NVIM_CFG.lsp_fmt_async,
bufnr = vim.api.nvim_get_current_buf(), bufnr = vim.api.nvim_get_current_buf(),
name = 'gopls', name = 'gopls',
}) })
if not _GO_NVIM_CFG.lsp_fmt_async then if not _GO_NVIM_CFG.lsp_fmt_async then
vim.defer_fn(function() if vfn.getbufinfo('%')[1].changed == 1 then
if vfn.getbufinfo('%')[1].changed == 1 then vim.cmd('noautocmd write')
vim.cmd('noautocmd write') end
end
end, write_delay)
end end
-- otherwise use the format handler
end end
local run = function(fmtargs, bufnr, cmd) local run = function(fmtargs, bufnr, cmd)
bufnr = bufnr or vim.api.nvim_get_current_buf() bufnr = bufnr or vim.api.nvim_get_current_buf()
log(fmtargs, bufnr, cmd)
cmd = cmd or _GO_NVIM_CFG.gofmt or 'gofumpt' cmd = cmd or _GO_NVIM_CFG.gofmt or 'gofumpt'
if vim.o.mod == true then log(fmtargs, bufnr, cmd)
vim.cmd('noautocmd write')
end
if cmd == 'gopls' then if cmd == 'gopls' then
if not vim.api.nvim_buf_is_loaded(bufnr) then if not vim.api.nvim_buf_is_loaded(bufnr) then
vfn.bufload(bufnr) vfn.bufload(bufnr)
@ -54,6 +41,10 @@ local run = function(fmtargs, bufnr, cmd)
return M.lsp_format() return M.lsp_format()
end end
-- for none lsp format we need to check if the buffer is modified and save to disk first
if vim.o.mod == true then
vim.cmd('noautocmd write')
end
local args = vim.deepcopy(fmtargs) local args = vim.deepcopy(fmtargs)
table.insert(args, api.nvim_buf_get_name(bufnr)) table.insert(args, api.nvim_buf_get_name(bufnr))
log('formatting buffer... ' .. vim.inspect(args), vim.log.levels.DEBUG) log('formatting buffer... ' .. vim.inspect(args), vim.log.levels.DEBUG)
@ -107,6 +98,13 @@ local run = function(fmtargs, bufnr, cmd)
end end
M.gofmt = function(...) M.gofmt = function(...)
local gofmt = _GO_NVIM_CFG.gofmt or 'gopls'
local gofmt_args = _GO_NVIM_CFG.gofmt_args
or gofmt == 'golines' and {
'--max-len=' .. tostring(max_len),
'--base-formatter=gofumpt',
}
or {}
local long_opts = { local long_opts = {
all = 'a', all = 'a',
} }
@ -116,7 +114,6 @@ M.gofmt = function(...)
local getopt = require('go.alt_getopt') local getopt = require('go.alt_getopt')
local optarg = getopt.get_opts(args, short_opts, long_opts) local optarg = getopt.get_opts(args, short_opts, long_opts)
log('formatting', optarg)
local all_buf = false local all_buf = false
if optarg['a'] then if optarg['a'] then
@ -128,10 +125,7 @@ M.gofmt = function(...)
end end
local a = {} local a = {}
utils.copy_array(gofmt_args, a) utils.copy_array(gofmt_args, a)
local fmtcmd log('formatting', optarg, gofmt, gofmt_args)
if gofmt == 'gopls' then
fmtcmd = 'gopls'
end
if all_buf then if all_buf then
log('fmt all buffers') log('fmt all buffers')
vim.cmd('wall') vim.cmd('wall')
@ -140,10 +134,10 @@ M.gofmt = function(...)
for _, b in ipairs(bufs) do for _, b in ipairs(bufs) do
log(a, b) log(a, b)
run(a, b.bufnr, fmtcmd) run(a, b.bufnr, gofmt)
end end
else else
run(a, 0, fmtcmd) run(a, vim.api.nvim_get_current_buf(), gofmt)
end end
end end
@ -151,11 +145,10 @@ M.org_imports = function()
require('go.lsp').codeaction('', 'source.organizeImports', M.gofmt) require('go.lsp').codeaction('', 'source.organizeImports', M.gofmt)
end end
M.goimports = function(...) M.goimports = function(...)
local goimports = _GO_NVIM_CFG.goimports or 'gopls' local goimports = _GO_NVIM_CFG.goimports or 'gopls'
local args = { ... } local args = { ... }
log(args, goimports) log('imports', args, goimports)
if goimports == 'gopls' then if goimports == 'gopls' then
if vfn.empty(args) == 1 then if vfn.empty(args) == 1 then
return M.org_imports() return M.org_imports()

Loading…
Cancel
Save