Skip to content

[Repo Assist] fix: use TextEdit[] in onWillSaveTextDocument instead of executeCommand (fixes #35)#53

Draft
github-actions[bot] wants to merge 1 commit intomasterfrom
repo-assist/fix-issue-35-onsave-textedit-58ff6745aac72a0e
Draft

[Repo Assist] fix: use TextEdit[] in onWillSaveTextDocument instead of executeCommand (fixes #35)#53
github-actions[bot] wants to merge 1 commit intomasterfrom
repo-assist/fix-issue-35-onsave-textedit-58ff6745aac72a0e

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

@github-actions github-actions bot commented Mar 9, 2026

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

Summary

Fixes a bug in onWillSaveTextDocument where editor.action.formatDocument was dispatched inside event.waitUntil() instead of returning TextEdit[] directly.

Root Cause

The TextDocumentWillSaveEvent.waitUntil() API accepts a Thenable(TextEdit[]) to apply edits atomically as part of the save transaction. The previous code passed a Thenable(void) from commands.executeCommand("editor.action.formatDocument"), which has two problems:

  1. Wrong document: editor.action.formatDocument always formats the active text editor, not event.document. If a user saves a non-focused PHP tab, the active (wrong) document gets formatted instead.

  2. Outside the save transaction: Edits applied via the command bypass VS Code's save-edit mechanism. The waitUntil API is designed to inject TextEdit[] directly into the pending save, so the changes are part of the same undo step.

These issues can cause the onWillSaveTextDocument-listener ERROR: timeout reported 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 as provideDocumentFormattingEdits does — and return [new vscode.TextEdit(range, text)] to waitUntil. On rejection (nothing to fix, or error), return [] so VS Code's save cycle completes normally without a timeout.

Files Changed

File Change
extension.js Replace commands.executeCommand("editor.action.formatDocument") with direct phpcbf.format(event.document) returning TextEdit[]

Relationship to Other PRs

Closes #35

Test Status

This extension requires a live VS Code instance and cannot be tested headlessly.

Manual test steps:

  1. Set "phpcbf.onsave": true and "editor.formatOnSave": false in settings
  2. Open a PHP file with fixable errors and save → file should be corrected
  3. Open a PHP file already clean and save → save should complete immediately with no spinner hang
  4. Open two PHP tabs, focus one, then save the other → the correct (saved) file should be formatted, not the focused one

Generated by Repo Assist ·

Generated by Repo Assist ·

To install this agentic workflow, run

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

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>
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>
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.

onWillSaveTextDocument-listener error

0 participants