#26 add goimports -local support

pull/27/head
ray-x 3 years ago
parent 200dcd22e4
commit e17b25eee7

@ -31,7 +31,6 @@ if nothing shows up, you can add the following to your shell config file
```bash ```bash
export PATH=$PATH:$GOPATH/bin export PATH=$PATH:$GOPATH/bin
``` ```
add 'ray-x/go.nvim' to your package manager, the dependency is `treesitter` (and optionally, treesitter-objects) 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 related binaries will be installed the first time you use it
Add format in your vimrc. Add format in your vimrc.
@ -313,7 +312,7 @@ Configure from lua suggested, The default setup:
```lua ```lua
require('go').setup({ require('go').setup({
goimport='gofumports', -- goimport command goimport='gopls', -- goimport command, can be gopls[default], gofumports[deprecated] or goimport
gofmt = 'gofumpt', --gofmt cmd, gofmt = 'gofumpt', --gofmt cmd,
max_line_len = 120, -- max line length in goline format max_line_len = 120, -- max line length in goline format
tag_transform = false, -- tag_transfer check gomodifytags for details 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 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. ﳑ     comment_placeholder = '' , -- comment_placeholder your cool placeholder e.g. ﳑ    
verbose = false, -- output loginf in messages 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_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 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 -- 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_keymap = true, -- set keymaps for debugger
dap_debug_gui = true, -- set to true to enable dap gui, highly recommand 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 dap_debug_vt = true, -- set to true to enable dap virtual text
}) })
``` ```

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

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

@ -1,6 +1,6 @@
local vim, api = vim, vim.api 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 diagnostic_map = function(bufnr)
local opts = {noremap = true, silent = true} local opts = {noremap = true, silent = true}
api.nvim_buf_set_keymap(bufnr, "n", "]O", ":lua vim.lsp.diagnostic.set_loclist()<CR>", opts) api.nvim_buf_set_keymap(bufnr, "n", "]O", ":lua vim.lsp.diagnostic.set_loclist()<CR>", opts)
@ -78,11 +78,12 @@ local gopls = {
usePlaceholders = true, usePlaceholders = true,
completeUnimported = true, completeUnimported = true,
staticcheck = true, staticcheck = true,
matcher = "fuzzy", matcher = "Fuzzy",
-- experimentalDiagnosticsDelay = "500ms", -- experimentalDiagnosticsDelay = "500ms",
diagnosticsDelay = "500ms", diagnosticsDelay = "500ms",
experimentalWatchedFileDelay = "100ms", experimentalWatchedFileDelay = "100ms",
symbolMatcher = "fuzzy", symbolMatcher = "fuzzy",
['local'] = "",
gofumpt = false, -- true, -- turn on for new repos, gofmpt is good but also create code turmoils gofumpt = false, -- true, -- turn on for new repos, gofmpt is good but also create code turmoils
buildFlags = {"-tags", "integration"} buildFlags = {"-tags", "integration"}
-- buildFlags = {"-tags", "functional"} -- 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 = {} local M = {}
function M.setup() function M.setup()
@ -113,6 +130,10 @@ function M.setup()
table.insert(gopls.cmd, "-remote=auto") table.insert(gopls.cmd, "-remote=auto")
end 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) require'lspconfig'.gopls.setup(gopls)
end end

Loading…
Cancel
Save