go.nvim/lua/go.lua

140 lines
5.6 KiB
Lua
Raw Normal View History

2021-03-10 12:15:06 +00:00
-- some of commands extracted from gopher.vim
local go = {}
local vfn = vim.fn
2022-07-05 22:28:29 +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-01-10 10:26:41 +00:00
go = "go", -- set to go1.18beta1 if necessary
2021-12-06 10:32:23 +00:00
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,
2022-05-15 12:25:50 +00:00
gotests_template = "", -- sets gotests -template parameter (check gotests for details)
gotests_template_dir = "", -- sets gotests -template_dir parameter (check gotests for details)
2021-12-06 10:32:23 +00:00
comment_placeholder = "",
icons = { breakpoint = "🧘", currentpos = "🏃" }, -- set to false to disable icons setup
2021-07-10 11:04:24 +00:00
verbose = false,
log_path = vfn.expand("$HOME") .. "/tmp/gonvim.log",
lsp_cfg = false, -- false: do nothing
-- 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.
lsp_gofumpt = false, -- true: set default gofmt in gopls format to gofumpt
lsp_on_attach = nil, -- nil: use on_attach function defined in go/lsp.lua for gopls,
-- 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
lsp_format_on_save = 1,
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
-- if enable gopls to format the code and you also instlled and enabled null-ls, you may
-- 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
lsp_codelens = true,
lsp_diag_hdlr = true, -- hook lsp diag handler
-- virtual text setup
lsp_diag_virtual_text = { space = 0, prefix = "" },
lsp_diag_signs = true,
lsp_diag_update_in_insert = false,
2022-05-31 11:44:28 +00:00
lsp_fmt_async = false, -- async lsp.buf.format
2022-05-15 12:25:50 +00:00
go_boilplater_url = "https://github.com/thockin/go-build-template.git",
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" }
gopls_remote_auto = true,
2021-12-06 10:32:23 +00:00
gocoverage_sign = "",
2021-08-25 15:42:17 +00:00
gocoverage_sign_priority = 5,
launch_json = nil, -- the launch.json file path, default to .vscode/launch.json
-- launch_json = vfn.getcwd() .. "/.vscode/launch.json",
dap_debug = true,
dap_debug_gui = true,
dap_debug_keymap = true, -- true: use keymap for debugger defined in go/dap.lua
-- false: do not use keymap in go/dap.lua. you must define your own.
2021-07-26 00:35:58 +00:00
dap_vt = true, -- false, true and 'all frames'
2022-01-30 04:57:56 +00:00
dap_port = 38697, -- can be set to a number or `-1` so go.nvim will pickup a random port
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
test_runner = "go", -- one of {`go`, `richgo`, `dlv`, `ginkgo`}
verbose_tests = true, -- set to add verbose flag to tests
2021-12-06 10:32:23 +00:00
run_in_floaterm = false, -- set to true to run in float window.
test_efm = false, -- errorfomat for quickfix, default mix mode, set to true will be efm only
2022-05-15 12:25:50 +00:00
luasnip = false, -- enable included luasnip
2022-05-15 12:25:50 +00:00
username = "",
useremail = "",
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)
cfg = cfg or {}
2021-07-10 11:04:24 +00:00
if cfg.max_len then
vim.notify("go.nvim max_len renamed to max_line_len", vim.lsp.log_levels.WARN)
2021-07-08 16:16:22 +00:00
end
2021-07-10 11:04:24 +00:00
_GO_NVIM_CFG = vim.tbl_extend("force", _GO_NVIM_CFG, cfg)
require("go.commands").add_cmds()
2022-05-15 12:34:44 +00:00
require("go.project").load_project()
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()]])
end
2022-05-15 12:34:44 +00:00
require("go.utils").set_nulls()
2021-07-11 11:50:09 +00:00
if _GO_NVIM_CFG.lsp_cfg then
2021-12-06 10:32:23 +00:00
require("go.lsp").setup()
2021-07-12 04:25:07 +00:00
if _GO_NVIM_CFG.lsp_diag_hdlr then
2021-12-06 10:32:23 +00:00
require("go.lsp_diag")
2021-07-12 04:25:07 +00:00
end
elseif not _GO_NVIM_CFG.lsp_cfg and _GO_NVIM_CFG.lsp_on_attach then
2022-01-11 11:33:23 +00:00
vim.notify("lsp_on_attach ignored, because lsp_cfg is false", vim.lsp.log_levels.WARN)
end
2021-12-06 10:32:23 +00:00
require("go.coverage").highlight()
if _GO_NVIM_CFG.lsp_codelens then
2021-12-06 10:32:23 +00:00
require("go.codelens").setup()
end
if _GO_NVIM_CFG.textobjects then
2021-12-22 04:01:15 +00:00
require("go.ts.textobjects").setup()
end
2022-07-05 22:28:29 +00:00
require("go.env").setup()
2021-11-13 03:29:42 +00:00
if _GO_NVIM_CFG.luasnip then
local ls = require("go.utils").load_plugin("LuaSnip", "luasnip")
if ls then
require("snips.go")
end
end
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
end
2021-11-23 23:13:40 +00:00
go.set_test_runner = function(runner)
-- richgo, go test, richgo, dlv, ginkgo
2021-12-06 10:32:23 +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
vim.notify("runner not supported " .. runner, vim.lsp.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