fix(lsp): handle errors and add missing client filters (#6900)

- Add nil-check when no LSP client is found matching the filter
- Add error handling with pcall for exec_cmd failures
- Add client filter to typescript.goToSourceDefinition and typescript.findAllFileReferences
- Show warning notification when command execution fails or client not found

This fixes the issue where LSP commands would fail silently or cause crashes when
executing on unfiltered clients, and where irrelevant LSP clients (like Copilot)
would report errors for unsupported commands.
This commit is contained in:
Your Name 2026-03-16 01:04:37 -07:00
parent 31caef21fd
commit 56f0bb258e
2 changed files with 18 additions and 2 deletions

View file

@ -72,6 +72,7 @@ return {
local win = vim.api.nvim_get_current_win()
local params = vim.lsp.util.make_position_params(win, "utf-16")
LazyVim.lsp.execute({
filter = "vtsls",
command = "typescript.goToSourceDefinition",
arguments = { params.textDocument.uri, params.position },
open = true,
@ -83,6 +84,7 @@ return {
"gR",
function()
LazyVim.lsp.execute({
filter = "vtsls",
command = "typescript.findAllFileReferences",
arguments = { vim.uri_from_bufnr(0) },
open = true,

View file

@ -76,7 +76,14 @@ function M.execute(opts)
local buf = vim.api.nvim_get_current_buf()
---@cast filter vim.lsp.get_clients.Filter
local client = vim.lsp.get_clients(LazyVim.merge({}, filter, { bufnr = buf }))[1]
local clients = vim.lsp.get_clients(LazyVim.merge({}, filter, { bufnr = buf }))
local client = clients[1]
if not client then
local filter_name = type(opts.filter) == "string" and opts.filter or (filter.name or "any")
LazyVim.warn("No LSP client found for filter: " .. filter_name, { title = "LSP Execute" })
return
end
local params = {
command = opts.command,
@ -89,7 +96,14 @@ function M.execute(opts)
})
else
vim.list_extend(params, { title = opts.title })
return client:exec_cmd(params, { bufnr = buf }, opts.handler)
local ok, result = pcall(function()
return client:exec_cmd(params, { bufnr = buf }, opts.handler)
end)
if not ok then
LazyVim.warn("Failed to execute command '" .. opts.command .. "': " .. tostring(result), { title = "LSP Execute" })
return
end
return result
end
end