force format after import

pull/429/merge
ray-x 2 months ago
parent d217a74fa8
commit 737c44f5b4

@ -30,6 +30,22 @@ if vim.lsp.buf.format == nil then
require('go.lsp') -- this set default value of format require('go.lsp') -- this set default value of format
end 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) 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) log(fmtargs, bufnr, cmd)
@ -41,18 +57,7 @@ local run = function(fmtargs, bufnr, cmd)
vfn.bufload(bufnr) vfn.bufload(bufnr)
end end
-- log gopls format -- log gopls format
vim.lsp.buf.format({ return M.lsp_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
end end
local args = vim.deepcopy(fmtargs) local args = vim.deepcopy(fmtargs)
@ -108,7 +113,6 @@ local run = function(fmtargs, bufnr, cmd)
vfn.chanclose(j, 'stdin') vfn.chanclose(j, 'stdin')
end end
local M = {}
M.gofmt = function(...) M.gofmt = function(...)
local long_opts = { local long_opts = {
all = 'a', all = 'a',
@ -119,7 +123,7 @@ 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(optarg) log('formatting', optarg)
local all_buf = false local all_buf = false
if optarg['a'] then if optarg['a'] then
@ -156,18 +160,7 @@ end
M.org_imports = function() M.org_imports = function()
require('go.lsp').codeaction('', 'source.organizeImports', function() require('go.lsp').codeaction('', 'source.organizeImports', function()
vim.lsp.buf.format({ M.gofmt()
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) end)
end end

@ -41,7 +41,7 @@ local function check_for_error(msg)
if msg ~= nil and type(msg[1]) == 'table' then if msg ~= nil and type(msg[1]) == 'table' then
for k, v in pairs(msg[1]) do for k, v in pairs(msg[1]) do
if k == 'error' then if k == 'error' then
log('LSP', v.message) log('LSP error:', v.message)
break break
end end
end end
@ -50,7 +50,7 @@ end
for _, value in ipairs(gopls_cmds) do for _, value in ipairs(gopls_cmds) do
local fname = string.sub(value, #'gopls.' + 1) 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 b = vim.api.nvim_get_current_buf()
local uri = vim.uri_from_bufnr(b) local uri = vim.uri_from_bufnr(b)
local arguments = { { URI = uri } } local arguments = { { URI = uri } }
@ -74,12 +74,16 @@ for _, value in ipairs(gopls_cmds) do
end end
vim.schedule(function() vim.schedule(function()
-- it likely to be a edit command
local resp = vim.lsp.buf.execute_command({ local resp = vim.lsp.buf.execute_command({
command = value, command = value,
arguments = arguments, arguments = arguments,
}) })
check_for_error(resp) check_for_error(resp)
log(resp) log(resp)
if callback then
callback(resp)
end
end) end)
end end
end end
@ -87,7 +91,7 @@ M.cmds = cmds
M.import = function(path) M.import = function(path)
cmds.add_import({ cmds.add_import({
ImportPath = path, ImportPath = path,
}) }, require('go.format').gofmt)
end end
M.list_imports = function(path) M.list_imports = function(path)

Loading…
Cancel
Save