merge master
This commit is contained in:
commit
80e94444bb
10
README.md
10
README.md
@ -25,7 +25,7 @@ The plugin covers most features required for a gopher.
|
||||
- Test with ginkgo, richgo inside floaterm (to enable floaterm, guihua.lua has to be installed)
|
||||
- Go 1.18 support, configure your go to `go1.18` in config
|
||||
- GoFixPlural, FixStruct, FxiSwitch, Add comment, IfErr, ModTidy, GoGet ... Most of the tools are built on top of
|
||||
treesitter AST or go AST. It is fast and accurate.
|
||||
treesitter AST or go AST. It is fast and accurate.
|
||||
|
||||
## Installation
|
||||
|
||||
@ -71,12 +71,12 @@ require('go').setup()
|
||||
```
|
||||
## Project setup
|
||||
|
||||
`go.nvim` allow you override your setup by a project file. Put `.gonvim` in your root folder. It is a small lua
|
||||
`go.nvim` allow you override your setup by a project file. Put `.gonvim/init.lua` in your root folder. It is a small lua
|
||||
script and will be run durning go.setup(). The return value is used to override `go.nvim` setup. The sample project
|
||||
setup
|
||||
setup. You can check the youtube video [here](https://www.youtube.com/watch?v=XrxSUp0E9Qw) on how to use this feature.
|
||||
|
||||
```lua
|
||||
-- .gonvim project config
|
||||
-- .gonvim/init.lua project config
|
||||
vim.g.null_ls_disable = true
|
||||
|
||||
return {
|
||||
@ -85,7 +85,7 @@ return {
|
||||
fillstruct = "gopls",
|
||||
gofmt = "gofumpt", -- if set to gopls will use gopls format
|
||||
max_line_len = 120
|
||||
null_ls_document_formatting_disable = 'golines'
|
||||
null_ls_document_formatting_disable = true
|
||||
}
|
||||
```
|
||||
This will override your global `go.nvim` setup
|
||||
|
13
doc/go.txt
13
doc/go.txt
@ -199,7 +199,7 @@ COMMANDS *go-nvim-commands*
|
||||
current line and use it as url if valid
|
||||
|
||||
:GoLint *:GoLint*
|
||||
Run Golint
|
||||
Run golangci-lint
|
||||
|
||||
|
||||
:GoRename *:GoRename*
|
||||
@ -255,10 +255,21 @@ COMMANDS *go-nvim-commands*
|
||||
:GoDebugConfig *:GoDebugConfig*
|
||||
Open launch.json
|
||||
|
||||
:GoCreateLaunch *:GoCreateLaunch*
|
||||
Create alaunch.json
|
||||
|
||||
:GoBreakToggle *:GoBreakToggle*
|
||||
Debuger breakpoint toggle
|
||||
|
||||
:GoBreakSave *:GoBreakSave*
|
||||
Debuger breakpoint save to project file
|
||||
|
||||
:GoBreakLoad *:GoBreakLoad*
|
||||
Debuger breakpoint load from project file
|
||||
|
||||
:GoEnv {envfile} {load} *:GoEnv*
|
||||
Load envfile and set environment variable
|
||||
|
||||
:GoAlt *:GoAlt*
|
||||
Open alternative file (test/go), Also GoAltS/GoAltV
|
||||
|
||||
|
19
lua/go.lua
19
lua/go.lua
@ -23,17 +23,11 @@ _GO_NVIM_CFG = {
|
||||
-- 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
|
||||
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)
|
||||
|
||||
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
|
||||
lsp_keymaps = true, -- true: use default keymaps defined in go/lsp.lua
|
||||
lsp_codelens = true,
|
||||
lsp_diag_hdlr = true, -- hook lsp diag handler
|
||||
@ -41,7 +35,6 @@ _GO_NVIM_CFG = {
|
||||
lsp_diag_virtual_text = { space = 0, prefix = "" },
|
||||
lsp_diag_signs = true,
|
||||
lsp_diag_update_in_insert = false,
|
||||
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,
|
||||
gocoverage_sign = "█",
|
||||
@ -59,9 +52,6 @@ _GO_NVIM_CFG = {
|
||||
test_runner = "go", -- richgo, go test, richgo, dlv, ginkgo
|
||||
verbose_tests = true, -- set to add verbose flag to tests
|
||||
run_in_floaterm = false, -- set to true to run in float window.
|
||||
|
||||
username = "",
|
||||
useremail = "",
|
||||
}
|
||||
|
||||
local dap_config = function()
|
||||
@ -142,9 +132,7 @@ function go.setup(cfg)
|
||||
|
||||
-- e.g. GoTestFile unit
|
||||
vim.cmd([[command! -nargs=* GoTestFile lua require('go.gotest').test_file(<f-args>)]])
|
||||
vim.cmd(
|
||||
[[command! -nargs=* -complete=custom,v:lua.package.loaded.go.package_complete GoTestPkg lua require('go.gotest').test_package(<f-args>)]]
|
||||
)
|
||||
vim.cmd([[command! -nargs=* -complete=custom,v:lua.package.loaded.go.package_complete GoTestPkg lua require('go.gotest').test_package(<f-args>)]])
|
||||
vim.cmd([[command! -nargs=* GoAddTest lua require("go.gotests").fun_test(<f-args>)]])
|
||||
vim.cmd([[command! -nargs=* GoAddExpTest lua require("go.gotests").exported_test(<f-args>)]])
|
||||
vim.cmd([[command! -nargs=* GoAddAllTest lua require("go.gotests").all_test(<f-args>)]])
|
||||
@ -195,6 +183,7 @@ function go.setup(cfg)
|
||||
vim.cmd(
|
||||
[[command! -nargs=* -complete=custom,v:lua.package.loaded.go.dbg_complete GoDebug lua require"go.dap".run(<f-args>)]]
|
||||
)
|
||||
vim.cmd([[command! GoCreateLaunch lua require"go.launch".config()]])
|
||||
vim.cmd([[command! GoBreakSave lua require"go.dap".save_brks()]])
|
||||
vim.cmd([[command! GoBreakLoad lua require"go.dap".load_brks()]])
|
||||
|
||||
@ -211,14 +200,10 @@ function go.setup(cfg)
|
||||
vim.cmd([[command! GoDbgStop lua require'go.dap'.stop()]])
|
||||
end
|
||||
|
||||
require("go.project_setup").load_project()
|
||||
|
||||
if _GO_NVIM_CFG.run_in_floaterm then
|
||||
vim.cmd([[command! -nargs=* GoTermClose lua require("go.term").close()]])
|
||||
end
|
||||
|
||||
require("go.utils").set_nulls()
|
||||
|
||||
if _GO_NVIM_CFG.lsp_cfg then
|
||||
require("go.lsp").setup()
|
||||
if _GO_NVIM_CFG.lsp_diag_hdlr then
|
||||
|
@ -123,13 +123,18 @@ M.breakpt = function()
|
||||
require("dap").toggle_breakpoint()
|
||||
end
|
||||
|
||||
M.save_bks = function()
|
||||
M.save_brks = function()
|
||||
M.prepare()
|
||||
local bks = require("dap.breakpoints").get()
|
||||
local all_bks = {}
|
||||
if bks and next(bks) then
|
||||
local cfg, fld = require("go.project_setup").setup_project()
|
||||
local cfg, fld = require("go.project").setup()
|
||||
for bufnr, bk in pairs(bks) do
|
||||
local uri = vim.uri_from_bufnr(bufnr)
|
||||
local _bk ={}
|
||||
for _, value in pairs(bk) do
|
||||
table.insert(_bk, {line = value.line})
|
||||
end
|
||||
all_bks[uri] = bk
|
||||
end
|
||||
local bkfile = fld .. utils.sep() .. "breakpoints.lua"
|
||||
@ -141,9 +146,31 @@ M.save_bks = function()
|
||||
end
|
||||
end
|
||||
|
||||
M.load_bks = function()
|
||||
M.load_brks = function()
|
||||
M.prepare()
|
||||
local _, brkfile = require("go.project").project_existed()
|
||||
if vim.fn.filereadable(brkfile) == 0 then
|
||||
return
|
||||
end
|
||||
local f = assert(loadfile(brkfile))
|
||||
local brks = f()
|
||||
for uri, brk in pairs(brks) do
|
||||
local bufnr = vim.uri_to_bufnr(uri)
|
||||
if not vim.api.nvim_buf_is_loaded(bufnr) then
|
||||
vim.fn.bufload(bufnr)
|
||||
end
|
||||
for index, lnum in ipairs(brk) do
|
||||
require("dap.breakpoints").set({}, bufnr, lnum.line)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
M.clear_bks = function()
|
||||
utils.load_plugin("nvim-dap", "dap")
|
||||
local _, brkfile = require("go.project_setup").project_existed()
|
||||
|
||||
require("dap.breakpoints").clear()
|
||||
M.save_bks()
|
||||
local _, brkfile = require("go.project").project_existed()
|
||||
if vim.fn.filereadable(brkfile) == 0 then
|
||||
return
|
||||
end
|
||||
|
@ -14,19 +14,20 @@ function M.envfile(f)
|
||||
end
|
||||
|
||||
function M.load_env(env, setToEnv)
|
||||
setToEnv = setToEnv or true
|
||||
env = env or M.envfile()
|
||||
if vim.fn.filereadable(env) == 0 then
|
||||
return false
|
||||
end
|
||||
local e = io.open(env, "r")
|
||||
local lines = util.lines_from(e)
|
||||
local lines = util.lines_from(env)
|
||||
local envs = {}
|
||||
for _, line in ipairs(lines) do
|
||||
for k, v in string.gmatch(line, "(%w+)=(%w+)") do
|
||||
for k, v in string.gmatch(line, "([%w_]+)=([%w%c%p%z]+)") do
|
||||
envs[k] = v
|
||||
end
|
||||
end
|
||||
|
||||
log(envs)
|
||||
if setToEnv then
|
||||
for key, val in pairs(envs) do
|
||||
vim.fn.setenv(key, val)
|
||||
@ -36,6 +37,4 @@ function M.load_env(env, setToEnv)
|
||||
return envs
|
||||
end
|
||||
|
||||
M.load_project()
|
||||
|
||||
return M
|
||||
|
@ -102,6 +102,10 @@ M.list_pkgs = function()
|
||||
return pkgs
|
||||
end
|
||||
|
||||
M.tidy = function()
|
||||
cmds.tidy()
|
||||
end
|
||||
|
||||
-- check_for_upgrades({Modules = {'package'}})
|
||||
function M.version()
|
||||
local cache_dir = vim.fn.stdpath("cache")
|
||||
|
@ -2,13 +2,13 @@
|
||||
-- https://github.com/cweill/gotests
|
||||
local ut = {}
|
||||
local gotests = "gotests"
|
||||
local test_dir = _GO_NVIM_CFG.test_dir or ""
|
||||
local test_template = vim.go_nvim_test_template or ""
|
||||
local gotests_template = _GO_NVIM_CFG.gotests_template or ""
|
||||
local gotests_template_dir = _GO_NVIM_CFG.gotests_template_dir or ""
|
||||
local utils = require("go.utils")
|
||||
local empty = utils.empty
|
||||
local run = function(setup)
|
||||
print(vim.inspect(setup))
|
||||
vim.fn.jobstart(setup, {
|
||||
vim.fn.jobstart(setup, {
|
||||
stdout_buffered = true,
|
||||
on_stdout = function(_, data, _)
|
||||
print("unit tests generate " .. vim.inspect(data))
|
||||
@ -29,12 +29,12 @@ local new_gotests_args = function(parallel)
|
||||
if parallel then
|
||||
table.insert(args, "-parallel")
|
||||
end
|
||||
if string.len(test_template) > 0 then
|
||||
if string.len(gotests_template) > 0 then
|
||||
table.insert(args, "-template")
|
||||
table.insert(args, test_template)
|
||||
if string.len(test_dir) > 0 then
|
||||
table.insert(args, gotests_template)
|
||||
if string.len(gotests_template_dir) > 0 then
|
||||
table.insert(args, "-template_dir")
|
||||
table.insert(args, test_dir)
|
||||
table.insert(args, gotests_template_dir)
|
||||
end
|
||||
end
|
||||
return args
|
||||
|
@ -3,39 +3,50 @@ local launch_json_content = [[
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Launch main",
|
||||
"name": "Launch package",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "exec",
|
||||
"mode": "auto",
|
||||
"remotePath": "",
|
||||
"port": 38697,
|
||||
"host": "127.0.0.1",
|
||||
"program": "${workspaceFolder}/main.go",
|
||||
"program": "${workspaceFolder}",
|
||||
"env": {
|
||||
},
|
||||
"args": [],
|
||||
"cwd": ${workspaceFolder}",
|
||||
"envFile", "${workspaceFolder}/.env"
|
||||
"cwd": "${workspaceFolder}",
|
||||
"envFile": "${workspaceFolder}/.env",
|
||||
"buildFlags":""
|
||||
},
|
||||
{
|
||||
"name": "debug main",
|
||||
"name": "Debug current package",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "debug",
|
||||
"remotePath": "",
|
||||
"port": 38697,
|
||||
"host": "127.0.0.1",
|
||||
"program": "${workspaceFolder}/main.go",
|
||||
"program": "${fileDirname}",
|
||||
"env": {
|
||||
},
|
||||
"args": [],
|
||||
"cwd": ${workspaceFolder}",
|
||||
"envFile", "${workspaceFolder}/.env"
|
||||
"cwd": "${workspaceFolder}",
|
||||
"envFile": "${workspaceFolder}/.env",
|
||||
"buildFlags":""
|
||||
},
|
||||
{
|
||||
"name": "debug main",
|
||||
"name": "Launch test function",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "test",
|
||||
"program": "${workspaceFolder}",
|
||||
"args": [
|
||||
"-test.run",
|
||||
"MyTestFunction"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Attach main",
|
||||
"type": "go",
|
||||
"request": "attach",
|
||||
"mode": "debug",
|
||||
@ -46,10 +57,24 @@ local launch_json_content = [[
|
||||
"env": {
|
||||
},
|
||||
"args": [],
|
||||
"cwd": ${workspaceFolder}",
|
||||
"cwd": "${workspaceFolder}",
|
||||
"processId":"",
|
||||
"envFile", "${workspaceFolder}/.env"
|
||||
"envFile": "${workspaceFolder}/.env",
|
||||
"buildFlags":""
|
||||
},
|
||||
{
|
||||
"name": "Attach to Process",
|
||||
"type": "go",
|
||||
"request": "attach",
|
||||
"mode": "local",
|
||||
"processId": 0
|
||||
},
|
||||
{
|
||||
"name": "Launch file",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "debug",
|
||||
"program": "${file}"
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -75,8 +100,14 @@ end
|
||||
function M.config()
|
||||
local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vim.fn.getcwd()
|
||||
local launch_json = _GO_NVIM_CFG.launch_json or (workfolder .. sep .. ".vscode" .. sep .. "launch.json")
|
||||
local launch_dir = string.match(launch_json, ".*" .. sep)
|
||||
|
||||
local cmd = "e " .. launch_json
|
||||
|
||||
if vim.fn.isdirectory(launch_dir) == 0 then
|
||||
vim.fn.mkdir(launch_dir)
|
||||
end
|
||||
|
||||
if vim.fn.filereadable(launch_json) == 1 then
|
||||
return vim.cmd(cmd)
|
||||
end
|
||||
|
@ -6,7 +6,8 @@ local M = {}
|
||||
function M.run(...)
|
||||
local args = { ... }
|
||||
local cmd = { "go", "mod" }
|
||||
vim.list_extend(cmd, args)
|
||||
cmd = vim.list_extend(cmd, args)
|
||||
utils.log(cmd)
|
||||
local opts = {
|
||||
after = function()
|
||||
vim.schedule(function()
|
||||
|
70
lua/go/project.lua
Normal file
70
lua/go/project.lua
Normal file
@ -0,0 +1,70 @@
|
||||
-- this file allow a setup load per project
|
||||
--[[
|
||||
-- sample cfg
|
||||
return {
|
||||
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
|
||||
max_line_len = 120,
|
||||
tag_transform = false,
|
||||
test_dir = "",
|
||||
gocoverage_sign_priority = 5,
|
||||
launch_json = nil, -- the launch.json file path, default to .vscode/launch.json
|
||||
-- launch_json = vim.fn.getcwd() .. "/.vscode/launch.json",
|
||||
|
||||
build_tags = "", --- you can provide extra build tags for tests or debugger
|
||||
|
||||
}
|
||||
|
||||
]]
|
||||
|
||||
-- if the file existed, load it into config
|
||||
|
||||
local util = require("go.utils")
|
||||
local log = util.log
|
||||
local M = {}
|
||||
local sep = require("go.utils").sep()
|
||||
|
||||
function M.project_existed()
|
||||
local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vim.fn.getcwd()
|
||||
local gocfgfd = workfolder .. sep .. ".gonvim"
|
||||
local gocfgbrks = gocfgfd .. sep .. "breakpoints.lua"
|
||||
local gocfg = gocfgfd .. sep .. "init.lua"
|
||||
if vim.fn.filereadable(gocfg) == 1 or vim.fn.filereadable(gocfgbrks) == 1 then
|
||||
log("projects existed", gocfg, gocfgbrks)
|
||||
return gocfg, gocfgbrks
|
||||
end
|
||||
end
|
||||
|
||||
function M.setup()
|
||||
local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vim.fn.getcwd()
|
||||
local gocfgfd = workfolder .. sep .. ".gonvim"
|
||||
local gocfg = gocfgfd .. sep .. "init.lua"
|
||||
|
||||
if vim.fn.isdirectory(gocfgfd) == 0 then
|
||||
vim.fn.mkdir(gocfgfd)
|
||||
end
|
||||
if vim.fn.filereadable(gocfg) == 0 then
|
||||
local f = io.open(gocfg, "w")
|
||||
f:write("return {}")
|
||||
f:close()
|
||||
end
|
||||
return gocfg, gocfgfd
|
||||
end
|
||||
|
||||
function M.load_project()
|
||||
local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vim.fn.getcwd()
|
||||
local gocfg = workfolder .. sep .. ".gonvim" .. sep .. "init.lua"
|
||||
if vim.fn.filereadable(gocfg) == 1 then
|
||||
local f = assert(loadfile(gocfg))
|
||||
log(f())
|
||||
_GO_NVIM_CFG = vim.tbl_deep_extend("force", _GO_NVIM_CFG, f())
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
M.load_project()
|
||||
|
||||
return M
|
Loading…
Reference in New Issue
Block a user