forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prototype autofix source code action
Part of microsoft#62110 * Adds a new `CodeActionKind`: `source.autoFix`. * Implements a simple auto fix provider for typescript. This provider can auto fix `implement interface` and `spelling` errors (provided there is only a single valid fix listed) The provider is likely not something we actually want to check it (especially in its current state), we should ask TS for proper autoFix support
- Loading branch information
Showing
6 changed files
with
149 additions
and
2 deletions.
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
extensions/typescript-language-features/src/features/autoFix.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as vscode from 'vscode'; | ||
import * as nls from 'vscode-nls'; | ||
import * as Proto from '../protocol'; | ||
import { ITypeScriptServiceClient } from '../typescriptService'; | ||
import API from '../utils/api'; | ||
import { VersionDependentRegistration } from '../utils/dependentRegistration'; | ||
import * as typeConverters from '../utils/typeConverters'; | ||
import { DiagnosticsManager } from './diagnostics'; | ||
import FileConfigurationManager from './fileConfigurationManager'; | ||
|
||
const localize = nls.loadMessageBundle(); | ||
|
||
const autoFixableDiagnosticCodes = new Set<number>([ | ||
2420, // Incorrectly implemented interface | ||
2552, // Cannot find name | ||
]); | ||
|
||
class TypeScriptAutoFixProvider implements vscode.CodeActionProvider { | ||
|
||
public static readonly metadata: vscode.CodeActionProviderMetadata = { | ||
providedCodeActionKinds: [vscode.CodeActionKind.SourceAutoFix] | ||
}; | ||
|
||
constructor( | ||
private readonly client: ITypeScriptServiceClient, | ||
private readonly fileConfigurationManager: FileConfigurationManager, | ||
private readonly diagnosticsManager: DiagnosticsManager, | ||
) { } | ||
|
||
public async provideCodeActions( | ||
document: vscode.TextDocument, | ||
_range: vscode.Range, | ||
context: vscode.CodeActionContext, | ||
token: vscode.CancellationToken | ||
): Promise<vscode.CodeAction[] | undefined> { | ||
if (!context.only || !context.only.contains(vscode.CodeActionKind.Source)) { | ||
return undefined; | ||
} | ||
|
||
const file = this.client.toOpenedFilePath(document); | ||
if (!file) { | ||
return undefined; | ||
} | ||
|
||
const autoFixableDiagnostics = this.getAutoFixableDiagnostics(document); | ||
if (!autoFixableDiagnostics.length) { | ||
return undefined; | ||
} | ||
|
||
const fixAllAction = await this.getFixAllCodeAction(document, file, autoFixableDiagnostics, token); | ||
return fixAllAction ? [fixAllAction] : undefined; | ||
} | ||
|
||
private getAutoFixableDiagnostics( | ||
document: vscode.TextDocument | ||
): vscode.Diagnostic[] { | ||
if (this.client.bufferSyncSupport.hasPendingDiagnostics(document.uri)) { | ||
return []; | ||
} | ||
|
||
return this.diagnosticsManager.getDiagnostics(document.uri) | ||
.filter(x => autoFixableDiagnosticCodes.has(x.code as number)); | ||
} | ||
|
||
private async getFixAllCodeAction( | ||
document: vscode.TextDocument, | ||
file: string, | ||
diagnostics: ReadonlyArray<vscode.Diagnostic>, | ||
token: vscode.CancellationToken, | ||
): Promise<vscode.CodeAction | undefined> { | ||
await this.fileConfigurationManager.ensureConfigurationForDocument(document, token); | ||
|
||
const autoFixResponse = await this.getAutoFixEdit(file, diagnostics, token); | ||
if (!autoFixResponse) { | ||
return undefined; | ||
} | ||
const { edit, fixedDiagnostics } = autoFixResponse; | ||
const codeAction = new vscode.CodeAction( | ||
localize('autoFix.label', 'Auto fix'), | ||
vscode.CodeActionKind.SourceAutoFix); | ||
codeAction.edit = edit; | ||
codeAction.diagnostics = fixedDiagnostics; | ||
|
||
return codeAction; | ||
} | ||
|
||
private async getAutoFixEdit( | ||
file: string, | ||
diagnostics: ReadonlyArray<vscode.Diagnostic>, | ||
token: vscode.CancellationToken, | ||
): Promise<{ edit: vscode.WorkspaceEdit, fixedDiagnostics: vscode.Diagnostic[] } | undefined> { | ||
const edit = new vscode.WorkspaceEdit(); | ||
const fixedDiagnostics: vscode.Diagnostic[] = []; | ||
for (const diagnostic of diagnostics) { | ||
const args: Proto.CodeFixRequestArgs = { | ||
...typeConverters.Range.toFileRangeRequestArgs(file, diagnostic.range), | ||
errorCodes: [+(diagnostic.code!)] | ||
}; | ||
const response = await this.client.execute('getCodeFixes', args, token); | ||
if (response.type !== 'response' || !response.body || response.body.length > 1) { | ||
return undefined; | ||
} | ||
|
||
const fix = response.body[0]; | ||
if (new Set<string>(['fixClassIncorrectlyImplementsInterface', 'spelling']).has(fix.fixName)) { | ||
typeConverters.WorkspaceEdit.withFileCodeEdits(edit, this.client, fix.changes); | ||
fixedDiagnostics.push(diagnostic); | ||
} | ||
} | ||
|
||
if (!fixedDiagnostics.length) { | ||
return undefined; | ||
} | ||
|
||
return { edit, fixedDiagnostics }; | ||
} | ||
} | ||
|
||
export function register( | ||
selector: vscode.DocumentSelector, | ||
client: ITypeScriptServiceClient, | ||
fileConfigurationManager: FileConfigurationManager, | ||
diagnosticsManager: DiagnosticsManager) { | ||
return new VersionDependentRegistration(client, API.v213, () => | ||
vscode.languages.registerCodeActionsProvider(selector, | ||
new TypeScriptAutoFixProvider(client, fileConfigurationManager, diagnosticsManager), | ||
TypeScriptAutoFixProvider.metadata)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters