diff options
Diffstat (limited to 'gopls/doc/vim.md')
-rw-r--r-- | gopls/doc/vim.md | 51 |
1 files changed, 21 insertions, 30 deletions
diff --git a/gopls/doc/vim.md b/gopls/doc/vim.md index 887a246ed..1fa44bfa7 100644 --- a/gopls/doc/vim.md +++ b/gopls/doc/vim.md @@ -168,41 +168,32 @@ EOF ### <a href="#neovim-imports" id="neovim-imports">Imports</a> -To get your imports ordered on save, like `goimports` does, you can define -a helper function in Lua: - -```vim -lua <<EOF - -- … - - function OrgImports(wait_ms) - local params = vim.lsp.util.make_range_params() - params.context = {only = {"source.organizeImports"}} - local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, wait_ms) - for _, res in pairs(result or {}) do - for _, r in pairs(res.result or {}) do - if r.edit then - vim.lsp.util.apply_workspace_edit(r.edit) - else - vim.lsp.buf.execute_command(r.command) - end - end - end +Use the following configuration to have your imports organized on save using +the logic of `goimports`. Note: this requires Neovim v0.7.0 or later. + +```lua +vim.api.nvim_create_autocmd('BufWritePre', { + pattern = '*.go', + callback = function() + vim.lsp.buf.code_action({ context = { only = { 'source.organizeImports' } }, apply = true }) end -EOF - -autocmd BufWritePre *.go lua OrgImports(1000) +}) ``` -(Taken from the [discussion][nvim-lspconfig-imports] on Neovim issue tracker.) - ### <a href="#neovim-omnifunc" id="neovim-omnifunc">Omnifunc</a> -To make your <kbd>Ctrl</kbd>+<kbd>x</kbd>,<kbd>Ctrl</kbd>+<kbd>o</kbd> work, add -this to your `init.vim`: - -```vim -autocmd FileType go setlocal omnifunc=v:lua.vim.lsp.omnifunc +In Neovim v0.8.1 and later if you don't set the option `omnifunc`, it will auto +set to `v:lua.vim.lsp.omnifunc`. If you are using an earlier version, you can +configure it manually: + +```lua +local on_attach = function(client, bufnr) + -- Enable completion triggered by <c-x><c-o> + vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc') +end +require('lspconfig').gopls.setup({ + on_attach = on_attach +}) ``` ### <a href="#neovim-links" id="neovim-links">Additional Links</a> |