Skip to content

Commit cd9d9dc

Browse files
committed
Expose precondition (closes microsoft/monaco-editor#340)
1 parent 3b7c599 commit cd9d9dc

6 files changed

Lines changed: 46 additions & 26 deletions

File tree

src/vs/editor/browser/standalone/simpleServices.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import { IConfigurationService, IConfigurationServiceEvent, IConfigurationValue,
1212
import { IEditor, IEditorInput, IEditorOptions, IEditorService, IResourceInput, Position } from 'vs/platform/editor/common/editor';
1313
import { ICommandService, ICommand, ICommandEvent, ICommandHandler, CommandsRegistry } from 'vs/platform/commands/common/commands';
1414
import { AbstractKeybindingService } from 'vs/platform/keybinding/common/abstractKeybindingService';
15-
import { KeybindingResolver, IOSupport } from 'vs/platform/keybinding/common/keybindingResolver';
15+
import { KeybindingResolver } from 'vs/platform/keybinding/common/keybindingResolver';
1616
import { IKeybindingEvent, IKeybindingItem, KeybindingSource } from 'vs/platform/keybinding/common/keybinding';
17-
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
17+
import { ContextKeyExpr, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
1818
import { IConfirmation, IMessageService } from 'vs/platform/message/common/message';
1919
import * as editorCommon from 'vs/editor/common/editorCommon';
2020
import { ICodeEditor, IDiffEditor } from 'vs/editor/browser/editorBrowser';
@@ -327,15 +327,13 @@ export class StandaloneKeybindingService extends AbstractKeybindingService {
327327
}));
328328
}
329329

