.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools / gopls@v0.6.9 / doc / vim.md
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
19 Use [vim-go] ver 1.20+, with the following configuration:
20
21 ```vim
22 let g:go_def_mode='gopls'
23 let g:go_info_mode='gopls'
24 ```
25
26 ## <a href="#lcneovim" id="lcneovim">LanguageClient-neovim</a>
27
28 Use [LanguageClient-neovim], with the following configuration:
29
30 ```vim
31 " Launch gopls when Go files are in use
32 let g:LanguageClient_serverCommands = {
33        \ 'go': ['gopls']
34        \ }
35 " Run gofmt on save
36 autocmd BufWritePre *.go :call LanguageClient#textDocument_formatting_sync()
37 ```
38
39 ## <a href="#ale" id="ale">Ale</a>
40
41 Use [ale]:
42
43 ```vim
44 let g:ale_linters = {
45   \ 'go': ['gopls'],
46   \}
47 ```
48
49 see [this issue][ale-issue-2179]
50
51 ## <a href="#vimlsp" id="vimlsp">vim-lsp</a>
52
53 Use [prabirshrestha/vim-lsp], with the following configuration:
54
55 ```vim
56 augroup 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)
67 augroup END
68 ```
69
70 ## <a href="#vimlsc" id="vimlsc">vim-lsc</a>
71
72 Use [natebosch/vim-lsc], with the following configuration:
73
74 ```vim
75 let g:lsc_server_commands = {
76 \  "go": {
77 \    "command": "gopls serve",
78 \    "log_level": -1,
79 \    "suppress_stderr": v:true,
80 \  },
81 \}
82 ```
83
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).
87
88 ## <a href="#cocnvim" id="cocnvim">coc.nvim</a>
89
90 Use [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
105 Other [settings](settings.md) can be added in `initializationOptions` too.
106
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`:
108
109 ```vim
110 autocmd BufWritePre *.go :call CocAction('runCommand', 'editor.action.organizeImport')
111 ```
112
113 ## <a href="#govim" id="govim">govim</a>
114
115 In 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
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.
123
124 ### <a href="#neovim-install" id="neovim-install">Installation</a>
125
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:
128
129 ```sh
130 dir="${HOME}/.local/share/nvim/site/pack/nvim-lspconfig/opt/nvim-lspconfig/"
131 mkdir -p "$dir"
132 cd "$dir"
133 git clone 'https://github.com/neovim/nvim-lspconfig.git' .
134 ```
135
136 ### <a href="#neovim-config" id="neovim-config">Custom Configuration</a>
137
138 You can add custom configuration using Lua.  Here is an example of enabling the
139 `unusedparams` check as well as `staticcheck`:
140
141 ```vim
142 lua <<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   }
155 EOF
156 ```
157
158 ### <a href="#neovim-imports" id="neovim-imports">Imports</a>
159
160 To get your imports ordered on save, like `goimports` does, you can define
161 a helper function in Lua:
162
163 ```vim
164 lua <<EOF
165   -- …
166
167   function goimports(timeoutms)
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
196 EOF
197
198 autocmd 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
205 To make your <kbd>Ctrl</kbd>+<kbd>x</kbd>,<kbd>Ctrl</kbd>+<kbd>o</kbd> work, add
206 this to your `init.vim`:
207
208 ```vim
209 autocmd 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