4 * [LanguageClient-neovim](#lcneovim)
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)
17 ## <a href="#vimgo" id="vimgo">vim-go</a>
19 Use [vim-go] ver 1.20+, with the following configuration:
22 let g:go_def_mode='gopls'
23 let g:go_info_mode='gopls'
26 ## <a href="#lcneovim" id="lcneovim">LanguageClient-neovim</a>
28 Use [LanguageClient-neovim], with the following configuration:
31 " Launch gopls when Go files are in use
32 let g:LanguageClient_serverCommands = {
36 autocmd BufWritePre *.go :call LanguageClient#textDocument_formatting_sync()
39 ## <a href="#ale" id="ale">Ale</a>
49 see [this issue][ale-issue-2179]
51 ## <a href="#vimlsp" id="vimlsp">vim-lsp</a>
53 Use [prabirshrestha/vim-lsp], with the following configuration:
58 autocmd User lsp_setup call lsp#register_server({
60 \ 'cmd': {server_info->['gopls']},
61 \ 'whitelist': ['go'],
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)
70 ## <a href="#vimlsc" id="vimlsc">vim-lsc</a>
72 Use [natebosch/vim-lsc], with the following configuration:
75 let g:lsc_server_commands = {
77 \ "command": "gopls serve",
79 \ "suppress_stderr": v:true,
84 The `log_level` and `suppress_stderr` parts are needed to prevent breakage from logging. See
85 issues [#180](https://github.com/natebosch/vim-lsc/issues/180) and
86 [#213](https://github.com/natebosch/vim-lsc/issues/213).
88 ## <a href="#cocnvim" id="cocnvim">coc.nvim</a>
90 Use [coc.nvim], with the following `coc-settings.json` configuration:
96 "rootPatterns": ["go.mod", ".vim/", ".git/", ".hg/"],
98 "initializationOptions": {
99 "usePlaceholders": true
105 Other [settings](settings.md) can be added in `initializationOptions` too.
107 The `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`:
110 autocmd BufWritePre *.go :call CocAction('runCommand', 'editor.action.organizeImport')
113 ## <a href="#govim" id="govim">govim</a>
115 In vim classic only, use the experimental [`govim`], simply follow the [install steps][govim-install].
117 ## <a href="#neovim" id="neovim">Neovim v0.5.0+</a>
119 To use the new (still experimental) native LSP client in Neovim, make sure you
120 [install][nvim-install] the prerelease v0.5.0 version of Neovim (aka “nightly”),
121 the `nvim-lspconfig` configuration helper plugin, and check the
122 [`gopls` configuration section][nvim-lspconfig] there.
124 ### <a href="#neovim-install" id="neovim-install">Installation</a>
126 You can use Neovim's native plugin system. On a Unix system, you can do that by
127 cloning the `nvim-lspconfig` repository into the correct directory:
130 dir="${HOME}/.local/share/nvim/site/pack/nvim-lspconfig/opt/nvim-lspconfig/"
133 git clone 'https://github.com/neovim/nvim-lspconfig.git' .
136 ### <a href="#neovim-config" id="neovim-config">Custom Configuration</a>
138 You can add custom configuration using Lua. Here is an example of enabling the
139 `unusedparams` check as well as `staticcheck`:
143 lspconfig = require "lspconfig"
144 lspconfig.gopls.setup {
145 cmd = {"gopls", "serve"},
158 ### <a href="#neovim-imports" id="neovim-imports">Imports</a>
160 To get your imports ordered on save, like `goimports` does, you can define
161 a helper function in Lua:
167 function goimports(timeoutms)
168 local context = { source = { organizeImports = true } }
169 vim.validate { context = { context, "t", true } }
171 local params = vim.lsp.util.make_range_params()
172 params.context = context
174 -- See the implementation of the textDocument/codeAction callback
175 -- (lua/vim/lsp/handler.lua) for how to do this properly.
176 local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, timeout_ms)
177 if not result or next(result) == nil then return end
178 local actions = result[1].result
179 if not actions then return end
180 local action = actions[1]
182 -- textDocument/codeAction can return either Command[] or CodeAction[]. If it
183 -- is a CodeAction, it can have either an edit, a command or both. Edits
184 -- should be executed first.
185 if action.edit or type(action.command) == "table" then
187 vim.lsp.util.apply_workspace_edit(action.edit)
189 if type(action.command) == "table" then
190 vim.lsp.buf.execute_command(action.command)
193 vim.lsp.buf.execute_command(action)
198 autocmd BufWritePre *.go lua goimports(1000)
201 (Taken from the [discussion][nvim-lspconfig-imports] on Neovim issue tracker.)
203 ### <a href="#neovim-omnifunc" id="neovim-omnifunc">Omnifunc</a>
205 To make your <kbd>Ctrl</kbd>+<kbd>x</kbd>,<kbd>Ctrl</kbd>+<kbd>o</kbd> work, add
206 this to your `init.vim`:
209 autocmd FileType go setlocal omnifunc=v:lua.vim.lsp.omnifunc
212 ### <a href="#neovim-links" id="neovim-links">Additional Links</a>
214 * [Neovim's official LSP documentation][nvim-docs].
216 [vim-go]: https://github.com/fatih/vim-go
217 [LanguageClient-neovim]: https://github.com/autozimu/LanguageClient-neovim
218 [ale]: https://github.com/w0rp/ale
219 [ale-issue-2179]: https://github.com/w0rp/ale/issues/2179
220 [prabirshrestha/vim-lsp]: https://github.com/prabirshrestha/vim-lsp/
221 [natebosch/vim-lsc]: https://github.com/natebosch/vim-lsc/
222 [natebosch/vim-lsc#180]: https://github.com/natebosch/vim-lsc/issues/180
223 [coc.nvim]: https://github.com/neoclide/coc.nvim/
224 [`govim`]: https://github.com/myitcv/govim
225 [govim-install]: https://github.com/myitcv/govim/blob/master/README.md#govim---go-development-plugin-for-vim8
226 [nvim-docs]: https://neovim.io/doc/user/lsp.html
227 [nvim-install]: https://github.com/neovim/neovim/wiki/Installing-Neovim
228 [nvim-lspconfig]: https://github.com/neovim/nvim-lspconfig/blob/master/CONFIG.md#gopls
229 [nvim-lspconfig-imports]: https://github.com/neovim/nvim-lspconfig/issues/115