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. 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 ```lua
use { {
"olexsmir/gopher.nvim", "olexsmir/gopher.nvim",
requires = { -- dependencies ft = "go",
dependencies = {
"nvim-lua/plenary.nvim", "nvim-lua/plenary.nvim",
"nvim-treesitter/nvim-treesitter", "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 >[!IMPORTANT]
>
By `.setup` function you can configure the plugin. > If you need more info look `:h gopher.nvim`
Note:
- `installer` does not install the tool in user set path
```lua ```lua
require("gopher").setup { require("gopher").setup {
@ -36,163 +43,189 @@ require("gopher").setup {
go = "go", go = "go",
gomodifytags = "gomodifytags", gomodifytags = "gomodifytags",
gotests = "~/go/bin/gotests", -- also you can set custom command path gotests = "~/go/bin/gotests", -- also you can set custom command path
impl = "impl",
iferr = "iferr",
}, },
gotests = { gotests = {
-- gotests doesn't have template named "default" so this plugin uses "default" to set the default template
template = "default", 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 ```lua
require("gopher").setup({ -- using mason-tool-installer
gotests = { require("mason-tool-installer").setup {
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({
ensure_installed = { ensure_installed = {
{ "gotests", version = "develop" }, { "gotests", version = "develop" },
} }
}) }
```
Or by calling `vim.fn.jobstart`: -- using `vim.fn.jobstart`
```lua
vim.fn.jobstart("go install github.com/cweill/gotests/...@develop") 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 ## Features
1. Installation requires this go tool: <!-- markdownlint-disable -->
<details>
<summary>
<b>Install plugin's go deps</b>
</summary>
```vim ```vim
:GoInstallDeps :GoInstallDeps
``` ```
It will install next tools: This will install next tools:
- [gomodifytags](https://github.com/fatih/gomodifytags) - [gomodifytags](https://github.com/fatih/gomodifytags)
- [impl](https://github.com/josharian/impl) - [impl](https://github.com/josharian/impl)
- [gotests](https://github.com/cweill/gotests) - [gotests](https://github.com/cweill/gotests)
- [iferr](https://github.com/koron/iferr) - [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: By default `json` tag will be added/removed, if not set:
```vim ```vim
:GoTagAdd json " For add json tag " add json tag
:GoTagRm yaml " For remove yaml tag :GoTagAdd json
" remove yaml tag
:GoTagRm yaml
``` ```
3. Run `go mod` command: ```lua
--- or you can use lua api
```vim require("gopher").tags.add "xml"
:GoMod tidy " Runs `go mod tidy` require("gopher").tags.rm "proto"
:GoMod init asdf " Runs `go mod init asdf`
``` ```
</details>
4. Run `go get` command <details>
<summary>
Link can have a `http` or `https` prefix. <b>Generating tests via [gotests](https://github.com/cweill)</b>
</summary>
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:
```vim ```vim
" Generate one test for a specific function/method(one under cursor)
:GoTestAdd :GoTestAdd
```
Generate all tests for all functions/methods in current file: " Generate all tests for all functions/methods in current file
```vim
:GoTestsAll :GoTestsAll
```
Generate tests only for exported functions/methods in current file: " Generate tests only for exported functions/methods in current file:
```vim
:GoTestsExp :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 ```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 :GoGenerate
" Run `go generate` for current file " run go generate for current file
:GoGenerate % :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: First set a cursor on **public** package/function/interface/struct and execute:
```vim ```vim
:GoCmt :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 ```vim
:GoIfErr :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 ```lua
require"gopher.dap".setup() require("gopher.dap").setup()
``` ```
</details>
## Contributing ## Contributing
@ -202,3 +235,4 @@ PRs are always welcome. See [CONTRIBUTING.md](./CONTRIBUTING.md)
- [go.nvim](https://github.com/ray-x/go.nvim) - [go.nvim](https://github.com/ray-x/go.nvim)
- [nvim-dap-go](https://github.com/leoluz/nvim-dap-go) - [nvim-dap-go](https://github.com/leoluz/nvim-dap-go)
- [iferr](https://github.com/koron/iferr)