Skip to content

Commit

Permalink
Initial attempt to support editor commands
Browse files Browse the repository at this point in the history
This depends on #193937 and would allow Editor Commands' descriptions to be picked up in the TF-IDF indexing of commands.

The problem I currently struggle with is what to do with this introduction of `ICommandMetadata`... do we want to add it to monaco? Do we want to add maybe a subset of it (just the description property)?

Additionally, I noticed that the concept of a LocalizedString isn't a thing in the monaco API... so that would be _another_ thing to introduce.

Should we add these types? Or should we have `description` be a `string` and then use that instead?
  • Loading branch information
TylerLeonhardt committed Oct 4, 2023
1 parent f0101f1 commit 51fb6d6
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 42 deletions.
2 changes: 2 additions & 0 deletions src/vs/editor/browser/editorExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,13 @@ export abstract class EditorAction extends EditorCommand {

public readonly label: string;
public readonly alias: string;
public readonly metadata: ICommandMetadata | undefined;

constructor(opts: IActionOptions) {
super(EditorAction.convertOptions(opts));
this.label = opts.label;
this.alias = opts.alias;
this.metadata = opts.metadata;
}

public runEditorCommand(accessor: ServicesAccessor, editor: ICodeEditor, args: any): void | Promise<void> {
Expand Down
1 change: 1 addition & 0 deletions src/vs/editor/browser/widget/codeEditorWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ export class CodeEditorWidget extends Disposable implements editorBrowser.ICodeE
action.id,
action.label,
action.alias,
action.metadata,
action.precondition ?? undefined,
(): Promise<void> => {
return this._instantiationService.invokeFunction((accessor) => {
Expand Down
31 changes: 9 additions & 22 deletions src/vs/editor/common/editorAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,20 @@
*--------------------------------------------------------------------------------------------*/

import { IEditorAction } from 'vs/editor/common/editorCommon';
import { ICommandMetadata } from 'vs/platform/commands/common/commands';
import { IContextKeyService, ContextKeyExpression } from 'vs/platform/contextkey/common/contextkey';

export class InternalEditorAction implements IEditorAction {

public readonly id: string;
public readonly label: string;
public readonly alias: string;

private readonly _precondition: ContextKeyExpression | undefined;
private readonly _run: (args: unknown) => Promise<void>;
private readonly _contextKeyService: IContextKeyService;

constructor(
id: string,
label: string,
alias: string,
precondition: ContextKeyExpression | undefined,
run: () => Promise<void>,
contextKeyService: IContextKeyService
) {
this.id = id;
this.label = label;
this.alias = alias;
this._precondition = precondition;
this._run = run;
this._contextKeyService = contextKeyService;
}
public readonly id: string,
public readonly label: string,
public readonly alias: string,
public readonly metadata: ICommandMetadata | undefined,
private readonly _precondition: ContextKeyExpression | undefined,
private readonly _run: (args: unknown) => Promise<void>,
private readonly _contextKeyService: IContextKeyService
) { }

public isSupported(): boolean {
return this._contextKeyService.contextMatchesRules(this._precondition);
Expand Down
2 changes: 2 additions & 0 deletions src/vs/editor/common/editorCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { IRange, Range } from 'vs/editor/common/core/range';
import { ISelection, Selection } from 'vs/editor/common/core/selection';
import { IModelDecoration, IModelDecorationsChangeAccessor, IModelDeltaDecoration, ITextModel, IValidEditOperation, OverviewRulerLane, TrackedRangeStickiness } from 'vs/editor/common/model';
import { IModelDecorationsChangedEvent } from 'vs/editor/common/textModelEvents';
import { ICommandMetadata } from 'vs/platform/commands/common/commands';

/**
* A builder and helper for edit operations for a command.
Expand Down Expand Up @@ -155,6 +156,7 @@ export interface IEditorAction {
readonly id: string;
readonly label: string;
readonly alias: string;
readonly metadata: ICommandMetadata | undefined;
isSupported(): boolean;
run(args?: unknown): Promise<void>;
}
Expand Down
38 changes: 19 additions & 19 deletions src/vs/editor/common/standalone/standaloneEnums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,28 +572,28 @@ export enum KeyCode {
* Either the angle bracket key or the backslash key on the RT 102-key keyboard.
*/
IntlBackslash = 97,
Numpad0 = 98,
Numpad1 = 99,
Numpad2 = 100,
Numpad3 = 101,
Numpad4 = 102,
Numpad5 = 103,
Numpad6 = 104,
Numpad7 = 105,
Numpad8 = 106,
Numpad9 = 107,
NumpadMultiply = 108,
NumpadAdd = 109,
NUMPAD_SEPARATOR = 110,
NumpadSubtract = 111,
NumpadDecimal = 112,
NumpadDivide = 113,
Numpad0 = 98,// VK_NUMPAD0, 0x60, Numeric keypad 0 key
Numpad1 = 99,// VK_NUMPAD1, 0x61, Numeric keypad 1 key
Numpad2 = 100,// VK_NUMPAD2, 0x62, Numeric keypad 2 key
Numpad3 = 101,// VK_NUMPAD3, 0x63, Numeric keypad 3 key
Numpad4 = 102,// VK_NUMPAD4, 0x64, Numeric keypad 4 key
Numpad5 = 103,// VK_NUMPAD5, 0x65, Numeric keypad 5 key
Numpad6 = 104,// VK_NUMPAD6, 0x66, Numeric keypad 6 key
Numpad7 = 105,// VK_NUMPAD7, 0x67, Numeric keypad 7 key
Numpad8 = 106,// VK_NUMPAD8, 0x68, Numeric keypad 8 key
Numpad9 = 107,// VK_NUMPAD9, 0x69, Numeric keypad 9 key
NumpadMultiply = 108,// VK_MULTIPLY, 0x6A, Multiply key
NumpadAdd = 109,// VK_ADD, 0x6B, Add key
NUMPAD_SEPARATOR = 110,// VK_SEPARATOR, 0x6C, Separator key
NumpadSubtract = 111,// VK_SUBTRACT, 0x6D, Subtract key
NumpadDecimal = 112,// VK_DECIMAL, 0x6E, Decimal key
NumpadDivide = 113,// VK_DIVIDE, 0x6F,
/**
* Cover all key codes when IME is processing input.
*/
KEY_IN_COMPOSITION = 114,
ABNT_C1 = 115,
ABNT_C2 = 116,
ABNT_C1 = 115,// Brazilian (ABNT) Keyboard
ABNT_C2 = 116,// Brazilian (ABNT) Keyboard
AudioVolumeMute = 117,
AudioVolumeUp = 118,
AudioVolumeDown = 119,
Expand Down Expand Up @@ -924,4 +924,4 @@ export enum WrappingIndent {
* DeepIndent => wrapped lines get +2 indentation toward the parent.
*/
DeepIndent = 3
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class SelectToBracketAction extends EditorAction {
alias: 'Select to Bracket',
precondition: undefined,
metadata: {
description: `Select to Bracket`,
description: nls.localize2('smartSelect.selectToBracketDescription', "Select the text inside and including the brackets or curly braces"),
args: [{
name: 'args',
schema: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import { stripIcons } from 'vs/base/common/iconLabels';
import { IEditor } from 'vs/editor/common/editorCommon';
import { isLocalizedString } from 'vs/platform/action/common/action';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
Expand Down Expand Up @@ -38,10 +39,17 @@ export abstract class AbstractEditorCommandsQuickAccessProvider extends Abstract

const editorCommandPicks: ICommandQuickPick[] = [];
for (const editorAction of activeTextEditorControl.getSupportedActions()) {
const metadataDescription = editorAction.metadata?.description;
const commandDescription = metadataDescription === undefined || isLocalizedString(metadataDescription)
? metadataDescription
// TODO: this type will eventually not be a string and when that happens, this should simplified.
: { value: metadataDescription, original: metadataDescription };

editorCommandPicks.push({
commandId: editorAction.id,
commandAlias: editorAction.alias,
label: stripIcons(editorAction.label) || editorAction.id,
commandDescription,
});
}

Expand Down
2 changes: 2 additions & 0 deletions src/vs/editor/standalone/browser/standaloneCodeEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ export class StandaloneCodeEditor extends CodeEditorWidget implements IStandalon
uniqueId,
label,
label,
// TODO: support this somehow? Do I need the metadata to be a part of the API?
undefined,
precondition,
(...args: unknown[]) => Promise.resolve(_descriptor.run(this, ...args)),
this._contextKeyService
Expand Down
5 changes: 5 additions & 0 deletions src/vs/monaco.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

// TODO
// eslint-disable-next-line local/code-import-patterns
import { ICommandMetadata } from 'vs/platform/commands/common/commands';

declare let MonacoEnvironment: monaco.Environment | undefined;

interface Window {
Expand Down Expand Up @@ -2469,6 +2473,7 @@ declare namespace monaco.editor {
readonly id: string;
readonly label: string;
readonly alias: string;
readonly metadata: ICommandMetadata | undefined;
isSupported(): boolean;
run(args?: unknown): Promise<void>;
}
Expand Down

0 comments on commit 51fb6d6

Please sign in to comment.