Skip to content

Commit

Permalink
Re-check opened files while executing refactoring
Browse files Browse the repository at this point in the history
Fixes #79650
  • Loading branch information
mjbvz committed Aug 24, 2019
1 parent dcb3f60 commit e3b9b8e
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions extensions/typescript-language-features/src/features/refactor.ts
Expand Up @@ -14,7 +14,7 @@ import { VersionDependentRegistration } from '../utils/dependentRegistration';
import TelemetryReporter from '../utils/telemetry';
import * as typeConverters from '../utils/typeConverters';
import FormattingOptionsManager from './fileConfigurationManager';
import { file } from '../utils/fileSchemes';
import * as fileSchemes from '../utils/fileSchemes';

const localize = nls.loadMessageBundle();

Expand All @@ -30,11 +30,15 @@ class ApplyRefactoringCommand implements Command {

public async execute(
document: vscode.TextDocument,
file: string,
refactor: string,
action: string,
range: vscode.Range
): Promise<boolean> {
const file = this.client.toOpenedFilePath(document);
if (!file) {
return false;
}

/* __GDPR__
"refactor.execute" : {
"action" : { "classification": "PublicNonPersonalData", "purpose": "FeatureInsight" },
Expand Down Expand Up @@ -81,7 +85,7 @@ class ApplyRefactoringCommand implements Command {
const workspaceEdit = new vscode.WorkspaceEdit();
for (const edit of body.edits) {
const resource = this.client.toResource(edit.fileName);
if (resource.scheme === file) {
if (resource.scheme === fileSchemes.file) {
workspaceEdit.createFile(resource, { ignoreIfExists: true });
}
}
Expand All @@ -95,23 +99,27 @@ class SelectRefactorCommand implements Command {
public readonly id = SelectRefactorCommand.ID;

constructor(
private readonly client: ITypeScriptServiceClient,
private readonly doRefactoring: ApplyRefactoringCommand
) { }

public async execute(
document: vscode.TextDocument,
file: string,
info: Proto.ApplicableRefactorInfo,
range: vscode.Range
): Promise<boolean> {
const file = this.client.toOpenedFilePath(document);
if (!file) {
return false;
}
const selected = await vscode.window.showQuickPick(info.actions.map((action): vscode.QuickPickItem => ({
label: action.name,
description: action.description,
})));
if (!selected) {
return false;
}
return this.doRefactoring.execute(document, file, info.name, selected.label, range);
return this.doRefactoring.execute(document, info.name, selected.label, range);
}
}

Expand All @@ -130,7 +138,7 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider {
telemetryReporter: TelemetryReporter
) {
const doRefactoringCommand = commandManager.register(new ApplyRefactoringCommand(this.client, telemetryReporter));
commandManager.register(new SelectRefactorCommand(doRefactoringCommand));
commandManager.register(new SelectRefactorCommand(this.client, doRefactoringCommand));
}

public static readonly metadata: vscode.CodeActionProviderMetadata = {
Expand All @@ -146,29 +154,30 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider {
if (!this.shouldTrigger(rangeOrSelection, context)) {
return undefined;
}

const file = this.client.toOpenedFilePath(document);
if (!file) {
if (!this.client.toOpenedFilePath(document)) {
return undefined;
}

const args: Proto.GetApplicableRefactorsRequestArgs = typeConverters.Range.toFileRangeRequestArgs(file, rangeOrSelection);
const args: Proto.GetApplicableRefactorsRequestArgs = typeConverters.Range.toFileRangeRequestArgs(fileSchemes.file, rangeOrSelection);
const response = await this.client.interruptGetErr(() => {
const file = this.client.toOpenedFilePath(document);
if (!file) {
return undefined;
}
this.formattingOptionsManager.ensureConfigurationForDocument(document, token);

return this.client.execute('getApplicableRefactors', args, token);
});
if (response.type !== 'response' || !response.body) {
if (!response || response.type !== 'response' || !response.body) {
return undefined;
}

return this.convertApplicableRefactors(response.body, document, file, rangeOrSelection);
return this.convertApplicableRefactors(response.body, document, rangeOrSelection);
}

private convertApplicableRefactors(
body: Proto.ApplicableRefactorInfo[],
document: vscode.TextDocument,
file: string,
rangeOrSelection: vscode.Range | vscode.Selection
) {
const actions: vscode.CodeAction[] = [];
Expand All @@ -178,12 +187,12 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider {
codeAction.command = {
title: info.description,
command: SelectRefactorCommand.ID,
arguments: [document, file, info, rangeOrSelection]
arguments: [document, info, rangeOrSelection]
};
actions.push(codeAction);
} else {
for (const action of info.actions) {
actions.push(this.refactorActionToCodeAction(action, document, file, info, rangeOrSelection));
actions.push(this.refactorActionToCodeAction(action, document, info, rangeOrSelection));
}
}
}
Expand All @@ -193,15 +202,14 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider {
private refactorActionToCodeAction(
action: Proto.RefactorActionInfo,
document: vscode.TextDocument,
file: string,
info: Proto.ApplicableRefactorInfo,
rangeOrSelection: vscode.Range | vscode.Selection
) {
const codeAction = new vscode.CodeAction(action.description, TypeScriptRefactorProvider.getKind(action));
codeAction.command = {
title: action.description,
command: ApplyRefactoringCommand.ID,
arguments: [document, file, info.name, action.name, rangeOrSelection],
arguments: [document, info.name, action.name, rangeOrSelection],
};
codeAction.isPreferred = TypeScriptRefactorProvider.isPreferred(action);
return codeAction;
Expand Down

0 comments on commit e3b9b8e

Please sign in to comment.