Additionally, I've set gopls as the default tool for both gofmt and goimports. This integration will be quicker than executing a separate command in the background and analyzing its output.
2 months ago
18 changed files with 230 additions and 222 deletions
@ -10,28 +11,31 @@ 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), [starry.nvim](https://github.com/ray-x/starry.nvim). Also, there are quite a few listed in [awesome-neovim](https://github.com/rockerBOO/awesome-neovim)
- All the GoToXxx (E.g reference, implementation, definition, goto doc, peek code/doc etc) You need lspconfig setup. There are lots of posts on how to
set it up. You can also check my [navigator](https://github.com/ray-x/navigator.lua) gopls setup [lspconfig.lua](https://github.com/ray-x/navigator.lua/blob/master/lua/navigator/lspclient/clients.lua)
- Syntax highlight & Texobject: Native treesitter support is faster and more accurate. All you need is a theme support
- gopls commands: e.g. fillstruct, organize imports, list modules, list packages, gc_details, generate, change
signature, etc.
- Runtime lint/vet/compile: Supported by LSP (once you set up your LSP client), GoLint with golangci-lint also supported
- Build/Make/Test: Go.nvim provides support for these by an async job wrapper.
- Test coverage: run test coverage and show coverage sign and function metrics
- Dlv Debug: with [nvim-dap](https://github.com/mfussenegger/nvim-dap) and [Dap UI](https://github.com/rcarriga/nvim-dap-ui). Go adapter included, zero config for your debug setup.
- Dlv Debug: with [nvim-dap](https://github.com/mfussenegger/nvim-dap) and
[Dap UI](https://github.com/rcarriga/nvim-dap-ui). Go adapter included, zero config for your debug setup.
- Load vscode launch configuration
- Unit test: generate unit test framework with [gotests](https://github.com/cweill/gotests). Run test with
richgo/ginkgo/gotestsum/go test
- Add and remove tag for struct with tag modify(gomodifytags)
- Code format: Supports LSP format and GoFmt(with golines)
- CodeLens : gopls codelens and codelens action support
- Comments: Add autodocument for your package/function/struct/interface. This feature is unique and can help you suppress golint
errors...
- Comments: Add autodocument for your package/function/struct/interface. This feature is unique and can help you
suppress golint errors...
- Go to alternative go file (between test and source)
- Test with ginkgo, richgo inside floaterm (to enable floaterm, guihua.lua has to be installed)
- Code refactor made easy: GoFixPlural, FixStruct, FixSwitch, Add comment, IfErr, ModTidy, GoGet, extract function/block with codeactions... Most of the tools are built on top of
go.nvim support launch debuger from vscode-go .vscode/launch.json configurations
If launch.json is valid, run `GoDebug` will launch from the launch.json configuration.
for more info go.nvim support launch debuger from vscode-go .vscode/launch.json configurations If launch.json is valid,
run `GoDebug` will launch from the launch.json configuration.
### Inlay hints
@ -638,22 +622,18 @@ Here is a sample [launch.json](https://github.com/ray-x/go.nvim/blob/master/play
### Json to Go struct
- ["x]GoJson2Struct!
Visual select the json and run `GoJson2Struct youStructName`
-bang will put result to register `a`
- ["x]GoJson2Struct! Visual select the json and run `GoJson2Struct youStructName` -bang will put result to register `a`
if ["x] specified, will put get json from clipboard
### Load Env file
- GoEnv {filename}
By default load .env file in current directory, if you want to load other file, use {filename} option
- GoEnv {filename} By default load .env file in current directory, if you want to load other file, use {filename} option
### Generate return value
- GoGenReturn
create return value for current function
e.g. if we have
create return value for current function e.g. if we have
```go
func Foo() (int, error) {
@ -678,28 +658,23 @@ if err != nil {
### Rename modules
- Gomvp
Rename module name in under cursor
e.g.
Gomvp
Gomvp old_mod_name
Gomvp old_mod_name new_mod_name
- Gomvp Rename module name in under cursor e.g. Gomvp Gomvp old_mod_name Gomvp old_mod_name new_mod_name
### govulncheck
- GoVulnCheck {arguments}
Run govulncheck on current project
- GoVulnCheck {arguments} Run govulncheck on current project
### goenum
- Goenum {arguments}
Run goenum on current project
- Goenum {arguments} Run goenum on current project
### gonew
- GoNew {filename}
Create new go file. It will use template file. e.g. `GoNew ./pkg/string.go` will create string.go with template file
GoNew also support using `gonew` command to create new file with template file [gonew cli](https://go.dev/blog/gonew), e.g `GoNew hello package_name/folder` is same as `gonew golang.org/x/example/hello package_name/folder` if package_name/folder not provided, a hello project will be created in current folder
- GoNew {filename} Create new go file. It will use template file. e.g. `GoNew ./pkg/string.go` will create string.go
with template file GoNew also support using `gonew` command to create new file with template file
[gonew cli](https://go.dev/blog/gonew), e.g `GoNew hello package_name/folder` is same as
`gonew golang.org/x/example/hello package_name/folder` if package_name/folder not provided, a hello project will be
created in current folder
### ginkgo
@ -749,7 +724,6 @@ Sample vimrc for DAP
Plug 'rcarriga/nvim-dap-ui'
Plug 'theHamsta/nvim-dap-virtual-text'
" Plug 'nvim-telescope/telescope-dap.nvim'
```
## Commands
@ -778,8 +752,8 @@ require('go').setup({
disable_defaults = false, -- true|false when true set false to all boolean settings and replace all table
-- settings with {}
go='go', -- go command, can be go[default] or go1.18beta1
goimport='gopls', -- goimport command, can be gopls[default] or either goimport or golines if need to split long lines
gofmt = 'gofumpt', --gofmt cmd,
goimports ='gopls', -- goimports command, can be gopls[default] or either goimports or golines if need to split long lines
gofmt = 'gopls', -- gofmt through gopls: alternative is gofumpt, goimports, golines, gofmt, etc
max_line_len = 0, -- max line length in golines format, Target maximum line length for golines
tag_transform = false, -- can be transform option("snakecase", "camelcase", etc) check gomodifytags for details and more options
tag_options = 'json=omitempty', -- sets options sent to gomodifytags, i.e., json=omitempty
@ -792,7 +766,8 @@ require('go').setup({
-- false: do nothing
-- 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_gofumpt = true, -- true: set default gofmt in gopls format to gofumpt
-- false: do not set default gofmt in gopls format to gofumpt
lsp_on_attach = nil, -- nil: use on_attach function defined in go/lsp.lua,
-- when lsp_cfg is true
-- if lsp_on_attach is a function: use this function as on_attach function for gopls
@ -887,8 +862,7 @@ require('go').setup({
})
```
You will need to add keybind yourself:
e.g
You will need to add keybind yourself: e.g
```lua
vim.cmd("autocmd FileType go nmap <Leader><Leader>l GoLint")
@ -907,7 +881,7 @@ 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
goimports = "gopls", -- if set to 'gopls' will use gopls format, also goimports
gofmt = "gofumpt", -- if set to gopls will use gopls format
null_ls_document_formatting_disable = true
}
@ -917,8 +891,7 @@ This will override your global `go.nvim` setup
## Text object
I did not provide textobject support in the plugin. Please use treesitter textobject plugin.
My treesitter config:
I did not provide textobject support in the plugin. Please use treesitter textobject plugin. My treesitter config:
```lua
require "nvim-treesitter.configs".setup {
@ -1015,26 +988,38 @@ My treesitter config:
}
}
```
</details>
## LuaSnip supports
go.nvim provides a better snippet support for go.
Please check [snippets for all languages](https://github.com/ray-x/go.nvim/blob/master/lua/snips/all.lua)
and [snippets for go](https://github.com/ray-x/go.nvim/blob/master/lua/snips/go.lua)
go.nvim provides a better snippet support for go. Please check
[snippets for all languages](https://github.com/ray-x/go.nvim/blob/master/lua/snips/all.lua) and
[snippets for go](https://github.com/ray-x/go.nvim/blob/master/lua/snips/go.lua)
For a video demo, please check this:
[go.nvim new features work through](https://www.youtube.com/watch?v=tsLnEfYTgcM)
For a video demo, please check this: [go.nvim new features work through](https://www.youtube.com/watch?v=tsLnEfYTgcM)
If you are not familiar with luasnip, please checkout [LuaSnip Tutorial](https://www.youtube.com/watch?v=ub0REXjhpmk) and [TJ's Introduction to LuaSnip](https://www.youtube.com/watch?v=Dn800rlPIho)
If you are not familiar with luasnip, please checkout [LuaSnip Tutorial](https://www.youtube.com/watch?v=ub0REXjhpmk)
and [TJ's Introduction to LuaSnip](https://www.youtube.com/watch?v=Dn800rlPIho)
## Nvim LSP setup
go.nvim provided a better non-default setup for gopls (includes debounce, staticcheck, diagnosticsDelay etc)
This gopls setup provided by go.nvim works perfectly fine for most of the cases. You can also install [navigator.lua](https://github.com/ray-x/navigator.lua) which can auto setup all lsp clients and provides a better GUI.
This gopls setup provided by go.nvim works perfectly fine for most of the cases. You can also install
[navigator.lua](https://github.com/ray-x/navigator.lua) which can auto setup all lsp clients and provides a better GUI.
For diagnostic issue, you can use the default setup. There are also quite a few plugins that you can use to explore
issues, e.g. [navigator.lua](https://github.com/ray-x/navigator.lua),
[Nvim-tree](https://github.com/kyazdani42/nvim-tree.lua) and
[Bufferline](https://github.com/akinsho/nvim-bufferline.lua) also introduced lsp diagnostic hooks.
> [!IMPORTANT]
> I will integrate more gopls functions into go.nvim, please make sure you have the latest version installed
> Also, enable gopls experimental features if it is configure somewhere other than go.nvim
> Otherwise, set `lsp_cfg` to `true` in your go.nvim setup to enable gopls setup in go.nvim
For diagnostic issue, you can use the default setup. There are also quite a few plugins that you can use to explore issues, e.g. [navigator.lua](https://github.com/ray-x/navigator.lua), [folke/lsp-trouble.nvim](https://github.com/folke/lsp-trouble.nvim). [Nvim-tree](https://github.com/kyazdani42/nvim-tree.lua) and [Bufferline](https://github.com/akinsho/nvim-bufferline.lua) also introduced lsp diagnostic hooks.
<details>
<summary>Gopls default settings in go.nvim</summary>
@ -1121,6 +1106,9 @@ gopls = {
matcher = 'Fuzzy',
diagnosticsDelay = '500ms',
symbolMatcher = 'fuzzy',
semanticTokens = true,
noSemanticTokens = true, -- disable semantic string tokens so we can use treesitter highlight injection
['local'] = get_current_gomod(),
gofumpt = _GO_NVIM_CFG.lsp_gofumpt or false, -- true|false, -- turn on for new repos, gofmpt is good but also create code turmoils
buildFlags = { '-tags', 'integration' },
@ -1144,8 +1132,8 @@ gopls = {
end,
},
}
```
</details>
## Integrate with mason-lspconfig
@ -1154,16 +1142,13 @@ gopls = {
require("mason").setup()
require("mason-lspconfig").setup()
require('lspconfig').gopls.setup({
gopls_cmd = {install_root_dir .. '/go/gopls'},
dap_debug = true,
dap_debug_gui = true
-- your gopls setup
})
```
If you want to use gopls setup provided by go.nvim
```lua
-- setup your go.nvim
-- make sure lsp_cfg is disabled
require("mason").setup()
@ -1175,12 +1160,12 @@ require('go').setup{
local cfg = require'go.lsp'.config() -- config() return the go.nvim gopls setup
require('lspconfig').gopls.setup(cfg)
```
## Highlighting for gomod, gosum, gohtmltmpl, gotmpl, gotexttmpl
You can install treesitter parser for gomod and gosum
```vim
:TSInstall gomod gosum
```
@ -1210,7 +1195,6 @@ The plugin injects the tmpl to html syntax so you should see this: