From d37afc56a81048a8b199ba0c8da4cff8dce63d2d Mon Sep 17 00:00:00 2001 From: elliot Date: Fri, 26 Sep 2025 16:04:52 -0400 Subject: [PATCH] Add cell-format test --- apps/vscode/src/test/examples/cell-format.qmd | 5 ++ apps/vscode/src/test/quartoDoc.test.ts | 64 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 apps/vscode/src/test/examples/cell-format.qmd diff --git a/apps/vscode/src/test/examples/cell-format.qmd b/apps/vscode/src/test/examples/cell-format.qmd new file mode 100644 index 00000000..c3fbd011 --- /dev/null +++ b/apps/vscode/src/test/examples/cell-format.qmd @@ -0,0 +1,5 @@ +See https://github.com/quarto-dev/quarto/issues/745 "Reformating R cell in VSCODE with air does not correctly close chunk." + +```{r} +list(foobar, foobar, foobar, foobar, foobar, foobar, foobar, foobar, foobar, foobar, foobar, foobar) +``` \ No newline at end of file diff --git a/apps/vscode/src/test/quartoDoc.test.ts b/apps/vscode/src/test/quartoDoc.test.ts index 9e0c804d..efbfb452 100644 --- a/apps/vscode/src/test/quartoDoc.test.ts +++ b/apps/vscode/src/test/quartoDoc.test.ts @@ -55,8 +55,72 @@ suite("Quarto basics", function () { assert.equal(vscode.window.activeTextEditor, editor, 'quarto extension interferes with other files opened in VSCode!'); }); + + test("quarto.formatCell deals with formatters that do or don't add trailing newline consistently", async function () { + + async function testFormatter(filename: string, [line, character]: [number, number], format: (sourceText: string) => string) { + const { doc } = await openAndShowTextDocument(filename); + + const formattingEditProvider = vscode.languages.registerDocumentFormattingEditProvider( + { scheme: 'file', language: 'r' }, + createFormatterFromStringFunc(format) + ); + + setCursorPosition(line, character); + await wait(450); + await vscode.commands.executeCommand("quarto.formatCell"); + await wait(450); + + const result = doc.getText(); + formattingEditProvider.dispose(); + await vscode.commands.executeCommand("workbench.action.closeActiveEditor"); + + return result; + } + + const newlineFormatterResult = await testFormatter( + "cell-format.qmd", + [3, 1], + (sourceText) => sourceText + 'FORMAT SUCCESS\n' + ); + const noopFormatterResult = await testFormatter( + "cell-format.qmd", + [3, 1], + (sourceText) => sourceText + 'FORMAT SUCCESS' + ); + + assert.ok(newlineFormatterResult.includes('FORMAT SUCCESS'), 'newlineFormatter failed'); + assert.ok(noopFormatterResult.includes('FORMAT SUCCESS'), 'noopFormatter failed'); + + assert.equal(newlineFormatterResult, noopFormatterResult); + }); + + suiteTeardown(() => { + vscode.window.showInformationMessage('All tests done!'); + }); }); +function createFormatterFromStringFunc(format: (sourceText: string) => string) { + return { + provideDocumentFormattingEdits(document: vscode.TextDocument): vscode.ProviderResult { + const fileStart = new vscode.Position(0, 0); + const fileEnd = document.lineAt(document.lineCount - 1).range.end; + + return [new vscode.TextEdit(new vscode.Range(fileStart, fileEnd), format(document.getText()))]; + } + }; +} + +function setCursorPosition(line: number, character: number) { + const editor = vscode.window.activeTextEditor; + if (editor) { + const position = new vscode.Position(line, character); + const newSelection = new vscode.Selection(position, position); + editor.selection = newSelection; + editor.revealRange(newSelection, vscode.TextEditorRevealType.InCenter); // Optional: scroll to the new position + } +} + /** * * When the test is run on the dev's machine for the first time, saves the roundtripped file as a snapshot.