@@ -3257,7 +3257,8 @@ export class DefaultClient implements Client {
3257
3257
}
3258
3258
const formatEdits : vscode . WorkspaceEdit = new vscode . WorkspaceEdit ( ) ;
3259
3259
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 } ) ;
3261
3262
if ( formatTextEdits && formatTextEdits . length > 0 ) {
3262
3263
formatEdits . set ( uri , formatTextEdits ) ;
3263
3264
}
@@ -3539,7 +3540,7 @@ export class DefaultClient implements Client {
3539
3540
if ( edit . newText . includes ( "#pragma once" ) ) {
3540
3541
// Commit this so that it can be undone separately, to avoid leaving an empty file,
3541
3542
// 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 } ) ;
3543
3544
headerFileLineOffset = nextLineOffset ;
3544
3545
workspaceEdits = new vscode . WorkspaceEdit ( ) ;
3545
3546
continue ;
@@ -3599,10 +3600,9 @@ export class DefaultClient implements Client {
3599
3600
}
3600
3601
3601
3602
// Apply the extract to function text edits.
3602
- await vscode . workspace . applyEdit ( workspaceEdits ) ;
3603
+ await vscode . workspace . applyEdit ( workspaceEdits , { isRefactoring : true } ) ;
3603
3604
3604
3605
const firstUri : vscode . Uri = formatUriAndRanges [ 0 ] . uri ;
3605
- await vscode . window . showTextDocument ( firstUri , { selection : replaceEditRange } ) ;
3606
3606
3607
3607
// Format the new text edits.
3608
3608
const formatEdits : vscode . WorkspaceEdit = new vscode . WorkspaceEdit ( ) ;
@@ -3611,7 +3611,8 @@ export class DefaultClient implements Client {
3611
3611
const formatOptions : vscode . FormattingOptions = {
3612
3612
insertSpaces : settings . editorInsertSpaces ?? true ,
3613
3613
tabSize : settings . editorTabSize ?? 4 ,
3614
- onChanges : true
3614
+ onChanges : true ,
3615
+ preserveFocus : true
3615
3616
} ;
3616
3617
3617
3618
const doFormat = async ( ) => {
@@ -3641,8 +3642,12 @@ export class DefaultClient implements Client {
3641
3642
}
3642
3643
3643
3644
if ( formatEdits . size > 0 ) {
3644
- await vscode . workspace . applyEdit ( formatEdits ) ;
3645
+ await vscode . workspace . applyEdit ( formatEdits , { isRefactoring : true } ) ;
3645
3646
}
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 } ) ;
3646
3651
}
3647
3652
3648
3653
public onInterval ( ) : void {
0 commit comments