提交 4f8d546a 编写于 作者: M Matt Bierner

De-duplicate "fix all" quick fixes across requests for multiple diagnostics in selection range

For https://github.com/Microsoft/typescript-tslint-plugin/issues/49
上级 232006c4
...@@ -128,8 +128,8 @@ class DiagnosticsSet { ...@@ -128,8 +128,8 @@ class DiagnosticsSet {
} }
class CodeActionSet { class CodeActionSet {
private _actions = new Set<vscode.CodeAction>(); private readonly _actions = new Set<vscode.CodeAction>();
private _fixAllActions = new Map<{}, vscode.CodeAction>(); private readonly _fixAllActions = new Map<{}, vscode.CodeAction>();
public get values(): Iterable<vscode.CodeAction> { public get values(): Iterable<vscode.CodeAction> {
return this._actions; return this._actions;
...@@ -142,7 +142,7 @@ class CodeActionSet { ...@@ -142,7 +142,7 @@ class CodeActionSet {
public addFixAllAction(fixId: {}, action: vscode.CodeAction) { public addFixAllAction(fixId: {}, action: vscode.CodeAction) {
const existing = this._fixAllActions.get(fixId); const existing = this._fixAllActions.get(fixId);
if (existing) { if (existing) {
// reinsert action at back // reinsert action at back of actions list
this._actions.delete(existing); this._actions.delete(existing);
} }
this.addAction(action); this.addAction(action);
...@@ -216,33 +216,33 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider { ...@@ -216,33 +216,33 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider {
await this.formattingConfigurationManager.ensureConfigurationForDocument(document, token); await this.formattingConfigurationManager.ensureConfigurationForDocument(document, token);
const results: vscode.CodeAction[] = []; const results = new CodeActionSet();
for (const diagnostic of fixableDiagnostics.values) { for (const diagnostic of fixableDiagnostics.values) {
results.push(...await this.getFixesForDiagnostic(document, file, diagnostic, token)); await this.getFixesForDiagnostic(document, file, diagnostic, results, token);
} }
return results; return Array.from(results.values);
} }
private async getFixesForDiagnostic( private async getFixesForDiagnostic(
document: vscode.TextDocument, document: vscode.TextDocument,
file: string, file: string,
diagnostic: vscode.Diagnostic, diagnostic: vscode.Diagnostic,
token: vscode.CancellationToken results: CodeActionSet,
): Promise<Iterable<vscode.CodeAction>> { token: vscode.CancellationToken,
): Promise<CodeActionSet> {
const args: Proto.CodeFixRequestArgs = { const args: Proto.CodeFixRequestArgs = {
...typeConverters.Range.toFileRangeRequestArgs(file, diagnostic.range), ...typeConverters.Range.toFileRangeRequestArgs(file, diagnostic.range),
errorCodes: [+(diagnostic.code!)] errorCodes: [+(diagnostic.code!)]
}; };
const response = await this.client.execute('getCodeFixes', args, token); const response = await this.client.execute('getCodeFixes', args, token);
if (response.type !== 'response' || !response.body) { if (response.type !== 'response' || !response.body) {
return []; return results;
} }
const results = new CodeActionSet();
for (const tsCodeFix of response.body) { for (const tsCodeFix of response.body) {
this.addAllFixesForTsCodeAction(results, document, file, diagnostic, tsCodeFix as Proto.CodeFixAction); this.addAllFixesForTsCodeAction(results, document, file, diagnostic, tsCodeFix as Proto.CodeFixAction);
} }
return results.values; return results;
} }
private addAllFixesForTsCodeAction( private addAllFixesForTsCodeAction(
...@@ -282,7 +282,7 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider { ...@@ -282,7 +282,7 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider {
diagnostic: vscode.Diagnostic, diagnostic: vscode.Diagnostic,
tsAction: Proto.CodeFixAction, tsAction: Proto.CodeFixAction,
): CodeActionSet { ): CodeActionSet {
if (!tsAction.fixId || this.client.apiVersion.lt(API.v270) || results.hasFixAllAction(results)) { if (!tsAction.fixId || this.client.apiVersion.lt(API.v270) || results.hasFixAllAction(tsAction.fixId)) {
return results; return results;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册