go.nvim/README.md

830 lines
33 KiB
Markdown
Raw Normal View History

2021-04-19 06:09:39 +00:00
# go.nvim
2021-03-10 12:15:06 +00:00
A modern go neovim plugin based on treesitter, nvim-lsp and dap debugger. It is written in Lua and async as much as possible.
2021-03-10 13:21:01 +00:00
PR & Suggestions welcome.
2021-03-12 02:54:08 +00:00
The plugin covers most features required for a gopher.
2022-05-13 09:27:02 +00:00
- 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
2022-06-18 05:43:07 +00:00
[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)
- Runtime lint/vet/compile: Supported by lsp (once you setup up your lsp client), GoLint with golangci-lint also supported
2021-03-12 02:54:08 +00:00
- Build/Make/Test: Go.nvim provides supports for these by an async job wrapper.
2022-06-22 03:53:26 +00:00
- Test coverage: run test coverage and show coverage sign and function metrics
2022-01-15 10:06:07 +00:00
- Dlv Debug: with [nvim-dap](https://github.com/mfussenegger/nvim-dap) and [Dap UI](https://github.com/rcarriga/nvim-dap-ui).
- Load vscode launch configuration
2021-12-07 01:05:35 +00:00
- Unit test: generate unit test framework with [gotests](https://github.com/cweill/gotests). Run test with
richgo/ginkgo/gotestsum/go test
2022-05-10 05:20:16 +00:00
- 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
2021-03-12 02:54:08 +00:00
- Comments: Add autodocument for your package/function/struct/interface. This feature is unique and can help you suppress golint
errors...
2022-01-10 04:47:21 +00:00
- Go to alternative go file (between test and source)
- Test with ginkgo, richgo inside floaterm (to enable floaterm, guihua.lua has to be installed)
2022-04-30 02:47:41 +00:00
- Go 1.18 support, configure your go to `go1.18` in config
2022-06-18 05:43:07 +00:00
- 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
treesitter AST or go AST. It is fast and accurate.
2022-05-25 04:36:34 +00:00
- GoCheat get go cheatsheet from [cheat.sh](https://cheat.sh/).
2022-06-16 04:10:49 +00:00
- Smart build tag detection when debug/run tests (e.g. `//go:build integration`)
- Generate mocks with mockgen
2021-03-10 12:15:06 +00:00
## Installation
2021-03-10 12:15:06 +00:00
2021-12-21 11:04:45 +00:00
Use your favorite package manager to install. The dependency `treesitter` (and optionally, treesitter-objects)
should be installed the first time you use it.
Also Run `TSInstall go` to install the go parser if not installed yet.
2022-05-10 05:20:16 +00:00
`sed` is recommand to run this plugin.
### [vim-plug](https://github.com/junegunn/vim-plug)
```vim
Plug 'ray-x/go.nvim'
2022-05-28 10:54:45 +00:00
Plug 'ray-x/guihua.lua' ; recommanded if need floating window support
```
### [packer.nvim](https://github.com/wbthomason/packer.nvim)
```lua
use 'ray-x/go.nvim'
2022-05-28 10:54:45 +00:00
use 'ray-x/guihua.lua' -- recommanded if need floating window support
```
2022-05-28 10:54:45 +00:00
Make sure the `$GOPATH/bin` path is added to your `$PATH` environment variable. To check this you can run
2021-09-18 11:14:02 +00:00
```bash
echo $PATH | grep "$GOPATH/bin"
```
If nothing shows up, you can add the following to your shell config file:
2021-09-18 11:14:02 +00:00
```bash
export PATH=$PATH:$GOPATH/bin
```
2021-09-18 11:14:02 +00:00
Add format in your vimrc.
2021-09-18 11:14:02 +00:00
```vim
autocmd BufWritePre *.go :silent! lua require('go.format').gofmt()
```
2021-03-12 02:54:08 +00:00
To startup/setup the plugin
2021-03-10 12:15:06 +00:00
```lua
require('go').setup()
```
2022-05-13 09:27:02 +00:00
2021-03-10 12:15:06 +00:00
2021-04-19 06:09:39 +00:00
## Screenshots
### Add comments
![auto comments](https://github.com/ray-x/files/blob/master/img/go.nvim/comment.gif?raw=true)
### Add/Remove tags
![auto tag](https://github.com/ray-x/files/blob/master/img/go.nvim/tags.gif?raw=true)
2021-11-24 09:42:44 +00:00
### GoTest in floating term
![gotest](https://user-images.githubusercontent.com/1681295/143160335-b8046ffa-82cd-4d84-af3e-3b0dbb4c609e.png)
Use:
```vim
:GoTermClose
```
To close the floating term.
2021-04-21 11:24:07 +00:00
## refactor gorename
gorename as an alternative to gopls rename as it supports rename across packages
2022-06-01 11:29:34 +00:00
Note: use with care
Command: GoRename
2021-04-21 11:24:07 +00:00
2021-03-10 12:15:06 +00:00
## code format
nvim-lsp support goimport by default.
2021-03-10 13:21:01 +00:00
2021-03-10 12:15:06 +00:00
```vim
autocmd BufWritePre (InsertLeave?) <buffer> lua vim.lsp.buf.formatting_sync(nil,500)
```
2021-09-25 02:44:52 +00:00
The plugin provides code format, by default is goline + gofumpt (stricter version of gofmt)
2021-03-10 12:15:06 +00:00
2021-07-26 10:17:50 +00:00
Use following code to format go code
2021-09-18 11:14:02 +00:00
2021-03-10 12:15:06 +00:00
```lua
2022-01-10 21:44:14 +00:00
require("go.format").gofmt() -- gofmt only
2021-07-26 10:17:50 +00:00
require("go.format").goimport() -- goimport + gofmt
2021-03-10 12:15:06 +00:00
```
2022-01-10 21:44:14 +00:00
### Format on save
To config format on save, add one of the following to your init.lua:
#### Run gofmt on save
2021-07-26 10:17:50 +00:00
2021-09-18 11:14:02 +00:00
```lua
2022-01-10 21:44:14 +00:00
-- Run gofmt on save
2021-07-26 10:17:50 +00:00
vim.api.nvim_exec([[ autocmd BufWritePre *.go :silent! lua require('go.format').gofmt() ]], false)
2022-01-10 21:44:14 +00:00
```
2021-07-26 10:17:50 +00:00
2022-01-10 21:44:14 +00:00
#### Run gofmt + goimport on save
```lua
-- Run gofmt + goimport on save
vim.api.nvim_exec([[ autocmd BufWritePre *.go :silent! lua require('go.format').goimport() ]], false)
2021-07-26 10:17:50 +00:00
```
2021-07-15 09:18:04 +00:00
## Auto fill
Note: auto fill struct also supported by gopls lsp-action
2021-04-23 06:31:00 +00:00
2021-10-23 09:26:11 +00:00
| command | Description |
| ------------ | ------------------------------------------------------------- |
| GoFillStruct | auto fill struct |
| GoFillSwitch | fill switch |
| GoIfErr | Add if err |
| GoFixPlurals | change func foo(b int, a int, r int) -> func foo(b, a, r int) |
2021-07-15 09:18:04 +00:00
2022-05-15 12:25:50 +00:00
![GoFixPlurals Youtube video](https://www.youtube.com/watch?v=IP67Gkb5-qA)
2021-07-15 09:18:04 +00:00
```go
package foo
2021-07-15 09:18:04 +00:00
import "io"
func Foo() (io.Reader, error) { // the cursor on this line to add if err statement
}
```
2021-04-23 06:31:00 +00:00
![auto struct](https://github.com/ray-x/files/blob/master/img/go.nvim/fstruct.gif?raw=true)
2021-03-10 12:15:06 +00:00
## Textobject
Supported by treesitter. TS provided better parse result compared to regular expression.
See the example [treesitter config file](https://github.com/ray-x/go.nvim#text-object) on how to setup
textobjects. Also with treesitter-objects, you can move, swap the selected blocks of codes, which is fast and accurate.
2022-02-16 10:25:21 +00:00
`go.nvim` will load textobject with treesiteter, with default keybindings, if you what to set it up yourself, you can
set `textobject` to false.
2021-03-10 12:15:06 +00:00
2021-12-07 00:51:58 +00:00
## Go binaries install and update
The following go binaries are used in `go.nvim` (depends on your setup):
2021-12-07 00:51:58 +00:00
- gofumpt
- golines
- goimports
- gorename
- gomodifytags
- gotests
- iferr
- impl
- fillstruct
- fillswitch
- dlv
- ginkgo
- richgo
Normally if you run `GoFmt` and the configured binary (e.g. golines) was not installed, the plugin will install it for you. But the
first run of `GoFmt` may fail. It is recommended to run `GoInstallBinaries` to install all binaries before using the plugin.
2021-12-07 00:51:58 +00:00
| command | Description |
| ------------------------------ | ------------------------------------------------------------------------------------------------------------------- |
| GoInstallBinary go_binary_name | use `go install go_binary_url@latest` to install tool, if installed will skip |
| GoUpdateBinary go_binary_name | use `go install go_binary_url@latest` Will force re-install if already installed, otherwise same as GoInstallBinary |
| GoInstallBinaries | use `go install` to install all tools, skip the ones installed |
| GoUpdateBinaries | use `go install` to update all tools to the latest version |
2021-12-06 10:32:23 +00:00
2021-03-10 12:15:06 +00:00
## Build and test
2021-11-23 23:13:40 +00:00
| command | Description |
| --------------------------------------------- | ------------------------------------------------------------------------ |
2022-05-15 12:25:50 +00:00
| GoMake | async make, use with other commands |
2021-11-23 23:13:40 +00:00
| GoBuild | |
| GoGenerate | |
2022-05-16 23:47:45 +00:00
| GoRun {args} | e.g. GoRun equal to `go run .`; or `GoRun ./cmd` equal to `go run ./cmd, Additional args: -F run in floaterm` |
2022-05-15 12:25:50 +00:00
| GoStop {job_id} | `stop the job started with GoRun` |
2021-11-23 23:13:40 +00:00
| GoTest | go test ./... |
| GoTest -c | go test -c current_file_path |
2022-05-30 14:04:52 +00:00
| GoTest -n | test nearest, see GoTestFunc |
| GoTest -f | test current file, see GoTestFile |
| GoTest -p | test current package, see GoTestPkg |
| GoTest -t yourtags | go test ./... -tags=yourtags, see notes |
| GoTest package_path -t yourtags | go test packagepath -tags=yourtags |
| GoTest package_path -t yourtags other_args | go test packagepath -tags=yourtags other_args |
2021-11-23 23:13:40 +00:00
| GoLint | golangci-lint |
| GoGet {package_url} | go get package_url and restart gopls. Note1 |
| GoVet | go vet |
2021-11-23 23:13:40 +00:00
| GoCoverage | go test -coverprofile |
2022-06-05 22:24:55 +00:00
| GoCoverage -f coverage_file_name | load coverage file |
| GoCoverage {flags} | -t : toggle, -r: remove signs, -R remove sings from all files, -m show metrics|
| GoTermClose | `closes the floating term` |
Note:
1. if package_url not provided, will check current line is a valid package url or not, if it is valid, will
fetch current url
2. tags: if ``//+build tags`` exist it will be added automatically
2021-08-25 15:42:17 +00:00
Show test coverage:
<img width="479" alt="GoTestCoverage" src="https://user-images.githubusercontent.com/1681295/130821038-fa2545c6-16f6-4448-9a0c-91a1ab333750.png">
2021-03-12 02:54:08 +00:00
Provided wrapper for gobulild/test etc with async make
Also suggest to use [vim-test](https://github.com/vim-test/vim-test), which can run running tests on different
granularities.
2021-03-10 12:15:06 +00:00
2021-03-12 02:54:08 +00:00
## Unit test with [gotests](https://github.com/cweill/gotests) and testify
2021-03-10 12:15:06 +00:00
Support table based unit test auto generate, parse current function/method name using treesitter
2021-11-23 23:13:40 +00:00
| command | Description |
| ------------------------ | ------------------------------------------------------- |
| GoTestFunc | run test for current func |
2022-05-25 04:36:34 +00:00
| GoTestFunc -s | select the test function you want to run |
2021-11-23 23:13:40 +00:00
| GoTestFunc -tags=yourtag | run test for current func with `-tags yourtag` option |
| GoTestFile | run test for current file |
| GoTestFile -tags=yourtag | run test for current folder with `-tags yourtag` option |
| GoTestPkg | run test for current package/folder |
| GoTestPkg -tags=yourtag | run test for current folder with `-tags yourtag` option |
| GoAddTest [-parallel] | Add test for current func |
| GoAddExpTest [-parallel] | Add tests for exported funcs |
| GoAddAllTest [-parallel] | Add tests for all funcs |
2021-11-23 23:13:40 +00:00
GoTestXXX Arguments
2022-05-15 12:25:50 +00:00
| arguments | Description |
2022-05-15 12:25:50 +00:00
| ------------------------ | ------------------------------------------------------- |
| -v | verbose mode |
| -c | compile |
| -t | tags |
| -b | bench |
| -F | floaterm mode |
2021-11-23 23:13:40 +00:00
Note: For GoTestXXX
2022-05-15 12:25:50 +00:00
You can add available arguments e.g. `GoTest -tags=integration ./internal/web -bench=. -count=1 -`
2022-05-25 04:36:34 +00:00
## GoCheat
Show cheat.sh for api in neovim new buffer. e.g. `GoCheat `sort
2021-09-18 11:14:02 +00:00
## GoDoc
2021-09-18 11:14:02 +00:00
Show go doc for api in neovim floating window. e.g. `GoDoc fmt.Println`
2021-03-10 12:15:06 +00:00
2021-09-18 11:14:02 +00:00
![Godoc](https://user-images.githubusercontent.com/1681295/133886804-cc110fae-6fbf-4218-9c22-07fc9d6a64d2.jpg)
2022-01-11 11:37:33 +00:00
If no argument provided, fallback to lsp.hover()
## GoPkgOutline
A symbole outline for all symbols (var, const, func, struct, interface etc) inside a package
You can still use navigator or sidebar plugins (e.g. vista, symbols-outline) to check outline within a file. But it
is more useful for go to check the symbols in a package, as those symbols are visuals inside package, also the
method can be defined in different source file.
Command format:
GoPkgOutline {options}
options:
-f: show in floatwing window (default side panel, both require quihua.lua)
-p package_name: the package you want to list. e.g. GoPkgOutline -p json; default package is current file's package
If guihua not installed fallback to loclist
<img width="902" alt="image" src="https://user-images.githubusercontent.com/1681295/175231905-82df4e4b-a508-4bb8-b878-9f0029643005.png">
2021-09-18 11:14:02 +00:00
## Modifytags
2021-03-12 02:54:08 +00:00
Modify struct tags by [`gomodifytags`](https://github.com/fatih/gomodifytags) and treesitter
2021-03-10 12:15:06 +00:00
2021-09-18 11:14:02 +00:00
| command | Description |
| ---------- | ----------- |
| GoAddTag | |
| GoRmTag | |
| GoClearTag | |
2021-03-10 12:15:06 +00:00
## GoFmt
2021-09-25 02:44:52 +00:00
nvim-lsp support goimport by default. The plugin provided a new formatter, goline + gofumpt (stricter version of
gofmt)
2021-03-10 12:15:06 +00:00
2022-05-15 12:25:50 +00:00
| command | Description |
| --------------------- | --------------------------- |
2022-07-05 22:28:29 +00:00
| GoFmt {opts} | default: gofumpt |
| GoImport | default: goimport |
2022-05-15 12:25:50 +00:00
| GoImport package_path | gopls add_import package |
{opts} : ``-a`` format all buffers
2021-07-14 15:35:38 +00:00
## GoImpl
generate method stubs for implementing an interface
Usage:
2021-12-22 04:01:15 +00:00
````
2021-07-14 15:35:38 +00:00
:GoImpl {receiver} {interface}
2021-12-22 04:01:15 +00:00
Also you can put cursor on struct and run
```vim
:GoImpl {interface}
````
2021-07-14 15:35:38 +00:00
e.g:
```
:GoImpl f *File io.Reader
```
or simply put your cursor in a struct and do
```
:GoImpl io.Reader
```
2021-07-14 15:35:38 +00:00
## Debug
2021-09-18 11:14:02 +00:00
| command | Description |
| ---------------- | ------------------------------------------------ |
| GoDebug | start debug session, Note 1 |
2022-06-30 00:23:56 +00:00
| GoDebug -h | show helps info |
| GoDebug -c | compile only |
| GoDebug -t | start debug session for go test file, Note 2 |
2022-06-30 00:23:56 +00:00
| GoDebug -R | restart debug session |
2022-05-15 12:25:50 +00:00
| GoDebug -n | start debug session for nearest go test function |
2022-06-30 00:23:56 +00:00
| GoDebug -p | launch package test and start debug |
2022-05-15 12:25:50 +00:00
| GoDebug -a | attach to remote process |
| GoDebug -s | stop debug session and unmap debug keymap |
| GoDbgKeys | show debug keymaps in a floating window (guihua) |
2022-05-15 12:25:50 +00:00
| GoBreakToggle | GoDebug -b |
2022-07-02 23:11:26 +00:00
| GoDbgStop | Same as GoDebug -s |
| GoDbgContinue | Continue debug session |
| BreakCondition | conditional break |
Notes:
1. Without any argument, will check if launch.json existed or not, if existed, using launch.json and popup input.
If launch.json not existed, will start debug session for current file, if current file is package main will run
main(), else will start debug package test
2. with -t option, if current file is not test file, will switch to test file and run test for current function
## Switch between go and test file
2021-09-18 11:14:02 +00:00
| command | Description |
| ---------------- | ------------------------------------------------------- |
| GoAlt / GoAlt! | open alternative go file (use ! to create if not exist) |
| GoAltS / GoAltS! | open alternative go file in split |
| GoAltV / GoAltV! | open alternative go file in vertical split |
2022-07-05 22:28:29 +00:00
## Go Mock
go mock with mockgen is supported
| command | Description |
| ---------------- | ------------------------------------------------------- |
| GoMockGen | default: generate mocks for current file |
options:
-s source mode(default)
-i interface mode, provide interface name or put cursor on interface
-p package name default: mocks
-d destination directory, default: ./mocks
2021-03-10 12:15:06 +00:00
## Comments and Doc
Auto doc (to suppress golang-lint warning), generate comments by treesitter parsing result
```go
type GoLintComplaining struct{}
```
2021-03-10 13:21:01 +00:00
2021-03-10 13:26:55 +00:00
And run
2021-03-10 12:15:06 +00:00
```lua
2021-03-10 13:26:55 +00:00
lua.require('go.comment').gen() -- or your faviourite key binding and setup placeholder "no more complaint ;P"
2021-03-10 12:15:06 +00:00
```
2021-03-10 13:21:01 +00:00
2021-03-10 12:15:06 +00:00
The code will be:
2021-03-10 13:21:01 +00:00
2021-03-10 12:15:06 +00:00
```go
// GoLintComplaining struct no more complaint ;P
type GoLintComplaining struct{}
```
2022-05-15 12:25:50 +00:00
| command | Description |
| ---------------- | ------------------------------------------------------- |
| GoCmt | Add comment |
2021-03-10 12:15:06 +00:00
2022-05-15 12:25:50 +00:00
## GoModTidy
2022-02-21 22:46:59 +00:00
2022-05-15 12:25:50 +00:00
| command | Description |
| ---------------- | ------------------------------------------------------- |
| GoModInit | run `go mod init` and restart gopls |
| GoModTidy | run `go mod tidy` and restart gopls |
| GoModVendor | run `go mod vendor` and restart gopls |
2022-02-21 22:46:59 +00:00
2022-05-15 12:25:50 +00:00
run `go mod tidy` and restart gopls
2021-11-13 03:27:06 +00:00
2021-03-10 12:15:06 +00:00
## LSP
Nvim-lsp is good enough for a gopher. If you looking for a better GUI. You can install
[navigator](https://github.com/ray-x/navigator.lua), or lspsaga, and lsp-utils etc.
The goal of go.nvim is more provide unique functions releated to gopls instead of a general lsp gui client.
The lsp config in go.nvim has a none default setup and contains some improvement and I would suggest you to use.
2021-03-10 12:15:06 +00:00
2022-02-17 02:32:37 +00:00
## LSP cmp support
2022-05-15 12:25:50 +00:00
2022-02-17 02:32:37 +00:00
The latest version enabled lsp snippets (and other setups) by default. In case you need flowing the setup from cmp
README.md, please use flowing command:
```lua
local capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities())
require('go').setup({
-- other setups ....
lsp_cfg = {
capabilities = capabilities,
-- other setups
},
})
```
## LSP CodeLens
2021-09-18 11:14:02 +00:00
Gopls supports code lens. To run gopls code lens action `GoCodeLenAct`
Note: codelens need to be enabled in gopls, check default config in
## LSP CodeActions
You can use native code action provided by lspconfig. If you installed guihua, you can also use a GUI version of
code action `GoCodeAction`
2021-03-10 12:15:06 +00:00
## Lint
2021-03-12 02:54:08 +00:00
Supported by LSP, also GoLint command (by calling golangcl-lint) if you need background golangci-lint check, you can
configure it with ALE
2021-03-10 12:15:06 +00:00
2021-07-10 11:04:24 +00:00
## Debug with dlv
2021-09-18 11:14:02 +00:00
2021-07-10 11:04:24 +00:00
Setup for Debug provided. Need Dap and Dap UI plugin
![dap](https://user-images.githubusercontent.com/1681295/125160289-743ba080-e1bf-11eb-804f-6a6d227ec33b.jpg)
GDB style key mapping is used
2021-09-18 11:14:02 +00:00
2021-07-10 11:04:24 +00:00
### Keymaps
2021-09-18 11:14:02 +00:00
| key | Description |
| --- | ---------------------------------------- |
| c | continue |
| n | next |
| s | step |
| o | stepout |
| S | cap S: stop debug |
| u | up |
| D | cap D: down |
| C | cap C: run to cursor |
| b | toggle breakpoint |
| P | cap P: pause |
| p | print, hover value (also in visual mode) |
2021-07-10 11:04:24 +00:00
### Moving from vscode-go debug
Please check [Vscode Launch configurations](https://code.visualstudio.com/docs/editor/debugging#_launch-configurations)
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.
#### Note:
Please use jsonls/null-ls check your launch.json is valid json file. Following syntax is not supported
- Trailing comma
- Comment
Here is a sample [launch.json](https://github.com/ray-x/go.nvim/blob/master/playground/sampleApp/.vscode/launch.json)
2021-07-10 11:04:24 +00:00
### Commands
| Command | Description |
| -------------- | ----------------------------------------------------------------------------------------------- |
| GoDebug | Start debugger, to debug test, run `GoDebug test`, to add addition args run `GoDebug arg1 arg2` |
| GoDebugConfig | Open launch.json file |
| GoBreakSave | save all breakpoints to project file |
| GoBreakLoad | load all breakpoints from project file |
2022-05-15 12:25:50 +00:00
| GoBreakToggle | toggle break point |
| BreakCondition | conditional break point |
| ReplRun | dap repl run_last |
| ReplToggle | dap repl toggle |
2021-07-10 11:04:24 +00:00
### Required DAP plugins
2021-09-18 11:14:02 +00:00
2021-07-10 11:04:24 +00:00
The plugin will setup debugger. But you need to install
2021-09-18 11:14:02 +00:00
- dap
- 'mfussenegger/nvim-dap'
- dap ui (optional)
2021-07-10 11:04:24 +00:00
2021-09-18 11:14:02 +00:00
- 'rcarriga/nvim-dap-ui'
- dap virtual text (optional)
- 'theHamsta/nvim-dap-virtual-text'
2021-07-10 11:04:24 +00:00
Also you can check telescope dap extension : nvim-telescope/telescope-dap.nvim
Sample vimrc for DAP
2021-09-18 11:14:02 +00:00
2021-07-10 11:04:24 +00:00
```viml
Plug 'mfussenegger/nvim-dap'
Plug 'rcarriga/nvim-dap-ui'
Plug 'theHamsta/nvim-dap-virtual-text'
" Plug 'nvim-telescope/telescope-dap.nvim'
```
## Commands
2021-09-18 11:14:02 +00:00
2021-07-10 11:04:24 +00:00
Check [go.lua](https://github.com/ray-x/go.nvim/blob/master/lua/go.lua) on all the commands provided
2021-03-10 12:15:06 +00:00
## configuration
2021-07-12 04:25:07 +00:00
Configure from lua suggested, The default setup:
2021-03-10 13:21:01 +00:00
2021-03-10 12:15:06 +00:00
```lua
require('go').setup({
2022-01-10 10:26:41 +00:00
go='go', -- go command, can be go[default] or go1.18beta1
2021-09-25 02:44:52 +00:00
goimport='gopls', -- goimport command, can be gopls[default] or goimport
fillstruct = 'gopls', -- can be nil (use fillstruct, slower) and gopls
2021-07-10 11:04:24 +00:00
gofmt = 'gofumpt', --gofmt cmd,
2022-07-05 22:28:29 +00:00
max_line_len = 128, -- 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
2022-05-15 12:25:50 +00:00
gotests_template = "", -- sets gotests -template parameter (check gotests for details)
gotests_template_dir = "", -- sets gotests -template_dir parameter (check gotests for details)
2021-07-10 11:04:24 +00:00
comment_placeholder = '' , -- comment_placeholder your cool placeholder e.g. ﳑ    
icons = {breakpoint = '🧘', currentpos = '🏃'}, -- setup to `false` to disable icons setup
2021-03-10 12:15:06 +00:00
verbose = false, -- output loginf in messages
lsp_cfg = false, -- true: use non-default gopls setup specified in go/lsp.lua
-- false: do nothing
-- if lsp_cfg is a table, merge table with with non-default gopls setup in go/lsp.lua, e.g.
-- lsp_cfg = {settings={gopls={matcher='CaseInsensitive', ['local'] = 'your_local_module_path', gofumpt = true }}}
2021-07-12 04:25:07 +00:00
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,
-- when lsp_cfg is true
-- if lsp_on_attach is a function: use this function as on_attach function for gopls
2022-04-20 11:24:21 +00:00
lsp_keymaps = true, -- set to false to disable gopls/lsp keymap
lsp_codelens = true, -- set to false to disable codelens, true by default, you can use a function
-- function(bufnr)
-- vim.api.nvim_buf_set_keymap(bufnr, "n", "<space>F", "<cmd>lua vim.lsp.buf.formatting()<CR>", {noremap=true, silent=true})
-- end
-- to setup a table of codelens
2021-07-12 04:25:07 +00:00
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,
lsp_document_formatting = true,
-- set to true: use gopls to format
-- false if you want to use other formatter tool(e.g. efm, nulls)
gopls_cmd = nil, -- if you need to specify gopls path and cmd, e.g {"/home/user/lsp/gopls", "-logfile","/var/log/gopls.log" }
gopls_remote_auto = true, -- add -remote=auto to gopls
dap_debug = true, -- set to false to disable dap
2022-01-12 05:02:40 +00:00
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.
2022-07-02 23:11:26 +00:00
-- windows: use visual studio keymap
2021-07-10 11:04:24 +00:00
dap_debug_gui = true, -- set to true to enable dap gui, highly recommand
dap_debug_vt = true, -- set to true to enable dap virtual text
build_tags = "tag1,tag2", -- set default build tags
textobjects = true, -- enable default text jobects through treesittter-text-objects
test_runner = 'go', -- one of {`go`, `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. :GoTermClose closes the floatterm
-- float term recommand if you use richgo/ginkgo with terminal color
2021-03-10 12:15:06 +00:00
})
```
2021-03-10 13:21:01 +00:00
You will need to add keybind yourself:
e.g
```lua
vim.cmd("autocmd FileType go nmap <Leader><Leader>l GoLint")
vim.cmd("autocmd FileType go nmap <Leader>gc :lua require('go.comment').gen()")
```
2022-05-28 10:54:45 +00:00
## Project setup
`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. You can check the [youtube video here](https://www.youtube.com/watch?v=XrxSUp0E9Qw) on how to use this feature.
```lua
-- .gonvim/init.lua 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 = true
}
```
This will override your global `go.nvim` setup
2021-11-13 03:27:06 +00:00
## Text object
I did not provide textobject support in the plugin. Please use treesitter textobject plugin.
My treesitter config:
```lua
require "nvim-treesitter.configs".setup {
incremental_selection = {
enable = enable,
keymaps = {
-- mappings for incremental selection (visual mappings)
init_selection = "gnn", -- maps in normal mode to init the node/scope selection
node_incremental = "grn", -- increment to the upper named parent
scope_incremental = "grc", -- increment to the upper scope (as defined in locals.scm)
node_decremental = "grm" -- decrement to the previous node
}
},
textobjects = {
-- syntax-aware textobjects
enable = enable,
lsp_interop = {
enable = enable,
peek_definition_code = {
["DF"] = "@function.outer",
["DF"] = "@class.outer"
}
},
keymaps = {
["iL"] = {
-- you can define your own textobjects directly here
go = "(function_definition) @function",
},
-- or you use the queries from supported languages with textobjects.scm
["af"] = "@function.outer",
["if"] = "@function.inner",
["aC"] = "@class.outer",
["iC"] = "@class.inner",
["ac"] = "@conditional.outer",
["ic"] = "@conditional.inner",
["ae"] = "@block.outer",
["ie"] = "@block.inner",
["al"] = "@loop.outer",
["il"] = "@loop.inner",
["is"] = "@statement.inner",
["as"] = "@statement.outer",
["ad"] = "@comment.outer",
["am"] = "@call.outer",
["im"] = "@call.inner"
},
move = {
enable = enable,
set_jumps = true, -- whether to set jumps in the jumplist
goto_next_start = {
["]m"] = "@function.outer",
["]]"] = "@class.outer"
},
goto_next_end = {
["]M"] = "@function.outer",
["]["] = "@class.outer"
},
goto_previous_start = {
["[m"] = "@function.outer",
["[["] = "@class.outer"
},
goto_previous_end = {
["[M"] = "@function.outer",
["[]"] = "@class.outer"
}
},
select = {
enable = enable,
keymaps = {
-- You can use the capture groups defined in textobjects.scm
["af"] = "@function.outer",
["if"] = "@function.inner",
["ac"] = "@class.outer",
["ic"] = "@class.inner",
-- Or you can define your own textobjects like this
["iF"] = {
python = "(function_definition) @function",
cpp = "(function_definition) @function",
c = "(function_definition) @function",
java = "(method_declaration) @function",
go = "(method_declaration) @function"
}
}
},
swap = {
enable = enable,
swap_next = {
["<leader>a"] = "@parameter.inner"
},
swap_previous = {
["<leader>A"] = "@parameter.inner"
}
}
}
}
```
2021-03-10 13:26:55 +00:00
## 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.
2021-09-18 11:14:02 +00:00
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.
2021-03-10 13:21:01 +00:00
2021-12-15 23:37:02 +00:00
## Integrate with nvim-lsp-installer
2021-12-15 23:37:02 +00:00
(suggested by @mattbailey)
2021-12-15 23:37:02 +00:00
```lua
local path = require 'nvim-lsp-installer.core.path'
2021-12-15 23:37:02 +00:00
local install_root_dir = path.concat {vim.fn.stdpath 'data', 'lsp_servers'}
require('go').setup({
gopls_cmd = {install_root_dir .. '/go/gopls'},
2022-05-15 12:25:50 +00:00
fillstruct = 'gopls',
2021-12-15 23:37:02 +00:00
dap_debug = true,
dap_debug_gui = true
})
```
If you want to use gopls setup provided by go.nvim
2021-12-15 23:37:02 +00:00
```lua
-- setup your go.nvim
-- make sure lsp_cfg is disabled
require('go').setup{...}
local lsp_installer_servers = require'nvim-lsp-installer.servers'
local server_available, requested_server = lsp_installer_servers.get_server("gopls")
if server_available then
requested_server:on_ready(function ()
local opts = require'go.lsp'.config() -- config() return the go.nvim gopls setup
requested_server:setup(opts)
end)
if not requested_server:is_installed() then
-- Queue the server to be installed
requested_server:install()
end
end
```
2021-07-12 04:25:07 +00:00
## Sample vimrc
2021-09-18 11:14:02 +00:00
The following vimrc will enable all features provided by go.nvim
2021-03-10 13:21:01 +00:00
2021-07-12 04:25:07 +00:00
```viml
set termguicolors
call plug#begin('~/.vim/plugged')
Plug 'neovim/nvim-lspconfig'
Plug 'nvim-treesitter/nvim-treesitter'
2021-07-12 04:27:55 +00:00
Plug 'mfussenegger/nvim-dap'
Plug 'rcarriga/nvim-dap-ui'
Plug 'theHamsta/nvim-dap-virtual-text'
2022-01-05 22:55:53 +00:00
Plug 'ray-x/guihua.lua' " float term, codeaction and codelens gui support
2021-07-12 04:27:55 +00:00
2021-07-12 04:25:07 +00:00
Plug 'ray-x/go.nvim'
call plug#end()
lua <<EOF
require 'go'.setup({
goimport = 'gopls', -- if set to 'gopls' will use golsp format
gofmt = 'gopls', -- if set to gopls will use golsp format
2021-08-14 00:27:55 +00:00
max_line_len = 120,
2021-07-12 04:25:07 +00:00
tag_transform = false,
test_dir = '',
comment_placeholder = '  ',
lsp_cfg = true, -- false: use your own lspconfig
lsp_gofumpt = true, -- true: set default gofmt in gopls format to gofumpt
lsp_on_attach = true, -- use on_attach from go.nvim
dap_debug = true,
})
local protocol = require'vim.lsp.protocol'
EOF
2021-03-10 13:21:01 +00:00
```
2021-07-12 04:25:07 +00:00
This will setup gopls with non default configure provided by go.nvim (Includes lspconfig default keymaps)
2022-01-15 11:18:19 +00:00
2022-05-28 10:54:45 +00:00
## Q & A:
2022-01-15 11:18:19 +00:00
Q: What is `Toggle gc annotation details`
2022-05-28 10:54:45 +00:00
2022-01-15 11:18:19 +00:00
A: This is a codelens message, please run codelens `GoCodeLenAct` and get more info