mirror of
https://github.com/LazyVim/LazyVim.git
synced 2026-07-25 14:01:06 +00:00
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:
parent
31caef21fd
commit
56f0bb258e
2 changed files with 18 additions and 2 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue