mirror of
https://github.com/Microsoft/vscode
synced 2024-10-04 02:14:06 +00:00
Re-register TS sort/remove unused commands (#210984)
Fixes #210728 Removed these as I thought they were unused
This commit is contained in:
parent
fe94eaae4e
commit
5d25f31b7e
|
@ -12,19 +12,30 @@ export interface Command {
|
|||
}
|
||||
|
||||
export class CommandManager {
|
||||
private readonly commands = new Map<string, vscode.Disposable>();
|
||||
private readonly commands = new Map<string, { refCount: number; readonly registration: vscode.Disposable }>();
|
||||
|
||||
public dispose() {
|
||||
for (const registration of this.commands.values()) {
|
||||
registration.dispose();
|
||||
registration.registration.dispose();
|
||||
}
|
||||
this.commands.clear();
|
||||
}
|
||||
|
||||
public register<T extends Command>(command: T): T {
|
||||
if (!this.commands.has(command.id)) {
|
||||
this.commands.set(command.id, vscode.commands.registerCommand(command.id, command.execute, command));
|
||||
public register<T extends Command>(command: T): vscode.Disposable {
|
||||
let entry = this.commands.get(command.id);
|
||||
if (!entry) {
|
||||
entry = { refCount: 1, registration: vscode.commands.registerCommand(command.id, command.execute, command) };
|
||||
this.commands.set(command.id, entry);
|
||||
} else {
|
||||
entry.refCount += 1;
|
||||
}
|
||||
return command;
|
||||
|
||||
return new vscode.Disposable(() => {
|
||||
entry.refCount -= 1;
|
||||
if (entry.refCount <= 0) {
|
||||
entry.registration.dispose();
|
||||
this.commands.delete(command.id);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ import { conditionalRegistration, requireMinVersion, requireSomeCapability } fro
|
|||
|
||||
|
||||
interface OrganizeImportsCommandMetadata {
|
||||
readonly commandIds: readonly string[];
|
||||
readonly title: string;
|
||||
readonly minVersion?: API;
|
||||
readonly kind: vscode.CodeActionKind;
|
||||
|
@ -25,12 +26,14 @@ interface OrganizeImportsCommandMetadata {
|
|||
}
|
||||
|
||||
const organizeImportsCommand: OrganizeImportsCommandMetadata = {
|
||||
commandIds: [], // We use the generic 'Organize imports' command
|
||||
title: vscode.l10n.t("Organize Imports"),
|
||||
kind: vscode.CodeActionKind.SourceOrganizeImports,
|
||||
mode: OrganizeImportsMode.All,
|
||||
};
|
||||
|
||||
const sortImportsCommand: OrganizeImportsCommandMetadata = {
|
||||
commandIds: ['typescript.sortImports', 'javascript.sortImports'],
|
||||
minVersion: API.v430,
|
||||
title: vscode.l10n.t("Sort Imports"),
|
||||
kind: vscode.CodeActionKind.Source.append('sortImports'),
|
||||
|
@ -38,16 +41,17 @@ const sortImportsCommand: OrganizeImportsCommandMetadata = {
|
|||
};
|
||||
|
||||
const removeUnusedImportsCommand: OrganizeImportsCommandMetadata = {
|
||||
commandIds: ['typescript.removeUnusedImports', 'javascript.removeUnusedImports'],
|
||||
minVersion: API.v490,
|
||||
title: vscode.l10n.t("Remove Unused Imports"),
|
||||
kind: vscode.CodeActionKind.Source.append('removeUnusedImports'),
|
||||
mode: OrganizeImportsMode.RemoveUnused,
|
||||
};
|
||||
|
||||
class OrganizeImportsCommand implements Command {
|
||||
class DidOrganizeImportsCommand implements Command {
|
||||
|
||||
public static readonly ID = '_typescript.organizeImports';
|
||||
public readonly id = OrganizeImportsCommand.ID;
|
||||
public static readonly ID = '_typescript.didOrganizeImports';
|
||||
public readonly id = DidOrganizeImportsCommand.ID;
|
||||
|
||||
constructor(
|
||||
private readonly telemetryReporter: TelemetryReporter,
|
||||
|
@ -85,7 +89,7 @@ class ImportsCodeActionProvider implements vscode.CodeActionProvider<ImportCodeA
|
|||
private readonly fileConfigManager: FileConfigurationManager,
|
||||
telemetryReporter: TelemetryReporter,
|
||||
) {
|
||||
commandManager.register(new OrganizeImportsCommand(telemetryReporter));
|
||||
commandManager.register(new DidOrganizeImportsCommand(telemetryReporter));
|
||||
}
|
||||
|
||||
public provideCodeActions(
|
||||
|
@ -138,7 +142,7 @@ class ImportsCodeActionProvider implements vscode.CodeActionProvider<ImportCodeA
|
|||
codeAction.edit = typeConverters.WorkspaceEdit.fromFileCodeEdits(this.client, response.body);
|
||||
}
|
||||
|
||||
codeAction.command = { command: OrganizeImportsCommand.ID, title: '', arguments: [] };
|
||||
codeAction.command = { command: DidOrganizeImportsCommand.ID, title: '', arguments: [] };
|
||||
|
||||
return codeAction;
|
||||
}
|
||||
|
@ -154,15 +158,29 @@ export function register(
|
|||
const disposables: vscode.Disposable[] = [];
|
||||
|
||||
for (const command of [organizeImportsCommand, sortImportsCommand, removeUnusedImportsCommand]) {
|
||||
disposables.push(conditionalRegistration([
|
||||
requireMinVersion(client, command.minVersion ?? API.defaultVersion),
|
||||
requireSomeCapability(client, ClientCapability.Semantic),
|
||||
], () => {
|
||||
const provider = new ImportsCodeActionProvider(client, command, commandManager, fileConfigurationManager, telemetryReporter);
|
||||
return vscode.languages.registerCodeActionsProvider(selector.semantic, provider, {
|
||||
providedCodeActionKinds: [command.kind]
|
||||
});
|
||||
}));
|
||||
disposables.push(
|
||||
conditionalRegistration([
|
||||
requireMinVersion(client, command.minVersion ?? API.defaultVersion),
|
||||
requireSomeCapability(client, ClientCapability.Semantic),
|
||||
], () => {
|
||||
const provider = new ImportsCodeActionProvider(client, command, commandManager, fileConfigurationManager, telemetryReporter);
|
||||
return vscode.Disposable.from(
|
||||
vscode.languages.registerCodeActionsProvider(selector.semantic, provider, {
|
||||
providedCodeActionKinds: [command.kind]
|
||||
}));
|
||||
}),
|
||||
// Always register these commands. We will show a warning if the user tries to run them on an unsupported version
|
||||
...command.commandIds.map(id =>
|
||||
commandManager.register({
|
||||
id,
|
||||
execute() {
|
||||
return vscode.commands.executeCommand('editor.action.sourceAction', {
|
||||
kind: command.kind.value,
|
||||
apply: 'first',
|
||||
});
|
||||
}
|
||||
}))
|
||||
);
|
||||
}
|
||||
|
||||
return vscode.Disposable.from(...disposables);
|
||||
|
|
|
@ -495,7 +495,9 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider<TsCodeActi
|
|||
commandManager: CommandManager,
|
||||
telemetryReporter: TelemetryReporter
|
||||
) {
|
||||
const didApplyRefactoringCommand = commandManager.register(new DidApplyRefactoringCommand(telemetryReporter));
|
||||
const didApplyRefactoringCommand = new DidApplyRefactoringCommand(telemetryReporter);
|
||||
commandManager.register(didApplyRefactoringCommand);
|
||||
|
||||
commandManager.register(new CompositeCommand());
|
||||
commandManager.register(new SelectRefactorCommand(this.client));
|
||||
commandManager.register(new MoveToFileRefactorCommand(this.client, didApplyRefactoringCommand));
|
||||
|
|
Loading…
Reference in a new issue