docs: update readme

This commit is contained in:
Smirnov Oleksandr 2024-03-01 23:17:42 +02:00
parent 04eb2c3d76
commit 85f4ae9f57

236
README.md
View file

@ -4,31 +4,38 @@
Minimalistic plugin for Go development in Neovim written in Lua.
It's not an LSP tool, the main goal of this plugin is add go tooling support in Neovim.
It's **NOT** an LSP tool, the main goal of this plugin is add go tooling support in Neovim.
## Install
## Install (using [lazy.nvim](https://github.com/folke/lazy.nvim)
Pre-dependency: [go](https://github.com/golang/go) (tested on 1.17 and 1.18)
Pre-dependency:
- [go](https://github.com/golang/go) (tested on 1.17 and 1.18)
- Go treesitter parser, install by `:TSInstall go`
```lua
use {
{
"olexsmir/gopher.nvim",
requires = { -- dependencies
ft = "go",
dependencies = {
"nvim-lua/plenary.nvim",
"nvim-treesitter/nvim-treesitter",
"mfussenegger/nvim-dap", -- (optional) only if you use `gopher.dap`
},
-- (optional) update pluign's deps on every update
build = function()
vim.cmd.GoInstallDeps()
end,
---@type gopher.Config
opts = {},
}
```
Also, run `TSInstall go` if `go` parser if isn't installed yet.
## Configuratoin
## Config
By `.setup` function you can configure the plugin.
Note:
- `installer` does not install the tool in user set path
>[!IMPORTANT]
>
> If you need more info look `:h gopher.nvim`
```lua
require("gopher").setup {
@ -36,163 +43,189 @@ require("gopher").setup {
go = "go",
gomodifytags = "gomodifytags",
gotests = "~/go/bin/gotests", -- also you can set custom command path
impl = "impl",
iferr = "iferr",
},
gotests = {
-- gotests doesn't have template named "default" so this plugin uses "default" to set the default template
template = "default",
-- path to a directory containing custom test code templates
template_dir = nil,
-- switch table tests from using slice to map (with test name for the key)
-- works only with gotests installed from develop branch
named = false,
},
gotag = {
transform = "pascalcase"
}
}
```
### Named tests with testify (using map instead of slice for test cases)
>[!IMPORTANT]
>
> For named tests to work you have to install gotests from develop branch. Next code snippets could be placed into build step in Lazy plugin declatation
```lua
require("gopher").setup({
gotests = {
template = "testify",
named = true
}
})
```
For named tests to work you have to install gotests from develop branch, for example using [mason-tool-installer](https://github.com/WhoIsSethDaniel/mason-tool-installer.nvim):
```lua
require('mason-tool-installer').setup({
-- using mason-tool-installer
require("mason-tool-installer").setup {
ensure_installed = {
{ "gotests", version = "develop" },
}
})
```
}
Or by calling `vim.fn.jobstart`:
```lua
-- using `vim.fn.jobstart`
vim.fn.jobstart("go install github.com/cweill/gotests/...@develop")
```
If you're using `lazy.nvim` you can put in `build` function inside `setup()`
## Features
1. Installation requires this go tool:
<!-- markdownlint-disable -->
<details>
<summary>
<b>Install plugin's go deps</b>
</summary>
```vim
:GoInstallDeps
```
It will install next tools:
This will install next tools:
- [gomodifytags](https://github.com/fatih/gomodifytags)
- [impl](https://github.com/josharian/impl)
- [gotests](https://github.com/cweill/gotests)
- [iferr](https://github.com/koron/iferr)
- [dlv](github.com/go-delve/delve/cmd/dlv)
</details>
<details>
<summary>
<b>Add and remove tags for structs(via gomodifytags)</b>
</summary>
2. Modify struct tags:
By default `json` tag will be added/removed, if not set:
```vim
:GoTagAdd json " For add json tag
:GoTagRm yaml " For remove yaml tag
" add json tag
:GoTagAdd json
" remove yaml tag
:GoTagRm yaml
```
3. Run `go mod` command:
```vim
:GoMod tidy " Runs `go mod tidy`
:GoMod init asdf " Runs `go mod init asdf`
```lua
--- or you can use lua api
require("gopher").tags.add "xml"
require("gopher").tags.rm "proto"
```
</details>
4. Run `go get` command
Link can have a `http` or `https` prefix.
You can provide more than one package url:
```vim
:GoGet github.com/gorilla/mux
```
5. Interface implementation
Command syntax:
```vim
:GoImpl [receiver] [interface]
" Also you can put cursor on the struct and run:
:GoImpl [interface]
```
Example of usage:
```vim
" Example
:GoImpl r Read io.Reader
" or simply put your cursor in the struct and run:
:GoImpl io.Reader
```
6. Generate tests with [gotests](https://github.com/cweill/gotests)
Generate one test for a specific function/method:
<details>
<summary>
<b>Generating tests via [gotests](https://github.com/cweill)</b>
</summary>
```vim
" Generate one test for a specific function/method(one under cursor)
:GoTestAdd
```
Generate all tests for all functions/methods in current file:
```vim
" Generate all tests for all functions/methods in current file
:GoTestsAll
```
Generate tests only for exported functions/methods in current file:
```vim
" Generate tests only for exported functions/methods in current file:
:GoTestsExp
```
7. Run `go generate` command;
```lua
--- or you can use lua api
require("gopher").test.add()
```
</details>
<details>
<summary>
<b>Run commands like `go mod/get/etc` inside of nvim</b>
</summary>
```vim
" Run `go generate` in cwd path
:GoGet github.com/gorilla/mux
" Link can have a `http` or `https` prefix.
:GoGet https://github.com/lib/pq
" You can provide more than one package url:
:GoGet github.com/jackc/pgx/v5 github.com/google/uuid/
" go mod commands
:GoMod tidy
:GoMod init new-shiny-project
" go work commands
:GoWork sync
" run go generate in cwd
:GoGenerate
" Run `go generate` for current file
" run go generate for current file
:GoGenerate %
```
</details>
8. Generate doc comment
<details>
<summary>
<b>Interface implementation via [impl](https://github.com/josharian/impl)</b>
</summary>
Syntax of the command:
```vim
:GoImpl [receiver] [interface]
" also you can put cursor on the struct and run
:GoImpl [interface]
```
Usage examples:
```vim
:GoImpl r Read io.Reader
:GoImpl Write io.Writer
" or your can put cursor on the struct and run
:GoImpl io.Reader
```
</details>
<details>
<summary>
<b>Generate boilerplate for doc comments</b>
</summary>
First set a cursor on **public** package/function/interface/struct and execute:
```vim
:GoCmt
```
</details>
9. Generate `if err`
Set cursor on the line with **err** and execute:
<details>
<summary>
<b>Generate `if err != nil {` via [iferr](https://github.com/koron/iferr)</b>
</summary>
Set cursor on the line with `err` and execute
```vim
:GoIfErr
```
</details>
10. Setup nvim-dap for go in one line.
<details>
<summary>
<b>Setup [nvim-dap](https://github.com/mfussenegger/nvim-dap) for go in one line</b>
</summary>
Notice: [nvim-dap](https://github.com/mfussenegger/nvim-dap) is required
>[!IMPORTANT]
>
> [nvim-dap](https://github.com/mfussenegger/nvim-dap) has to be installed
```lua
require"gopher.dap".setup()
require("gopher.dap").setup()
```
</details>
## Contributing
@ -202,3 +235,4 @@ PRs are always welcome. See [CONTRIBUTING.md](./CONTRIBUTING.md)
- [go.nvim](https://github.com/ray-x/go.nvim)
- [nvim-dap-go](https://github.com/leoluz/nvim-dap-go)
- [iferr](https://github.com/koron/iferr)