提交 44d11076 编写于 作者: M Matt Bierner

Make a code action request for each diagnostic

Fixes #27392

**Bug**
We currently only make a single code action request for all diagnostics, with a range the covers the span of all diagnostics. However the TS Server API actually expects a code action request for each diagnostic (both the code and range of the code action request has to exactly match that of the diagnostic it aims to fix).

The result is that for overlapping diagnotics where there are multiple fixes, we only  would get code actions for the outermost diagnostic

**Fix**
Make a code action request per diagnostic
上级 2f2bfea2
......@@ -34,11 +34,9 @@ class SupportedCodeActionProvider {
private readonly client: ITypeScriptServiceClient
) { }
public async getSupportedActionsForContext(context: vscode.CodeActionContext): Promise<Set<number>> {
public async getFixableDiagnosticsForContext(context: vscode.CodeActionContext): Promise<vscode.Diagnostic[]> {
const supportedActions = await this.supportedCodeActions;
return new Set(context.diagnostics
.map(diagnostic => +diagnostic.code)
.filter(code => supportedActions.has(code)));
return context.diagnostics.filter(diagnostic => supportedActions.has(+diagnostic.code));
}
private get supportedCodeActions(): Thenable<Set<number>> {
......@@ -80,23 +78,33 @@ export default class TypeScriptQuickFixProvider implements vscode.CodeActionProv
return [];
}
const supportedActions = await this.supportedCodeActionProvider.getSupportedActionsForContext(context);
if (!supportedActions.size) {
const fixableDiagnostics = await this.supportedCodeActionProvider.getFixableDiagnosticsForContext(context);
if (!fixableDiagnostics.length) {
return [];
}
await this.formattingConfigurationManager.ensureFormatOptionsForDocument(document, token);
const args: Proto.CodeFixRequestArgs = {
...vsRangeToTsFileRange(file, range),
errorCodes: Array.from(supportedActions)
};
const response = await this.client.execute('getCodeFixes', args, token);
return (response.body || []).map(action => this.getCommandForAction(action));
const results: vscode.CodeAction[] = [];
for (const diagnostic of fixableDiagnostics) {
const args: Proto.CodeFixRequestArgs = {
...vsRangeToTsFileRange(file, diagnostic.range),
errorCodes: [+diagnostic.code]
};
const response = await this.client.execute('getCodeFixes', args, token);
if (response.body) {
results.push(...response.body.map(action => this.getCommandForAction(diagnostic, action)));
}
}
return results;
}
private getCommandForAction(tsAction: Proto.CodeAction): vscode.CodeAction {
private getCommandForAction(
diagnostic: vscode.Diagnostic,
tsAction: Proto.CodeAction
): vscode.CodeAction {
const codeAction = new vscode.CodeAction(tsAction.description, getEditForCodeAction(this.client, tsAction));
codeAction.diagnostics = [diagnostic];
if (tsAction.commands) {
codeAction.command = {
command: ApplyCodeActionCommand.ID,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册