feat: quick fix for redundant activation events (#192495)

This commit is contained in:
Joyce Er 2023-09-07 15:12:23 -07:00 committed by GitHub
parent 53d03d0742
commit 0b4fd719e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 3 deletions

View file

@ -12,10 +12,12 @@ export function activate(context: vscode.ExtensionContext) {
//package.json suggestions
context.subscriptions.push(registerPackageDocumentCompletions());
//package.json code actions for lint warnings
context.subscriptions.push(registerCodeActionsProvider());
context.subscriptions.push(new ExtensionLinter());
}
function registerPackageDocumentCompletions(): vscode.Disposable {
return vscode.languages.registerCompletionItemProvider({ language: 'json', pattern: '**/package.json' }, {
provideCompletionItems(document, position, token) {
@ -23,3 +25,11 @@ function registerPackageDocumentCompletions(): vscode.Disposable {
}
});
}
function registerCodeActionsProvider(): vscode.Disposable {
return vscode.languages.registerCodeActionsProvider({ language: 'json', pattern: '**/package.json' }, {
provideCodeActions(document, range, context, token) {
return new PackageDocument(document).provideCodeActions(range, context, token);
}
});
}

View file

@ -32,8 +32,8 @@ const dataUrlsNotValid = l10n.t("Data URLs are not a valid image source.");
const relativeUrlRequiresHttpsRepository = l10n.t("Relative image URLs require a repository with HTTPS protocol to be specified in the package.json.");
const relativeBadgeUrlRequiresHttpsRepository = l10n.t("Relative badge URLs require a repository with HTTPS protocol to be specified in this package.json.");
const apiProposalNotListed = l10n.t("This proposal cannot be used because for this extension the product defines a fixed set of API proposals. You can test your extension but before publishing you MUST reach out to the VS Code team.");
const implicitActivationEvent = l10n.t("This activation event cannot be explicitly listed by your extension.");
const redundantImplicitActivationEvent = l10n.t("This activation event can be removed as VS Code generates these automatically from your package.json contribution declarations.");
export const implicitActivationEvent = l10n.t("This activation event cannot be explicitly listed by your extension.");
export const redundantImplicitActivationEvent = l10n.t("This activation event can be removed as VS Code generates these automatically from your package.json contribution declarations.");
const bumpEngineForImplicitActivationEvents = l10n.t("This activation event can be removed for extensions targeting engine version ^1.75 as VS Code will generate these automatically from your package.json contribution declarations.");
const starActivation = l10n.t("Using '*' activation is usually a bad idea as it impacts performance.");
const parsingErrorHeader = l10n.t("Error parsing the when-clause:");

View file

@ -5,6 +5,7 @@
import * as vscode from 'vscode';
import { getLocation, Location } from 'jsonc-parser';
import { implicitActivationEvent, redundantImplicitActivationEvent } from './extensionLinter';
export class PackageDocument {
@ -21,6 +22,24 @@ export class PackageDocument {
return undefined;
}
public provideCodeActions(_range: vscode.Range, context: vscode.CodeActionContext, _token: vscode.CancellationToken): vscode.ProviderResult<vscode.CodeAction[]> {
const codeActions: vscode.CodeAction[] = [];
for (const diagnostic of context.diagnostics) {
if (diagnostic.message === implicitActivationEvent || diagnostic.message === redundantImplicitActivationEvent) {
const codeAction = new vscode.CodeAction(vscode.l10n.t("Remove activation event"), vscode.CodeActionKind.QuickFix);
codeAction.edit = new vscode.WorkspaceEdit();
const rangeForCharAfter = diagnostic.range.with(diagnostic.range.end, diagnostic.range.end.translate(0, 1));
if (this.document.getText(rangeForCharAfter) === ',') {
codeAction.edit.delete(this.document.uri, diagnostic.range.with(undefined, diagnostic.range.end.translate(0, 1)));
} else {
codeAction.edit.delete(this.document.uri, diagnostic.range);
}
codeActions.push(codeAction);
}
}
return codeActions;
}
private provideLanguageOverridesCompletionItems(location: Location, position: vscode.Position): vscode.ProviderResult<vscode.CompletionItem[]> {
let range = this.getReplaceRange(location, position);
const text = this.document.getText(range);