Skip to content

[Repo Assist] feat: honour CancellationToken in provideDocumentFormattingEdits#81

Draft
github-actions[bot] wants to merge 1 commit intomasterfrom
repo-assist/improve-cancellation-token-2026-03-14-77ee77f29c3070b2
Draft

[Repo Assist] feat: honour CancellationToken in provideDocumentFormattingEdits#81
github-actions[bot] wants to merge 1 commit intomasterfrom
repo-assist/improve-cancellation-token-2026-03-14-77ee77f29c3070b2

Conversation

@github-actions
Copy link
Contributor

🤖 This is an automated draft PR from Repo Assist, an AI assistant.

Summary

The CancellationToken passed to provideDocumentFormattingEdits was previously ignored. This means that when VS Code cancels a format request (e.g. user makes a new edit, closes the document, or retriggers formatting), the extension kept waiting for phpcbf to finish — even though the result would be discarded.

Changes

Three improvements in provideDocumentFormattingEdits:

  1. Fast path: if the token is already cancelled before format() is called, return [] immediately.
  2. Cancellation listener: register token.onCancellationRequested to reject the Promise the moment VS Code cancels, so VS Code stops waiting even while phpcbf is still running in the background.
  3. Race check: after format() resolves, re-check token.isCancellationRequested to catch cancellations that arrived during the phpcbf run.
  4. Listener disposal: the cancellation listener is disposed in all code paths (resolve, reject, catch) to prevent event listener leaks.
// Before
provideDocumentFormattingEdits: (document, options, token) => {
    return new Promise((resolve, reject) => {
        phpcbf.format(document).then(...).catch(...);
    });
}

// After
provideDocumentFormattingEdits: (document, options, token) => {
    if (token.isCancellationRequested) {
        return Promise.resolve([]);
    }
    return new Promise((resolve, reject) => {
        const cancellationListener = token.onCancellationRequested(() => {
            cancellationListener.dispose();
            reject();
        });
        phpcbf.format(document)
            .then(text => {
                cancellationListener.dispose();
                if (token.isCancellationRequested) { reject(); return; }
                ...
            })
            .catch(err => { cancellationListener.dispose(); ... });
    });
}

Why This Matters

When a user types quickly while the formatter is running, VS Code cancels the in-flight format request and starts a new one. Without cancellation support, multiple format operations pile up waiting for phpcbf responses that VS Code will never use. With this change, VS Code's cancellation is respected and the Promise rejects immediately, keeping the extension responsive.

No Conflicts

This change is entirely within the activate() function's registerDocumentFormattingEditProvider block and does not touch any of the areas modified by the other open Repo Assist PRs (#48, #49, #51, #53, #56, #58, #60, #61, #63, #65, #68, #69, #73, etc.).

Test Status

  • npm run test:unit — 7/7 unit tests pass
  • VS Code integration tests are not runnable in CI without a display server (infrastructure limitation)

Generated by Repo Assist ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@346204513ecfa08b81566450d7d599556807389f

The CancellationToken passed to provideDocumentFormattingEdits was
previously unused. VS Code issues cancellations when the user closes
a document, makes a new edit, or retriggers formatting before the
previous request completes.

This change:
1. Returns an empty edit list immediately if the token is already
   cancelled before format() is called (fast path).
2. Registers token.onCancellationRequested to reject the Promise as
   soon as VS Code cancels, so VS Code stops waiting for the result
   even while phpcbf is still running.
3. Double-checks token.isCancellationRequested when format() resolves
   so that a racing cancellation is also caught.
4. Disposes the cancellation listener in all code paths to avoid leaks.

This prevents VS Code from waiting unnecessarily for a phpcbf process
whose output will be discarded, improving responsiveness when the user
edits or closes a file mid-format.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants