Merge branch 'boilerplate'

pull/117/head
ray-x 2 years ago
commit 14f298af92

@ -0,0 +1,10 @@
vim.g.null_ls_disable = true
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
null_ls_document_formatting_disable = true
}

@ -3,7 +3,7 @@
A modern go neovim plugin based on treesitter, nvim-lsp and dap debugger. It is written in Lua and async as much as possible.
PR & Suggestions welcome.
The plugin covers most features required for a gopher.
- Perproject setup. Allows you setup plugin behavior per project based on project files(launch.json, .gonvim)
- Async jobs with libuv
- Syntax highlight & Texobject: Native treesitter support is faster and more accurate. All you need is a theme support treesitter, try
[aurora](https://github.com/ray-x/aurora). Also, there are quite a few listed in [awesome-neovim](https://github.com/rockerBOO/awesome-neovim)
@ -69,6 +69,28 @@ To startup/setup the plugin
```lua
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
script and will be run durning go.setup(). The return value is used to override `go.nvim` setup. The sample project
setup
```lua
-- .gonvim project config
vim.g.null_ls_disable = true
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
null_ls_document_formatting_disable = 'golines'
}
```
This will override your global `go.nvim` setup
## Screenshots

@ -23,11 +23,17 @@ _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
@ -35,6 +41,7 @@ _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 = "",
@ -52,6 +59,9 @@ _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()
@ -132,7 +142,9 @@ 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>)]])
@ -183,6 +195,9 @@ 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! GoBreakSave lua require"go.dap".save_brks()]])
vim.cmd([[command! GoBreakLoad lua require"go.dap".load_brks()]])
vim.cmd([[command! GoDebugConfig lua require"go.launch".config()]])
vim.cmd([[command! GoBreakToggle lua require"go.dap".breakpt()]])
vim.cmd([[command! BreakCondition lua require"dap".set_breakpoint(vim.fn.input("Breakpoint condition: "))]])
@ -196,10 +211,14 @@ 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

@ -0,0 +1,17 @@
local M = {}
local util = require("go.utils")
local log = util.log
local warn = require("go.utils").warn
local function create_boilerplate(name)
if not _GO_NVIM_CFG.go_boilplater_url then
return warn("go boilerplate url missing")
end
local path = name or vim.fn.expand("%:p:h")
local cmd = 'git clone --depth 1 --branch master ' .. _GO_NVIM_CFG.go_boilplater_url .. ' ' .. path
log(cmd)
vim.notify( "create boilerplate project: " .. vim.fn.system(cmd))
util.deletedir(path .. "/.git")
end
return {create_boilerplate=create_boilerplate}

@ -123,6 +123,43 @@ M.breakpt = function()
require("dap").toggle_breakpoint()
end
M.save_bks = function()
local bks = require("dap.breakpoints").get()
local all_bks = {}
if bks and next(bks) then
local cfg, fld = require("go.project_setup").setup_project()
for bufnr, bk in pairs(bks) do
local uri = vim.uri_from_bufnr(bufnr)
all_bks[uri] = bk
end
local bkfile = fld .. utils.sep() .. "breakpoints.lua"
local writeStr = "return " .. vim.inspect(all_bks)
local writeLst = vim.split(writeStr, "\n")
vim.fn.writefile(writeLst, bkfile, "b")
end
end
M.load_bks = function()
utils.load_plugin("nvim-dap", "dap")
local _, brkfile = require("go.project_setup").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
local stdout, stderr, handle
M.run = function(...)
local args = { ... }

@ -0,0 +1,41 @@
-- env fileread
local util = require("go.utils")
local log = util.log
local M = {}
local sep = require("go.utils").sep()
function M.envfile(f)
local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vim.fn.getcwd()
local goenv = workfolder .. sep .. (f or ".env")
if vim.fn.filereadable(goenv) == 1 then
return goenv
end
end
function M.load_env(env, setToEnv)
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 envs = {}
for _, line in ipairs(lines) do
for k, v in string.gmatch(line, "(%w+)=(%w+)") do
envs[k] = v
end
end
if setToEnv then
for key, val in pairs(envs) do
vim.fn.setenv(key, val)
end
end
return envs
end
M.load_project()
return M

@ -14,7 +14,6 @@ local url = {
iferr = "github.com/koron/iferr",
impl = "github.com/josharian/impl",
fillstruct = "github.com/davidrjenni/reftools/cmd/fillstruct",
fixplurals = "github.com/davidrjenni/reftools/cmd/fixplurals",
fillswitch = "github.com/davidrjenni/reftools/cmd/fillswitch",
dlv = "github.com/go-delve/delve/cmd/dlv",
ginkgo = "github.com/onsi/ginkgo/ginkgo",

@ -78,7 +78,7 @@ end
local M = {}
function M.client()
local clients = vim.lsp.get_active_clients()
local clients = vim.lsp.get_active_clients()
for _, cl in pairs(clients) do
if cl.name == "gopls" then
return cl
@ -98,6 +98,12 @@ function M.config()
if type(_GO_NVIM_CFG.lsp_on_attach) == "function" then
gopls.on_attach = _GO_NVIM_CFG.lsp_on_attach
end
if _GO_NVIM_CFG.lsp_on_client_start and type( type(_GO_NVIM_CFG.lsp_on_attach)) == "function" then
gopls.on_attach = function(client, bufnr)
gopls.on_attach(client, bufnr)
_GO_NVIM_CFG.lsp_on_client_start(client, bufnr)
end
end
if _GO_NVIM_CFG.gopls_cmd then
gopls.cmd = _GO_NVIM_CFG.gopls_cmd

@ -0,0 +1,69 @@
-- 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_project()
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"
if vim.fn.filereadable(gocfg) == 1 then
local f = assert(loadfile(gocfg))
_GO_NVIM_CFG = vim.tbl_deep_extend("force", _GO_NVIM_CFG, f())
else
return false
end
end
M.load_project()
return M

@ -495,4 +495,60 @@ function util.restart(cmd_args)
end
end
util.deletedir = function(dir)
local lfs = require("lfs")
for file in lfs.dir(dir) do
local file_path = dir .. "/" .. file
if file ~= "." and file ~= ".." then
if lfs.attributes(file_path, "mode") == "file" then
os.remove(file_path)
print("remove file", file_path)
elseif lfs.attributes(file_path, "mode") == "directory" then
print("dir", file_path)
util.deletedir(file_path)
end
end
end
lfs.rmdir(dir)
util.log("remove dir", dir)
end
function util.file_exists(file)
local f = io.open(file, "rb")
if f then f:close() end
return f ~= nil
end
-- get all lines from a file, returns an empty
-- list/table if the file does not exist
function util.lines_from(file)
if not util.file_exists(file) then return {} end
local lines = {}
for line in io.lines(file) do
lines[#lines + 1] = line
end
return lines
end
function util.set_env(key, val)
end
function util.list_directory()
local fn = vim.fn
local dirs = fn.map(fn.glob(fn.fnameescape('./')..'/{,.}*/', 1, 1), 'fnamemodify(v:val, ":h:t")')
end
function util.set_nulls()
if _GO_NVIM_CFG.null_ls_document_formatting_disable then
local query = {}
if type( _GO_NVIM_CFG.null_ls_document_formatting_disable) ~= 'boolean'
then
query = _GO_NVIM_CFG.null_ls_document_formatting_disable
end
local ok, nulls = pcall(require, "null-ls")
if ok then
nulls.disable(query)
end
end
end
return util

Loading…
Cancel
Save