lsp: Check which code actions are supported before request (#14666)

This fixes https://github.com/zed-industries/zed/issues/13633 by not
sending `source.organizeImports` to the ESLint language server anymore.

Turns out that ESLint tells us through its capabilities that it doesn't
support that code action kind, but we ignored that.

What this code does is to check whether a given server supports specific
code action kinds.

It does this in two places:

1. When constructing the request: we now filter down the list of
   requested `kinds`, in case we can do so. If we can't filter down the
   list, we keep the previous behavior of sending the
   `language_server.code_action_kinds()`
2. Before sending the request: we now check whether the server even
   supports sending the request.

This fixes the issue by only sending actions to servers that support it.

I tested this with various language servers and setups and everything
still works (or works better). But of course there are a ton of
different combinations of language servers and code actions and file
types, so I couldn't test them all.

Release Notes:

- Fix ESLint language server adding comments on save if the
`source.organizeImports` code action was used on save. Zed now filters
out code actions sent to the language servers by checking whether they
are supported first.
([#13633](https://github.com/zed-industries/zed/issues/13633)).
This commit is contained in:
Thorsten Ball 2024-07-18 13:40:06 +02:00 committed by GitHub
parent 49effeb7ba
commit 22a2cc6950
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 108 additions and 29 deletions

View file

@ -209,6 +209,14 @@ impl<F: Future> LspRequestFuture<F::Output> for LspRequest<F> {
}
}
/// Combined capabilities of the server and the adapter.
pub struct AdapterServerCapabilities {
// Reported capabilities by the server
pub server_capabilities: ServerCapabilities,
// List of code actions supported by the LspAdapter matching the server
pub code_action_kinds: Option<Vec<CodeActionKind>>,
}
/// Experimental: Informs the end user about the state of the server
///
/// [Rust Analyzer Specification](https://github.com/rust-lang/rust-analyzer/blob/master/docs/dev/lsp-extensions.md#server-status)
@ -916,6 +924,15 @@ impl LanguageServer {
self.capabilities.read().clone()
}
/// Get the reported capabilities of the running language server and
/// what we know on the client/adapter-side of its capabilities.
pub fn adapter_server_capabilities(&self) -> AdapterServerCapabilities {
AdapterServerCapabilities {
server_capabilities: self.capabilities(),
code_action_kinds: self.code_action_kinds(),
}
}
pub fn update_capabilities(&self, update: impl FnOnce(&mut ServerCapabilities)) {
update(self.capabilities.write().deref_mut());
}