From e4a53658876b45634e69e0144db5f70cdd31f927 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Mon, 9 Mar 2026 17:09:27 +0000 Subject: [PATCH] fix: use TextEdit[] in onWillSaveTextDocument instead of executeCommand The previous implementation dispatched `editor.action.formatDocument` inside `event.waitUntil()`. This has two problems: 1. `waitUntil` expects a `Thenable` to apply edits as part of the save transaction, but `executeCommand` returns `Thenable`. 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> --- extension.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/extension.js b/extension.js index 81a61ce..476b850 100644 --- a/extension.js +++ b/extension.js @@ -290,8 +290,19 @@ exports.activate = context => { .getConfiguration("editor", event.document.uri) .get("formatOnSave") === false ) { + const originalText = event.document.getText(); event.waitUntil( - commands.executeCommand("editor.action.formatDocument") + phpcbf.format(event.document).then(text => { + if (text !== originalText) { + const lastLine = event.document.lineAt(event.document.lineCount - 1); + const range = new Range( + new Position(0, 0), + lastLine.range.end + ); + return [new vscode.TextEdit(range, text)]; + } + return []; + }).catch(() => []) ); } })