| 1 | --- |
| 2 | title: "Gopls: Using Vim or Neovim" |
| 3 | --- |
| 4 | |
| 5 | * [vim-go](#vimgo) |
| 6 | * [LanguageClient-neovim](#lcneovim) |
| 7 | * [Ale](#ale) |
| 8 | * [vim-lsp](#vimlsp) |
| 9 | * [yegappan/lsp](#yegappanlsp) |
| 10 | * [vim-lsc](#vimlsc) |
| 11 | * [coc.nvim](#cocnvim) |
| 12 | * [govim](#govim) |
| 13 | * [Neovim v0.5.0+](#neovim) |
| 14 | * [Installation](#neovim-install) |
| 15 | * [Custom Configuration](#neovim-config) |
| 16 | * [Imports](#neovim-imports) |
| 17 | * [Omnifunc](#neovim-omnifunc) |
| 18 | * [Additional Links](#neovim-links) |
| 19 | |
| 20 | ## <a href="#vimgo" id="vimgo">vim-go</a> |
| 21 | |
| 22 | Use [vim-go] ver 1.20+, with the following configuration: |
| 23 | |
| 24 | ```vim |
| 25 | let g:go_def_mode='gopls' |
| 26 | let g:go_info_mode='gopls' |
| 27 | ``` |
| 28 | |
| 29 | ## <a href="#lcneovim" id="lcneovim">LanguageClient-neovim</a> |
| 30 | |
| 31 | Use [LanguageClient-neovim], with the following configuration: |
| 32 | |
| 33 | ```vim |
| 34 | " Launch gopls when Go files are in use |
| 35 | let g:LanguageClient_serverCommands = { |
| 36 | \ 'go': ['gopls'] |
| 37 | \ } |
| 38 | " Run gofmt on save |
| 39 | autocmd BufWritePre *.go :call LanguageClient#textDocument_formatting_sync() |
| 40 | ``` |
| 41 | |
| 42 | ## <a href="#ale" id="ale">Ale</a> |
| 43 | |
| 44 | Use [ale]: |
| 45 | |
| 46 | ```vim |
| 47 | let g:ale_linters = { |
| 48 | \ 'go': ['gopls'], |
| 49 | \} |
| 50 | ``` |
| 51 | |
| 52 | see [this issue][ale-issue-2179] |
| 53 | |
| 54 | ## <a href="#vimlsp" id="vimlsp">vim-lsp</a> |
| 55 | |
| 56 | Use [prabirshrestha/vim-lsp], with the following configuration: |
| 57 | |
| 58 | ```vim |
| 59 | augroup LspGo |
| 60 | au! |
| 61 | autocmd User lsp_setup call lsp#register_server({ |
| 62 | \ 'name': 'gopls', |
| 63 | \ 'cmd': {server_info->['gopls']}, |
| 64 | \ 'whitelist': ['go'], |
| 65 | \ }) |
| 66 | autocmd FileType go setlocal omnifunc=lsp#complete |
| 67 | "autocmd FileType go nmap <buffer> gd <plug>(lsp-definition) |
| 68 | "autocmd FileType go nmap <buffer> ,n <plug>(lsp-next-error) |
| 69 | "autocmd FileType go nmap <buffer> ,p <plug>(lsp-previous-error) |
| 70 | augroup END |
| 71 | ``` |
| 72 | |
| 73 | ## <a href="#yegappanlsp" id="yegappanlsp">yegappan/lsp</a> |
| 74 | |
| 75 | Use [yegappan/lsp] (requires Vim 9.0 or later), with the following |
| 76 | configuration in Vim 9 script: |
| 77 | |
| 78 | ```vim |
| 79 | vim9script |
| 80 | |
| 81 | var lspServers = [{ |
| 82 | name: 'gopls', |
| 83 | filetype: ['go'], |
| 84 | path: 'gopls', |
| 85 | args: ['serve'], |
| 86 | syncInit: true, |
| 87 | }] |
| 88 | |
| 89 | autocmd User LspSetup call LspAddServer(lspServers) |
| 90 | ``` |
| 91 | |
| 92 | ## <a href="#vimlsc" id="vimlsc">vim-lsc</a> |
| 93 | |
| 94 | Use [natebosch/vim-lsc], with the following configuration: |
| 95 | |
| 96 | ```vim |
| 97 | let g:lsc_server_commands = { |
| 98 | \ "go": { |
| 99 | \ "command": "gopls serve", |
| 100 | \ "log_level": -1, |
| 101 | \ "suppress_stderr": v:true, |
| 102 | \ }, |
| 103 | \} |
| 104 | ``` |
| 105 | |
| 106 | The `log_level` and `suppress_stderr` parts are needed to prevent breakage from logging. See |
| 107 | issues [#180](https://github.com/natebosch/vim-lsc/issues/180) and |
| 108 | [#213](https://github.com/natebosch/vim-lsc/issues/213). |
| 109 | |
| 110 | ## <a href="#cocnvim" id="cocnvim">coc.nvim</a> |
| 111 | |
| 112 | Use [coc.nvim], with the following `coc-settings.json` configuration: |
| 113 | |
| 114 | ```json |
| 115 | "languageserver": { |
| 116 | "go": { |
| 117 | "command": "gopls", |
| 118 | "rootPatterns": ["go.work", "go.mod", ".vim/", ".git/", ".hg/"], |
| 119 | "filetypes": ["go"], |
| 120 | "initializationOptions": { |
| 121 | "usePlaceholders": true |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | ``` |
| 126 | |
| 127 | If you use `go.work` files, you may want to set the |
| 128 | `workspace.workspaceFolderCheckCwd` option. This will force coc.nvim to search |
| 129 | parent directories for `go.work` files, even if the current open directory has |
| 130 | a `go.mod` file. See the |
| 131 | [coc.nvim documentation](https://github.com/neoclide/coc.nvim/wiki/Using-workspaceFolders) |
| 132 | for more details. |
| 133 | |
| 134 | Other [settings](../settings) can be added in `initializationOptions` too. |
| 135 | |
| 136 | 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`: |
| 137 | |
| 138 | ```vim |
| 139 | autocmd BufWritePre *.go :call CocAction('runCommand', 'editor.action.organizeImport') |
| 140 | ``` |
| 141 | |
| 142 | ## <a href="#govim" id="govim">govim</a> |
| 143 | |
| 144 | In vim classic only, use the experimental [`govim`], simply follow the [install steps][govim-install]. |
| 145 | |
| 146 | ## <a href="#neovim" id="neovim">Neovim v0.5.0+</a> |
| 147 | |
| 148 | To use the new native LSP client in Neovim, make sure you |
| 149 | [install][nvim-install] Neovim v.0.5.0+, |
| 150 | the `nvim-lspconfig` configuration helper plugin, and check the |
| 151 | [`gopls` configuration section][nvim-lspconfig] there. |
| 152 | |
| 153 | ### <a href="#neovim-install" id="neovim-install">Installation</a> |
| 154 | |
| 155 | You can use Neovim's native plugin system. On a Unix system, you can do that by |
| 156 | cloning the `nvim-lspconfig` repository into the correct directory: |
| 157 | |
| 158 | ```sh |
| 159 | dir="${HOME}/.local/share/nvim/site/pack/nvim-lspconfig/opt/nvim-lspconfig/" |
| 160 | mkdir -p "$dir" |
| 161 | cd "$dir" |
| 162 | git clone 'https://github.com/neovim/nvim-lspconfig.git' . |
| 163 | ``` |
| 164 | |
| 165 | ### <a href="#neovim-config" id="neovim-config">Configuration</a> |
| 166 | |
| 167 | nvim-lspconfig aims to provide reasonable defaults, so your setup can be very |
| 168 | brief. |
| 169 | |
| 170 | ```lua |
| 171 | local lspconfig = require("lspconfig") |
| 172 | lspconfig.gopls.setup({}) |
| 173 | ``` |
| 174 | |
| 175 | However, you can also configure `gopls` for your preferences. Here's an |
| 176 | example that enables `unusedparams`, `staticcheck`, and `gofumpt`. |
| 177 | |
| 178 | ```lua |
| 179 | local lspconfig = require("lspconfig") |
| 180 | lspconfig.gopls.setup({ |
| 181 | settings = { |
| 182 | gopls = { |
| 183 | analyses = { |
| 184 | unusedparams = true, |
| 185 | }, |
| 186 | staticcheck = true, |
| 187 | gofumpt = true, |
| 188 | }, |
| 189 | }, |
| 190 | }) |
| 191 | ``` |
| 192 | |
| 193 | ### <a href="#neovim-imports" id="neovim-imports">Imports and Formatting</a> |
| 194 | |
| 195 | Use the following configuration to have your imports organized on save using |
| 196 | the logic of `goimports` and your code formatted. |
| 197 | |
| 198 | ```lua |
| 199 | autocmd("BufWritePre", { |
| 200 | pattern = "*.go", |
| 201 | callback = function() |
| 202 | local params = vim.lsp.util.make_range_params() |
| 203 | params.context = {only = {"source.organizeImports"}} |
| 204 | -- buf_request_sync defaults to a 1000ms timeout. Depending on your |
| 205 | -- machine and codebase, you may want longer. Add an additional |
| 206 | -- argument after params if you find that you have to write the file |
| 207 | -- twice for changes to be saved. |
| 208 | -- E.g., vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, 3000) |
| 209 | local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params) |
| 210 | for cid, res in pairs(result or {}) do |
| 211 | for _, r in pairs(res.result or {}) do |
| 212 | if r.edit then |
| 213 | local enc = (vim.lsp.get_client_by_id(cid) or {}).offset_encoding or "utf-16" |
| 214 | vim.lsp.util.apply_workspace_edit(r.edit, enc) |
| 215 | end |
| 216 | end |
| 217 | end |
| 218 | vim.lsp.buf.format({async = false}) |
| 219 | end |
| 220 | }) |
| 221 | ``` |
| 222 | |
| 223 | ### <a href="#neovim-omnifunc" id="neovim-omnifunc">Omnifunc</a> |
| 224 | |
| 225 | In Neovim v0.8.1 and later if you don't set the option `omnifunc`, it will auto |
| 226 | set to `v:lua.vim.lsp.omnifunc`. If you are using an earlier version, you can |
| 227 | configure it manually: |
| 228 | |
| 229 | ```lua |
| 230 | local on_attach = function(client, bufnr) |
| 231 | -- Enable completion triggered by <c-x><c-o> |
| 232 | vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc') |
| 233 | end |
| 234 | require('lspconfig').gopls.setup({ |
| 235 | on_attach = on_attach |
| 236 | }) |
| 237 | ``` |
| 238 | |
| 239 | ### <a href="#semantic-tokens" id="semantic-tokens">Semantic Tokens</a> |
| 240 | |
| 241 | Semantic tokens are disabled by default in gopls, to enable pass |
| 242 | the [`semanticTokens`](../settings.md#semanticTokens) setting. |
| 243 | |
| 244 | #### <a href="#semantic-tokens-shadowing" id="semantic-tokens-shadowing">Shadowing semantic modifier</a> |
| 245 | |
| 246 | Gopls emits a `"shadowing"` semantic token modifier for each identifier that |
| 247 | shadows an existing symbol from an outer scope. Such declarations can be |
| 248 | highlighted with bold and underline with: |
| 249 | |
| 250 | ```lua |
| 251 | vim.api.nvim_set_hl(0, '@lsp.mod.shadowing', { bold = true, underline = true }) |
| 252 | ``` |
| 253 | |
| 254 | ### <a href="#neovim-links" id="neovim-links">Additional Links</a> |
| 255 | |
| 256 | * [Neovim's official LSP documentation][nvim-docs]. |
| 257 | |
| 258 | [vim-go]: https://github.com/fatih/vim-go |
| 259 | [LanguageClient-neovim]: https://github.com/autozimu/LanguageClient-neovim |
| 260 | [ale]: https://github.com/w0rp/ale |
| 261 | [ale-issue-2179]: https://github.com/w0rp/ale/issues/2179 |
| 262 | [prabirshrestha/vim-lsp]: https://github.com/prabirshrestha/vim-lsp/ |
| 263 | [yegappan/lsp]: https://github.com/yegappan/lsp/ |
| 264 | [natebosch/vim-lsc]: https://github.com/natebosch/vim-lsc/ |
| 265 | [natebosch/vim-lsc#180]: https://github.com/natebosch/vim-lsc/issues/180 |
| 266 | [coc.nvim]: https://github.com/neoclide/coc.nvim/ |
| 267 | [`govim`]: https://github.com/myitcv/govim |
| 268 | [govim-install]: https://github.com/myitcv/govim/blob/master/README.md#govim---go-development-plugin-for-vim8 |
| 269 | [nvim-docs]: https://neovim.io/doc/user/lsp.html |
| 270 | [nvim-install]: https://github.com/neovim/neovim/wiki/Installing-Neovim |
| 271 | [nvim-lspconfig]: https://github.com/neovim/nvim-lspconfig/blob/master/doc/configs.md#gopls |
| 272 | [nvim-lspconfig-imports]: https://github.com/neovim/nvim-lspconfig/issues/115 |