Skip to content

Handling of Diagnostics in code action contexts is questionable #4056

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

Open
michaelpj opened this issue Feb 7, 2024 · 2 comments
Open

Handling of Diagnostics in code action contexts is questionable #4056

michaelpj opened this issue Feb 7, 2024 · 2 comments
Labels
type: enhancement New feature or request

Comments

@michaelpj
Copy link
Collaborator

When we get a textDocument/codeAction request, the context contains some Diagnostics. These are (allegedly) diagnostics that are a) visible to the user and b) overlap with the requested range.

However, it's fairly unclear what the client is allowed to do in populating this field. In particular, it's not clear that they have to give it back to us as we sent it to them. The spec also indicates that this field is unreliable, the doc for it says

They are provided so that the server knows which errors are currently presented to the user for the given range. There is no guarantee that these accurately reflect the error state of the resource. The primary parameter to compute code actions is the provided range.

(emphasis mine)

We rely on the context Diagnostics quite a bit:

  • Various code actions that trigger off diagnostics inspect them
    • This seems questionable since in most cases we want to trigger based on the objective fact about the code that triggers the diagnostic (e.g. "redundant import"), and not on whether this fact is presented to the user.
  • In some places we compare them against existing diagnostics we know about

Moreover, we don't really need to do this at all: we can just consult our diagnostic store and filter it based on the given range. This is more direct and simpler, and we should probably just do this in almost all cases. The exception would be cases where we actually do care about presentation: for example, the "suppress this warning" code action does care about whether the diagnostic it is proposing to suppress is in fact visible to the user. But this is an exceptional case.

@michaelpj michaelpj added type: bug Something isn't right: doesn't work as intended, documentation is missing/outdated, etc.. status: needs triage labels Feb 7, 2024
@michaelpj
Copy link
Collaborator Author

See also #4051 (comment)

@keithfancher
Copy link
Contributor

keithfancher commented Feb 7, 2024

See also #4051 (comment)

To clarify/summarize my somewhat-rambly comment: I agree!

A process like this makes a lot of sense to me:

  1. Filter all available server-side diagnostics based only on the incoming range from the client.
  2. Based on the results of that filter, i.e. still using the server-side diags, decide which code actions to show. (The messy bits: regexes and other matching logic, etc.)

And I think there's potential to consolidate some of the logic for both of these steps to simplify things for plugin authors a bit.

As for edge cases/exceptions... I'll leave it to the experts!

keithfancher added a commit to keithfancher/haskell-language-server that referenced this issue Feb 9, 2024
Rather than doing a full compare with incoming `Diagnostic` objects from
the client. This brings the "remove redundant imports/exports" code
actions more in line with behavior described in haskell#4056, and has the
pleasant side-effect of fixing broken code actions in neovim (haskell#3857).
fendor added a commit that referenced this issue Feb 21, 2024
…ions are in scope (#4063)

* Use *only* incoming range to determine which code actions are in scope

Rather than doing a full compare with incoming `Diagnostic` objects from
the client. This brings the "remove redundant imports/exports" code
actions more in line with behavior described in #4056, and has the
pleasant side-effect of fixing broken code actions in neovim (#3857).

* Remove redundant imports ;)

* Rename param for clarity

---------

Co-authored-by: fendor <fendor@users.noreply.github.com>
soulomoon pushed a commit to soulomoon/haskell-language-server that referenced this issue Feb 23, 2024
…ions are in scope (haskell#4063)

* Use *only* incoming range to determine which code actions are in scope

Rather than doing a full compare with incoming `Diagnostic` objects from
the client. This brings the "remove redundant imports/exports" code
actions more in line with behavior described in haskell#4056, and has the
pleasant side-effect of fixing broken code actions in neovim (haskell#3857).

* Remove redundant imports ;)

* Rename param for clarity

---------

Co-authored-by: fendor <fendor@users.noreply.github.com>
@fendor fendor added type: enhancement New feature or request and removed type: bug Something isn't right: doesn't work as intended, documentation is missing/outdated, etc.. status: needs triage labels Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants