Add move to new file refactoring for JS/TS

Fixes #49866
This commit is contained in:
Matt Bierner
2018-05-14 18:21:15 -07:00
parent 6da964f278
commit 4c57171eb4
3 changed files with 25 additions and 10 deletions

View File

@@ -90,7 +90,7 @@ export default class FileConfigurationManager {
const args: Proto.ConfigureRequestArguments = {
file,
...currentOptions
...currentOptions,
};
await this.client.execute('configure', args, token);
this.formatOptions[key] = currentOptions;
@@ -160,7 +160,8 @@ export default class FileConfigurationManager {
quotePreference: getQuoteStylePreference(preferences),
importModuleSpecifierPreference: getImportModuleSpecifierPreference(preferences),
disableSuggestions: disableSuggestionsPreference(config),
};
allowTextChangesInNewFiles: document.uri.scheme === 'file'
} as any; // TODO: waiting for offical TS d.ts with allowTextChangesInNewFiles
}
}

View File

@@ -6,6 +6,7 @@
'use strict';
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as Proto from '../protocol';
import { ITypeScriptServiceClient } from '../typescriptService';
@@ -18,8 +19,7 @@ class ApplyRefactoringCommand implements Command {
public readonly id = ApplyRefactoringCommand.ID;
constructor(
private readonly client: ITypeScriptServiceClient,
private readonly formattingOptionsManager: FormattingOptionsManager
private readonly client: ITypeScriptServiceClient
) { }
public async execute(
@@ -29,8 +29,6 @@ class ApplyRefactoringCommand implements Command {
action: string,
range: vscode.Range
): Promise<boolean> {
await this.formattingOptionsManager.ensureConfigurationForDocument(document, undefined);
const args: Proto.GetEditsForRefactorRequestArgs = {
...typeConverters.Range.toFileRangeRequestArgs(file, range),
refactor,
@@ -41,6 +39,20 @@ class ApplyRefactoringCommand implements Command {
return false;
}
for (const edit of response.body.edits) {
try {
await vscode.workspace.openTextDocument(edit.fileName);
} catch {
try {
if (!fs.existsSync(edit.fileName)) {
fs.writeFileSync(edit.fileName, '');
}
} catch {
// noop
}
}
}
const edit = typeConverters.WorkspaceEdit.fromFromFileCodeEdits(this.client, response.body.edits);
if (!(await vscode.workspace.applyEdit(edit))) {
return false;
@@ -89,10 +101,10 @@ export default class TypeScriptRefactorProvider implements vscode.CodeActionProv
constructor(
private readonly client: ITypeScriptServiceClient,
formattingOptionsManager: FormattingOptionsManager,
private readonly formattingOptionsManager: FormattingOptionsManager,
commandManager: CommandManager
) {
const doRefactoringCommand = commandManager.register(new ApplyRefactoringCommand(this.client, formattingOptionsManager));
const doRefactoringCommand = commandManager.register(new ApplyRefactoringCommand(this.client));
commandManager.register(new SelectRefactorCommand(doRefactoringCommand));
}
@@ -123,6 +135,8 @@ export default class TypeScriptRefactorProvider implements vscode.CodeActionProv
return [];
}
await this.formattingOptionsManager.ensureConfigurationForDocument(document, undefined);
const args: Proto.GetApplicableRefactorsRequestArgs = typeConverters.Range.toFileRangeRequestArgs(file, rangeOrSelection);
try {
const response = await this.client.execute('getApplicableRefactors', args, token);

View File

@@ -14,8 +14,8 @@ import { ITypeScriptServiceClient } from '../typescriptService';
export namespace Range {
export const fromTextSpan = (span: Proto.TextSpan): vscode.Range =>
new vscode.Range(
span.start.line - 1, span.start.offset - 1,
span.end.line - 1, span.end.offset - 1);
Math.max(0, span.start.line - 1), Math.max(span.start.offset - 1, 0),
Math.max(0, span.end.line - 1), Math.max(0, span.end.offset - 1));
export const toFileRangeRequestArgs = (file: string, range: vscode.Range): Proto.FileRangeRequestArgs => ({
file,