Compare commits

...

2 Commits

@ -780,7 +780,7 @@ require('go').setup({
go='go', -- go command, can be go[default] or go1.18beta1
goimport='gopls', -- goimport command, can be gopls[default] or either goimport or golines if need to split long lines
gofmt = 'gofumpt', --gofmt cmd,
max_line_len = 128, -- max line length in golines format, Target maximum line length for golines
max_line_len = 0, -- max line length in golines format, Target maximum line length for golines
tag_transform = false, -- can be transform option("snakecase", "camelcase", etc) check gomodifytags for details and more options
tag_options = 'json=omitempty', -- sets options sent to gomodifytags, i.e., json=omitempty
gotests_template = "", -- sets gotests -template parameter (check gotests for details)
@ -909,7 +909,6 @@ return {
go = "go", -- set to go1.18beta1 if necessary
goimport = "gopls", -- if set to 'gopls' will use gopls format, also goimport
gofmt = "gofumpt", -- if set to gopls will use gopls format
max_line_len = 120
null_ls_document_formatting_disable = true
}
```
@ -1274,7 +1273,6 @@ lua <<EOF
require 'go'.setup({
goimport = 'gopls', -- if set to 'gopls' will use golsp format
gofmt = 'gopls', -- if set to gopls will use golsp format
max_line_len = 120,
tag_transform = false,
test_dir = '',
comment_placeholder = '  ',

@ -10,7 +10,7 @@ _GO_NVIM_CFG = {
goimport = 'gopls', -- if set to 'gopls' will use gopls format, also goimport
fillstruct = 'gopls',
gofmt = 'gofumpt', -- if set to gopls will use gopls format
max_line_len = 128,
max_line_len = 0,
tag_transform = false,
tag_options = 'json=omitempty',
@ -158,9 +158,6 @@ function go.setup(cfg)
vim.notify('go.nvim master branch requires nvim 0.9', vim.log.levels.WARN)
end
cfg = cfg or {}
if cfg.max_len then
vim.notify('go.nvim max_len renamed to max_line_len', vim.log.levels.WARN)
end
if cfg.lsp_diag_hdlr ~= nil then
vim.notify('go.nvim lsp_diag_hdlr deprecated, use diagnostic.hdlr', vim.log.levels.WARN)
end
@ -191,6 +188,10 @@ function go.setup(cfg)
end
_GO_NVIM_CFG = vim.tbl_deep_extend('force', _GO_NVIM_CFG, cfg)
if _GO_NVIM_CFG.max_line_len > 0 and _GO_NVIM_CFG.gofmt ~= 'golines' then
vim.notify('go.nvim max_line_len only effective when gofmt is golines', vim.log.levels.WARN)
end
require('go.commands').add_cmds()
vim.defer_fn(function()
require('go.project').load_project()

@ -1,9 +1,7 @@
-- golines A golang formatter that fixes long lines
-- golines + goimport
local api = vim.api
local utils = require('go.utils')
local log = utils.log
local max_len = _GO_NVIM_CFG.max_line_len or 120
local max_len = _GO_NVIM_CFG.max_line_len or 128
local gofmt = _GO_NVIM_CFG.gofmt or 'gofumpt'
local vfn = vim.fn
local write_delay = 10
@ -13,10 +11,11 @@ end
local install = require('go.install').install
local gofmt_args = _GO_NVIM_CFG.gofmt_args
or {
or gofmt == 'golines' and {
'--max-len=' .. tostring(max_len),
'--base-formatter=' .. gofmt,
'--base-formatter=gofumpt',
}
or {}
local goimport_args = _GO_NVIM_CFG.goimport_args
or {
@ -24,12 +23,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 +42,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 +59,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, {
@ -97,10 +87,13 @@ local run = function(fmtargs, bufnr, cmd)
on_exit = function(_, data, _) -- id, data, event
-- log(vim.inspect(data) .. "exit")
if data ~= 0 then
return vim.notify('golines failed ' .. tostring(data), vim.log.levels.ERROR)
return vim.notify(cmd .. ' failed ' .. tostring(data), vim.log.levels.ERROR)
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
@ -133,10 +126,6 @@ M.gofmt = function(...)
utils.warn('installing ' .. gofmt .. ' please retry after installation')
return
end
if not install('golines') then
utils.warn('installing golines , please rerun format after install finished')
return
end
local a = {}
utils.copy_array(gofmt_args, a)
local fmtcmd
@ -159,9 +148,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(...)

@ -82,7 +82,7 @@ local on_attach = function(client, bufnr)
table.insert(keymaps, {
key = '<space>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)

@ -5,7 +5,6 @@ return {
go = "go", -- set to go1.18beta1 if necessary
goimport = "gopls", -- if set to 'gopls' will use gopls format, also goimport
gofmt = "gofumpt", -- if set to gopls will use gopls format
max_line_len = 120,
tag_transform = false,
test_dir = "",
sign_priority = 5,

Loading…
Cancel
Save