@@ -34,6 +34,9 @@ vi.mock("vscode", () => ({
3434 fs : {
3535 stat : vi . fn ( ) ,
3636 } ,
37+ getConfiguration : vi . fn ( ( ) => ( {
38+ get : vi . fn ( ) . mockReturnValue ( false ) , // Default value for experimentalPreventFocusDisruption
39+ } ) ) ,
3740 } ,
3841 window : {
3942 createTextEditorDecorationType : vi . fn ( ) ,
@@ -81,6 +84,7 @@ vi.mock("vscode", () => ({
8184 InCenter : 2 ,
8285 } ,
8386 TabInputTextDiff : class TabInputTextDiff { } ,
87+ TabInputText : class TabInputText { } ,
8488 Uri : {
8589 file : vi . fn ( ( path ) => ( { fsPath : path } ) ) ,
8690 parse : vi . fn ( ( uri ) => ( { with : vi . fn ( ( ) => ( { } ) ) } ) ) ,
@@ -188,7 +192,7 @@ describe("DiffViewProvider", () => {
188192 // Mock showTextDocument to track when it's called
189193 vi . mocked ( vscode . window . showTextDocument ) . mockImplementation ( async ( uri , options ) => {
190194 callOrder . push ( "showTextDocument" )
191- expect ( options ) . toEqual ( { preview : false , viewColumn : vscode . ViewColumn . Active , preserveFocus : true } )
195+ expect ( options ) . toEqual ( { preview : false , viewColumn : vscode . ViewColumn . Active , preserveFocus : false } )
192196 return mockEditor as any
193197 } )
194198
@@ -220,10 +224,10 @@ describe("DiffViewProvider", () => {
220224 // Verify that showTextDocument was called before executeCommand
221225 expect ( callOrder ) . toEqual ( [ "showTextDocument" , "executeCommand" ] )
222226
223- // Verify that showTextDocument was called with preview: false and preserveFocus: true
227+ // Verify that showTextDocument was called with preview: false and preserveFocus: false (default)
224228 expect ( vscode . window . showTextDocument ) . toHaveBeenCalledWith (
225229 expect . objectContaining ( { fsPath : `${ mockCwd } /test.md` } ) ,
226- { preview : false , viewColumn : vscode . ViewColumn . Active , preserveFocus : true } ,
230+ { preview : false , viewColumn : vscode . ViewColumn . Active , preserveFocus : false } ,
227231 )
228232
229233 // Verify that the diff command was executed
@@ -232,7 +236,7 @@ describe("DiffViewProvider", () => {
232236 expect . any ( Object ) ,
233237 expect . any ( Object ) ,
234238 `test.md: ${ DIFF_VIEW_LABEL_CHANGES } (Editable)` ,
235- { preserveFocus : true } ,
239+ { preserveFocus : false } ,
236240 )
237241 } )
238242
@@ -418,4 +422,187 @@ describe("DiffViewProvider", () => {
418422 expect ( vscode . languages . getDiagnostics ) . toHaveBeenCalled ( )
419423 } )
420424 } )
425+
426+ describe ( "experimentalPreventFocusDisruption setting" , ( ) => {
427+ it ( "should preserve focus when experimentalPreventFocusDisruption is enabled" , async ( ) => {
428+ // Mock the configuration to return true for experimentalPreventFocusDisruption
429+ vi . mocked ( vscode . workspace . getConfiguration ) . mockReturnValue ( {
430+ get : vi . fn ( ) . mockReturnValue ( true ) ,
431+ } as any )
432+
433+ // Setup mock editor
434+ const mockEditor = {
435+ document : {
436+ uri : { fsPath : `${ mockCwd } /test.ts` } ,
437+ getText : vi . fn ( ) . mockReturnValue ( "" ) ,
438+ lineCount : 0 ,
439+ } ,
440+ selection : {
441+ active : { line : 0 , character : 0 } ,
442+ anchor : { line : 0 , character : 0 } ,
443+ } ,
444+ edit : vi . fn ( ) . mockResolvedValue ( true ) ,
445+ revealRange : vi . fn ( ) ,
446+ }
447+
448+ // Mock showTextDocument
449+ vi . mocked ( vscode . window . showTextDocument ) . mockResolvedValue ( mockEditor as any )
450+
451+ // Mock workspace.onDidOpenTextDocument
452+ vi . mocked ( vscode . workspace . onDidOpenTextDocument ) . mockImplementation ( ( callback ) => {
453+ setTimeout ( ( ) => {
454+ callback ( { uri : { fsPath : `${ mockCwd } /test.ts` } } as any )
455+ } , 0 )
456+ return { dispose : vi . fn ( ) }
457+ } )
458+
459+ // Mock window.visibleTextEditors
460+ vi . mocked ( vscode . window ) . visibleTextEditors = [ mockEditor as any ]
461+
462+ // Set up for file
463+ ; ( diffViewProvider as any ) . editType = "modify"
464+
465+ // Execute open
466+ await diffViewProvider . open ( "test.ts" )
467+
468+ // Verify that showTextDocument was called with preserveFocus: true
469+ expect ( vscode . window . showTextDocument ) . toHaveBeenCalledWith (
470+ expect . objectContaining ( { fsPath : `${ mockCwd } /test.ts` } ) ,
471+ { preview : false , viewColumn : vscode . ViewColumn . Active , preserveFocus : true } ,
472+ )
473+
474+ // Verify that the diff command was executed with preserveFocus: true
475+ expect ( vscode . commands . executeCommand ) . toHaveBeenCalledWith (
476+ "vscode.diff" ,
477+ expect . any ( Object ) ,
478+ expect . any ( Object ) ,
479+ expect . any ( String ) ,
480+ { preserveFocus : true } ,
481+ )
482+ } )
483+
484+ it ( "should not preserve focus when experimentalPreventFocusDisruption is disabled" , async ( ) => {
485+ // Mock the configuration to return false for experimentalPreventFocusDisruption
486+ vi . mocked ( vscode . workspace . getConfiguration ) . mockReturnValue ( {
487+ get : vi . fn ( ) . mockReturnValue ( false ) ,
488+ } as any )
489+
490+ // Setup mock editor
491+ const mockEditor = {
492+ document : {
493+ uri : { fsPath : `${ mockCwd } /test.ts` } ,
494+ getText : vi . fn ( ) . mockReturnValue ( "" ) ,
495+ lineCount : 0 ,
496+ } ,
497+ selection : {
498+ active : { line : 0 , character : 0 } ,
499+ anchor : { line : 0 , character : 0 } ,
500+ } ,
501+ edit : vi . fn ( ) . mockResolvedValue ( true ) ,
502+ revealRange : vi . fn ( ) ,
503+ }
504+
505+ // Mock showTextDocument
506+ vi . mocked ( vscode . window . showTextDocument ) . mockResolvedValue ( mockEditor as any )
507+
508+ // Mock workspace.onDidOpenTextDocument
509+ vi . mocked ( vscode . workspace . onDidOpenTextDocument ) . mockImplementation ( ( callback ) => {
510+ setTimeout ( ( ) => {
511+ callback ( { uri : { fsPath : `${ mockCwd } /test.ts` } } as any )
512+ } , 0 )
513+ return { dispose : vi . fn ( ) }
514+ } )
515+
516+ // Mock window.visibleTextEditors
517+ vi . mocked ( vscode . window ) . visibleTextEditors = [ mockEditor as any ]
518+
519+ // Set up for file
520+ ; ( diffViewProvider as any ) . editType = "modify"
521+
522+ // Execute open
523+ await diffViewProvider . open ( "test.ts" )
524+
525+ // Verify that showTextDocument was called with preserveFocus: false
526+ expect ( vscode . window . showTextDocument ) . toHaveBeenCalledWith (
527+ expect . objectContaining ( { fsPath : `${ mockCwd } /test.ts` } ) ,
528+ { preview : false , viewColumn : vscode . ViewColumn . Active , preserveFocus : false } ,
529+ )
530+
531+ // Verify that the diff command was executed with preserveFocus: false
532+ expect ( vscode . commands . executeCommand ) . toHaveBeenCalledWith (
533+ "vscode.diff" ,
534+ expect . any ( Object ) ,
535+ expect . any ( Object ) ,
536+ expect . any ( String ) ,
537+ { preserveFocus : false } ,
538+ )
539+ } )
540+
541+ it ( "should preserve focus in saveChanges when experimentalPreventFocusDisruption is enabled" , async ( ) => {
542+ // Mock the configuration to return true
543+ vi . mocked ( vscode . workspace . getConfiguration ) . mockReturnValue ( {
544+ get : vi . fn ( ) . mockReturnValue ( true ) ,
545+ } as any )
546+
547+ // Setup for saveChanges
548+ ; ( diffViewProvider as any ) . relPath = "test.ts"
549+ ; ( diffViewProvider as any ) . newContent = "new content"
550+ ; ( diffViewProvider as any ) . activeDiffEditor = {
551+ document : {
552+ getText : vi . fn ( ) . mockReturnValue ( "new content" ) ,
553+ isDirty : false ,
554+ save : vi . fn ( ) . mockResolvedValue ( undefined ) ,
555+ } ,
556+ }
557+ ; ( diffViewProvider as any ) . preDiagnostics = [ ]
558+ ; ( diffViewProvider as any ) . closeAllDiffViews = vi . fn ( ) . mockResolvedValue ( undefined )
559+
560+ // Mock vscode functions
561+ vi . mocked ( vscode . window . showTextDocument ) . mockResolvedValue ( { } as any )
562+ vi . mocked ( vscode . languages . getDiagnostics ) . mockReturnValue ( [ ] )
563+
564+ await diffViewProvider . saveChanges ( false ) // Disable diagnostics for simplicity
565+
566+ // Verify showTextDocument was called with preserveFocus: true
567+ expect ( vscode . window . showTextDocument ) . toHaveBeenCalledWith (
568+ expect . objectContaining ( { fsPath : `${ mockCwd } /test.ts` } ) ,
569+ { preview : false , preserveFocus : true } ,
570+ )
571+ } )
572+
573+ it ( "should preserve focus in revertChanges when experimentalPreventFocusDisruption is enabled" , async ( ) => {
574+ // Mock the configuration to return true
575+ vi . mocked ( vscode . workspace . getConfiguration ) . mockReturnValue ( {
576+ get : vi . fn ( ) . mockReturnValue ( true ) ,
577+ } as any )
578+
579+ // Setup for revertChanges
580+ ; ( diffViewProvider as any ) . relPath = "test.ts"
581+ ; ( diffViewProvider as any ) . editType = "modify"
582+ ; ( diffViewProvider as any ) . documentWasOpen = true
583+ ; ( diffViewProvider as any ) . originalContent = "original content"
584+ ; ( diffViewProvider as any ) . activeDiffEditor = {
585+ document : {
586+ uri : { fsPath : `${ mockCwd } /test.ts` } ,
587+ getText : vi . fn ( ) . mockReturnValue ( "modified content" ) ,
588+ isDirty : false ,
589+ save : vi . fn ( ) . mockResolvedValue ( undefined ) ,
590+ positionAt : vi . fn ( ) . mockReturnValue ( { line : 0 , character : 0 } ) ,
591+ } ,
592+ }
593+ ; ( diffViewProvider as any ) . closeAllDiffViews = vi . fn ( ) . mockResolvedValue ( undefined )
594+ ; ( diffViewProvider as any ) . reset = vi . fn ( ) . mockResolvedValue ( undefined )
595+
596+ // Mock vscode functions
597+ vi . mocked ( vscode . window . showTextDocument ) . mockResolvedValue ( { } as any )
598+
599+ await diffViewProvider . revertChanges ( )
600+
601+ // Verify showTextDocument was called with preserveFocus: true
602+ expect ( vscode . window . showTextDocument ) . toHaveBeenCalledWith (
603+ expect . objectContaining ( { fsPath : `${ mockCwd } /test.ts` } ) ,
604+ { preview : false , preserveFocus : true } ,
605+ )
606+ } )
607+ } )
421608} )
0 commit comments