Skip to content

Commit 6d5e4c4

Browse files
authored
Fix 'Extract to function' switching to the header. (#11616)
* Fix 'Extract to function' switching to the header.
1 parent c26054a commit 6d5e4c4

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

Extension/src/LanguageServer/Providers/documentFormattingEditProvider.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* ------------------------------------------------------------------------------------------ */
55
import * as vscode from 'vscode';
66
import { DefaultClient, FormatDocumentRequest, FormatParams, FormatResult } from '../client';
7-
import { CppSettings, getEditorConfigSettings, OtherSettings } from '../settings';
7+
import { CppSettings, OtherSettings, getEditorConfigSettings } from '../settings';
88
import { makeVscodeTextEdits } from '../utils';
99

1010
export class DocumentFormattingEditProvider implements vscode.DocumentFormattingEditProvider {
@@ -20,11 +20,12 @@ export class DocumentFormattingEditProvider implements vscode.DocumentFormatting
2020
}
2121
await this.client.ready;
2222
const filePath: string = document.uri.fsPath;
23-
const onChanges: string | number | boolean = options.onChanges;
24-
if (onChanges) {
23+
if (options.onChanges) {
2524
let insertSpacesSet: boolean = false;
2625
let tabSizeSet: boolean = false;
27-
const editor: vscode.TextEditor = await vscode.window.showTextDocument(document, undefined, true);
26+
// Even when preserveFocus is true, VS Code is making the document active (when we don't want that).
27+
// The workaround is for the code invoking the formatting to call showTextDocument again afterwards on the previously active document.
28+
const editor: vscode.TextEditor = await vscode.window.showTextDocument(document, { preserveFocus: options.preserveFocus as boolean });
2829
if (editor.options.insertSpaces && typeof editor.options.insertSpaces === "boolean") {
2930
options.insertSpaces = editor.options.insertSpaces;
3031
insertSpacesSet = true;
@@ -63,7 +64,7 @@ export class DocumentFormattingEditProvider implements vscode.DocumentFormatting
6364
line: 0
6465
}
6566
},
66-
onChanges: onChanges === true
67+
onChanges: options.onChanges === true
6768
};
6869
// We do not currently pass the CancellationToken to sendRequest
6970
// because there is not currently cancellation logic for formatting

Extension/src/LanguageServer/client.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)