[Repo Assist] fix: use TextEdit[] in onWillSaveTextDocument instead of executeCommand (fixes #35)#53
Draft
github-actions[bot] wants to merge 1 commit intomasterfrom
Conversation
The previous implementation dispatched `editor.action.formatDocument` inside `event.waitUntil()`. This has two problems: 1. `waitUntil` expects a `Thenable<TextEdit[]>` to apply edits as part of the save transaction, but `executeCommand` returns `Thenable<void>`. The edits were applied independently, outside of VS Code's save undo stack. 2. `editor.action.formatDocument` operates on the *active* text editor, not the document being saved. If a user saves a non-focused PHP tab (e.g. programmatically), the wrong document would be formatted. The fix calls `phpcbf.format(event.document)` directly, then wraps the result in `TextEdit[]` exactly as the DocumentFormattingProvider does. An empty array is returned on rejection (nothing to fix / error), which satisfies VS Code's API and avoids the timeout logged in issue #35. Closes #35 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This was referenced Mar 9, 2026
github-actions bot
added a commit
that referenced
this pull request
Mar 9, 2026
Documents the bug fixes and improvements from the open Repo Assist PRs: - Fix formatter hang on exit code 0 (PR #49, closes #39) - Fix phpcbf.enable=false bypass (PR #56) - Fix temp file leak on process error (PR #58) - Fix onWillSaveTextDocument save timeout (PR #53, closes #35) - Fix deprecated fs.exists (PR #51, closes #36) - Fix undefined showErrorMessage on unknown exit code (PR #61) - Add VS Code output channel for debug/error display (PR #50) - Refactor getArgs standard variable (PR #60) - Fix eslintrc and no-case-declarations lint issues Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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 is an automated PR from Repo Assist, an AI assistant.
Summary
Fixes a bug in
onWillSaveTextDocumentwhereeditor.action.formatDocumentwas dispatched insideevent.waitUntil()instead of returningTextEdit[]directly.Root Cause
The
TextDocumentWillSaveEvent.waitUntil()API accepts aThenable(TextEdit[])to apply edits atomically as part of the save transaction. The previous code passed aThenable(void)fromcommands.executeCommand("editor.action.formatDocument"), which has two problems:Wrong document:
editor.action.formatDocumentalways formats the active text editor, notevent.document. If a user saves a non-focused PHP tab, the active (wrong) document gets formatted instead.Outside the save transaction: Edits applied via the command bypass VS Code's save-edit mechanism. The
waitUntilAPI is designed to injectTextEdit[]directly into the pending save, so the changes are part of the same undo step.These issues can cause the
onWillSaveTextDocument-listener ERROR: timeoutreported in #35, especially when combined with phpcbf's Promise not settling on exit code 0 (a separate issue addressed by PR #49).Fix
Call
phpcbf.format(event.document)directly — exactly asprovideDocumentFormattingEditsdoes — and return[new vscode.TextEdit(range, text)]towaitUntil. On rejection (nothing to fix, or error), return[]so VS Code's save cycle completes normally without a timeout.Files Changed
extension.jscommands.executeCommand("editor.action.formatDocument")with directphpcbf.format(event.document)returningTextEdit[]Relationship to Other PRs
Closes #35
Test Status
This extension requires a live VS Code instance and cannot be tested headlessly.
Manual test steps:
"phpcbf.onsave": trueand"editor.formatOnSave": falsein settings