@@ -55,8 +55,72 @@ suite("Quarto basics", function () {
5555
5656 assert . equal ( vscode . window . activeTextEditor , editor , 'quarto extension interferes with other files opened in VSCode!' ) ;
5757 } ) ;
58+
59+ test ( "quarto.formatCell deals with formatters that do or don't add trailing newline consistently" , async function ( ) {
60+
61+ async function testFormatter ( filename : string , [ line , character ] : [ number , number ] , format : ( sourceText : string ) => string ) {
62+ const { doc } = await openAndShowTextDocument ( filename ) ;
63+
64+ const formattingEditProvider = vscode . languages . registerDocumentFormattingEditProvider (
65+ { scheme : 'file' , language : 'r' } ,
66+ createFormatterFromStringFunc ( format )
67+ ) ;
68+
69+ setCursorPosition ( line , character ) ;
70+ await wait ( 450 ) ;
71+ await vscode . commands . executeCommand ( "quarto.formatCell" ) ;
72+ await wait ( 450 ) ;
73+
74+ const result = doc . getText ( ) ;
75+ formattingEditProvider . dispose ( ) ;
76+ await vscode . commands . executeCommand ( "workbench.action.closeActiveEditor" ) ;
77+
78+ return result ;
79+ }
80+
81+ const newlineFormatterResult = await testFormatter (
82+ "cell-format.qmd" ,
83+ [ 3 , 1 ] ,
84+ ( sourceText ) => sourceText + 'FORMAT SUCCESS\n'
85+ ) ;
86+ const noopFormatterResult = await testFormatter (
87+ "cell-format.qmd" ,
88+ [ 3 , 1 ] ,
89+ ( sourceText ) => sourceText + 'FORMAT SUCCESS'
90+ ) ;
91+
92+ assert . ok ( newlineFormatterResult . includes ( 'FORMAT SUCCESS' ) , 'newlineFormatter failed' ) ;
93+ assert . ok ( noopFormatterResult . includes ( 'FORMAT SUCCESS' ) , 'noopFormatter failed' ) ;
94+
95+ assert . equal ( newlineFormatterResult , noopFormatterResult ) ;
96+ } ) ;
97+
98+ suiteTeardown ( ( ) => {
99+ vscode . window . showInformationMessage ( 'All tests done!' ) ;
100+ } ) ;
58101} ) ;
59102
103+ function createFormatterFromStringFunc ( format : ( sourceText : string ) => string ) {
104+ return {
105+ provideDocumentFormattingEdits ( document : vscode . TextDocument ) : vscode . ProviderResult < vscode . TextEdit [ ] > {
106+ const fileStart = new vscode . Position ( 0 , 0 ) ;
107+ const fileEnd = document . lineAt ( document . lineCount - 1 ) . range . end ;
108+
109+ return [ new vscode . TextEdit ( new vscode . Range ( fileStart , fileEnd ) , format ( document . getText ( ) ) ) ] ;
110+ }
111+ } ;
112+ }
113+
114+ function setCursorPosition ( line : number , character : number ) {
115+ const editor = vscode . window . activeTextEditor ;
116+ if ( editor ) {
117+ const position = new vscode . Position ( line , character ) ;
118+ const newSelection = new vscode . Selection ( position , position ) ;
119+ editor . selection = newSelection ;
120+ editor . revealRange ( newSelection , vscode . TextEditorRevealType . InCenter ) ; // Optional: scroll to the new position
121+ }
122+ }
123+
60124/**
61125 *
62126 * When the test is run on the dev's machine for the first time, saves the roundtripped file as a snapshot.
0 commit comments