#26 add goimports -local support

This commit is contained in:
ray-x 2021-09-01 11:22:05 +10:00
parent 200dcd22e4
commit e17b25eee7
4 changed files with 44 additions and 15 deletions

View File

@ -31,7 +31,6 @@ if nothing shows up, you can add the following to your shell config file
```bash
export PATH=$PATH:$GOPATH/bin
```
add 'ray-x/go.nvim' to your package manager, the dependency is `treesitter` (and optionally, treesitter-objects)
related binaries will be installed the first time you use it
Add format in your vimrc.
@ -313,7 +312,7 @@ Configure from lua suggested, The default setup:
```lua
require('go').setup({
goimport='gofumports', -- goimport command
goimport='gopls', -- goimport command, can be gopls[default], gofumports[deprecated] or goimport
gofmt = 'gofumpt', --gofmt cmd,
max_line_len = 120, -- max line length in goline format
tag_transform = false, -- tag_transfer check gomodifytags for details
@ -321,7 +320,8 @@ require('go').setup({
test_template_dir = '', -- default to nil if not set; g:go_nvim_tests_template_dir check gotests for details
comment_placeholder = '' , -- comment_placeholder your cool placeholder e.g. ﳑ    
verbose = false, -- output loginf in messages
lsp_cfg = false, -- true: apply go.nvim non-default gopls setup
lsp_cfg = false, -- true: apply go.nvim non-default gopls setup, if it is a list, will merge with gopls setup e.g.
-- lsp_cfg = {settings={gopls={matcher='CaseInsensitive', ['local'] = 'your_local_module_path', gofumpt = true }}}
lsp_gofumpt = false, -- true: set default gofmt in gopls format to gofumpt
lsp_on_attach = true, -- if a on_attach function provided: attach on_attach function to gopls
-- true: will use go.nvim on_attach if true
@ -335,7 +335,6 @@ require('go').setup({
dap_debug_keymap = true, -- set keymaps for debugger
dap_debug_gui = true, -- set to true to enable dap gui, highly recommand
dap_debug_vt = true, -- set to true to enable dap virtual text
})
```

View File

@ -51,7 +51,7 @@ function go.setup(cfg)
vim.cmd([[command! GoMake silent lua require'go.asyncmake'.make()]])
vim.cmd([[command! Gofmt lua require("go.format").gofmt()]])
vim.cmd([[command! Goimport lua require("go.format").goimport()]])
vim.cmd([[command! -nargs=* Goimport lua require("go.format").goimport(<f-args>)]])
vim.cmd([[command! GoGenerate :setl makeprg=go\ generate | :GoMake]])
vim.cmd(
[[command! -nargs=* GoBuild :setl makeprg=go\ build | lua require'go.asyncmake'.make(<f-args>)]])

View File

@ -12,7 +12,7 @@ local gofmt_args = _GO_NVIM_CFG.gofmt_args and _GO_NVIM_CFG.gofmt_args
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)
local run = function(args, from_buffer, cmd)
if not from_buffer then
table.insert(args, api.nvim_buf_get_name(0))
@ -20,7 +20,12 @@ local run = function(args, from_buffer)
end
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
log(args)
local j = vim.fn.jobstart(args, {
on_stdout = function(job_id, data, event)
@ -98,17 +103,21 @@ M.org_imports = function(wait_ms)
vim.lsp.buf.formatting()
end
M.goimport = function(buf)
M.goimport = function(...)
if _GO_NVIM_CFG.goimport == 'gopls' then
M.org_imports(1000)
return
end
buf = buf or false
local args = {...}
require("go.install").install(goimport)
require("go.install").install("golines")
local a = {}
if args and _GO_NVIM_CFG.goimport == 'goimports' then
run(args, true, 'goimports')
else
utils.copy_array(goimport_args, a)
run(a, buf)
run({}, true)
end
end
return M

View File

@ -1,6 +1,6 @@
local vim, api = vim, vim.api
local lsp = require("vim.lsp")
local utils = require('go.utils')
local log = utils.log
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)
@ -78,11 +78,12 @@ local gopls = {
usePlaceholders = true,
completeUnimported = true,
staticcheck = true,
matcher = "fuzzy",
matcher = "Fuzzy",
-- experimentalDiagnosticsDelay = "500ms",
diagnosticsDelay = "500ms",
experimentalWatchedFileDelay = "100ms",
symbolMatcher = "fuzzy",
['local'] = "",
gofumpt = false, -- true, -- turn on for new repos, gofmpt is good but also create code turmoils
buildFlags = {"-tags", "integration"}
-- buildFlags = {"-tags", "functional"}
@ -90,6 +91,22 @@ local gopls = {
}
}
local extend_config = function(opts)
opts = opts or {}
if next(opts) == nil then
return
end
for key, value in pairs(opts) do
if type(gopls[key]) == "table" then
for k, v in pairs(value) do
gopls[key][k] = v
end
else
gopls[key] = value
end
end
end
local M = {}
function M.setup()
@ -113,6 +130,10 @@ function M.setup()
table.insert(gopls.cmd, "-remote=auto")
end
if type(_GO_NVIM_CFG.lsp_cfg) == "table" then
extend_config(_GO_NVIM_CFG.lsp_cfg)
end
log(gopls)
require'lspconfig'.gopls.setup(gopls)
end