2021-03-10 12:15:06 +00:00
|
|
|
-- some of commands extracted from gopher.vim
|
|
|
|
local go = {}
|
2022-06-30 16:44:39 +00:00
|
|
|
local vfn = vim.fn
|
2022-07-05 22:28:29 +00:00
|
|
|
|
2022-01-11 21:57:21 +00:00
|
|
|
-- Keep this in sync with README.md
|
|
|
|
-- Keep this in sync with doc/go.txt
|
2021-07-10 11:04:24 +00:00
|
|
|
_GO_NVIM_CFG = {
|
2022-07-25 07:44:43 +00:00
|
|
|
disable_defaults = false, -- either true when true disable all default settings
|
2023-01-12 04:25:47 +00:00
|
|
|
go = 'go', -- set to go1.18beta1 if necessary
|
|
|
|
goimport = 'gopls', -- if set to 'gopls' will use gopls format, also goimport
|
|
|
|
fillstruct = 'gopls',
|
|
|
|
gofmt = 'gofumpt', -- if set to gopls will use gopls format
|
2022-07-05 22:28:29 +00:00
|
|
|
max_line_len = 128,
|
2021-07-10 11:04:24 +00:00
|
|
|
tag_transform = false,
|
2023-01-12 04:25:47 +00:00
|
|
|
tag_options = 'json=omitempty',
|
2022-05-15 12:25:50 +00:00
|
|
|
|
2023-01-12 04:25:47 +00:00
|
|
|
gotests_template = '', -- sets gotests -template parameter (check gotests for details)
|
|
|
|
gotests_template_dir = '', -- sets gotests -template_dir parameter (check gotests for details)
|
2022-05-15 12:25:50 +00:00
|
|
|
|
2023-01-12 04:25:47 +00:00
|
|
|
comment_placeholder = ' ',
|
|
|
|
icons = { breakpoint = '🧘', currentpos = '🏃' }, -- set to false to disable icons setup
|
|
|
|
sign_priority = 7, -- set priority of signs used by go.nevim
|
2021-07-10 11:04:24 +00:00
|
|
|
verbose = false,
|
2023-01-12 04:25:47 +00:00
|
|
|
log_path = vfn.expand('$HOME') .. '/tmp/gonvim.log',
|
2021-12-21 23:51:20 +00:00
|
|
|
lsp_cfg = false, -- false: do nothing
|
2022-01-15 09:59:03 +00:00
|
|
|
-- true: apply non-default gopls setup defined in go/lsp.lua
|
|
|
|
-- if lsp_cfg is a table, merge table with with non-default gopls setup in go/lsp.lua, e.g.
|
2021-07-11 11:33:22 +00:00
|
|
|
lsp_gofumpt = false, -- true: set default gofmt in gopls format to gofumpt
|
2021-12-21 23:51:20 +00:00
|
|
|
lsp_on_attach = nil, -- nil: use on_attach function defined in go/lsp.lua for gopls,
|
2022-01-15 09:59:03 +00:00
|
|
|
-- when lsp_cfg is true
|
|
|
|
-- if lsp_on_attach is a function: use this function as on_attach function for gopls,
|
|
|
|
-- when lsp_cfg is true
|
2022-05-15 12:25:50 +00:00
|
|
|
lsp_on_client_start = nil, -- it is a function with same signature as on_attach, will be called at end of
|
|
|
|
-- on_attach and allows you override some setup
|
2022-01-17 04:29:38 +00:00
|
|
|
lsp_document_formatting = true,
|
|
|
|
-- set to true: use gopls to format
|
|
|
|
-- false if you want to use other formatter tool(e.g. efm, nulls)
|
|
|
|
|
2022-05-15 12:25:50 +00:00
|
|
|
null_ls_document_formatting_disable = false, -- true: disable null-ls formatting
|
2023-02-10 10:17:43 +00:00
|
|
|
-- if enable gopls to format the code and you also installed and enabled null-ls, you may
|
2022-05-15 12:25:50 +00:00
|
|
|
-- want to disable null-ls by setting this to true
|
|
|
|
-- it can be a nulls source name e.g. `golines` or a nulls query table
|
2022-04-20 11:24:07 +00:00
|
|
|
lsp_keymaps = true, -- true: use default keymaps defined in go/lsp.lua
|
2021-08-23 02:24:48 +00:00
|
|
|
lsp_codelens = true,
|
2022-01-11 21:57:21 +00:00
|
|
|
lsp_diag_hdlr = true, -- hook lsp diag handler
|
2022-09-27 22:28:05 +00:00
|
|
|
lsp_diag_underline = true,
|
2022-02-15 09:30:42 +00:00
|
|
|
-- virtual text setup
|
2023-01-12 04:25:47 +00:00
|
|
|
lsp_diag_virtual_text = { space = 0, prefix = '' },
|
2022-02-15 09:30:42 +00:00
|
|
|
lsp_diag_signs = true,
|
2022-07-19 23:27:59 +00:00
|
|
|
lsp_inlay_hints = {
|
|
|
|
enable = true,
|
|
|
|
|
|
|
|
-- Only show inlay hints for the current line
|
|
|
|
only_current_line = false,
|
|
|
|
|
2023-02-10 10:17:43 +00:00
|
|
|
-- Event which triggers a refresh of the inlay hints.
|
2022-07-19 23:27:59 +00:00
|
|
|
-- You can make this "CursorMoved" or "CursorMoved,CursorMovedI" but
|
|
|
|
-- not that this may cause higher CPU usage.
|
|
|
|
-- This option is only respected when only_current_line and
|
|
|
|
-- autoSetHints both are true.
|
2023-01-12 04:25:47 +00:00
|
|
|
only_current_line_autocmd = 'CursorHold',
|
2022-07-19 23:27:59 +00:00
|
|
|
|
|
|
|
-- whether to show variable name before type hints with the inlay hints or not
|
|
|
|
-- default: false
|
|
|
|
show_variable_name = true,
|
|
|
|
|
|
|
|
-- prefix for parameter hints
|
2023-01-12 04:25:47 +00:00
|
|
|
parameter_hints_prefix = ' ',
|
2022-07-19 23:27:59 +00:00
|
|
|
show_parameter_hints = true,
|
|
|
|
|
|
|
|
-- prefix for all the other hints (type, chaining)
|
|
|
|
-- default: "=>"
|
2023-01-12 04:25:47 +00:00
|
|
|
other_hints_prefix = '=> ',
|
2022-07-19 23:27:59 +00:00
|
|
|
|
2023-02-10 10:17:43 +00:00
|
|
|
-- whether to align to the length of the longest line in the file
|
2022-07-19 23:27:59 +00:00
|
|
|
max_len_align = false,
|
|
|
|
|
|
|
|
-- padding from the left if max_len_align is true
|
|
|
|
max_len_align_padding = 1,
|
|
|
|
|
|
|
|
-- whether to align to the extreme right or not
|
|
|
|
right_align = false,
|
|
|
|
|
|
|
|
-- padding from the right if right_align is true
|
|
|
|
right_align_padding = 6,
|
|
|
|
|
|
|
|
-- The color of the hints
|
2023-01-12 04:25:47 +00:00
|
|
|
highlight = 'Comment',
|
2022-07-19 23:27:59 +00:00
|
|
|
},
|
2022-02-15 09:30:42 +00:00
|
|
|
lsp_diag_update_in_insert = false,
|
2022-05-31 11:44:28 +00:00
|
|
|
lsp_fmt_async = false, -- async lsp.buf.format
|
2023-01-12 04:25:47 +00:00
|
|
|
go_boilplater_url = 'https://github.com/thockin/go-build-template.git',
|
2022-01-11 21:57:21 +00:00
|
|
|
gopls_cmd = nil, --- you can provide gopls path and cmd if it not in PATH, e.g. cmd = { "/home/ray/.local/nvim/data/lspinstall/go/gopls" }
|
2021-08-23 02:24:48 +00:00
|
|
|
gopls_remote_auto = true,
|
2023-01-12 04:25:47 +00:00
|
|
|
gocoverage_sign = '█',
|
2022-12-28 00:46:31 +00:00
|
|
|
gocoverage_skip_covered = false,
|
2023-01-12 04:25:47 +00:00
|
|
|
sign_covered_hl = 'String', --- highlight group for test covered sign
|
2023-02-10 10:17:43 +00:00
|
|
|
sign_partial_hl = 'WarningMsg', --- highlight group for test partially covered sign
|
2023-01-12 04:25:47 +00:00
|
|
|
sign_uncovered_hl = 'Error', -- highlight group for uncovered code
|
2022-01-15 09:59:03 +00:00
|
|
|
launch_json = nil, -- the launch.json file path, default to .vscode/launch.json
|
2022-06-30 16:44:39 +00:00
|
|
|
-- launch_json = vfn.getcwd() .. "/.vscode/launch.json",
|
2021-08-23 02:24:48 +00:00
|
|
|
dap_debug = true,
|
2023-01-12 11:48:51 +00:00
|
|
|
dap_debug_gui = {}, -- bool|table put your dap-ui setup here set to false to disable
|
2022-01-11 22:35:42 +00:00
|
|
|
dap_debug_keymap = true, -- true: use keymap for debugger defined in go/dap.lua
|
2022-01-15 09:59:03 +00:00
|
|
|
-- false: do not use keymap in go/dap.lua. you must define your own.
|
2023-01-12 11:48:51 +00:00
|
|
|
dap_debug_vt = { enabled_commands = true, all_frames = true }, -- bool|table put your dap-virtual-text setup here set to false to disable
|
2022-12-28 06:14:21 +00:00
|
|
|
dap_port = 38697, -- can be set to a number or -1 so go.nvim will pickup a random port
|
|
|
|
dap_timeout = 15, -- see dap option initialize_timeout_sec = 15,
|
|
|
|
dap_retries = 20, -- see dap option max_retries
|
2023-01-12 04:25:47 +00:00
|
|
|
build_tags = '', --- you can provide extra build tags for tests or debugger
|
2022-02-16 10:25:21 +00:00
|
|
|
textobjects = true, -- treesitter binding for text objects
|
2023-01-12 04:25:47 +00:00
|
|
|
test_runner = 'go', -- one of {`go`, `richgo`, `dlv`, `ginkgo`, `gotestsum`}
|
2022-12-05 15:08:45 +00:00
|
|
|
verbose_tests = false, -- set to add verbose flag to tests deprecated see '-v'
|
2021-12-06 10:32:23 +00:00
|
|
|
run_in_floaterm = false, -- set to true to run in float window.
|
2023-01-25 05:09:13 +00:00
|
|
|
floaterm = {
|
|
|
|
posititon = 'auto', -- one of {`top`, `bottom`, `left`, `right`, `center`, `auto`}
|
|
|
|
width = 0.45, -- width of float window if not auto
|
|
|
|
height = 0.98, -- height of float window if not auto
|
|
|
|
},
|
2022-07-30 13:05:27 +00:00
|
|
|
trouble = false, -- true: use trouble to open quickfix
|
2022-07-07 16:34:50 +00:00
|
|
|
test_efm = false, -- errorfomat for quickfix, default mix mode, set to true will be efm only
|
2022-05-15 12:25:50 +00:00
|
|
|
|
2022-07-11 13:32:06 +00:00
|
|
|
luasnip = false, -- enable included luasnip
|
2023-01-12 04:25:47 +00:00
|
|
|
username = '',
|
|
|
|
useremail = '',
|
2022-07-08 10:30:23 +00:00
|
|
|
disable_per_project_cfg = false, -- set to true to disable load script from .gonvim/init.lua
|
2021-07-10 11:04:24 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 02:50:43 +00:00
|
|
|
-- TODO: nvim_{add,del}_user_command https://github.com/neovim/neovim/pull/16752
|
|
|
|
|
2021-03-10 12:15:06 +00:00
|
|
|
function go.setup(cfg)
|
2021-06-22 00:07:46 +00:00
|
|
|
cfg = cfg or {}
|
2021-07-10 11:04:24 +00:00
|
|
|
if cfg.max_len then
|
2023-02-12 20:55:00 +00:00
|
|
|
vim.notify('go.nvim max_len renamed to max_line_len', vim.log.levels.WARN)
|
2021-07-08 16:16:22 +00:00
|
|
|
end
|
2022-07-25 07:44:43 +00:00
|
|
|
if cfg.disable_defaults then
|
|
|
|
for k, _ in pairs(_GO_NVIM_CFG) do
|
2023-01-12 04:25:47 +00:00
|
|
|
if type(cfg[k]) == 'boolean' then
|
2022-07-25 07:44:43 +00:00
|
|
|
cfg[k] = false
|
|
|
|
end
|
2023-01-12 04:25:47 +00:00
|
|
|
if type(_GO_NVIM_CFG[k]) == 'table' then
|
2022-07-25 07:44:43 +00:00
|
|
|
_GO_NVIM_CFG[k] = {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-01-12 04:25:47 +00:00
|
|
|
_GO_NVIM_CFG = vim.tbl_deep_extend('force', _GO_NVIM_CFG, cfg)
|
2023-02-11 03:39:28 +00:00
|
|
|
|
|
|
|
require('go.commands').add_cmds()
|
2023-02-11 01:54:19 +00:00
|
|
|
vim.defer_fn(function()
|
|
|
|
require('go.project').load_project()
|
|
|
|
require('go.utils').set_nulls()
|
|
|
|
end, 1)
|
2022-05-15 12:34:44 +00:00
|
|
|
|
2022-05-01 22:47:45 +00:00
|
|
|
if _GO_NVIM_CFG.run_in_floaterm then
|
2022-05-03 14:14:55 +00:00
|
|
|
vim.cmd([[command! -nargs=* GoTermClose lua require("go.term").close()]])
|
2022-05-01 22:47:45 +00:00
|
|
|
end
|
2023-02-11 00:44:44 +00:00
|
|
|
|
|
|
|
if _GO_NVIM_CFG.lsp_cfg then
|
|
|
|
require('go.lsp').setup()
|
|
|
|
if _GO_NVIM_CFG.lsp_diag_hdlr then
|
|
|
|
require('go.lsp_diag')
|
|
|
|
end
|
2023-02-11 01:36:38 +00:00
|
|
|
elseif not _GO_NVIM_CFG.lsp_cfg and _GO_NVIM_CFG.lsp_on_attach then
|
2023-02-12 20:55:00 +00:00
|
|
|
vim.notify('lsp_on_attach ignored, because lsp_cfg is false', vim.log.levels.WARN)
|
2023-02-11 00:44:44 +00:00
|
|
|
end
|
2022-05-01 22:47:45 +00:00
|
|
|
|
2023-02-11 01:54:19 +00:00
|
|
|
vim.defer_fn(function()
|
|
|
|
require('go.coverage').setup()
|
|
|
|
if _GO_NVIM_CFG.lsp_codelens then
|
|
|
|
require('go.codelens').setup()
|
|
|
|
end
|
|
|
|
|
|
|
|
if _GO_NVIM_CFG.textobjects then
|
|
|
|
require('go.ts.textobjects').setup()
|
|
|
|
end
|
2022-07-05 22:28:29 +00:00
|
|
|
|
2023-02-11 01:54:19 +00:00
|
|
|
require('go.env').setup()
|
|
|
|
end, 1)
|
2021-11-13 03:29:42 +00:00
|
|
|
|
2023-02-11 01:54:19 +00:00
|
|
|
vim.defer_fn(function()
|
|
|
|
if _GO_NVIM_CFG.luasnip then
|
|
|
|
local ls = require('go.utils').load_plugin('LuaSnip', 'luasnip')
|
|
|
|
if ls then
|
|
|
|
require('snips.go')
|
|
|
|
require('snips.all')
|
|
|
|
end
|
2023-02-11 01:36:38 +00:00
|
|
|
end
|
2023-02-11 01:54:19 +00:00
|
|
|
if _GO_NVIM_CFG.lsp_inlay_hints.enable then
|
|
|
|
require('go.inlay').setup()
|
|
|
|
end
|
|
|
|
end, 2)
|
|
|
|
|
2023-02-11 01:36:38 +00:00
|
|
|
go.doc_complete = require('go.godoc').doc_complete
|
|
|
|
go.package_complete = require('go.package').complete
|
|
|
|
go.dbg_complete = require('go.complete').dbg_complete
|
|
|
|
go.tools_complete = require('go.complete').tools_complete
|
|
|
|
go.impl_complete = require('go.complete').impl_complete
|
|
|
|
go.modify_tags_complete = require('go.complete').modify_tags_complete
|
|
|
|
go.add_tags_complete = require('go.complete').add_tags_complete
|
|
|
|
require('go.mod').setup()
|
2022-07-11 13:32:06 +00:00
|
|
|
end
|
2021-11-23 23:13:40 +00:00
|
|
|
|
|
|
|
go.set_test_runner = function(runner)
|
|
|
|
-- richgo, go test, richgo, dlv, ginkgo
|
2023-01-12 04:25:47 +00:00
|
|
|
local runners = { 'richgo', 'go', 'richgo', 'ginkgo' } -- dlv
|
2021-11-23 23:13:40 +00:00
|
|
|
for _, v in pairs(runners) do
|
|
|
|
if v == runner then
|
|
|
|
_GO_NVIM_CFG.test_runner = runner
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
2023-02-12 20:55:00 +00:00
|
|
|
vim.notify('runner not supported ' .. runner, vim.log.levels.ERROR)
|
2021-11-23 23:13:40 +00:00
|
|
|
end
|
2021-11-24 09:05:02 +00:00
|
|
|
|
2021-03-10 12:15:06 +00:00
|
|
|
return go
|