Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools / gopls@v0.5.2 / doc / vim.md
1 # Vim / Neovim
2
3 ## vim-go
4
5 Use [vim-go] ver 1.20+, with the following configuration:
6
7 ```
8 let g:go_def_mode='gopls'
9 let g:go_info_mode='gopls'
10 ```
11
12 ## LanguageClient-neovim
13
14 Use [LanguageClient-neovim], with the following configuration:
15
16 ```
17 " Launch gopls when Go files are in use
18 let g:LanguageClient_serverCommands = {
19        \ 'go': ['gopls']
20        \ }
21 " Run gofmt on save
22 autocmd BufWritePre *.go :call LanguageClient#textDocument_formatting_sync()
23 ```
24
25 ## Ale
26
27 Use [ale]:
28
29 ```vim
30 let g:ale_linters = {
31         \ 'go': ['gopls'],
32         \}
33 ```
34
35 see [this issue][ale-issue-2179]
36
37 ## vim-lsp
38
39 Use [prabirshrestha/vim-lsp], with the following configuration:
40
41 ```vim
42 augroup LspGo
43   au!
44   autocmd User lsp_setup call lsp#register_server({
45       \ 'name': 'go-lang',
46       \ 'cmd': {server_info->['gopls']},
47       \ 'whitelist': ['go'],
48       \ })
49   autocmd FileType go setlocal omnifunc=lsp#complete
50   "autocmd FileType go nmap <buffer> gd <plug>(lsp-definition)
51   "autocmd FileType go nmap <buffer> ,n <plug>(lsp-next-error)
52   "autocmd FileType go nmap <buffer> ,p <plug>(lsp-previous-error)
53 augroup END
54 ```
55
56 ## vim-lsc
57
58 Use [natebosch/vim-lsc], with the following configuration:
59
60 ```vim
61 let g:lsc_server_commands = {
62 \  "go": {
63 \    "command": "gopls serve",
64 \    "log_level": -1,
65 \    "suppress_stderr": v:true,
66 \  },
67 \}
68 ```
69
70 The `log_level` and `suppress_stderr` parts are needed to prevent breakage from logging. See
71 issues [#180](https://github.com/natebosch/vim-lsc/issues/180) and
72 [#213](https://github.com/natebosch/vim-lsc/issues/213).
73
74 ## coc.nvim
75
76 Use [coc.nvim], with the following `coc-settings.json` configuration:
77
78 ```json
79   "languageserver": {
80     "golang": {
81       "command": "gopls",
82       "rootPatterns": ["go.mod", ".vim/", ".git/", ".hg/"],
83       "filetypes": ["go"],
84       "initializationOptions": {
85         "usePlaceholders": true
86       }
87     }
88   }
89 ```
90
91 Other [settings](settings.md) can be added in `initializationOptions` too.
92
93 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`:
94
95 ```vim
96 autocmd BufWritePre *.go :call CocAction('runCommand', 'editor.action.organizeImport')
97 ```
98
99 ## govim
100
101 In vim classic only, use the experimental [`govim`], simply follow the [install steps][govim-install].
102
103 ## Neovim v0.5.0+
104
105 To use the new (still experimental) native LSP client in Neovim, make sure you
106 [install][nvim-install] the prerelease v0.5.0 version of Neovim (aka “nightly”),
107 the `nvim-lspconfig` configuration helper plugin, and check the
108 [`gopls` configuration section][nvim-lspconfig] there.
109
110 ### Custom configuration
111
112 You can add custom configuration using Lua.  Here is an example of enabling the
113 `unusedparams` check as well as `staticcheck`:
114
115 ```vim
116 lua <<EOF
117   nvim_lsp = require "nvim_lsp"
118   nvim_lsp.gopls.setup {
119     cmd = {"gopls", "serve"},
120     settings = {
121       gopls = {
122         analyses = {
123           unusedparams = true,
124         },
125         staticcheck = true,
126       },
127     },
128   }
129 EOF
130 ```
131
132 ### Imports
133
134 To get your imports ordered on save, like `goimports` does, you can define
135 a helper function in Lua:
136
137 ```vim
138 lua <<EOF
139   -- …
140
141   function goimports(timeoutms)
142     local context = { source = { organizeImports = true } }
143     vim.validate { context = { context, "t", true } }
144
145     local params = vim.lsp.util.make_range_params()
146     params.context = context
147
148     local method = "textDocument/codeAction"
149     local resp = vim.lsp.buf_request_sync(0, method, params, timeoutms)
150     if resp and resp[1] then
151       local result = resp[1].result
152       if result and result[1] then
153         local edit = result[1].edit
154         vim.lsp.util.apply_workspace_edit(edit)
155       end
156     end
157
158     vim.lsp.buf.formatting()
159   end
160 EOF
161
162 autocmd BufWritePre *.go lua goimports(1000)
163 ```
164
165 (Taken from the [discussion][nvim-lspconfig-imports] on Neovim issue tracker.)
166
167 ### Omnifunc
168
169 To make your <kbd>Ctrl</kbd>+<kbd>x</kbd>,<kbd>Ctrl</kbd>+<kbd>o</kbd> work, add
170 this to your `init.vim`:
171
172 ```vim
173 autocmd FileType go setlocal omnifunc=v:lua.vim.lsp.omnifunc
174 ```
175
176 ### Additional Links
177
178 * [Neovim's official LSP documentation][nvim-docs].
179
180 [vim-go]: https://github.com/fatih/vim-go
181 [LanguageClient-neovim]: https://github.com/autozimu/LanguageClient-neovim
182 [ale]: https://github.com/w0rp/ale
183 [ale-issue-2179]: https://github.com/w0rp/ale/issues/2179
184 [prabirshrestha/vim-lsp]: https://github.com/prabirshrestha/vim-lsp/
185 [natebosch/vim-lsc]: https://github.com/natebosch/vim-lsc/
186 [natebosch/vim-lsc#180]: https://github.com/natebosch/vim-lsc/issues/180
187 [coc.nvim]: https://github.com/neoclide/coc.nvim/
188 [`govim`]: https://github.com/myitcv/govim
189 [govim-install]: https://github.com/myitcv/govim/blob/master/README.md#govim---go-development-plugin-for-vim8
190 [nvim-docs]: https://neovim.io/doc/user/lsp.html
191 [nvim-install]: https://github.com/neovim/neovim/wiki/Installing-Neovim
192 [nvim-lspconfig]: https://github.com/neovim/nvim-lspconfig#gopls
193 [nvim-lspconfig-imports]: https://github.com/neovim/nvim-lspconfig/issues/115