go.nvim/lua/go.lua

193 lines
8.3 KiB
Lua
Raw Normal View History

2021-03-10 12:15:06 +00:00
-- some of commands extracted from gopher.vim
local go = {}
2021-07-10 11:04:24 +00:00
_GO_NVIM_CFG = {
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
2021-08-14 00:27:55 +00:00
max_line_len = 120,
2021-07-10 11:04:24 +00:00
tag_transform = false,
2021-12-06 10:32:23 +00:00
test_dir = "",
comment_placeholder = "",
icons = { breakpoint = "🧘", currentpos = "🏃" },
2021-07-10 11:04:24 +00:00
verbose = false,
log_path = vim.fn.expand("$HOME") .. "/tmp/gonvim.log",
2021-07-11 11:50:09 +00:00
lsp_cfg = false, -- true: apply go.nvim non-default gopls setup
lsp_gofumpt = false, -- true: set default gofmt in gopls format to gofumpt
2021-07-11 11:50:09 +00:00
lsp_on_attach = nil, -- provides a on_attach function to gopls, will use go.nvim on_attach if nil
2021-07-12 04:25:07 +00:00
lsp_diag_hdlr = true, -- hook lsp diag handler
lsp_codelens = true,
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,
dap_debug = true,
dap_debug_gui = true,
2021-07-26 00:35:58 +00:00
dap_vt = true, -- false, true and 'all frames'
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-11-23 23:13:40 +00:00
build_tags = "", --- you can provide extra build tags for tests or debugger
2021-12-06 10:32:23 +00:00
test_runner = "go", -- richgo, go test, richgo, dlv, ginkgo
run_in_floaterm = false, -- set to true to run in float window.
2021-07-10 11:04:24 +00:00
}
local dap_config = function()
2021-10-23 09:26:11 +00:00
vim.cmd([[command! BreakCondition lua require"dap".set_breakpoint(vim.fn.input("Breakpoint condition: "))]])
2021-07-10 11:04:24 +00:00
vim.cmd([[command! ReplRun lua require"dap".repl.run_last()]])
vim.cmd([[command! ReplToggle lua require"dap".repl.toggle()]])
vim.cmd([[command! ReplOpen lua require"dap".repl.open(), 'split']])
2021-10-23 09:26:11 +00:00
vim.cmd([[command! DapRerun lua require'dap'.disconnect();require'dap'.close();require'dap'.run_last()]])
2021-07-10 11:04:24 +00:00
2021-08-14 09:18:08 +00:00
vim.cmd([[command! DapStop lua require'go.dap'.stop()]])
2021-07-10 11:04:24 +00:00
end
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
2021-12-06 10:32:23 +00:00
print("go.nvim max_len renamed to max_line_len")
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)
2021-12-06 10:32:23 +00:00
vim.cmd([[autocmd FileType go setlocal omnifunc=v:lua.vim.lsp.omnifunc]])
vim.cmd([[command! GoMake silent lua require'go.asyncmake'.make()]])
2021-07-10 11:04:24 +00:00
vim.cmd([[command! GoFmt lua require("go.format").gofmt()]])
vim.cmd([[command! -nargs=* GoImport lua require("go.format").goimport(<f-args>)]])
2021-07-16 14:37:41 +00:00
vim.cmd([[command! GoGenerate :setl makeprg=go\ generate | :GoMake]])
2021-11-13 03:29:42 +00:00
vim.cmd(
2021-12-06 10:32:23 +00:00
[[command! -nargs=* -complete=custom,v:lua.package.loaded.go.package_complete GoBuild :setl makeprg=go\ build | lua require'go.asyncmake'.make(<f-args>)]]
)
2021-11-13 03:29:42 +00:00
vim.cmd(
2021-12-06 10:32:23 +00:00
[[command! -nargs=* -complete=custom,v:lua.package.loaded.go.package_complete GoRun :setl makeprg=go\ run | lua require'go.asyncmake'.make(<f-args>)]]
)
2021-09-03 02:13:15 +00:00
-- if you want to output to quickfix
-- vim.cmd(
-- [[command! -nargs=* GoTest :setl makeprg=go\ test\ -v\ ./...| lua require'go.asyncmake'.make(<f-args>)]])
2021-12-06 10:32:23 +00:00
local sep = require("go.utils").sep()
2021-09-03 02:13:15 +00:00
-- example to running test in split buffer
2021-11-23 23:13:40 +00:00
-- vim.cmd(
-- [[command! -nargs=* -complete=custom,v:lua.package.loaded.go.package_complete GoTest :setl makeprg=go\ test\ -v\ | lua require'go.runner'.make(<f-args>)]])
2021-11-13 03:29:42 +00:00
vim.cmd(
2021-12-06 10:32:23 +00:00
[[command! -nargs=* -complete=custom,v:lua.package.loaded.go.package_complete GoTest lua require('go.gotest').test(<f-args>)]]
)
2021-08-25 15:42:17 +00:00
2021-11-13 03:29:42 +00:00
vim.cmd(
2021-12-06 10:32:23 +00:00
[[command! -nargs=* -complete=custom,v:lua.package.loaded.go.package_complete GoCoverage lua require'go.coverage'.run(<f-args>)]]
)
-- vim.cmd([[command! GoTestCompile :setl makeprg=go\ build | :GoMake]])
2021-12-10 00:43:59 +00:00
--print-issued-lines=false
vim.cmd([[command! GoLint :setl makeprg=golangci-lint\ run\ --print-issued-lines=false\ --exclude-use-default=false | :GoMake]])
2021-07-10 11:04:24 +00:00
2021-08-31 11:19:14 +00:00
-- e.g. GoTestFunc unit
vim.cmd([[command! -nargs=* GoTestFunc lua require('go.gotest').test_fun(<f-args>)]])
-- e.g. GoTestFile unit
vim.cmd([[command! -nargs=* GoTestFile lua require('go.gotest').test_file(<f-args>)]])
2021-11-23 23:13:40 +00:00
vim.cmd([[command! -nargs=* GoTestPkg lua require('go.gotest').test_package(<f-args>)]])
2021-07-10 11:04:24 +00:00
vim.cmd([[command! GoAddTest lua require("go.gotests").fun_test()]])
vim.cmd([[command! GoAddExpTest lua require("go.gotests").exported_test()]])
vim.cmd([[command! GoAddAllTest lua require("go.gotests").all_test()]])
2021-03-10 12:15:06 +00:00
vim.cmd([[command! GoCodeLenAct lua require("go.codelens").run_action()]])
2021-03-10 12:15:06 +00:00
vim.cmd([[command! -nargs=* GoAddTag lua require("go.tags").add(<f-args>)]])
vim.cmd([[command! -nargs=* GoRmTag lua require("go.tags").rm(<f-args>)]])
2021-07-14 15:35:38 +00:00
vim.cmd([[command! -nargs=* GoImpl lua require("go.impl").run(<f-args>)]])
2021-09-18 11:14:02 +00:00
vim.cmd([[command! -nargs=* GoDoc lua require("go.godoc").run(<f-args>)]])
2021-11-13 03:29:42 +00:00
vim.cmd(
2021-12-06 10:32:23 +00:00
[[command! -nargs=+ -complete=custom,v:lua.package.loaded.go.doc_complete GoDoc lua require'go.godoc'.run('doc', {<f-args>})]]
)
vim.cmd(
[[command! -nargs=+ -complete=custom,v:lua.package.loaded.go.tools_complete GoInstallBinary lua require'go.install'.install(<f-args>)]]
)
vim.cmd(
[[command! -nargs=+ -complete=custom,v:lua.package.loaded.go.tools_complete GoUpdateBinary lua require'go.install'.update(<f-args>)]]
)
vim.cmd([[command! GoInstallBinaries lua require'go.install'.install_all()]])
vim.cmd([[command! GoUpdateBinaries lua require'go.install'.update_all()]])
2021-06-22 00:11:26 +00:00
vim.cmd([[command! GoClearTag lua require("go.tags").clear()]])
vim.cmd([[command! GoCmt lua require("go.comment").gen()]])
vim.cmd([[command! GoRename lua require("go.rename").run()]])
2021-07-15 09:18:04 +00:00
vim.cmd([[command! GoIfErr lua require("go.iferr").run()]])
vim.cmd([[command! GoFillStruct lua require("go.reftool").fillstruct()]])
vim.cmd([[command! GoFillSwitch lua require("go.reftool").fillswitch()]])
vim.cmd([[command! GoFixPlurals lua require("go.reftool").fixplurals()]])
vim.cmd([[command! -bang GoAlt lua require"go.alternate".switch("<bang>"=="!", '')]])
vim.cmd([[command! -bang GoAltV lua require"go.alternate".switch("<bang>"=="!", 'vsplit')]])
vim.cmd([[command! -bang GoAltS lua require"go.alternate".switch("<bang>"=="!", 'split')]])
2021-03-10 21:35:30 +00:00
vim.cmd("au FileType go au QuickFixCmdPost [^l]* nested cwindow")
vim.cmd("au FileType go au QuickFixCmdPost l* nested lwindow")
2021-03-10 12:15:06 +00:00
2021-11-13 03:29:42 +00:00
vim.cmd([[command! -bang GoModTidy lua require"go.gopls".tidy()]])
2021-07-10 11:04:24 +00:00
if _GO_NVIM_CFG.dap_debug then
dap_config()
2021-11-24 09:05:02 +00:00
vim.cmd(
2021-12-06 10:32:23 +00:00
[[command! -nargs=* -complete=custom,v:lua.package.loaded.go.dbg_complete GoDebug lua require"go.dap".run(<f-args>)]]
)
2021-07-10 11:10:46 +00:00
vim.cmd([[command! GoBreakToggle lua require"go.dap".breakpt()]])
2021-10-23 09:26:11 +00:00
vim.cmd([[command! BreakCondition lua require"dap".set_breakpoint(vim.fn.input("Breakpoint condition: "))]])
2021-07-10 11:04:24 +00:00
vim.cmd([[command! ReplRun lua require"dap".repl.run_last()]])
vim.cmd([[command! ReplToggle lua require"dap".repl.toggle()]])
vim.cmd([[command! ReplOpen lua require"dap".repl.open(), 'split']])
2021-10-23 09:26:11 +00:00
vim.cmd([[command! DapRerun lua require'dap'.disconnect();require'dap'.close();require'dap'.run_last()]])
2021-11-24 09:05:02 +00:00
vim.cmd([[command! DapUiFloat lua require("dapui").float_element()]])
vim.cmd([[command! DapUiToggle lua require("dapui").toggle()]])
2021-07-14 14:15:15 +00:00
vim.cmd([[command! GoDbgStop lua require'go.dap'.stop()]])
2021-07-10 11:04:24 +00:00
end
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
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
-- TODO remove in future
vim.cmd([[command! Gofmt echo 'use GoFmt']])
vim.cmd([[command! -nargs=* Goimport echo 'use GoImport']])
2021-03-10 12:15:06 +00:00
end
2021-11-13 03:29:42 +00:00
2021-12-06 10:32:23 +00:00
go.doc_complete = require("go.godoc").doc_complete
go.package_complete = require("go.package").complete
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
print("runner not supported " .. runner)
end
2021-11-24 09:05:02 +00:00
go.dbg_complete = function(arglead, cmdline, cursorpos)
-- richgo, go test, richgo, dlv, ginkgo
2021-12-06 10:32:23 +00:00
local testopts = { "test", "nearest", "file", "stop", "restart" }
return table.concat(testopts, "\n")
2021-11-24 09:05:02 +00:00
end
2021-12-06 10:32:23 +00:00
go.tools_complete = function(arglead, cmdline, cursorpos)
local gotools = require("go.install").gotools
table.sort(gotools)
return table.concat(gotools, "\n")
end
2021-03-10 12:15:06 +00:00
return go