@@ -41,8 +41,72 @@ suite("Quarto basics", function () {
4141 roundtripSnapshotTest ( 'capsule-leak.qmd' ) ;
4242
4343 roundtripSnapshotTest ( 'attr-equals.qmd' ) ;
44+
45+ test ( "quarto.formatCell deals with formatters that do or don't add trailing newline consistently" , async function ( ) {
46+
47+ async function testFormatter ( filename : string , [ line , character ] : [ number , number ] , format : ( sourceText : string ) => string ) {
48+ const { doc } = await openAndShowTextDocument ( filename ) ;
49+
50+ const formattingEditProvider = vscode . languages . registerDocumentFormattingEditProvider (
51+ { scheme : 'file' , language : 'r' } ,
52+ createFormatterFromStringFunc ( format )
53+ )
54+
55+ setCursorPosition ( line , character ) ;
56+ await wait ( 450 ) ;
57+ await vscode . commands . executeCommand ( "quarto.formatCell" ) ;
58+ await wait ( 450 )
59+
60+ const result = doc . getText ( )
61+ formattingEditProvider . dispose ( )
62+ await vscode . commands . executeCommand ( "workbench.action.closeActiveEditor" ) ;
63+
64+ return result
65+ }
66+
67+ const newlineFormatterResult = await testFormatter (
68+ "cell-format.qmd" ,
69+ [ 3 , 1 ] ,
70+ ( sourceText ) => sourceText + 'FORMAT SUCCESS\n'
71+ )
72+ const noopFormatterResult = await testFormatter (
73+ "cell-format.qmd" ,
74+ [ 3 , 1 ] ,
75+ ( sourceText ) => sourceText + 'FORMAT SUCCESS'
76+ )
77+
78+ assert . ok ( newlineFormatterResult . includes ( 'FORMAT SUCCESS' ) , 'newlineFormatter failed' )
79+ assert . ok ( noopFormatterResult . includes ( 'FORMAT SUCCESS' ) , 'noopFormatter failed' )
80+
81+ assert . equal ( newlineFormatterResult , noopFormatterResult )
82+ } ) ;
83+
84+ suiteTeardown ( ( ) => {
85+ vscode . window . showInformationMessage ( 'All tests done!' ) ;
86+ } ) ;
4487} ) ;
4588
89+ function createFormatterFromStringFunc ( format : ( sourceText : string ) => string ) {
90+ return {
91+ provideDocumentFormattingEdits ( document : vscode . TextDocument ) : vscode . ProviderResult < vscode . TextEdit [ ] > {
92+ const fileStart = new vscode . Position ( 0 , 0 ) ;
93+ const fileEnd = document . lineAt ( document . lineCount - 1 ) . range . end ;
94+
95+ return [ new vscode . TextEdit ( new vscode . Range ( fileStart , fileEnd ) , format ( document . getText ( ) ) ) ] ;
96+ }
97+ }
98+ }
99+
100+ function setCursorPosition ( line : number , character : number ) {
101+ const editor = vscode . window . activeTextEditor ;
102+ if ( editor ) {
103+ const position = new vscode . Position ( line , character ) ;
104+ const newSelection = new vscode . Selection ( position , position ) ;
105+ editor . selection = newSelection ;
106+ editor . revealRange ( newSelection , vscode . TextEditorRevealType . InCenter ) ; // Optional: scroll to the new position
107+ }
108+ }
109+
46110/**
47111 *
48112 * When the test is run on the dev's machine for the first time, saves the roundtripped file as a snapshot.
0 commit comments