all repos

gopher.nvim @ cdb1cd0

Minimalistic plugin for Go development

gopher.nvim/README.md(view raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# gopher.nvim

[![Stand With Ukraine](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner-direct-single.svg)](https://stand-with-ukraine.pp.ua)

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.

## Install

Pre-dependency: [go](https://github.com/golang/go) (tested on 1.17 and 1.18)

```lua
use {
  "olexsmir/gopher.nvim",
  requires = { -- dependencies
    "nvim-lua/plenary.nvim",
    "nvim-treesitter/nvim-treesitter",
  },
}
```

Also, run `TSInstall go` if `go` parser if isn't installed yet.

## Config

By `.setup` function you can configure the plugin.

Note:

- `installer` does not install the tool in user set path

```lua
require("gopher").setup {
  commands = {
    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,
  },
}
```

### Named tests with testify (using map instead of slice for test cases)

```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({
  ensure_installed = {
    { "gotests", version = "develop" },
  }
})
```

Or by calling `vim.fn.jobstart`:

```lua
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:

```vim
:GoInstallDeps
```

It 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)

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
```

3. Run `go mod` command:

```vim
:GoMod tidy " Runs `go mod tidy`
:GoMod init asdf " Runs `go mod init asdf`
```

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:

```vim
:GoTestAdd
```

Generate all tests for all functions/methods in current file:

```vim
:GoTestsAll
```

Generate tests only for exported functions/methods in current file:

```vim
:GoTestsExp
```

7. Run `go generate` command;

```vim
" Run `go generate` in cwd path
:GoGenerate

" Run `go generate` for current file
:GoGenerate %
```

8. Generate doc comment

First set a cursor on **public** package/function/interface/struct and execute:

```vim
:GoCmt
```

9. Generate `if err`

Set cursor on the line with **err** and execute:

```vim
:GoIfErr
```

10. Setup nvim-dap for go in one line.

Notice: [nvim-dap](https://github.com/mfussenegger/nvim-dap) is required

```lua
require"gopher.dap".setup()
```

## Contributing

PRs are always welcome. See [CONTRIBUTING.md](./CONTRIBUTING.md)

## Thanks

- [go.nvim](https://github.com/ray-x/go.nvim)
- [nvim-dap-go](https://github.com/leoluz/nvim-dap-go)