From 59946adc1d2f14aa084a2238cf84cfc67c18df2f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sun, 22 Mar 2026 01:10:19 +0000 Subject: [PATCH] feat: register DocumentRangeFormattingEditProvider to enable editor.formatOnPaste phpcbf operates on the full document context, so range-only formatting is not meaningful. The range provider delegates to the same full-document pipeline as the existing DocumentFormattingEditProvider. This allows users to enable paste-triggered formatting with: "[php]": { "editor.defaultFormatter": "persoderlind.vscode-phpcbf", "editor.formatOnPaste": true } Closes #20 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- extension.js | 58 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/extension.js b/extension.js index 6457e71..8878864 100644 --- a/extension.js +++ b/extension.js @@ -292,30 +292,46 @@ exports.activate = context => { ); if (phpcbf.documentFormattingProvider) { + // Shared helper: run phpcbf on the full document and return TextEdits. + const getFullDocumentEdits = (document) => { + return new Promise((resolve, reject) => { + const originalText = document.getText(); + let lastLine = document.lineAt(document.lineCount - 1); + let range = new Range( + new Position(0, 0), + lastLine.range.end + ); + phpcbf + .format(document) + .then(text => { + if (text != originalText) { + resolve([new vscode.TextEdit(range, text)]); + } else { + reject(); + } + }) + .catch(err => { + console.log(err); + reject(); + }); + }); + }; + context.subscriptions.push( languages.registerDocumentFormattingEditProvider("php", { provideDocumentFormattingEdits: (document, options, token) => { - return new Promise((resolve, reject) => { - const originalText = document.getText(); - let lastLine = document.lineAt(document.lineCount - 1); - let range = new Range( - new Position(0, 0), - lastLine.range.end - ); - phpcbf - .format(document) - .then(text => { - if (text != originalText) { - resolve([new vscode.TextEdit(range, text)]); - } else { - reject(); - } - }) - .catch(err => { - console.log(err); - reject(); - }); - }); + return getFullDocumentEdits(document); + } + }) + ); + + // Register a range formatting provider so that VS Code's + // editor.formatOnPaste works. phpcbf always operates on the full + // document, so the range is ignored and the full document is fixed. + context.subscriptions.push( + languages.registerDocumentRangeFormattingEditProvider("php", { + provideDocumentRangeFormattingEdits: (document, range, options, token) => { + return getFullDocumentEdits(document); } }) );