Add options allows goimport and gofmt with lsp. Add options for go.nvim gopls config be loaded

pull/21/head
ray-x 3 years ago
parent 8bbabcbbbb
commit a1dc56973d

@ -1,14 +1,17 @@
-- some of commands extracted from gopher.vim
local go = {}
_GO_NVIM_CFG = {
goimport = 'gofumports',
gofmt = 'gofumpt',
goimport = 'gofumports', -- if set to 'lsp' will use golsp format
gofmt = 'gofumpt', -- if set to lsp will use golsp format
max_line_line = 120,
tag_transform = false,
test_dir = '',
comment_placeholder = '',
verbose = false,
log_path = vim.fn.expand("$HOME") .. "/tmp/gonvim.log",
lsp_enable = false, -- true: lsp in the plugin and apply non-default setup
lsp_gofumpt = false, -- true: set default gofmt in gopls format to gofumpt
lsp_on_attach = nil, -- provides a on_attach function to gopls
dap_debug = false,
dap_debug_gui = false,
dap_vt = true -- false, true and 'all frames'
@ -80,5 +83,9 @@ function go.setup(cfg)
vim.cmd(
[[command! DapRerun require'dap'.disconnect();require'dap'.stop();require'dap'.run_last()]])
end
if _GO_NVIM_CFG.lsp_enable then
require 'go.lsp'
end
end
return go

@ -5,15 +5,14 @@ local utils = require("go.utils")
local max_len = _GO_NVIM_CFG.max_len or 120
local goimport = _GO_NVIM_CFG.goimport ~= nil and _GO_NVIM_CFG.goimport or "gofumports"
local gofmt = _GO_NVIM_CFG.gofmt ~= nil and _GO_NVIM_CFG.gofmt or "gofumpt"
local gofmt_args =
_GO_NVIM_CFG.gofmt_args and _GO_NVIM_CFG.gofmt_args or
{"--max-len=" .. tostring(max_len), "--base-formatter=" .. gofmt}
local gofmt_args = _GO_NVIM_CFG.gofmt_args and _GO_NVIM_CFG.gofmt_args
or {"--max-len=" .. tostring(max_len), "--base-formatter=" .. gofmt}
local goimport_args =
_GO_NVIM_CFG.goimport_args and _GO_NVIM_CFG.goimport_args or
{"--max-len=" .. tostring(max_len), "--base-formatter=" .. goimport}
local goimport_args = _GO_NVIM_CFG.goimport_args and _GO_NVIM_CFG.goimport_args
or {"--max-len=" .. tostring(max_len), "--base-formatter=" .. goimport}
local run = function(args, from_buffer)
if not from_buffer then
table.insert(args, api.nvim_buf_get_name(0))
print('formatting... ' .. api.nvim_buf_get_name(0) .. vim.inspect(args))
@ -22,42 +21,44 @@ local run = function(args, from_buffer)
local old_lines = api.nvim_buf_get_lines(0, 0, -1, true)
table.insert(args, 1, "golines")
local j =
vim.fn.jobstart(
args,
{
on_stdout = function(job_id, data, event)
data = utils.handle_job_data(data)
if not data then return end
if not utils.check_same(old_lines, data) then
print("updating codes")
api.nvim_buf_set_lines(0, 0, -1, false, data)
api.nvim_command("write")
else
print("already formatted")
end
utils.log("stdout" .. vim.inspect(data))
old_lines = nil
local j = vim.fn.jobstart(args, {
on_stdout = function(job_id, data, event)
data = utils.handle_job_data(data)
if not data then
return
end
if not utils.check_same(old_lines, data) then
print("updating codes")
api.nvim_buf_set_lines(0, 0, -1, false, data)
api.nvim_command("write")
else
print("already formatted")
end
utils.log("stdout" .. vim.inspect(data))
old_lines = nil
end,
on_stderr = function(job_id, data, event)
utils.log(vim.inspect(data) .. "stderr")
end,
on_exit = function(id, data, event)
utils.log(vim.inspect(data) .. "exit")
-- utils.log("current data " .. vim.inspect(new_lines))
old_lines = nil
end,
stdout_buffered = true,
stderr_buffered = true,
}
)
end,
on_stderr = function(job_id, data, event)
utils.log(vim.inspect(data) .. "stderr")
end,
on_exit = function(id, data, event)
utils.log(vim.inspect(data) .. "exit")
-- utils.log("current data " .. vim.inspect(new_lines))
old_lines = nil
end,
stdout_buffered = true,
stderr_buffered = true
})
vim.fn.chansend(j, old_lines)
vim.fn.chanclose(j, "stdin")
end
local M = {}
M.gofmt = function(buf)
if _GO_NVIM_CFG.gofmt == 'lsp' then
vim.lsp.buf.formatting()
return
end
vim.env.GO_FMT = "gofumpt"
buf = buf or false
require("go.install").install(gofmt)
@ -67,7 +68,27 @@ M.gofmt = function(buf)
run(a, buf)
end
M.OrgImports = function(wait_ms)
local params = vim.lsp.util.make_range_params()
params.context = {only = {"source.organizeImports"}}
local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, wait_ms)
for _, res in pairs(result or {}) do
for _, r in pairs(res.result or {}) do
if r.edit then
vim.lsp.util.apply_workspace_edit(r.edit)
else
vim.lsp.buf.execute_command(r.command)
end
end
end
vim.lsp.buf.formatting()
end
M.goimport = function()
if _GO_NVIM_CFG.goimport == 'lsp' then
M.OrgImports(1000)
return
end
buf = buf or false
require("go.install").install(goimport)
require("go.install").install("golines")

