From 80c99fed33e291b98bbf6c02a292bfcc26a198eb Mon Sep 17 00:00:00 2001 From: Waldir Borba Junior Date: Mon, 6 Jun 2022 10:18:22 -0300 Subject: [PATCH] Update lspconfig.lua Organize unused imports for GOLang --- lua/plugins/configs/lspconfig.lua | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lua/plugins/configs/lspconfig.lua b/lua/plugins/configs/lspconfig.lua index ded646b..745b388 100644 --- a/lua/plugins/configs/lspconfig.lua +++ b/lua/plugins/configs/lspconfig.lua @@ -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"