@@ -3257,7 +3257,8 @@ export class DefaultClient implements Client {
32573257 }
32583258 const formatEdits : vscode . WorkspaceEdit = new vscode . WorkspaceEdit ( ) ;
32593259 for ( const uri of editedFiles ) {
3260- const formatTextEdits : vscode . TextEdit [ ] | undefined = await vscode . commands . executeCommand < vscode . TextEdit [ ] | undefined > ( "vscode.executeFormatDocumentProvider" , uri , { onChanges : true } ) ;
3260+ const formatTextEdits : vscode . TextEdit [ ] | undefined = await vscode . commands . executeCommand < vscode . TextEdit [ ] | undefined > (
3261+ "vscode.executeFormatDocumentProvider" , uri , { onChanges : true , preserveFocus : false } ) ;
32613262 if ( formatTextEdits && formatTextEdits . length > 0 ) {
32623263 formatEdits . set ( uri , formatTextEdits ) ;
32633264 }
@@ -3539,7 +3540,7 @@ export class DefaultClient implements Client {
35393540 if ( edit . newText . includes ( "#pragma once" ) ) {
35403541 // Commit this so that it can be undone separately, to avoid leaving an empty file,
35413542 // which causes the next refactor to not add the #pragma once.
3542- await vscode . workspace . applyEdit ( workspaceEdits ) ;
3543+ await vscode . workspace . applyEdit ( workspaceEdits , { isRefactoring : true } ) ;
35433544 headerFileLineOffset = nextLineOffset ;
35443545 workspaceEdits = new vscode . WorkspaceEdit ( ) ;
35453546 continue ;
@@ -3599,10 +3600,9 @@ export class DefaultClient implements Client {
35993600 }
36003601
36013602 // Apply the extract to function text edits.
3602- await vscode . workspace . applyEdit ( workspaceEdits ) ;
3603+ await vscode . workspace . applyEdit ( workspaceEdits , { isRefactoring : true } ) ;
36033604
36043605 const firstUri : vscode . Uri = formatUriAndRanges [ 0 ] . uri ;
3605- await vscode . window . showTextDocument ( firstUri , { selection : replaceEditRange } ) ;
36063606
36073607 // Format the new text edits.
36083608 const formatEdits : vscode . WorkspaceEdit = new vscode . WorkspaceEdit ( ) ;
@@ -3611,7 +3611,8 @@ export class DefaultClient implements Client {
36113611 const formatOptions : vscode . FormattingOptions = {
36123612 insertSpaces : settings . editorInsertSpaces ?? true ,
36133613 tabSize : settings . editorTabSize ?? 4 ,
3614- onChanges : true
3614+ onChanges : true ,
3615+ preserveFocus : true
36153616 } ;
36163617
36173618 const doFormat = async ( ) => {
@@ -3641,8 +3642,12 @@ export class DefaultClient implements Client {
36413642 }
36423643
36433644 if ( formatEdits . size > 0 ) {
3644- await vscode . workspace . applyEdit ( formatEdits ) ;
3645+ await vscode . workspace . applyEdit ( formatEdits , { isRefactoring : true } ) ;
36453646 }
3647+
3648+ // This is required to be done after the formatting is done, because that can trigger the
3649+ // active document to switch to the wrong file (the header).
3650+ await vscode . window . showTextDocument ( firstUri , { selection : replaceEditRange , preserveFocus : false } ) ;
36463651 }
36473652
36483653 public onInterval ( ) : void {
0 commit comments