Skip to content

feat: defer code action execution#761

Open
Moosieus wants to merge 14 commits into
expert-lsp:mainfrom
Moosieus:remove-pipe-parenless-target
Open

feat: defer code action execution#761
Moosieus wants to merge 14 commits into
expert-lsp:mainfrom
Moosieus:remove-pipe-parenless-target

Conversation

@Moosieus

@Moosieus Moosieus commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

This pull request implements support for codeAction/resolve requests.

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 make textDocument/codeAction requests as users are editing, and the resolve request exists exactly to offload things accordingly.

How it works

The server now advertises resolveProvider based on client capability. When a client declares it can resolve the edit property (textDocument.codeAction.resolveSupport.properties contains "edit"), Expert responds to textDocument/codeAction with 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 sends codeAction/resolve with 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.CodeAction owns the deferred payload's schema via to_refactor_data/4 and from_refactor_data/1, so the wire format lives in one place rather than being spelled out at each producer and consumer.
  • The code-action Handler behaviour is unified on actions/4, removing per-handler reflection over an optional callback.
  • Formatter configuration is resolved once per request and shared across a request's refactorings, rather than re-read per action.

Not in scope / follow-ups

  • Request -> document discovery: Routing 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.

@doorgan doorgan self-requested a review July 8, 2026 15:34
Moosieus added 9 commits July 9, 2026 14:29
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.
@doorgan doorgan force-pushed the remove-pipe-parenless-target branch from f4e4915 to 4e5fd8e Compare July 9, 2026 17:29

@doorgan doorgan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread apps/engine/lib/engine/code_action/handlers/refactorex.ex Outdated
Comment thread apps/engine/lib/engine/code_action/handlers/refactorex.ex Outdated
Comment thread apps/engine/lib/engine/code_action/handlers/refactorex.ex Outdated
Comment thread apps/engine/lib/engine/code_action/handlers/refactorex.ex
Comment thread apps/engine/lib/engine/code_action/handler.ex Outdated
Comment thread apps/engine/lib/engine/code_action.ex
Comment thread apps/engine/test/engine/code_action/handlers/refactorex_test.exs
Comment thread apps/expert/lib/expert/document/lookup.ex Outdated
Comment thread apps/expert/lib/expert/provider/handlers/code_action_resolve.ex Outdated
Moosieus added 5 commits July 9, 2026 14:41
* `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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants