From aee5eaa103331f03d954a895fa2a60243f2ec27a Mon Sep 17 00:00:00 2001 From: Marcelo Jacobus Date: Tue, 22 Jul 2025 14:42:17 -0300 Subject: [PATCH] Attempt to improve lsp --- lua/plugins/lsp.lua | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/lua/plugins/lsp.lua b/lua/plugins/lsp.lua index 2cb8554..87863d7 100644 --- a/lua/plugins/lsp.lua +++ b/lua/plugins/lsp.lua @@ -94,6 +94,35 @@ return { capabilities = capabilities, on_attach = on_attach, }) + + + -- NOTE: Not sure this works + -- Auto-import missing imports on save for JS/TS + vim.api.nvim_create_autocmd("BufWritePre", { + pattern = { "*.ts", "*.tsx", "*.js", "*.jsx" }, + callback = function() + local params = { + context = { + only = { "source.addMissingImports.ts" }, + }, + } + local clients = vim.lsp.get_active_clients({ name = "ts_ls" }) + for _, client in ipairs(clients) do + if client.supports_method("textDocument/codeAction") then + vim.lsp.buf_request(0, "textDocument/codeAction", vim.lsp.util.make_range_params(), function(_, result) + if result and result[1] then + if result[1].edit then + vim.lsp.util.apply_workspace_edit(result[1].edit, client.offset_encoding or "utf-8") + end + if result[1].command then + vim.lsp.buf.execute_command(result[1].command) + end + end + end) + end + end + end, + }) end, } }