feat: defer code action execution#761
Open
Moosieus wants to merge 14 commits into
Open
Conversation
…rget like `arg |> foo`, and fix the crash.
Language clients make frequent `textDocument/codeAction` requests as users are editing. Currently Expert will eagerly compute possible refactors for these requests, even though users haven't expressly executed any. This commit adds support for `codeAction/resolve`, to allow refactor execution to be deferred until users expressly select one. This removes computing refactors from the `codeAction` hot path when language clients support doing so. - `Forge.Refactor`: split `available_refactorings/4` into `list/3` + `execute/3` - `Forge.CodeAction`: `deferred/4` carrying the resolve data payload - `Engine` handler: `:defer_edits?` option + `resolve/3`, with per-refactoring isolation on the eager fallback - Expert: advertise `resolve_provider` when the client resolves `"edit"`; route `codeAction/resolve` to a new handler - Unify the code action Handler behaviour on `actions/4`
- Gate codeAction/resolve on engine readiness, and give it a resolved context document, by matching the uri in its data payload in Expert.document_request?/1 and Expert.Document.Lookup.extract_uri/1. Remove context resolution from the handler since it's handled upstream. - Map resolve failures to their LSP error codes (`ContentModified` for a stale document or a refactoring that no longer applies, `InvalidParams` for a malformed payload), echo back actions that aren't ours, and validate the round-tripped range against the current document instead of trusting it.
ast_to_changes looked up the formatter (a global lock plus a .formatter.exs disk read) once per refactoring on the eager path. Resolve it once per request and share it, and skip the lookup entirely when no refactorings apply so the common case stays off it.
The wire shape of a deferred refactor action's data payload (its field set and nested range structure) was written by hand in the engine handler and parsed by hand in the resolve handler. Move it behind `Forge.CodeAction.to_refactor_data/4` and `from_refactor_data/1` so the schema lives in one place. The routing seams keep head-matching the provider tag, which is request identity rather than payload structure.
Fix a stale comment referencing the renamed `from_refactor_data`, and extract the duplicated resolve-capable `ClientCapabilities` builder into `Expert.Test.ConfigurationSupport`.
execute_eagerly rescued raises but not throws/exits, so a refactoring that exited could fail the whole codeAction request. Catch all three so one broken rewrite only loses its own action.
f4e4915 to
4e5fd8e
Compare
doorgan
reviewed
Jul 9, 2026
doorgan
left a comment
Contributor
There was a problem hiding this comment.
Generally speaking the code looks good, I tested this and it seems to work as advertised
I have several comments, mostly around noisy comments which are probably byproduct/leftover of development.
The only comments I have some real concerns are about error handling, we should not be dropping stacktraces and we should not guard against process failures in code actions code.
* `refactor.ex` - Streamline comments, remove `catch` block. * `handler.ex` - Remove out of scope comments. * `code_action.ex` - Streamline moduledoc, keep note about the hot path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request implements support for
codeAction/resolverequests.Prior to this pull request, Expert did not support
codeAction/resolve, which meant all possible code actions (including refactors) were evaluated eagerly and sent to the client, even when a user did not expressly request them. This led to an outsized number of errors and excess processing that would effectively be discarded. Language server clients frequently maketextDocument/codeActionrequests as users are editing, and the resolve request exists exactly to offload things accordingly.How it works
The server now advertises
resolveProviderbased on client capability. When a client declares it can resolve the edit property (textDocument.codeAction.resolveSupport.propertiescontains"edit"), Expert responds totextDocument/codeActionwith a list of actions that carry only their title, kind, and an opaque data payload — not their edits. When the user actually invokes one, the client sendscodeAction/resolvewith that payload, and only then does Expert compute the edit for that single action.Clients that don't support resolve fall back to the previous eager behavior where edits are computed inline, so no client regresses.
Ancillary: RemovePipe crash fix
This addresses a specific crash I ran into, which begot all the above. Included here since it's what surfaced the problem, but it stands on its own.
Implementation notes
Forge.CodeActionowns the deferred payload's schema viato_refactor_data/4andfrom_refactor_data/1, so the wire format lives in one place rather than being spelled out at each producer and consumer.actions/4, removing per-handler reflection over an optional callback.Not in scope / follow-ups
codeAction/resolve(whose document URI lives in its data payload rather than a textDocument field) currently adds small special-case clauses to the request-dispatch predicates. If more special cases accrete, the dispatch mechanism can be reconsidered.