Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a concept of Command Descriptions for TF-IDF indexing #193937

Merged
merged 5 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/vs/editor/browser/coreCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { Range } from 'vs/editor/common/core/range';
import { Handler, ScrollType } from 'vs/editor/common/editorCommon';
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
import { VerticalRevealType } from 'vs/editor/common/viewEvents';
import { ICommandHandlerDescription } from 'vs/platform/commands/common/commands';
import { ICommandMetadata } from 'vs/platform/commands/common/commands';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { KeybindingWeight, KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRegistry';
Expand Down Expand Up @@ -74,7 +74,7 @@ export namespace EditorScroll_ {
return true;
};

export const description = <ICommandHandlerDescription>{
export const metadata = <ICommandMetadata>{
description: 'Scroll editor in the given direction',
args: [
{
Expand Down Expand Up @@ -252,7 +252,7 @@ export namespace RevealLine_ {
return true;
};

export const description = <ICommandHandlerDescription>{
export const metadata = <ICommandMetadata>{
description: 'Reveal the given line at the given logical position',
args: [
{
Expand Down Expand Up @@ -589,7 +589,7 @@ export namespace CoreNavigationCommands {
super({
id: 'cursorMove',
precondition: undefined,
description: CursorMove_.description
metadata: CursorMove_.metadata
});
}

Expand Down Expand Up @@ -1110,7 +1110,7 @@ export namespace CoreNavigationCommands {
primary: KeyCode.End,
mac: { primary: KeyCode.End, secondary: [KeyMod.CtrlCmd | KeyCode.RightArrow] }
},
description: {
metadata: {
description: `Go to End`,
args: [{
name: 'args',
Expand Down Expand Up @@ -1139,7 +1139,7 @@ export namespace CoreNavigationCommands {
primary: KeyMod.Shift | KeyCode.End,
mac: { primary: KeyMod.Shift | KeyCode.End, secondary: [KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.RightArrow] }
},
description: {
metadata: {
description: `Select to End`,
args: [{
name: 'args',
Expand Down Expand Up @@ -1307,7 +1307,7 @@ export namespace CoreNavigationCommands {
super({
id: 'editorScroll',
precondition: undefined,
description: EditorScroll_.description
metadata: EditorScroll_.metadata
});
}

Expand Down Expand Up @@ -1828,7 +1828,7 @@ export namespace CoreNavigationCommands {
super({
id: 'revealLine',
precondition: undefined,
description: RevealLine_.description
metadata: RevealLine_.metadata
});
}

Expand Down Expand Up @@ -2125,11 +2125,11 @@ class EditorHandlerCommand extends Command {

private readonly _handlerId: string;

constructor(id: string, handlerId: string, description?: ICommandHandlerDescription) {
constructor(id: string, handlerId: string, metadata?: ICommandMetadata) {
super({
id: id,
precondition: undefined,
description: description
metadata
});
this._handlerId = handlerId;
}
Expand All @@ -2144,9 +2144,9 @@ class EditorHandlerCommand extends Command {
}
}

function registerOverwritableCommand(handlerId: string, description?: ICommandHandlerDescription): void {
function registerOverwritableCommand(handlerId: string, metadata?: ICommandMetadata): void {
registerCommand(new EditorHandlerCommand('default:' + handlerId, handlerId));
registerCommand(new EditorHandlerCommand(handlerId, handlerId, description));
registerCommand(new EditorHandlerCommand(handlerId, handlerId, metadata));
}

registerOverwritableCommand(Handler.Type, {
Expand Down
10 changes: 5 additions & 5 deletions src/vs/editor/browser/editorExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { ITextModel } from 'vs/editor/common/model';
import { IModelService } from 'vs/editor/common/services/model';
import { ITextModelService } from 'vs/editor/common/services/resolverService';
import { MenuId, MenuRegistry, Action2 } from 'vs/platform/actions/common/actions';
import { CommandsRegistry, ICommandHandlerDescription } from 'vs/platform/commands/common/commands';
import { CommandsRegistry, ICommandMetadata } from 'vs/platform/commands/common/commands';
import { ContextKeyExpr, IContextKeyService, ContextKeyExpression } from 'vs/platform/contextkey/common/contextkey';
import { ServicesAccessor as InstantiationServicesAccessor, BrandedService, IInstantiationService, IConstructorSignature } from 'vs/platform/instantiation/common/instantiation';
import { IKeybindings, KeybindingsRegistry, KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
Expand Down Expand Up @@ -96,22 +96,22 @@ export interface ICommandOptions {
id: string;
precondition: ContextKeyExpression | undefined;
kbOpts?: ICommandKeybindingsOptions | ICommandKeybindingsOptions[];
description?: ICommandHandlerDescription;
metadata?: ICommandMetadata;
menuOpts?: ICommandMenuOptions | ICommandMenuOptions[];
}
export abstract class Command {
public readonly id: string;
public readonly precondition: ContextKeyExpression | undefined;
private readonly _kbOpts: ICommandKeybindingsOptions | ICommandKeybindingsOptions[] | undefined;
private readonly _menuOpts: ICommandMenuOptions | ICommandMenuOptions[] | undefined;
private readonly _description: ICommandHandlerDescription | undefined;
private readonly _metadata: ICommandMetadata | undefined;

constructor(opts: ICommandOptions) {
this.id = opts.id;
this.precondition = opts.precondition;
this._kbOpts = opts.kbOpts;
this._menuOpts = opts.menuOpts;
this._description = opts.description;
this._metadata = opts.metadata;
}

public register(): void {
Expand Down Expand Up @@ -153,7 +153,7 @@ export abstract class Command {
CommandsRegistry.registerCommand({
id: this.id,
handler: (accessor, args) => this.runCommand(accessor, args),
description: this._description
metadata: this._metadata
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/vs/editor/common/cursor/cursorMoveCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { MoveOperations } from 'vs/editor/common/cursor/cursorMoveOperations';
import { WordOperations } from 'vs/editor/common/cursor/cursorWordOperations';
import { IPosition, Position } from 'vs/editor/common/core/position';
import { Range } from 'vs/editor/common/core/range';
import { ICommandHandlerDescription } from 'vs/platform/commands/common/commands';
import { ICommandMetadata } from 'vs/platform/commands/common/commands';
import { IViewModel } from 'vs/editor/common/viewModel';

export class CursorMoveCommands {
Expand Down Expand Up @@ -596,7 +596,7 @@ export namespace CursorMove {
return true;
};

export const description = <ICommandHandlerDescription>{
export const metadata = <ICommandMetadata>{
description: 'Move cursor to a logical position in the view',
args: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class SelectToBracketAction extends EditorAction {
label: nls.localize('smartSelect.selectToBracket', "Select to Bracket"),
alias: 'Select to Bracket',
precondition: undefined,
description: {
metadata: {
description: `Select to Bracket`,
args: [{
name: 'args',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class CodeActionCommand extends EditorCommand {
super({
id: codeActionCommandId,
precondition: ContextKeyExpr.and(EditorContextKeys.writable, EditorContextKeys.hasCodeActionsProvider),
description: {
metadata: {
description: 'Trigger a code action',
args: [{ name: 'args', schema: argsSchema, }]
}
Expand Down Expand Up @@ -143,7 +143,7 @@ export class RefactorAction extends EditorAction {
EditorContextKeys.writable,
contextKeyForSupportedActions(CodeActionKind.Refactor)),
},
description: {
metadata: {
description: 'Refactor...',
args: [{ name: 'args', schema: argsSchema }]
}
Expand Down Expand Up @@ -186,7 +186,7 @@ export class SourceAction extends EditorAction {
EditorContextKeys.writable,
contextKeyForSupportedActions(CodeActionKind.Source)),
},
description: {
metadata: {
description: 'Source Action...',
args: [{ name: 'args', schema: argsSchema }]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ registerEditorAction(class extends EditorAction {
label: nls.localize('pasteAs', "Paste As..."),
alias: 'Paste As...',
precondition: undefined,
description: {
metadata: {
description: 'Paste as',
args: [{
name: 'args',
Expand Down
2 changes: 1 addition & 1 deletion src/vs/editor/contrib/find/browser/findController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ export class StartFindWithArgsAction extends EditorAction {
primary: 0,
weight: KeybindingWeight.EditorContrib
},
description: findArgDescription
metadata: findArgDescription
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/vs/editor/contrib/folding/browser/folding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ class UnfoldAction extends FoldingAction<FoldingArguments> {
},
weight: KeybindingWeight.EditorContrib
},
description: {
metadata: {
description: 'Unfold the content in the editor',
args: [
{
Expand Down Expand Up @@ -708,7 +708,7 @@ class FoldAction extends FoldingAction<FoldingArguments> {
},
weight: KeybindingWeight.EditorContrib
},
description: {
metadata: {
description: 'Fold the content in the editor',
args: [
{
Expand Down
4 changes: 2 additions & 2 deletions src/vs/editor/contrib/gotoSymbol/browser/goToCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ class GenericGoToLocationAction extends SymbolNavigationAction {

CommandsRegistry.registerCommand({
id: 'editor.action.goToLocations',
description: {
metadata: {
description: 'Go to locations from a position in a file',
args: [
{ name: 'uri', description: 'The text document in which to start', constraint: URI },
Expand Down Expand Up @@ -841,7 +841,7 @@ CommandsRegistry.registerCommand({

CommandsRegistry.registerCommand({
id: 'editor.action.peekLocations',
description: {
metadata: {
description: 'Peek locations from a position in a file',
args: [
{ name: 'uri', description: 'The text document in which to start', constraint: URI },
Expand Down
2 changes: 1 addition & 1 deletion src/vs/editor/contrib/hover/browser/hover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ class ShowOrFocusHoverAction extends EditorAction {
'If the hover is already visible, it will take focus.'
]
}, "Show or Focus Hover"),
description: {
metadata: {
description: `Show or Focus Hover`,
args: [{
name: 'args',
Expand Down
4 changes: 2 additions & 2 deletions src/vs/editor/contrib/multicursor/browser/multicursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ export class FocusNextCursor extends EditorAction {
super({
id: 'editor.action.focusNextCursor',
label: nls.localize('mutlicursor.focusNextCursor', "Focus Next Cursor"),
description: {
metadata: {
description: nls.localize('mutlicursor.focusNextCursor.description', "Focuses the next cursor"),
args: [],
},
Expand Down Expand Up @@ -1118,7 +1118,7 @@ export class FocusPreviousCursor extends EditorAction {
super({
id: 'editor.action.focusPreviousCursor',
label: nls.localize('mutlicursor.focusPreviousCursor', "Focus Previous Cursor"),
description: {
metadata: {
description: nls.localize('mutlicursor.focusPreviousCursor.description', "Focuses the previous cursor"),
args: [],
},
Expand Down
15 changes: 15 additions & 0 deletions src/vs/platform/action/common/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { URI, UriDto } from 'vs/base/common/uri';
import { ContextKeyExpression } from 'vs/platform/contextkey/common/contextkey';
import { ThemeIcon } from 'vs/base/common/themables';
import { Categories } from './actionCommonCategories';
import { ICommandMetadata } from 'vs/platform/commands/common/commands';

export interface ILocalizedString {

Expand All @@ -21,6 +22,13 @@ export interface ILocalizedString {
original: string;
}

export function isLocalizedString(thing: any): thing is ILocalizedString {
return thing
&& typeof thing === 'object'
&& typeof thing.original === 'string'
&& typeof thing.value === 'string';
}

export interface ICommandActionTitle extends ILocalizedString {

/**
Expand Down Expand Up @@ -66,6 +74,13 @@ export interface ICommandAction {
id: string;
title: string | ICommandActionTitle;
shortTitle?: string | ICommandActionTitle;
/**
* Metadata about this command, used for:
* - API commands
* - when showing keybindings that have no other UX
* - when searching for commands in the Command Palette
*/
metadata?: ICommandMetadata;
category?: keyof typeof Categories | ILocalizedString | string;
tooltip?: string | ILocalizedString;
icon?: Icon;
Expand Down
12 changes: 3 additions & 9 deletions src/vs/platform/actions/common/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { DisposableStore, IDisposable, toDisposable } from 'vs/base/common/lifec
import { LinkedList } from 'vs/base/common/linkedList';
import { ICommandAction, ICommandActionTitle, Icon, ILocalizedString } from 'vs/platform/action/common/action';
import { Categories } from 'vs/platform/action/common/actionCommonCategories';
import { CommandsRegistry, ICommandHandlerDescription, ICommandService } from 'vs/platform/commands/common/commands';
import { CommandsRegistry, ICommandService } from 'vs/platform/commands/common/commands';
import { ContextKeyExpr, ContextKeyExpression, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { createDecorator, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { IKeybindingRule, KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRegistry';
Expand Down Expand Up @@ -514,12 +514,6 @@ interface IAction2CommonOptions extends ICommandAction {
* One keybinding.
*/
keybinding?: OneOrN<Omit<IKeybindingRule, 'id'>>;

/**
* Metadata about this command, used for API commands or when
* showing keybindings that have no other UX.
*/
description?: ICommandHandlerDescription;
}

interface IBaseAction2Options extends IAction2CommonOptions {
Expand Down Expand Up @@ -571,13 +565,13 @@ export function registerAction2(ctor: { new(): Action2 }): IDisposable {
const disposables = new DisposableStore();
const action = new ctor();

const { f1, menu, keybinding, description, ...command } = action.desc;
const { f1, menu, keybinding, ...command } = action.desc;

// command
disposables.add(CommandsRegistry.registerCommand({
id: command.id,
handler: (accessor, ...args) => action.run(accessor, ...args),
description: description,
metadata: command.metadata,
}));

// menu
Expand Down
20 changes: 14 additions & 6 deletions src/vs/platform/commands/common/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { IJSONSchema } from 'vs/base/common/jsonSchema';
import { IDisposable, toDisposable } from 'vs/base/common/lifecycle';
import { LinkedList } from 'vs/base/common/linkedList';
import { TypeConstraint, validateConstraints } from 'vs/base/common/types';
import { ILocalizedString } from 'vs/platform/action/common/action';
import { createDecorator, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';

export const ICommandService = createDecorator<ICommandService>('commandService');
Expand All @@ -34,12 +35,19 @@ export interface ICommandHandler {
export interface ICommand {
id: string;
handler: ICommandHandler;
description?: ICommandHandlerDescription | null;
metadata?: ICommandMetadata | null;
}

export interface ICommandHandlerDescription {
readonly description: string;
readonly args: ReadonlyArray<{
export interface ICommandMetadata {
/**
* NOTE: Please use an ILocalizedString. string is in the type for backcompat for now.
* A short summary of what the command does. This will be used in:
* - API commands
* - when showing keybindings that have no other UX
* - when searching for commands in the Command Palette
*/
readonly description: ILocalizedString | string;
readonly args?: ReadonlyArray<{
readonly name: string;
readonly isOptional?: boolean;
readonly description?: string;
Expand Down Expand Up @@ -79,9 +87,9 @@ export const CommandsRegistry: ICommandRegistry = new class implements ICommandR
}

// add argument validation if rich command metadata is provided
if (idOrCommand.description) {
if (idOrCommand.metadata && Array.isArray(idOrCommand.metadata.args)) {
const constraints: Array<TypeConstraint | undefined> = [];
for (const arg of idOrCommand.description.args) {
for (const arg of idOrCommand.metadata.args) {
constraints.push(arg.constraint);
}
const actualHandler = idOrCommand.handler;
Expand Down
Loading