提交 5a2a6c1e 编写于 作者: M Matt Bierner

Extract SupportedCodeActionProvider into own class

上级 fadbc9c6
......@@ -31,15 +31,46 @@ class ApplyCodeActionCommand implements Command {
}
}
export default class TypeScriptQuickFixProvider implements vscode.CodeActionProvider {
class SupportedCodeActionProvider {
private _supportedCodeActions?: Thenable<NumberSet>;
public constructor(
private readonly client: ITypeScriptServiceClient
) { }
public async getSupportedActionsForContext(context: vscode.CodeActionContext): Promise<Set<number>> {
const supportedActions = await this.supportedCodeActions;
return new Set(context.diagnostics
.map(diagnostic => +diagnostic.code)
.filter(code => supportedActions[code]));
}
private get supportedCodeActions(): Thenable<NumberSet> {
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<NumberSet> {
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<Set<number>> {
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) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册