diff --git a/extensions/typescript/src/features/quickFixProvider.ts b/extensions/typescript/src/features/quickFixProvider.ts index cbbc910e483..8c9f969f76b 100644 --- a/extensions/typescript/src/features/quickFixProvider.ts +++ b/extensions/typescript/src/features/quickFixProvider.ts @@ -31,15 +31,46 @@ class ApplyCodeActionCommand implements Command { } } -export default class TypeScriptQuickFixProvider implements vscode.CodeActionProvider { +class SupportedCodeActionProvider { private _supportedCodeActions?: Thenable; + public constructor( + private readonly client: ITypeScriptServiceClient + ) { } + + public async getSupportedActionsForContext(context: vscode.CodeActionContext): Promise> { + const supportedActions = await this.supportedCodeActions; + return new Set(context.diagnostics + .map(diagnostic => +diagnostic.code) + .filter(code => supportedActions[code])); + } + + private get supportedCodeActions(): Thenable { + if (!this._supportedCodeActions) { + this._supportedCodeActions = this.client.execute('getSupportedCodeFixes', null, undefined) + .then(response => response.body || []) + .then(codes => codes.map(code => +code).filter(code => !isNaN(code))) + .then(codes => + codes.reduce((obj, code) => { + obj[code] = true; + return obj; + }, Object.create(null))); + } + return this._supportedCodeActions; + } +} + +export default class TypeScriptQuickFixProvider implements vscode.CodeActionProvider { + + private readonly supportedCodeActionProvider: SupportedCodeActionProvider; + constructor( private readonly client: ITypeScriptServiceClient, private readonly formattingConfigurationManager: FormattingConfigurationManager, commandManager: CommandManager ) { commandManager.register(new ApplyCodeActionCommand(client)); + this.supportedCodeActionProvider = new SupportedCodeActionProvider(client); } public async provideCodeActions( @@ -57,7 +88,7 @@ export default class TypeScriptQuickFixProvider implements vscode.CodeActionProv return []; } - const supportedActions = await this.getSupportedActionsForContext(context); + const supportedActions = await this.supportedCodeActionProvider.getSupportedActionsForContext(context); if (!supportedActions.size) { return []; } @@ -72,27 +103,6 @@ export default class TypeScriptQuickFixProvider implements vscode.CodeActionProv return (response.body || []).map(action => this.getCommandForAction(action)); } - private get supportedCodeActions(): Thenable { - if (!this._supportedCodeActions) { - this._supportedCodeActions = this.client.execute('getSupportedCodeFixes', null, undefined) - .then(response => response.body || []) - .then(codes => codes.map(code => +code).filter(code => !isNaN(code))) - .then(codes => - codes.reduce((obj, code) => { - obj[code] = true; - return obj; - }, Object.create(null))); - } - return this._supportedCodeActions; - } - - private async getSupportedActionsForContext(context: vscode.CodeActionContext): Promise> { - const supportedActions = await this.supportedCodeActions; - return new Set(context.diagnostics - .map(diagnostic => +diagnostic.code) - .filter(code => supportedActions[code])); - } - private getCommandForAction(tsAction: Proto.CodeAction): vscode.CodeAction { const codeAction = new vscode.CodeAction(tsAction.description, getEditForCodeAction(this.client, tsAction)); if (tsAction.commands) {