better fallback to goimports

pull/450/head
ray-x 1 month ago
parent ae58327c9e
commit 32d1754fd4

@ -42,6 +42,12 @@ _GO_NVIM_CFG = {
-- it can be a nulls source name e.g. `golines` or a nulls query table
lsp_keymaps = true, -- true: use default keymaps defined in go/lsp.lua
lsp_codelens = true,
null_ls = {
golangci_lint = {
-- disable = {'errcheck', 'staticcheck'}, -- linters to disable empty by default
-- enable = {'govet', 'ineffassign','revive', 'gosimple'}, -- linters to enable; empty by default
},
},
diagnostic = { -- set diagnostic to false to disable diagnostic
hdlr = false, -- hook diagnostic handler and send error to quickfix
underline = true,

@ -147,7 +147,12 @@ end
M.goimports = function(...)
local goimports = _GO_NVIM_CFG.goimports or 'gopls'
require('go.install').install(goimports)
local args = { ... }
if args[1] == 'goimports' then
goimports = 'goimports' -- force goimports
table.remove(args, 1)
end
log('imports', args, goimports)
if goimports == 'gopls' then
if vfn.empty(args) == 1 then
@ -159,7 +164,6 @@ M.goimports = function(...)
end
end
local buf = vim.api.nvim_get_current_buf()
require('go.install').install(goimports)
-- specified the pkg name
if #args > 0 then -- dont use golines
return run(args, buf, 'goimports')

@ -303,7 +303,13 @@ M.codeaction = function(gopls_cmd, only, hdlr)
hdlr()
end
end
local function ca_hdlr(err, result)
local function fallback_imports()
if only == 'source.organizeImports' then
require('go.format').goimports('goimports')
end
end
local function ca_hdlr(err, result, hdl_ctx, config)
trace('codeaction', err, result, hdl_ctx, config)
if err then
return log('error', err)
end
@ -321,20 +327,24 @@ M.codeaction = function(gopls_cmd, only, hdlr)
end
if #actions == 0 then
log('no code actions available')
vim.notify('No code actions available', vim.log.levels.INFO)
vim.notify('No code actions available, fallback goimports', vim.log.levels.INFO)
-- fallback to gofmt/goimports
fallback_imports()
return hdlr()
end
local action = actions[1]
-- resolve
gopls.request('codeAction/resolve', action, function(_err, resolved_acrtion)
gopls.request('codeAction/resolve', action, function(_err, resolved_acrtion, ctx, config)
log('codeAction/resolve', _err, resolved_acrtion, ctx, config)
if _err then
log('error', _err)
if action.command then
apply_action(action)
else
log('resolved', resolved_acrtion)
vim.notify('No code actions available', vim.log.levels.INFO)
vim.notify('No code actions can be resolve fallback goimports', vim.log.levels.INFO)
fallback_imports()
hdlr()
end
else

@ -1,72 +0,0 @@
vim.cmd([[set runtimepath=$VIMRUNTIME]])
vim.cmd([[set packpath=/tmp/nvim/site]])
local package_root = '/tmp/nvim/site/pack'
local install_path = package_root .. '/packer/start/packer.nvim'
local plugin_folder = function()
local host = os.getenv('HOST_NAME')
if host and (host:find('Ray') or host:find('ray')) then
return [[~/github/]] -- vim.fn.expand("$HOME") .. '/github/'
else
return ''
end
end
local function load_plugins()
require('packer').startup({
function(use)
use({ 'wbthomason/packer.nvim' })
use({
'nvim-treesitter/nvim-treesitter',
config = function()
require('nvim-treesitter.configs').setup({
ensure_installed = { 'go' },
highlight = { enable = true },
})
end,
run = ':TSUpdate',
})
use({ 'neovim/nvim-lspconfig' })
use({
plugin_folder() .. 'ray-x/go.nvim',
requires = {
'mfussenegger/nvim-dap', -- Debug Adapter Protocol
'rcarriga/nvim-dap-ui',
'theHamsta/nvim-dap-virtual-text',
'ray-x/guihua.lua',
},
config = function()
require('go').setup({
verbose = true,
goimports = 'gopls',
lsp_cfg = {
handlers = {
['textDocument/hover'] = vim.lsp.with(vim.lsp.handlers.hover, { border = 'double' }),
['textDocument/signatureHelp'] = vim.lsp.with(vim.lsp.handlers.signature_help, { border = 'round' }),
},
}, -- false: do nothing
run_in_floaterm = true,
})
end,
})
end,
config = {
package_root = package_root,
compile_path = install_path .. '/plugin/packer_compiled.lua',
},
})
end
if vim.fn.isdirectory(install_path) == 0 then
vim.fn.system({
'git',
'clone',
'https://github.com/wbthomason/packer.nvim',
install_path,
})
load_plugins()
require('packer').sync()
else
load_plugins()
end
vim.cmd('colorscheme murphy')
Loading…
Cancel
Save