330-
public addDynamicKeybinding(commandId: string, keybinding: number, handler: ICommandHandler, when: string): IDisposable {
330+
public addDynamicKeybinding(commandId: string, keybinding: number, handler: ICommandHandler, when: ContextKeyExpr): IDisposable {
331331
let toDispose: IDisposable[] = [];
332332

333-
let parsedContext = IOSupport.readKeybindingWhen(when);
334-
335333
this._dynamicKeybindings.push({
336334
keybinding: keybinding,
337335
command: commandId,
338-
when: parsedContext,
336+
when: when,
339337
weight1: 1000,
340338
weight2: 0
341339
});

src/vs/editor/browser/standalone/standaloneCodeEditor.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { IContextViewService } from 'vs/platform/contextview/browser/contextView
1010
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
1111
import { ICommandService, ICommandHandler } from 'vs/platform/commands/common/commands';
1212
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
13-
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
13+
import { ContextKeyExpr, IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
1414
import { IEditorOptions, IActionDescriptor, ICodeEditorWidgetCreationOptions, IDiffEditorOptions, IModel, IModelChangedEvent, EventType } from 'vs/editor/common/editorCommon';
1515
import { ICodeEditorService } from 'vs/editor/common/services/codeEditorService';
1616
import { IEditorWorkerService } from 'vs/editor/common/services/editorWorkerService';
@@ -20,6 +20,7 @@ import { CodeEditor } from 'vs/editor/browser/codeEditor';
2020
import { DiffEditorWidget } from 'vs/editor/browser/widget/diffEditorWidget';
2121
import { ICodeEditor, IDiffEditor } from 'vs/editor/browser/editorBrowser';
2222
import { IStandaloneColorService } from 'vs/editor/common/services/standaloneColorService';
23+
import { IOSupport } from 'vs/platform/keybinding/common/keybindingResolver';
2324

2425
/**
2526
* The options to create an editor.
@@ -133,8 +134,8 @@ export class StandaloneEditor extends CodeEditor implements IStandaloneCodeEdito
133134
return null;
134135
}
135136
let commandId = 'DYNAMIC_' + (++LAST_GENERATED_COMMAND_ID);
136-
137-
this._standaloneKeybindingService.addDynamicKeybinding(commandId, keybinding, handler, context);
137+
let whenExpression = IOSupport.readKeybindingWhen(context);
138+
this._standaloneKeybindingService.addDynamicKeybinding(commandId, keybinding, handler, whenExpression);
138139
return commandId;
139140
}
140141

@@ -157,9 +158,13 @@ export class StandaloneEditor extends CodeEditor implements IStandaloneCodeEdito
157158
let handler: ICommandHandler = (accessor) => {
158159
return this.trigger('keyboard', descriptor.id, null);
159160
};
161+
let whenExpression = ContextKeyExpr.and(
162+
IOSupport.readKeybindingWhen(descriptor.precondition),
163+
IOSupport.readKeybindingWhen(descriptor.keybindingContext),
164+
);
160165
toDispose = toDispose.concat(
161166
descriptor.keybindings.map((kb) => {
162-
return this._standaloneKeybindingService.addDynamicKeybinding(addedAction.uniqueId, kb, handler, descriptor.keybindingContext);
167+
return this._standaloneKeybindingService.addDynamicKeybinding(addedAction.uniqueId, kb, handler, whenExpression);
163168
})
164169
);
165170
}
@@ -226,7 +231,8 @@ export class StandaloneDiffEditor extends DiffEditorWidget implements IStandalon
226231
return null;
227232
}
228233
let commandId = 'DYNAMIC_' + (++LAST_GENERATED_COMMAND_ID);
229-
this._standaloneKeybindingService.addDynamicKeybinding(commandId, keybinding, handler, context);
234+
let whenExpression = IOSupport.readKeybindingWhen(context);
235+
this._standaloneKeybindingService.addDynamicKeybinding(commandId, keybinding, handler, whenExpression);
230236
return commandId;
231237
}
232238

@@ -249,9 +255,13 @@ export class StandaloneDiffEditor extends DiffEditorWidget implements IStandalon
249255
let handler: ICommandHandler = (ctx) => {
250256
return this.trigger('keyboard', descriptor.id, null);
251257
};
258+
let whenExpression = ContextKeyExpr.and(
259+
IOSupport.readKeybindingWhen(descriptor.precondition),
260+
IOSupport.readKeybindingWhen(descriptor.keybindingContext),
261+
);
252262
toDispose = toDispose.concat(
253263
descriptor.keybindings.map((kb) => {
254-
return this._standaloneKeybindingService.addDynamicKeybinding(addedAction.uniqueId, kb, handler, descriptor.keybindingContext);
264+
return this._standaloneKeybindingService.addDynamicKeybinding(addedAction.uniqueId, kb, handler, whenExpression);
255265
})
256266
);
257267
}

src/vs/editor/common/commonCodeEditor.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { hash } from 'vs/base/common/hash';
3030
import { EditorModeContext } from 'vs/editor/common/modes/editorModeContext';
3131
import { MenuId, MenuRegistry, IMenuItem } from 'vs/platform/actions/common/actions';
3232
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
33+
import { IOSupport } from 'vs/platform/keybinding/common/keybindingResolver';
3334

3435
import EditorContextKeys = editorCommon.EditorContextKeys;
3536

@@ -537,7 +538,7 @@ export abstract class CommonCodeEditor extends EventEmitter implements editorCom
537538
// Generate a unique id to allow the same descriptor.id across multiple editor instances
538539
let uniqueId = this.getId() + ':' + descriptor.id;
539540

540-
let action = new DynamicEditorAction(descriptor, this);
541+
let action = new DynamicEditorAction(descriptor.id, descriptor.label, descriptor.run, this);
541542

542543
// Register the command
543544
toDispose.push(CommandsRegistry.registerCommand(uniqueId, () => action.run()));
@@ -548,7 +549,10 @@ export abstract class CommonCodeEditor extends EventEmitter implements editorCom
548549
id: uniqueId,
549550
title: descriptor.label
550551
},
551-
when: ContextKeyExpr.equals('editorId', this.getId()),
552+
when: ContextKeyExpr.and(
553+
ContextKeyExpr.equals('editorId', this.getId()),
554+
IOSupport.readKeybindingWhen(descriptor.precondition)
555+
),
552556
group: descriptor.contextMenuGroupId,
553557
order: descriptor.contextMenuOrder || 0
554558
};

src/vs/editor/common/editorAction.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
'use strict';
66

77
import { TPromise } from 'vs/base/common/winjs.base';
8-
import { IActionDescriptor, ICommonCodeEditor, IEditorAction } from 'vs/editor/common/editorCommon';
8+
import { ICommonCodeEditor, IEditorAction } from 'vs/editor/common/editorCommon';
99
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
1010
import { EditorAction } from 'vs/editor/common/editorCommonExtensions';
1111
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
@@ -62,10 +62,10 @@ export class DynamicEditorAction extends AbstractInternalEditorAction implements
6262

6363
private _run: (editor: ICommonCodeEditor) => void;
6464

65-
constructor(descriptor: IActionDescriptor, editor: ICommonCodeEditor) {
66-
super(descriptor.id, descriptor.label, descriptor.label, editor);
65+
constructor(id: string, label: string, run: (editor: ICommonCodeEditor) => void | TPromise<void>, editor: ICommonCodeEditor) {
66+
super(id, label, label, editor);
6767

68-
this._run = descriptor.run;
68+
this._run = run;
6969
}
7070

7171
public isSupported(): boolean {

src/vs/editor/common/editorCommon.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3358,10 +3358,18 @@ export interface IActionDescriptor {
33583358
* A label of the action that will be presented to the user.
33593359
*/
33603360
label: string;
3361+
/**
3362+
* Precondition rule.
3363+
*/
3364+
precondition?: string;
33613365
/**
33623366
* An array of keybindings for the action.
33633367
*/
33643368
keybindings?: number[];
3369+
/**
3370+
* The keybinding rule (condition on top of precondition).
3371+
*/
3372+
keybindingContext?: string;
33653373
/**
33663374
* Control if the action should show up in the context menu and where.
33673375
* The context menu of the editor has these default:
@@ -3376,10 +3384,6 @@ export interface IActionDescriptor {
33763384
* Control the order in the context menu group.
33773385
*/
33783386
contextMenuOrder?: number;
3379-
/**
3380-
* The keybinding rule.
3381-
*/
3382-
keybindingContext?: string;
33833387
/**
33843388
* Method that will be executed when the action is triggered.
33853389
* @param editor The editor instance is passed in as a convinience

src/vs/monaco.d.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2802,10 +2802,18 @@ declare module monaco.editor {
28022802
* A label of the action that will be presented to the user.
28032803
*/
28042804
label: string;
2805+
/**
2806+
* Precondition rule.
2807+
*/
2808+
precondition?: string;
28052809
/**
28062810
* An array of keybindings for the action.
28072811
*/
28082812
keybindings?: number[];
2813+
/**
2814+
* The keybinding rule (condition on top of precondition).
2815+
*/
2816+
keybindingContext?: string;
28092817
/**
28102818
* Control if the action should show up in the context menu and where.
28112819
* The context menu of the editor has these default:
@@ -2820,10 +2828,6 @@ declare module monaco.editor {
28202828
* Control the order in the context menu group.
28212829
*/
28222830
contextMenuOrder?: number;
2823-
/**
2824-
* The keybinding rule.
2825-
*/
2826-
keybindingContext?: string;
28272831
/**
28282832
* Method that will be executed when the action is triggered.
28292833
* @param editor The editor instance is passed in as a convinience

0 commit comments

Comments
 (0)