Update lspconfig.lua

Organize unused imports for GOLang
This commit is contained in:
Waldir Borba Junior 2022-06-06 10:18:22 -03:00 committed by GitHub
parent 8791fafd4e
commit 80c99fed33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 0 deletions

View File

@ -13,6 +13,26 @@ require("plugins.configs.others").lsp_handlers()
local win = require "lspconfig.ui.windows"
local _default_opts = win.default_opts
-- Organize import for GOLang
vim.api.nvim_create_autocmd("BufWritePre", {
pattern = { "*.go" },
callback = function()
local params = vim.lsp.util.make_range_params(nil, vim.lsp.util._get_offset_encoding())
params.context = { only = { "source.organizeImports" } }
local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, 3000)
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, vim.lsp.util._get_offset_encoding())
else
vim.lsp.buf.execute_command(r.command)
end
end
end
end,
})
win.default_opts = function(options)
local opts = _default_opts(options)
opts.border = "single"