Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter LSP code actions based on the requested kinds #20847

Merged
merged 3 commits into from
Nov 22, 2024

Conversation

Techatrix
Copy link
Contributor

I've observed that Zed's implementation of Code Actions On Format uses the CodeActionContext.only parameter to request specific code action kinds from the server. The issue is that it does not filter out code actions from the response, believing that the server will do it.

The LSP specification says that the client is responsible for filtering out unwanted code actions:

/**
* Requested kind of actions to return.
*
* Actions not of this kind are filtered out by the client before being
* shown. So servers can omit computing them.
*/
only?: CodeActionKind[];

This PR will filter out unwanted code action on the client side.

I have initially encountered this issue because the ZLS language server (until zigtools/zls#2087) does not filter code action based on CodeActionContext.only so Zed runs all received code actions even if they are explicitly disabled in the code_actions_on_format setting.

Release Notes:

  • Fix the code_actions_on_format setting when used with a language server like ZLS

@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Nov 19, 2024
Co-authored-by: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com>
@SomeoneToIgnore
Copy link
Contributor

Passing by and suggesting to add a test for this.
Some examples for inspiration:

async fn test_multiple_language_server_actions(cx: &mut gpui::TestAppContext) {

Copy link
Contributor

@osiewicz osiewicz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@osiewicz osiewicz merged commit d5f2bca into zed-industries:main Nov 22, 2024
11 checks passed
Anthony-Eid pushed a commit to Anthony-Eid/zed that referenced this pull request Nov 22, 2024
…20847)

I've observed that Zed's implementation of [Code Actions On
Format](https://zed.dev/docs/configuring-zed#code-actions-on-format)
uses the
[CodeActionContext.only](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#codeActionContext)
parameter to request specific code action kinds from the server. The
issue is that it does not filter out code actions from the response,
believing that the server will do it.

The [LSP
specification](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#codeActionContext)
says that the client is responsible for filtering out unwanted code
actions:

```js
/**
* Requested kind of actions to return.
*
* Actions not of this kind are filtered out by the client before being
* shown. So servers can omit computing them.
*/
only?: CodeActionKind[];
```

This PR will filter out unwanted code action on the client side.

I have initially encountered this issue because the [ZLS language
server](https://github.com/zigtools/zls) (until
zigtools/zls#2087) does not filter code action
based on `CodeActionContext.only` so Zed runs all received code actions
even if they are explicitly disabled in the `code_actions_on_format`
setting.

Release Notes:

- Fix the `code_actions_on_format` setting when used with a language
server like ZLS

---------

Co-authored-by: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed The user has signed the Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants