add gonvim project file parser

pull/117/head
ray-x 2 years ago
parent dead9e2c70
commit 7c434918be

@ -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
@ -53,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()
@ -199,10 +208,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,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

@ -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,91 @@
-- 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 = "",
comment_placeholder = "",
icons = { breakpoint = "🧘", currentpos = "🏃" }, -- set to false to disable icons setup
verbose = false,
log_path = vim.fn.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
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)
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,
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 = "",
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",
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.
dap_vt = true, -- false, true and 'all frames'
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
textobjects = true, -- treesitter binding for text objects
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.
}
]]
-- 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.setup_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
return gocfg
else
local f = io.open(gocfg, "w")
f:write("return {}")
f:close()
end
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

@ -510,4 +510,42 @@ util.deletedir = function(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