@@ -3495,11 +3495,13 @@ export class DefaultClient implements Client {
3495
3495
}
3496
3496
3497
3497
let workspaceEdits : vscode . WorkspaceEdit = new vscode . WorkspaceEdit ( ) ;
3498
- let replaceEditRange : vscode . Range | undefined ;
3499
- let hasProcessedReplace : boolean = false ;
3498
+
3500
3499
// NOTE: References to source/header are in reference to the more common case when it's
3501
3500
// invoked on the source file (alternatively named the first file). When invoked on the header file,
3502
3501
// the header file operates as if it were the source file (isSourceFile stays true).
3502
+ let sourceReplaceEditRange : vscode . Range | undefined ;
3503
+ let headerReplaceEditRange : vscode . Range | undefined ;
3504
+ let hasProcessedReplace : boolean = false ;
3503
3505
const sourceFormatUriAndRanges : VsCodeUriAndRange [ ] = [ ] ;
3504
3506
const headerFormatUriAndRanges : VsCodeUriAndRange [ ] = [ ] ;
3505
3507
let lineOffset : number = 0 ;
@@ -3575,7 +3577,7 @@ export class DefaultClient implements Client {
3575
3577
} else {
3576
3578
headerFormatUriAndRanges . push ( { uri, range : newRange } ) ;
3577
3579
}
3578
- if ( isReplace ) {
3580
+ if ( isReplace || ! isSourceFile ) {
3579
3581
// Handle additional declaration lines added before the new function call.
3580
3582
let currentText : string = edit . newText . substring ( rangeStartCharacter ) ;
3581
3583
let currentTextNextLineStart : number = currentText . indexOf ( "\n" ) ;
@@ -3596,35 +3598,38 @@ export class DefaultClient implements Client {
3596
3598
"Extract to function failed. An invalid edit was generated: '{0}'" , edit . newText ) } `) ;
3597
3599
continue ;
3598
3600
}
3599
- replaceEditRange = new vscode . Range (
3601
+ const replaceEditRange = new vscode . Range (
3600
3602
new vscode . Position ( rangeStartLine , rangeStartCharacter ) ,
3601
3603
new vscode . Position ( rangeStartLine , rangeStartCharacter + functionName . length ) ) ;
3604
+ if ( isSourceFile ) {
3605
+ sourceReplaceEditRange = replaceEditRange ;
3606
+ } else {
3607
+ headerReplaceEditRange = replaceEditRange ;
3608
+ }
3602
3609
nextLineOffset -= range . end . line - range . start . line ;
3603
3610
}
3604
3611
}
3605
3612
}
3606
3613
3607
- if ( replaceEditRange === undefined || sourceFormatUriAndRanges . length === 0 ) {
3614
+ if ( sourceReplaceEditRange === undefined || sourceFormatUriAndRanges . length === 0 ) {
3608
3615
return ;
3609
3616
}
3610
3617
3611
- if ( headerFormatUriAndRanges . length > 0 ) {
3618
+ // Apply the extract to function text edits.
3619
+ await vscode . workspace . applyEdit ( workspaceEdits , { isRefactoring : true } ) ;
3620
+
3621
+ if ( headerFormatUriAndRanges . length > 0 && headerReplaceEditRange !== undefined ) {
3612
3622
// The header needs to be open and shown or the formatting will fail
3613
3623
// (due to issues/requirements in the cpptools process).
3614
3624
// It also seems strange and undesirable to have the header modified
3615
3625
// without being opened because otherwise users may not realize that
3616
3626
// the header had changed (unless they view source control differences).
3617
- await vscode . window . showTextDocument ( headerFormatUriAndRanges [ 0 ] . uri , { preserveFocus : true } ) ;
3627
+ await vscode . window . showTextDocument ( headerFormatUriAndRanges [ 0 ] . uri , {
3628
+ selection : headerReplaceEditRange , preserveFocus : false } ) ;
3618
3629
}
3619
3630
3620
- // Apply the extract to function text edits.
3621
- await vscode . workspace . applyEdit ( workspaceEdits , { isRefactoring : true } ) ;
3622
-
3623
- // Select the replaced code.
3624
- await vscode . window . showTextDocument ( sourceFormatUriAndRanges [ 0 ] . uri , { selection : replaceEditRange , preserveFocus : false } ) ;
3625
-
3626
3631
// Format the new text edits.
3627
- const formatEdits : vscode . WorkspaceEdit = new vscode . WorkspaceEdit ( ) ;
3632
+ let formatEdits : vscode . WorkspaceEdit = new vscode . WorkspaceEdit ( ) ;
3628
3633
const formatRanges = async ( formatUriAndRanges : VsCodeUriAndRange [ ] ) => {
3629
3634
if ( formatUriAndRanges . length === 0 ) {
3630
3635
return ;
@@ -3670,7 +3675,22 @@ export class DefaultClient implements Client {
3670
3675
}
3671
3676
} ;
3672
3677
3673
- await formatRanges ( headerFormatUriAndRanges ) ;
3678
+ if ( headerFormatUriAndRanges . length > 0 && headerReplaceEditRange !== undefined ) {
3679
+ await formatRanges ( headerFormatUriAndRanges ) ;
3680
+ if ( formatEdits . size > 0 ) {
3681
+ // This showTextDocument is required in order to get the selection to be
3682
+ // correct after the formatting edit is applied. It could be a VS Code bug.
3683
+ await vscode . window . showTextDocument ( headerFormatUriAndRanges [ 0 ] . uri , {
3684
+ selection : headerReplaceEditRange , preserveFocus : false } ) ;
3685
+ await vscode . workspace . applyEdit ( formatEdits , { isRefactoring : true } ) ;
3686
+ formatEdits = new vscode . WorkspaceEdit ( ) ;
3687
+ }
3688
+ }
3689
+
3690
+ // Select the replaced code.
3691
+ await vscode . window . showTextDocument ( sourceFormatUriAndRanges [ 0 ] . uri , {
3692
+ selection : sourceReplaceEditRange , preserveFocus : false } ) ;
3693
+
3674
3694
await formatRanges ( sourceFormatUriAndRanges ) ;
3675
3695
if ( formatEdits . size > 0 ) {
3676
3696
await vscode . workspace . applyEdit ( formatEdits , { isRefactoring : true } ) ;
0 commit comments