1# Vim / Neovim 2 3* [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> 18 19Use [vim-go] ver 1.20+, with the following configuration: 20 21```vim 22let g:go_def_mode='gopls' 23let g:go_info_mode='gopls' 24``` 25 26## <a href="#lcneovim" id="lcneovim">LanguageClient-neovim</a> 27 28Use [LanguageClient-neovim], with the following configuration: 29 30```vim 31" 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 39## <a href="#ale" id="ale">Ale</a> 40 41Use [ale]: 42 43```vim 44let g:ale_linters = { 45 \ 'go': ['gopls'], 46 \} 47``` 48 49see [this issue][ale-issue-2179] 50 51## <a href="#vimlsp" id="vimlsp">vim-lsp</a> 52 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 70## <a href="#vimlsc" id="vimlsc">vim-lsc</a> 71 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, 79\ "suppress_stderr": v:true, 80\ }, 81\} 82``` 83 84The `log_level` and `suppress_stderr` parts are needed to prevent breakage from logging. See 85issues [#180](https://github.com/natebosch/vim-lsc/issues/180) and 86[#213](https://github.com/natebosch/vim-lsc/issues/213). 87 88## <a href="#cocnvim" id="cocnvim">coc.nvim</a> 89 90Use [coc.nvim], with the following `coc-settings.json` configuration: 91 92```json 93 "languageserver": { 94 "golang": { 95 "command": "gopls", 96 "rootPatterns": ["go.mod", ".vim/", ".git/", ".hg/"], 97 "filetypes": ["go"], 98 "initializationOptions": { 99 "usePlaceholders": true 100 } 101 } 102 } 103``` 104 105Other [settings](settings.md) can be added in `initializationOptions` too. 106 107The `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`: 108 109```vim 110autocmd BufWritePre *.go :call CocAction('runCommand', 'editor.action.organizeImport') 111``` 112 113## <a href="#govim" id="govim">govim</a> 114 115In vim classic only, use the experimental [`govim`], simply follow the [install steps][govim-install]. 116 117## <a href="#neovim" id="neovim">Neovim v0.5.0+</a> 118 119To use the new native LSP client in Neovim, make sure you 120[install][nvim-install] Neovim v.0.5.0+, 121the `nvim-lspconfig` configuration helper plugin, and check the 122[`gopls` configuration section][nvim-lspconfig] there. 123 124### <a href="#neovim-install" id="neovim-install">Installation</a> 125 126You can use Neovim's native plugin system. On a Unix system, you can do that by 127cloning the `nvim-lspconfig` repository into the correct directory: 128 129```sh 130dir="${HOME}/.local/share/nvim/site/pack/nvim-lspconfig/opt/nvim-lspconfig/" 131mkdir -p "$dir" 132cd "$dir" 133git clone 'https://github.com/neovim/nvim-lspconfig.git' . 134``` 135 136### <a href="#neovim-config" id="neovim-config">Custom Configuration</a> 137 138You can add custom configuration using Lua. Here is an example of enabling the 139`unusedparams` check as well as `staticcheck`: 140 141```vim 142lua <<EOF 143 lspconfig = require "lspconfig" 144 lspconfig.gopls.setup { 145 cmd = {"gopls", "serve"}, 146 settings = { 147 gopls = { 148 analyses = { 149 unusedparams = true, 150 }, 151 staticcheck = true, 152 }, 153 }, 154 } 155EOF 156``` 157 158### <a href="#neovim-imports" id="neovim-imports">Imports</a> 159 160To get your imports ordered on save, like `goimports` does, you can define 161a helper function in Lua: 162 163```vim 164lua <<EOF 165 -- … 166 167 function goimports(timeout_ms) 168 local context = { source = { organizeImports = true } } 169 vim.validate { context = { context, "t", true } } 170 171 local params = vim.lsp.util.make_range_params() 172 params.context = context 173 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] 181 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 186 if action.edit then 187 vim.lsp.util.apply_workspace_edit(action.edit) 188 end 189 if type(action.command) == "table" then 190 vim.lsp.buf.execute_command(action.command) 191 end 192 else 193 vim.lsp.buf.execute_command(action) 194 end 195 end 196EOF 197 198autocmd BufWritePre *.go lua goimports(1000) 199``` 200 201(Taken from the [discussion][nvim-lspconfig-imports] on Neovim issue tracker.) 202 203### <a href="#neovim-omnifunc" id="neovim-omnifunc">Omnifunc</a> 204 205To make your <kbd>Ctrl</kbd>+<kbd>x</kbd>,<kbd>Ctrl</kbd>+<kbd>o</kbd> work, add 206this to your `init.vim`: 207 208```vim 209autocmd FileType go setlocal omnifunc=v:lua.vim.lsp.omnifunc 210``` 211 212### <a href="#neovim-links" id="neovim-links">Additional Links</a> 213 214* [Neovim's official LSP documentation][nvim-docs]. 215 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 230