blob: 1fa44bfa78c7132ee1823e39c2481ba21b0ac740 [file] [log] [blame] [view]
Ian Cottrell062dbae2019-05-09 10:35:27 -04001# Vim / Neovim
2
Ainar Garipov0f6027f2020-12-17 00:48:15 +03003* [vim-go](#vimgo)
4* [LanguageClient-neovim](#lcneovim)
5* [Ale](#ale)
6* [vim-lsp](#vimlsp)
7* [vim-lsc](#vimlsc)
8* [coc.nvim](#cocnvim)
9* [govim](#govim)
10* [Neovim v0.5.0+](#neovim)
11 * [Installation](#neovim-install)
12 * [Custom Configuration](#neovim-config)
13 * [Imports](#neovim-imports)
14 * [Omnifunc](#neovim-omnifunc)
15 * [Additional Links](#neovim-links)
16
17## <a href="#vimgo" id="vimgo">vim-go</a>
Ian Cottrell062dbae2019-05-09 10:35:27 -040018
19Use [vim-go] ver 1.20+, with the following configuration:
20
Rebecca Stamblerd78b04b2020-12-28 13:26:14 -050021```vim
Ian Cottrell062dbae2019-05-09 10:35:27 -040022let g:go_def_mode='gopls'
23let g:go_info_mode='gopls'
24```
25
Ainar Garipov0f6027f2020-12-17 00:48:15 +030026## <a href="#lcneovim" id="lcneovim">LanguageClient-neovim</a>
Ian Cottrell062dbae2019-05-09 10:35:27 -040027
28Use [LanguageClient-neovim], with the following configuration:
29
Rebecca Stamblerd78b04b2020-12-28 13:26:14 -050030```vim
Ian Cottrell062dbae2019-05-09 10:35:27 -040031" Launch gopls when Go files are in use
32let g:LanguageClient_serverCommands = {
33 \ 'go': ['gopls']
34 \ }
35" Run gofmt on save
36autocmd BufWritePre *.go :call LanguageClient#textDocument_formatting_sync()
37```
38
Ainar Garipov0f6027f2020-12-17 00:48:15 +030039## <a href="#ale" id="ale">Ale</a>
Ian Cottrell062dbae2019-05-09 10:35:27 -040040
41Use [ale]:
42
43```vim
44let g:ale_linters = {
Rebecca Stamblerd78b04b2020-12-28 13:26:14 -050045 \ 'go': ['gopls'],
46 \}
Ian Cottrell062dbae2019-05-09 10:35:27 -040047```
48
49see [this issue][ale-issue-2179]
50
Ainar Garipov0f6027f2020-12-17 00:48:15 +030051## <a href="#vimlsp" id="vimlsp">vim-lsp</a>
Ian Cottrell062dbae2019-05-09 10:35:27 -040052
53Use [prabirshrestha/vim-lsp], with the following configuration:
54
55```vim
56augroup LspGo
57 au!
58 autocmd User lsp_setup call lsp#register_server({
59 \ 'name': 'go-lang',
60 \ 'cmd': {server_info->['gopls']},
61 \ 'whitelist': ['go'],
62 \ })
63 autocmd FileType go setlocal omnifunc=lsp#complete
64 "autocmd FileType go nmap <buffer> gd <plug>(lsp-definition)
65 "autocmd FileType go nmap <buffer> ,n <plug>(lsp-next-error)
66 "autocmd FileType go nmap <buffer> ,p <plug>(lsp-previous-error)
67augroup END
68```
69
Ainar Garipov0f6027f2020-12-17 00:48:15 +030070## <a href="#vimlsc" id="vimlsc">vim-lsc</a>
Ian Cottrell062dbae2019-05-09 10:35:27 -040071
72Use [natebosch/vim-lsc], with the following configuration:
73
74```vim
75let g:lsc_server_commands = {
76\ "go": {
77\ "command": "gopls serve",
78\ "log_level": -1,
Ainar Garipov1ccc1102019-09-07 20:42:26 +030079\ "suppress_stderr": v:true,
Ian Cottrell062dbae2019-05-09 10:35:27 -040080\ },
81\}
82```
83
Ainar Garipov1ccc1102019-09-07 20:42:26 +030084The `log_level` and `suppress_stderr` parts are needed to prevent breakage from logging. See
Ainar Garipov846f8562019-10-17 10:08:14 +030085issues [#180](https://github.com/natebosch/vim-lsc/issues/180) and
86[#213](https://github.com/natebosch/vim-lsc/issues/213).
Ian Cottrell062dbae2019-05-09 10:35:27 -040087
Ainar Garipov0f6027f2020-12-17 00:48:15 +030088## <a href="#cocnvim" id="cocnvim">coc.nvim</a>
Ian Cottrell062dbae2019-05-09 10:35:27 -040089
90Use [coc.nvim], with the following `coc-settings.json` configuration:
91
92```json
93 "languageserver": {
94 "golang": {
95 "command": "gopls",
Robert Findleycf66aec2022-02-01 14:20:33 -050096 "rootPatterns": ["go.work", "go.mod", ".vim/", ".git/", ".hg/"],
Shengjing Zhu5fa5b172019-10-14 14:03:50 +080097 "filetypes": ["go"],
98 "initializationOptions": {
99 "usePlaceholders": true
100 }
Ian Cottrell062dbae2019-05-09 10:35:27 -0400101 }
102 }
103```
104
Robert Findleycf66aec2022-02-01 14:20:33 -0500105If you use `go.work` files, you may want to set the
106`workspace.workspaceFolderCheckCwd` option. This will force coc.nvim to search
107parent directories for `go.work` files, even if the current open directory has
108a `go.mod` file. See the
109[coc.nvim documentation](https://github.com/neoclide/coc.nvim/wiki/Using-workspaceFolders)
110for more details.
111
Shengjing Zhu5fa5b172019-10-14 14:03:50 +0800112Other [settings](settings.md) can be added in `initializationOptions` too.
113
Ian Cottrell062dbae2019-05-09 10:35:27 -0400114The `editor.action.organizeImport` code action will auto-format code and add missing imports. To run this automatically on save, add the following line to your `init.vim`:
115
116```vim
117autocmd BufWritePre *.go :call CocAction('runCommand', 'editor.action.organizeImport')
118```
119
Ainar Garipov0f6027f2020-12-17 00:48:15 +0300120## <a href="#govim" id="govim">govim</a>
Ian Cottrell062dbae2019-05-09 10:35:27 -0400121
122In vim classic only, use the experimental [`govim`], simply follow the [install steps][govim-install].
123
Ainar Garipov0f6027f2020-12-17 00:48:15 +0300124## <a href="#neovim" id="neovim">Neovim v0.5.0+</a>
Ellison Leãocd5a53e2020-04-07 18:57:22 +0000125
GGCristode447762021-07-09 13:24:30 +0000126To use the new native LSP client in Neovim, make sure you
127[install][nvim-install] Neovim v.0.5.0+,
Ainar Garipov34b80a02020-09-25 12:50:53 +0300128the `nvim-lspconfig` configuration helper plugin, and check the
129[`gopls` configuration section][nvim-lspconfig] there.
130
Ainar Garipov0f6027f2020-12-17 00:48:15 +0300131### <a href="#neovim-install" id="neovim-install">Installation</a>
132
133You can use Neovim's native plugin system. On a Unix system, you can do that by
134cloning the `nvim-lspconfig` repository into the correct directory:
135
136```sh
137dir="${HOME}/.local/share/nvim/site/pack/nvim-lspconfig/opt/nvim-lspconfig/"
138mkdir -p "$dir"
139cd "$dir"
140git clone 'https://github.com/neovim/nvim-lspconfig.git' .
141```
142
143### <a href="#neovim-config" id="neovim-config">Custom Configuration</a>
Ainar Garipov34b80a02020-09-25 12:50:53 +0300144
145You can add custom configuration using Lua. Here is an example of enabling the
146`unusedparams` check as well as `staticcheck`:
147
148```vim
149lua <<EOF
Ainar Garipov0f6027f2020-12-17 00:48:15 +0300150 lspconfig = require "lspconfig"
Simon Drake625c8712022-03-01 17:39:56 +0000151 util = require "lspconfig/util"
152
Ainar Garipov0f6027f2020-12-17 00:48:15 +0300153 lspconfig.gopls.setup {
Ainar Garipov34b80a02020-09-25 12:50:53 +0300154 cmd = {"gopls", "serve"},
Simon Drake625c8712022-03-01 17:39:56 +0000155 filetypes = {"go", "gomod"},
156 root_dir = util.root_pattern("go.work", "go.mod", ".git"),
Ainar Garipov34b80a02020-09-25 12:50:53 +0300157 settings = {
158 gopls = {
159 analyses = {
160 unusedparams = true,
161 },
162 staticcheck = true,
163 },
164 },
165 }
166EOF
167```
168
Ainar Garipov0f6027f2020-12-17 00:48:15 +0300169### <a href="#neovim-imports" id="neovim-imports">Imports</a>
Ainar Garipov34b80a02020-09-25 12:50:53 +0300170
Robert Findleyeec389d2023-02-13 10:28:41 -0500171Use the following configuration to have your imports organized on save using
172the logic of `goimports`. Note: this requires Neovim v0.7.0 or later.
Ainar Garipov34b80a02020-09-25 12:50:53 +0300173
Raphael668845e2023-02-13 14:37:13 +0000174```lua
175vim.api.nvim_create_autocmd('BufWritePre', {
176 pattern = '*.go',
177 callback = function()
178 vim.lsp.buf.code_action({ context = { only = { 'source.organizeImports' } }, apply = true })
Ainar Garipov34b80a02020-09-25 12:50:53 +0300179 end
Raphael668845e2023-02-13 14:37:13 +0000180})
Ainar Garipov34b80a02020-09-25 12:50:53 +0300181```
182
Ainar Garipov0f6027f2020-12-17 00:48:15 +0300183### <a href="#neovim-omnifunc" id="neovim-omnifunc">Omnifunc</a>
Ainar Garipov34b80a02020-09-25 12:50:53 +0300184
Robert Findleyeec389d2023-02-13 10:28:41 -0500185In Neovim v0.8.1 and later if you don't set the option `omnifunc`, it will auto
186set to `v:lua.vim.lsp.omnifunc`. If you are using an earlier version, you can
187configure it manually:
Raphael668845e2023-02-13 14:37:13 +0000188
189```lua
190local on_attach = function(client, bufnr)
191 -- Enable completion triggered by <c-x><c-o>
192 vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')
193end
194require('lspconfig').gopls.setup({
195 on_attach = on_attach
196})
Ainar Garipov34b80a02020-09-25 12:50:53 +0300197```
198
Ainar Garipov0f6027f2020-12-17 00:48:15 +0300199### <a href="#neovim-links" id="neovim-links">Additional Links</a>
Ainar Garipov34b80a02020-09-25 12:50:53 +0300200
201* [Neovim's official LSP documentation][nvim-docs].
Ellison Leãocd5a53e2020-04-07 18:57:22 +0000202
Ian Cottrell062dbae2019-05-09 10:35:27 -0400203[vim-go]: https://github.com/fatih/vim-go
204[LanguageClient-neovim]: https://github.com/autozimu/LanguageClient-neovim
205[ale]: https://github.com/w0rp/ale
206[ale-issue-2179]: https://github.com/w0rp/ale/issues/2179
207[prabirshrestha/vim-lsp]: https://github.com/prabirshrestha/vim-lsp/
208[natebosch/vim-lsc]: https://github.com/natebosch/vim-lsc/
209[natebosch/vim-lsc#180]: https://github.com/natebosch/vim-lsc/issues/180
210[coc.nvim]: https://github.com/neoclide/coc.nvim/
211[`govim`]: https://github.com/myitcv/govim
Ainar Garipov1ccc1102019-09-07 20:42:26 +0300212[govim-install]: https://github.com/myitcv/govim/blob/master/README.md#govim---go-development-plugin-for-vim8
Ainar Garipov34b80a02020-09-25 12:50:53 +0300213[nvim-docs]: https://neovim.io/doc/user/lsp.html
214[nvim-install]: https://github.com/neovim/neovim/wiki/Installing-Neovim
Akhilcadd57e2021-11-27 06:22:56 +0000215[nvim-lspconfig]: https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md#gopls
Ainar Garipov34b80a02020-09-25 12:50:53 +0300216[nvim-lspconfig-imports]: https://github.com/neovim/nvim-lspconfig/issues/115