@ -0,0 +1,93 @@
local vim, api = vim, vim.api
local lsp = require("vim.lsp")
local diagnostic_map = function(bufnr)
local opts = {noremap = true, silent = true}
api.nvim_buf_set_keymap(bufnr, "n", "]O", ":lua vim.lsp.diagnostic.set_loclist()<CR>", opts)
end
local on_attach = function(client, bufnr)
local function buf_set_keymap(...)
vim.api.nvim_buf_set_keymap(bufnr, ...)
end
local function buf_set_option(...)
vim.api.nvim_buf_set_option(bufnr, ...)
end
local uri = vim.uri_from_bufnr(bufnr)
if uri == "file://" or uri == "file:///" or #uri < 11 then
return {error = "invalid file", result = nil}
end
diagnostic_map(bufnr)
-- add highlight for Lspxxx
api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc")
local opts = {noremap = true, silent = true}
buf_set_keymap('n', 'gD', '<Cmd>lua vim.lsp.buf.declaration()<CR>', opts)
buf_set_keymap('n', 'gd', '<Cmd>lua vim.lsp.buf.definition()<CR>', opts)
buf_set_keymap('n', 'K', '<Cmd>lua vim.lsp.buf.hover()<CR>', opts)
buf_set_keymap('n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>', opts)
buf_set_keymap('n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts)
buf_set_keymap('n', '<space>wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>', opts)
buf_set_keymap('n', '<space>wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>', opts)
buf_set_keymap('n', '<space>wl',
'<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>', opts)
buf_set_keymap('n', '<space>D', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
buf_set_keymap('n', '<space>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
buf_set_keymap('n', '<space>ca', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts)
buf_set_keymap('n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>', opts)
buf_set_keymap('n', '<space>e', '<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>', opts)
buf_set_keymap('n', '[d', '<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>', opts)
buf_set_keymap('n', ']d', '<cmd>lua vim.lsp.diagnostic.goto_next()<CR>', opts)
buf_set_keymap('n', '<space>q', '<cmd>lua vim.lsp.diagnostic.set_loclist()<CR>', opts)
buf_set_keymap("n", "<space>f", "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
buf_set_keymap("n", "<space>ff", "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
if _GO_NVIM_CFG.lsp_on_attach then
_GO_NVIM_CFG.lsp_on_attach(client, bufnr)
end
end
local gopls = {
on_attach = on_attach,
-- capabilities = cap,
filetypes = {"go", "gomod"},
message_level = vim.lsp.protocol.MessageType.Error,
cmd = {
"gopls", -- share the gopls instance if there is one already
"-remote=auto", --[[ debug options ]] --
"-remote.debug=:0"
},
flags = {allow_incremental_sync = true, debounce_text_changes = 500},
settings = {
gopls = {
-- more settings: https://github.com/golang/tools/blob/master/gopls/doc/settings.md
-- flags = {allow_incremental_sync = true, debounce_text_changes = 500},
-- not supported
analyses = {unusedparams = true, unreachable = false},
codelenses = {
generate = true, -- show the `go generate` lens.
gc_details = true -- // Show a code lens toggling the display of gc's choices.
},
usePlaceholders = true,
completeUnimported = true,
staticcheck = true,
matcher = "fuzzy",
experimentalDiagnosticsDelay = "500ms",
-- diagnosticsDelay = "500ms",
-- experimentalWatchedFileDelay = "100ms",
symbolMatcher = "fuzzy",
gofumpt = false, -- true, -- turn on for new repos, gofmpt is good but also create code turmoils
buildFlags = {"-tags", "integration"}
-- buildFlags = {"-tags", "functional"}
}
}
}
if _GO_NVIM_CFG.lsp_gofumpt then
gopls.settings.gopls.gofumpt = true
end
require'lspconfig'.gopls.setup(gopls)
Loading…
Cancel
Save