merge master

This commit is contained in:
ray-x 2022-05-15 21:46:44 +10:00
commit 80e94444bb
10 changed files with 180 additions and 52 deletions

View File

@ -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) - 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 - 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 - 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 ## Installation
@ -71,12 +71,12 @@ require('go').setup()
``` ```
## Project 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 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 ```lua
-- .gonvim project config -- .gonvim/init.lua project config
vim.g.null_ls_disable = true vim.g.null_ls_disable = true
return { return {
@ -85,7 +85,7 @@ return {
fillstruct = "gopls", fillstruct = "gopls",
gofmt = "gofumpt", -- if set to gopls will use gopls format gofmt = "gofumpt", -- if set to gopls will use gopls format
max_line_len = 120 max_line_len = 120
null_ls_document_formatting_disable = 'golines' null_ls_document_formatting_disable = true
} }
``` ```
This will override your global `go.nvim` setup This will override your global `go.nvim` setup

View File

@ -199,7 +199,7 @@ COMMANDS *go-nvim-commands*
current line and use it as url if valid current line and use it as url if valid
:GoLint *:GoLint* :GoLint *:GoLint*
Run Golint Run golangci-lint
:GoRename *:GoRename* :GoRename *:GoRename*
@ -255,10 +255,21 @@ COMMANDS *go-nvim-commands*
:GoDebugConfig *:GoDebugConfig* :GoDebugConfig *:GoDebugConfig*
Open launch.json Open launch.json
:GoCreateLaunch *:GoCreateLaunch*
Create alaunch.json
:GoBreakToggle *:GoBreakToggle* :GoBreakToggle *:GoBreakToggle*
Debuger breakpoint toggle 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* :GoAlt *:GoAlt*
Open alternative file (test/go), Also GoAltS/GoAltV Open alternative file (test/go), Also GoAltS/GoAltV

View File

@ -23,17 +23,11 @@ _GO_NVIM_CFG = {
-- when lsp_cfg is true -- when lsp_cfg is true
-- if lsp_on_attach is a function: use this function as on_attach function for gopls, -- if lsp_on_attach is a function: use this function as on_attach function for gopls,
-- when lsp_cfg is true -- 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_format_on_save = 1,
lsp_document_formatting = true, lsp_document_formatting = true,
-- set to true: use gopls to format -- set to true: use gopls to format
-- false if you want to use other formatter tool(e.g. efm, nulls) -- 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_keymaps = true, -- true: use default keymaps defined in go/lsp.lua
lsp_codelens = true, lsp_codelens = true,
lsp_diag_hdlr = true, -- hook lsp diag handler lsp_diag_hdlr = true, -- hook lsp diag handler
@ -41,7 +35,6 @@ _GO_NVIM_CFG = {
lsp_diag_virtual_text = { space = 0, prefix = "" }, lsp_diag_virtual_text = { space = 0, prefix = "" },
lsp_diag_signs = true, lsp_diag_signs = true,
lsp_diag_update_in_insert = false, 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_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, gopls_remote_auto = true,
gocoverage_sign = "", gocoverage_sign = "",
@ -59,9 +52,6 @@ _GO_NVIM_CFG = {
test_runner = "go", -- richgo, go test, richgo, dlv, ginkgo test_runner = "go", -- richgo, go test, richgo, dlv, ginkgo
verbose_tests = true, -- set to add verbose flag to tests verbose_tests = true, -- set to add verbose flag to tests
run_in_floaterm = false, -- set to true to run in float window. run_in_floaterm = false, -- set to true to run in float window.
username = "",
useremail = "",
} }
local dap_config = function() local dap_config = function()
@ -142,9 +132,7 @@ function go.setup(cfg)
-- e.g. GoTestFile unit -- e.g. GoTestFile unit
vim.cmd([[command! -nargs=* GoTestFile lua require('go.gotest').test_file(<f-args>)]]) vim.cmd([[command! -nargs=* GoTestFile lua require('go.gotest').test_file(<f-args>)]])
vim.cmd( vim.cmd([[command! -nargs=* -complete=custom,v:lua.package.loaded.go.package_complete GoTestPkg lua require('go.gotest').test_package(<f-args>)]])
[[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=* 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=* GoAddExpTest lua require("go.gotests").exported_test(<f-args>)]])
vim.cmd([[command! -nargs=* GoAddAllTest lua require("go.gotests").all_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( vim.cmd(
[[command! -nargs=* -complete=custom,v:lua.package.loaded.go.dbg_complete GoDebug lua require"go.dap".run(<f-args>)]] [[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! GoBreakSave lua require"go.dap".save_brks()]])
vim.cmd([[command! GoBreakLoad lua require"go.dap".load_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()]]) vim.cmd([[command! GoDbgStop lua require'go.dap'.stop()]])
end end
require("go.project_setup").load_project()
if _GO_NVIM_CFG.run_in_floaterm then if _GO_NVIM_CFG.run_in_floaterm then
vim.cmd([[command! -nargs=* GoTermClose lua require("go.term").close()]]) vim.cmd([[command! -nargs=* GoTermClose lua require("go.term").close()]])
end end
require("go.utils").set_nulls()
if _GO_NVIM_CFG.lsp_cfg then if _GO_NVIM_CFG.lsp_cfg then
require("go.lsp").setup() require("go.lsp").setup()
if _GO_NVIM_CFG.lsp_diag_hdlr then if _GO_NVIM_CFG.lsp_diag_hdlr then

View File

@ -123,13 +123,18 @@ M.breakpt = function()
require("dap").toggle_breakpoint() require("dap").toggle_breakpoint()
end end
M.save_bks = function() M.save_brks = function()
M.prepare()
local bks = require("dap.breakpoints").get() local bks = require("dap.breakpoints").get()
local all_bks = {} local all_bks = {}
if bks and next(bks) then 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 for bufnr, bk in pairs(bks) do
local uri = vim.uri_from_bufnr(bufnr) 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 all_bks[uri] = bk
end end
local bkfile = fld .. utils.sep() .. "breakpoints.lua" local bkfile = fld .. utils.sep() .. "breakpoints.lua"
@ -141,9 +146,31 @@ M.save_bks = function()
end end
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") 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 if vim.fn.filereadable(brkfile) == 0 then
return return
end end

View File

@ -14,19 +14,20 @@ function M.envfile(f)
end end
function M.load_env(env, setToEnv) function M.load_env(env, setToEnv)
setToEnv = setToEnv or true
env = env or M.envfile() env = env or M.envfile()
if vim.fn.filereadable(env) == 0 then if vim.fn.filereadable(env) == 0 then
return false return false
end end
local e = io.open(env, "r") local lines = util.lines_from(env)
local lines = util.lines_from(e)
local envs = {} local envs = {}
for _, line in ipairs(lines) do 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 envs[k] = v
end end
end end
log(envs)
if setToEnv then if setToEnv then
for key, val in pairs(envs) do for key, val in pairs(envs) do
vim.fn.setenv(key, val) vim.fn.setenv(key, val)
@ -36,6 +37,4 @@ function M.load_env(env, setToEnv)
return envs return envs
end end
M.load_project()
return M return M

View File

@ -102,6 +102,10 @@ M.list_pkgs = function()
return pkgs return pkgs
end end
M.tidy = function()
cmds.tidy()
end
-- check_for_upgrades({Modules = {'package'}}) -- check_for_upgrades({Modules = {'package'}})
function M.version() function M.version()
local cache_dir = vim.fn.stdpath("cache") local cache_dir = vim.fn.stdpath("cache")

View File

@ -2,8 +2,8 @@
-- https://github.com/cweill/gotests -- https://github.com/cweill/gotests
local ut = {} local ut = {}
local gotests = "gotests" local gotests = "gotests"
local test_dir = _GO_NVIM_CFG.test_dir or "" local gotests_template = _GO_NVIM_CFG.gotests_template or ""
local test_template = vim.go_nvim_test_template or "" local gotests_template_dir = _GO_NVIM_CFG.gotests_template_dir or ""
local utils = require("go.utils") local utils = require("go.utils")
local empty = utils.empty local empty = utils.empty
local run = function(setup) local run = function(setup)
@ -29,12 +29,12 @@ local new_gotests_args = function(parallel)
if parallel then if parallel then
table.insert(args, "-parallel") table.insert(args, "-parallel")
end end
if string.len(test_template) > 0 then if string.len(gotests_template) > 0 then
table.insert(args, "-template") table.insert(args, "-template")
table.insert(args, test_template) table.insert(args, gotests_template)
if string.len(test_dir) > 0 then if string.len(gotests_template_dir) > 0 then
table.insert(args, "-template_dir") table.insert(args, "-template_dir")
table.insert(args, test_dir) table.insert(args, gotests_template_dir)
end end
end end
return args return args

View File

@ -3,39 +3,50 @@ local launch_json_content = [[
"version": "0.2.0", "version": "0.2.0",
"configurations": [ "configurations": [
{ {
"name": "Launch main", "name": "Launch package",
"type": "go", "type": "go",
"request": "launch", "request": "launch",
"mode": "exec", "mode": "auto",
"remotePath": "", "remotePath": "",
"port": 38697, "port": 38697,
"host": "127.0.0.1", "host": "127.0.0.1",
"program": "${workspaceFolder}/main.go", "program": "${workspaceFolder}",
"env": { "env": {
}, },
"args": [], "args": [],
"cwd": ${workspaceFolder}", "cwd": "${workspaceFolder}",
"envFile", "${workspaceFolder}/.env" "envFile": "${workspaceFolder}/.env",
"buildFlags":"" "buildFlags":""
}, },
{ {
"name": "debug main", "name": "Debug current package",
"type": "go", "type": "go",
"request": "launch", "request": "launch",
"mode": "debug", "mode": "debug",
"remotePath": "", "remotePath": "",
"port": 38697, "port": 38697,
"host": "127.0.0.1", "host": "127.0.0.1",
"program": "${workspaceFolder}/main.go", "program": "${fileDirname}",
"env": { "env": {
}, },
"args": [], "args": [],
"cwd": ${workspaceFolder}", "cwd": "${workspaceFolder}",
"envFile", "${workspaceFolder}/.env" "envFile": "${workspaceFolder}/.env",
"buildFlags":"" "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", "type": "go",
"request": "attach", "request": "attach",
"mode": "debug", "mode": "debug",
@ -46,10 +57,24 @@ local launch_json_content = [[
"env": { "env": {
}, },
"args": [], "args": [],
"cwd": ${workspaceFolder}", "cwd": "${workspaceFolder}",
"processId":"", "processId":"",
"envFile", "${workspaceFolder}/.env" "envFile": "${workspaceFolder}/.env",
"buildFlags":"" "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() function M.config()
local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vim.fn.getcwd() 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_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 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 if vim.fn.filereadable(launch_json) == 1 then
return vim.cmd(cmd) return vim.cmd(cmd)
end end

View File

@ -6,7 +6,8 @@ local M = {}
function M.run(...) function M.run(...)
local args = { ... } local args = { ... }
local cmd = { "go", "mod" } local cmd = { "go", "mod" }
vim.list_extend(cmd, args) cmd = vim.list_extend(cmd, args)
utils.log(cmd)
local opts = { local opts = {
after = function() after = function()
vim.schedule(function() vim.schedule(function()

70
lua/go/project.lua Normal file
View 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