From 61b42f733237a52c7e02e95c2a3aabb12433f964 Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Thu, 4 Aug 2016 20:51:14 +0200 Subject: [PATCH] Introduce EditorCommand and adopt it --- src/vs/editor/common/config/config.ts | 6 +- .../editor/common/editorCommonExtensions.ts | 145 ++++++++++++------ .../accessibility/browser/accessibility.ts | 25 ++- .../contrib/clipboard/browser/clipboard.ts | 79 +++++----- .../browser/defineKeybinding.ts | 4 +- .../contrib/find/common/findController.ts | 129 ++++++++++------ .../editor/contrib/folding/browser/folding.ts | 4 +- .../contrib/format/common/formatActions.ts | 4 +- .../browser/goToDeclaration.ts | 9 +- .../contrib/gotoError/browser/gotoError.ts | 33 ++-- src/vs/editor/contrib/hover/browser/hover.ts | 4 +- .../contrib/quickFix/browser/quickFix.ts | 84 ++++++---- .../contrib/quickOpen/browser/gotoLine.ts | 4 +- .../browser/referenceSearch.ts | 4 +- .../editor/contrib/rename/browser/rename.ts | 37 +++-- .../editor/contrib/snippet/common/snippet.ts | 57 +++++-- .../contrib/suggest/browser/tabCompletion.ts | 38 ++--- src/vs/monaco.d.ts | 1 + src/vs/platform/actions/common/menuService.ts | 2 +- .../actions/common/resourceContextKey.ts | 19 ++- .../browser/keybindingServiceImpl.ts | 4 + .../platform/keybinding/common/keybinding.ts | 9 ++ .../test/common/mockKeybindingService.ts | 4 + .../browser/parts/editor/editorCommands.ts | 4 +- .../parts/debug/browser/breakpointWidget.ts | 32 +++- .../parts/debug/browser/debugActions.ts | 10 +- src/vs/workbench/parts/debug/common/debug.ts | 4 +- .../electron-browser/debug.contribution.ts | 22 +-- .../debug/electron-browser/debugService.ts | 2 +- .../parts/search/browser/searchWidget.ts | 15 +- 30 files changed, 510 insertions(+), 284 deletions(-) diff --git a/src/vs/editor/common/config/config.ts b/src/vs/editor/common/config/config.ts index f64229af3fa..2f562186fe9 100644 --- a/src/vs/editor/common/config/config.ts +++ b/src/vs/editor/common/config/config.ts @@ -14,9 +14,9 @@ import * as editorCommon from 'vs/editor/common/editorCommon'; import {ICodeEditorService} from 'vs/editor/common/services/codeEditorService'; import {ICommandHandler} from 'vs/platform/commands/common/commands'; -const H = editorCommon.Handler; -const D = editorCommon.CommandDescription; -const EditorKbExpr = editorCommon.EditorKbExpr; +import H = editorCommon.Handler; +import D = editorCommon.CommandDescription; +import EditorKbExpr = editorCommon.EditorKbExpr; export function findFocusedEditor(commandId: string, accessor: ServicesAccessor, complain: boolean): editorCommon.ICommonCodeEditor { let editor = accessor.get(ICodeEditorService).getFocusedCodeEditor(); diff --git a/src/vs/editor/common/editorCommonExtensions.ts b/src/vs/editor/common/editorCommonExtensions.ts index ce639ab210f..a958445882a 100644 --- a/src/vs/editor/common/editorCommonExtensions.ts +++ b/src/vs/editor/common/editorCommonExtensions.ts @@ -4,7 +4,7 @@ *--------------------------------------------------------------------------------------------*/ 'use strict'; -import {illegalArgument, onUnexpectedError} from 'vs/base/common/errors'; +import {illegalArgument} from 'vs/base/common/errors'; import URI from 'vs/base/common/uri'; import {TPromise} from 'vs/base/common/winjs.base'; import {ServicesAccessor, IInstantiationService} from 'vs/platform/instantiation/common/instantiation'; @@ -19,7 +19,7 @@ import * as editorCommon from 'vs/editor/common/editorCommon'; import {IModelService} from 'vs/editor/common/services/modelService'; import {MenuId, MenuRegistry} from 'vs/platform/actions/common/actions'; -const EditorKbExpr = editorCommon.EditorKbExpr; +import EditorKbExpr = editorCommon.EditorKbExpr; export type ServicesAccessor = ServicesAccessor; @@ -76,6 +76,10 @@ export module CommonEditorRegistry { KeybindingsRegistry.registerCommandDesc(commandDesc); } + export function registerEditorCommand2(desc: EditorCommand): void { + KeybindingsRegistry.registerCommandDesc(desc.toCommandDescriptor(KeybindingsRegistry.WEIGHT.editorContrib())); + } + export function registerLanguageCommand(id: string, handler: (accessor: ServicesAccessor, args: { [n: string]: any }) => any) { KeybindingsRegistry.registerCommandDesc({ id, @@ -153,25 +157,7 @@ class EditorContributionRegistry { }); } - if (!action.kbOpts) { - action.kbOpts = { - primary: 0 - }; - } - - let commandDesc: ICommandDescriptor = { - id: action.id, - handler: action.kbOpts.commandHandler || triggerEditorActionGlobal.bind(null, action.id), - weight: KeybindingsRegistry.WEIGHT.editorContrib(), - when: action.kbOpts.kbExpr, - primary: action.kbOpts.primary, - secondary: action.kbOpts.secondary, - win: action.kbOpts.win, - linux: action.kbOpts.linux, - mac: action.kbOpts.mac, - }; - - KeybindingsRegistry.registerCommandDesc(commandDesc); + KeybindingsRegistry.registerCommandDesc(action.toCommandDescriptor(KeybindingsRegistry.WEIGHT.editorContrib())); this.editorActions.push(action); } @@ -186,26 +172,7 @@ class EditorContributionRegistry { } Registry.add(Extensions.EditorCommonContributions, new EditorContributionRegistry()); -function triggerEditorActionGlobal(actionId: string, accessor: ServicesAccessor, args: any): void { - // TODO: this is not necessarily keyboard - var focusedEditor = findFocusedEditor(actionId, accessor, false); - if (focusedEditor) { - focusedEditor.trigger('keyboard', actionId, args); - return; - } - - var activeEditor = getActiveEditor(accessor); - if (activeEditor) { - var action = activeEditor.getAction(actionId); - if (action) { - accessor.get(ITelemetryService).publicLog('editorActionInvoked', {name: action.label} ); - action.run().done(null, onUnexpectedError); - } - return; - } -} - -function whenRule(needsTextFocus: boolean, needsKey: string): KbExpr { +export function whenRule(needsTextFocus: boolean, needsKey: string): KbExpr { let base = (needsTextFocus ? EditorKbExpr.TextFocus : EditorKbExpr.Focus); @@ -226,28 +193,112 @@ function createCommandHandler(commandId: string, handler: IEditorCommandHandler) export interface IEditorActionKeybindingOptions2 extends IKeybindings { kbExpr?: KbExpr; - commandHandler?: ICommandHandler; + weight?: number; +} + +export abstract class Command { + public id: string; + public kbOpts: IEditorActionKeybindingOptions2; + + constructor(id:string) { + this.id = id; + this.kbOpts = null; + } + + public abstract runCommand(accessor:ServicesAccessor, args: any): void | TPromise; + + public toCommandDescriptor(defaultWeight:number): ICommandDescriptor { + const kbOpts = this.kbOpts || { primary: 0 }; + return { + id: this.id, + handler: (accessor, args) => this.runCommand(accessor, args), + weight: kbOpts.weight || defaultWeight, + when: kbOpts.kbExpr, + primary: kbOpts.primary, + secondary: kbOpts.secondary, + win: kbOpts.win, + linux: kbOpts.linux, + mac: kbOpts.mac, + }; + } +} + +export interface EditorControllerCommand { + new(id:string, callback:(controller:T)=>void, keybindings:IKeybindings): EditorCommand; } -export abstract class EditorAction { +export abstract class EditorCommand extends Command { + + public static bindToContribution(controllerGetter:(editor:editorCommon.ICommonCodeEditor) => T, weight: number, kbExpr: KbExpr): EditorControllerCommand { + return class EditorControllerCommandImpl extends EditorCommand { + private _callback:(controller:T)=>void; + + constructor(id:string, callback:(controller:T)=>void, keybindings:IKeybindings) { + super(id); + + this._callback = callback; + + this.kbOpts = { + weight: weight, + kbExpr: kbExpr, + primary: keybindings.primary, + secondary: keybindings.secondary, + win: keybindings.win, + linux: keybindings.linux, + mac: keybindings.mac, + }; + } + + protected runEditorCommand(accessor:ServicesAccessor, editor: editorCommon.ICommonCodeEditor, args: any): void { + this._callback(controllerGetter(editor)); + } + }; + } + + constructor(id:string) { + super(id); + } + + public runCommand(accessor:ServicesAccessor, args: any): void | TPromise { + let editor = findFocusedEditor(this.id, accessor, false); + if (!editor) { + editor = getActiveEditor(accessor); + } + if (!editor) { + // well, at least we tried... + return; + } + return this.runEditorCommand(accessor, editor, args); + } + + protected abstract runEditorCommand(accessor:ServicesAccessor, editor: editorCommon.ICommonCodeEditor, args: any): void | TPromise; +} + +export abstract class EditorAction extends EditorCommand { private _needsWritableEditor: boolean; - public id: string; public label: string; public alias: string; - public kbOpts: IEditorActionKeybindingOptions2; public menuOpts: IEditorCommandMenuOptions; constructor(id:string, label:string, alias:string, needsWritableEditor:boolean) { - this.id = id; + super(id); this.label = label; this.alias = alias; this._needsWritableEditor = needsWritableEditor; - this.kbOpts = null; this.menuOpts = null; } + protected runEditorCommand(accessor:ServicesAccessor, editor: editorCommon.ICommonCodeEditor, args: any): void | TPromise { + if (!this.enabled(accessor, editor)) { + return; + } + + accessor.get(ITelemetryService).publicLog('editorActionInvoked', { name: this.label, id: this.id }); + return this.run(accessor, editor); + } + public enabled(accessor:ServicesAccessor, editor:editorCommon.ICommonCodeEditor): boolean { if (this._needsWritableEditor) { return !editor.getConfiguration().readOnly; diff --git a/src/vs/editor/contrib/accessibility/browser/accessibility.ts b/src/vs/editor/contrib/accessibility/browser/accessibility.ts index 3bd75f99084..1b2d9f54187 100644 --- a/src/vs/editor/contrib/accessibility/browser/accessibility.ts +++ b/src/vs/editor/contrib/accessibility/browser/accessibility.ts @@ -15,16 +15,16 @@ import {renderHtml} from 'vs/base/browser/htmlContentRenderer'; import {StyleMutator} from 'vs/base/browser/styleMutator'; import {Widget} from 'vs/base/browser/ui/widget'; import {ServicesAccessor} from 'vs/platform/instantiation/common/instantiation'; -import {IKeybindingContextKey, IKeybindingService} from 'vs/platform/keybinding/common/keybinding'; +import {KbExpr, KbCtxKey, IKeybindingContextKey, IKeybindingService} from 'vs/platform/keybinding/common/keybinding'; import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry'; import {GlobalScreenReaderNVDA} from 'vs/editor/common/config/commonEditorConfig'; import {ICommonCodeEditor, IEditorContribution, EditorKbExpr, SHOW_ACCESSIBILITY_HELP_ACTION_ID} from 'vs/editor/common/editorCommon'; -import {CommonEditorRegistry, EditorAction} from 'vs/editor/common/editorCommonExtensions'; +import {CommonEditorRegistry, EditorAction, EditorCommand} from 'vs/editor/common/editorCommonExtensions'; import {ICodeEditor, IOverlayWidget, IOverlayWidgetPosition} from 'vs/editor/browser/editorBrowser'; import {EditorBrowserRegistry} from 'vs/editor/browser/editorBrowserExtensions'; import {ToggleTabFocusModeAction} from 'vs/editor/contrib/toggleTabFocusMode/common/toggleTabFocusMode'; -const CONTEXT_ACCESSIBILITY_WIDGET_VISIBLE = 'accessibilityHelpWidgetVisible'; +const CONTEXT_ACCESSIBILITY_WIDGET_VISIBLE = new KbCtxKey('accessibilityHelpWidgetVisible'); const TOGGLE_EXPERIMENTAL_SCREEN_READER_SUPPORT_COMMAND_ID = 'toggleExperimentalScreenReaderSupport'; class AccessibilityHelpController extends Disposable implements IEditorContribution { @@ -75,7 +75,7 @@ class AccessibilityHelpWidget extends Widget implements IOverlayWidget { this._editor = editor; this._keybindingService = keybindingService; - this._isVisibleKey = keybindingService.createKey(CONTEXT_ACCESSIBILITY_WIDGET_VISIBLE, false); + this._isVisibleKey = CONTEXT_ACCESSIBILITY_WIDGET_VISIBLE.bindTo(keybindingService, false); this._domNode = document.createElement('div'); this._domNode.className = 'accessibilityHelpWidget'; @@ -213,9 +213,20 @@ class ShowAccessibilityHelpAction extends EditorAction { EditorBrowserRegistry.registerEditorContribution(AccessibilityHelpController); CommonEditorRegistry.registerEditorAction(new ShowAccessibilityHelpAction()); -CommonEditorRegistry.registerEditorCommand('closeAccessibilityHelp', CommonEditorRegistry.commandWeight(100), { primary: KeyCode.Escape, secondary: [KeyMod.Shift | KeyCode.Escape] }, false, CONTEXT_ACCESSIBILITY_WIDGET_VISIBLE, (ctx, editor, args) => { - AccessibilityHelpController.get(editor).hide(); -}); + +const AccessibilityHelpCommand = EditorCommand.bindToContribution( + AccessibilityHelpController.get, + CommonEditorRegistry.commandWeight(100), + KbExpr.and(EditorKbExpr.Focus, CONTEXT_ACCESSIBILITY_WIDGET_VISIBLE) +); + +CommonEditorRegistry.registerEditorCommand2(new AccessibilityHelpCommand( + 'closeAccessibilityHelp', + x => x.hide(), + { + primary: KeyCode.Escape, secondary: [KeyMod.Shift | KeyCode.Escape] + } +)); KeybindingsRegistry.registerCommandDesc({ id: TOGGLE_EXPERIMENTAL_SCREEN_READER_SUPPORT_COMMAND_ID, handler: (accessor: ServicesAccessor) => { diff --git a/src/vs/editor/contrib/clipboard/browser/clipboard.ts b/src/vs/editor/contrib/clipboard/browser/clipboard.ts index 14dbbb6d8e0..0a35e0abaca 100644 --- a/src/vs/editor/contrib/clipboard/browser/clipboard.ts +++ b/src/vs/editor/contrib/clipboard/browser/clipboard.ts @@ -16,14 +16,39 @@ import * as editorCommon from 'vs/editor/common/editorCommon'; import {EditorAction, CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions'; import {MenuId} from 'vs/platform/actions/common/actions'; +import EditorKbExpr = editorCommon.EditorKbExpr; + const CLIPBOARD_CONTEXT_MENU_GROUP = '9_cutcopypaste'; -abstract class ClipboardWritingAction extends EditorAction { +abstract class ExecCommandAction extends EditorAction { + + private browserCommand:string; - constructor(id:string, label:string, alias:string, needsWritableEditor:boolean) { + constructor(id:string, label:string, alias:string, needsWritableEditor:boolean, browserCommand:string) { super(id, label, alias, needsWritableEditor); + this.browserCommand = browserCommand; + } + + public runCommand(accessor:ServicesAccessor, args: any): void { + let focusedEditor = findFocusedEditor(this.id, accessor, false); + // Only if editor text focus (i.e. not if editor has widget focus). + if (focusedEditor && focusedEditor.isFocused()) { + focusedEditor.trigger('keyboard', this.id, args); + return; + } + + document.execCommand(this.browserCommand); + } + public run(accessor:ServicesAccessor, editor:editorCommon.ICommonCodeEditor): void { + editor.focus(); + document.execCommand(this.browserCommand); + } +} + +abstract class ClipboardWritingAction extends ExecCommandAction { + public enabled(accessor:ServicesAccessor, editor:editorCommon.ICommonCodeEditor): boolean { if (!super.enabled(accessor, editor)) { return false; @@ -58,18 +83,18 @@ class ExecCommandCutAction extends ClipboardWritingAction { 'editor.action.clipboardCutAction', nls.localize('actions.clipboard.cutLabel', "Cut"), 'Cut', - true + true, + 'cut' ); this.kbOpts = { - commandHandler: execCommandToHandler.bind(null, this.id, 'cut'), - kbExpr: KbExpr.and(editorCommon.EditorKbExpr.TextFocus, editorCommon.EditorKbExpr.Writable), + kbExpr: KbExpr.and(EditorKbExpr.TextFocus, EditorKbExpr.Writable), primary: KeyMod.CtrlCmd | KeyCode.KEY_X, win: { primary: KeyMod.CtrlCmd | KeyCode.KEY_X, secondary: [KeyMod.Shift | KeyCode.Delete] } }; this.menuOpts = { - kbExpr: editorCommon.EditorKbExpr.Writable, + kbExpr: EditorKbExpr.Writable, menu: MenuId.EditorContext, group: CLIPBOARD_CONTEXT_MENU_GROUP, order: 1 @@ -82,11 +107,6 @@ class ExecCommandCutAction extends ClipboardWritingAction { } return editorCursorIsInEditableRange(editor); } - - public run(accessor:ServicesAccessor, editor:editorCommon.ICommonCodeEditor): void { - editor.focus(); - document.execCommand('cut'); - } } class ExecCommandCopyAction extends ClipboardWritingAction { @@ -96,12 +116,12 @@ class ExecCommandCopyAction extends ClipboardWritingAction { 'editor.action.clipboardCopyAction', nls.localize('actions.clipboard.copyLabel', "Copy"), 'Copy', - false + false, + 'copy' ); this.kbOpts = { - commandHandler: execCommandToHandler.bind(null, this.id, 'copy'), - kbExpr: editorCommon.EditorKbExpr.TextFocus, + kbExpr: EditorKbExpr.TextFocus, primary: KeyMod.CtrlCmd | KeyCode.KEY_C, win: { primary: KeyMod.CtrlCmd | KeyCode.KEY_C, secondary: [KeyMod.CtrlCmd | KeyCode.Insert] } }; @@ -113,32 +133,27 @@ class ExecCommandCopyAction extends ClipboardWritingAction { order: 2 }; } - - public run(accessor:ServicesAccessor, editor:editorCommon.ICommonCodeEditor): void { - editor.focus(); - document.execCommand('copy'); - } } -class ExecCommandPasteAction extends EditorAction { +class ExecCommandPasteAction extends ExecCommandAction { constructor() { super( 'editor.action.clipboardPasteAction', nls.localize('actions.clipboard.pasteLabel', "Paste"), 'Paste', - true + true, + 'paste' ); this.kbOpts = { - commandHandler: execCommandToHandler.bind(null, this.id, 'paste'), - kbExpr: KbExpr.and(editorCommon.EditorKbExpr.TextFocus, editorCommon.EditorKbExpr.Writable), + kbExpr: KbExpr.and(EditorKbExpr.TextFocus, EditorKbExpr.Writable), primary: KeyMod.CtrlCmd | KeyCode.KEY_V, win: { primary: KeyMod.CtrlCmd | KeyCode.KEY_V, secondary: [KeyMod.Shift | KeyCode.Insert] } }; this.menuOpts = { - kbExpr: editorCommon.EditorKbExpr.Writable, + kbExpr: EditorKbExpr.Writable, menu: MenuId.EditorContext, group: CLIPBOARD_CONTEXT_MENU_GROUP, order: 3 @@ -151,11 +166,6 @@ class ExecCommandPasteAction extends EditorAction { } return editorCursorIsInEditableRange(editor); } - - public run(accessor:ServicesAccessor, editor:editorCommon.ICommonCodeEditor): void { - editor.focus(); - document.execCommand('paste'); - } } if (browser.supportsExecCommand('cut')) { @@ -167,14 +177,3 @@ if (browser.supportsExecCommand('copy')) { if (browser.supportsExecCommand('paste')) { CommonEditorRegistry.registerEditorAction(new ExecCommandPasteAction()); } - -function execCommandToHandler(actionId: string, browserCommand: string, accessor: ServicesAccessor, args: any): void { - let focusedEditor = findFocusedEditor(actionId, accessor, false); - // Only if editor text focus (i.e. not if editor has widget focus). - if (focusedEditor && focusedEditor.isFocused()) { - focusedEditor.trigger('keyboard', actionId, args); - return; - } - - document.execCommand(browserCommand); -} diff --git a/src/vs/editor/contrib/defineKeybinding/browser/defineKeybinding.ts b/src/vs/editor/contrib/defineKeybinding/browser/defineKeybinding.ts index fba8d4054ed..7472f4bc546 100644 --- a/src/vs/editor/contrib/defineKeybinding/browser/defineKeybinding.ts +++ b/src/vs/editor/contrib/defineKeybinding/browser/defineKeybinding.ts @@ -25,6 +25,8 @@ import {EditorBrowserRegistry} from 'vs/editor/browser/editorBrowserExtensions'; import {CodeSnippet, getSnippetController} from 'vs/editor/contrib/snippet/common/snippet'; import {SmartSnippetInserter} from 'vs/editor/contrib/defineKeybinding/common/smartSnippetInserter'; +import EditorKbExpr = editorCommon.EditorKbExpr; + const NLS_LAUNCH_MESSAGE = nls.localize('defineKeybinding.start', "Define Keybinding"); const NLS_DEFINE_MESSAGE = nls.localize('defineKeybinding.initial', "Press desired key combination and ENTER"); const NLS_KB_LAYOUT_INFO_MESSAGE = nls.localize('defineKeybinding.kbLayoutInfoMessage', "For your current keyboard layout press "); @@ -456,7 +458,7 @@ export class DefineKeybindingAction extends EditorAction { ); this.kbOpts = { - kbExpr: editorCommon.EditorKbExpr.Focus, + kbExpr: EditorKbExpr.Focus, primary: KeyMod.chord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyMod.CtrlCmd | KeyCode.KEY_K) }; } diff --git a/src/vs/editor/contrib/find/common/findController.ts b/src/vs/editor/contrib/find/common/findController.ts index 4cc9569bf9d..52098d0599d 100644 --- a/src/vs/editor/contrib/find/common/findController.ts +++ b/src/vs/editor/contrib/find/common/findController.ts @@ -7,18 +7,18 @@ import * as nls from 'vs/nls'; import {KeyCode, KeyMod} from 'vs/base/common/keyCodes'; import {Disposable} from 'vs/base/common/lifecycle'; -import {IKeybindingContextKey, IKeybindingService, IKeybindings} from 'vs/platform/keybinding/common/keybinding'; +import {KbExpr, KbCtxKey, IKeybindingContextKey, IKeybindingService} from 'vs/platform/keybinding/common/keybinding'; import {Range} from 'vs/editor/common/core/range'; import {Selection} from 'vs/editor/common/core/selection'; import * as strings from 'vs/base/common/strings'; import * as editorCommon from 'vs/editor/common/editorCommon'; -import {ServicesAccessor, EditorAction, CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions'; +import {ServicesAccessor, EditorAction, EditorCommand, CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions'; import {FIND_IDS, FindModelBoundToEditorModel} from 'vs/editor/contrib/find/common/findModel'; import {FindReplaceState, FindReplaceStateChangedEvent, INewFindReplaceState} from 'vs/editor/contrib/find/common/findState'; import {DocumentHighlightProviderRegistry} from 'vs/editor/common/modes'; import {RunOnceScheduler} from 'vs/base/common/async'; -const EditorKbExpr = editorCommon.EditorKbExpr; +import EditorKbExpr = editorCommon.EditorKbExpr; export enum FindStartFocusAction { NoFocusChange, @@ -33,7 +33,8 @@ export interface IFindStartOptions { shouldAnimate:boolean; } -export const CONTEXT_FIND_WIDGET_VISIBLE = 'findWidgetVisible'; +export const CONTEXT_FIND_WIDGET_VISIBLE = new KbCtxKey('findWidgetVisible'); +export const CONTEXT_FIND_WIDGET_NOT_VISIBLE = CONTEXT_FIND_WIDGET_VISIBLE.negate(); export class CommonFindController extends Disposable implements editorCommon.IEditorContribution { @@ -51,7 +52,7 @@ export class CommonFindController extends Disposable implements editorCommon.IEd constructor(editor:editorCommon.ICommonCodeEditor, @IKeybindingService keybindingService: IKeybindingService) { super(); this._editor = editor; - this._findWidgetVisible = keybindingService.createKey(CONTEXT_FIND_WIDGET_VISIBLE, false); + this._findWidgetVisible = CONTEXT_FIND_WIDGET_VISIBLE.bindTo(keybindingService, false); this._state = this._register(new FindReplaceState()); this._register(this._state.addChangeListener((e) => this._onStateChanged(e))); @@ -290,7 +291,7 @@ export class NextMatchFindAction extends MatchFindAction { ); this.kbOpts = { - kbExpr: editorCommon.EditorKbExpr.Focus, + kbExpr: EditorKbExpr.Focus, primary: KeyCode.F3, mac: { primary: KeyMod.CtrlCmd | KeyCode.KEY_G, secondary: [KeyCode.F3] } }; @@ -311,7 +312,7 @@ export class PreviousMatchFindAction extends MatchFindAction { ); this.kbOpts = { - kbExpr: editorCommon.EditorKbExpr.Focus, + kbExpr: EditorKbExpr.Focus, primary: KeyMod.Shift | KeyCode.F3, mac: { primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_G, secondary: [KeyMod.Shift | KeyCode.F3] } }; @@ -358,7 +359,7 @@ export class NextSelectionMatchFindAction extends SelectionMatchFindAction { ); this.kbOpts = { - kbExpr: editorCommon.EditorKbExpr.Focus, + kbExpr: EditorKbExpr.Focus, primary: KeyMod.CtrlCmd | KeyCode.F3 }; } @@ -378,7 +379,7 @@ export class PreviousSelectionMatchFindAction extends SelectionMatchFindAction { ); this.kbOpts = { - kbExpr: editorCommon.EditorKbExpr.Focus, + kbExpr: EditorKbExpr.Focus, primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.F3 }; } @@ -540,7 +541,7 @@ export class AddSelectionToNextFindMatchAction extends SelectNextFindMatchAction ); this.kbOpts = { - kbExpr: editorCommon.EditorKbExpr.Focus, + kbExpr: EditorKbExpr.Focus, primary: KeyMod.CtrlCmd | KeyCode.KEY_D }; } @@ -591,7 +592,7 @@ export class MoveSelectionToNextFindMatchAction extends SelectNextFindMatchActio ); this.kbOpts = { - kbExpr: editorCommon.EditorKbExpr.Focus, + kbExpr: EditorKbExpr.Focus, primary: KeyMod.chord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyMod.CtrlCmd | KeyCode.KEY_D) }; } @@ -674,7 +675,7 @@ export class SelectHighlightsAction extends AbstractSelectHighlightsAction { ); this.kbOpts = { - kbExpr: editorCommon.EditorKbExpr.Focus, + kbExpr: EditorKbExpr.Focus, primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_L }; } @@ -689,7 +690,7 @@ export class CompatChangeAll extends AbstractSelectHighlightsAction { ); this.kbOpts = { - kbExpr: editorCommon.EditorKbExpr.TextFocus, + kbExpr: EditorKbExpr.TextFocus, primary: KeyMod.CtrlCmd | KeyCode.F2 }; @@ -875,34 +876,74 @@ CommonEditorRegistry.registerEditorAction(new MoveSelectionToPreviousFindMatchAc CommonEditorRegistry.registerEditorAction(new AddSelectionToNextFindMatchAction()); CommonEditorRegistry.registerEditorAction(new AddSelectionToPreviousFindMatchAction()); -function registerFindCommand(id:string, callback:(controller:CommonFindController)=>void, keybindings:IKeybindings, needsKey:string = null): void { - CommonEditorRegistry.registerEditorCommand(id, CommonEditorRegistry.commandWeight(5), keybindings, false, needsKey, (ctx, editor, args) => { - callback(CommonFindController.getFindController(editor)); - }); -} - -registerFindCommand(FIND_IDS.CloseFindWidgetCommand, x => x.closeFindWidget(), { - primary: KeyCode.Escape, - secondary: [KeyMod.Shift | KeyCode.Escape] -}, CONTEXT_FIND_WIDGET_VISIBLE); -registerFindCommand(FIND_IDS.ToggleCaseSensitiveCommand, x => x.toggleCaseSensitive(), { - primary: KeyMod.Alt | KeyCode.KEY_C, - mac: { primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KEY_C } -}); -registerFindCommand(FIND_IDS.ToggleWholeWordCommand, x => x.toggleWholeWords(), { - primary: KeyMod.Alt | KeyCode.KEY_W, - mac: { primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KEY_W } -}); -registerFindCommand(FIND_IDS.ToggleRegexCommand, x => x.toggleRegex(), { - primary: KeyMod.Alt | KeyCode.KEY_R, - mac: { primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KEY_R } -}); -registerFindCommand(FIND_IDS.ReplaceOneAction, x => x.replace(), { - primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_1 -}, CONTEXT_FIND_WIDGET_VISIBLE); -registerFindCommand(FIND_IDS.ReplaceAllAction, x => x.replaceAll(), { - primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.Enter -}, CONTEXT_FIND_WIDGET_VISIBLE); -registerFindCommand(FIND_IDS.SelectAllMatchesAction, x => x.selectAllMatches(), { - primary: KeyMod.Alt | KeyCode.Enter -}, CONTEXT_FIND_WIDGET_VISIBLE); +const FindCommand = EditorCommand.bindToContribution( + CommonFindController.getFindController, + CommonEditorRegistry.commandWeight(5), + EditorKbExpr.Focus +); + +const VisibleWidgetFindCommand = EditorCommand.bindToContribution( + CommonFindController.getFindController, + CommonEditorRegistry.commandWeight(5), + KbExpr.and(EditorKbExpr.Focus, CONTEXT_FIND_WIDGET_VISIBLE) +); + +CommonEditorRegistry.registerEditorCommand2(new VisibleWidgetFindCommand( + FIND_IDS.CloseFindWidgetCommand, + x => x.closeFindWidget(), + { + primary: KeyCode.Escape, + secondary: [KeyMod.Shift | KeyCode.Escape] + } +)); + +CommonEditorRegistry.registerEditorCommand2(new FindCommand( + FIND_IDS.ToggleCaseSensitiveCommand, + x => x.toggleCaseSensitive(), + { + primary: KeyMod.Alt | KeyCode.KEY_C, + mac: { primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KEY_C } + } +)); + +CommonEditorRegistry.registerEditorCommand2(new FindCommand( + FIND_IDS.ToggleWholeWordCommand, + x => x.toggleWholeWords(), + { + primary: KeyMod.Alt | KeyCode.KEY_W, + mac: { primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KEY_W } + } +)); + +CommonEditorRegistry.registerEditorCommand2(new FindCommand( + FIND_IDS.ToggleRegexCommand, + x => x.toggleRegex(), + { + primary: KeyMod.Alt | KeyCode.KEY_R, + mac: { primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KEY_R } + } +)); + +CommonEditorRegistry.registerEditorCommand2(new VisibleWidgetFindCommand( + FIND_IDS.ReplaceOneAction, + x => x.replace(), + { + primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_1 + } +)); + +CommonEditorRegistry.registerEditorCommand2(new VisibleWidgetFindCommand( + FIND_IDS.ReplaceAllAction, + x => x.replaceAll(), + { + primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.Enter + } +)); + +CommonEditorRegistry.registerEditorCommand2(new VisibleWidgetFindCommand( + FIND_IDS.SelectAllMatchesAction, + x => x.selectAllMatches(), + { + primary: KeyMod.Alt | KeyCode.Enter + } +)); diff --git a/src/vs/editor/contrib/folding/browser/folding.ts b/src/vs/editor/contrib/folding/browser/folding.ts index e37be0ed7e1..cbf1220ca52 100644 --- a/src/vs/editor/contrib/folding/browser/folding.ts +++ b/src/vs/editor/contrib/folding/browser/folding.ts @@ -20,6 +20,8 @@ import {IFoldingRange} from 'vs/editor/contrib/folding/common/foldingRange'; import {computeRanges, limitByIndent} from 'vs/editor/contrib/folding/common/indentFoldStrategy'; import {Selection} from 'vs/editor/common/core/selection'; +import EditorKbExpr = editorCommon.EditorKbExpr; + class CollapsibleRegion { private decorationIds: string[]; @@ -652,7 +654,7 @@ abstract class FoldingAction2 extends EditorAction { super(id, label, alias, false); this.kbOpts = { - kbExpr: editorCommon.EditorKbExpr.Focus, + kbExpr: EditorKbExpr.Focus, primary: keybinding }; } diff --git a/src/vs/editor/contrib/format/common/formatActions.ts b/src/vs/editor/contrib/format/common/formatActions.ts index e16798fa58c..0c03f552463 100644 --- a/src/vs/editor/contrib/format/common/formatActions.ts +++ b/src/vs/editor/contrib/format/common/formatActions.ts @@ -17,8 +17,8 @@ import {getOnTypeFormattingEdits, getDocumentFormattingEdits, getDocumentRangeFo import {EditOperationsCommand} from './formatCommand'; import {Selection} from 'vs/editor/common/core/selection'; -const ModeContextKeys = editorCommon.ModeContextKeys; -const EditorKbExpr = editorCommon.EditorKbExpr; +import ModeContextKeys = editorCommon.ModeContextKeys; +import EditorKbExpr = editorCommon.EditorKbExpr; class FormatOnType implements editorCommon.IEditorContribution { diff --git a/src/vs/editor/contrib/goToDeclaration/browser/goToDeclaration.ts b/src/vs/editor/contrib/goToDeclaration/browser/goToDeclaration.ts index 80e93eea16a..67cff03b1c0 100644 --- a/src/vs/editor/contrib/goToDeclaration/browser/goToDeclaration.ts +++ b/src/vs/editor/contrib/goToDeclaration/browser/goToDeclaration.ts @@ -32,7 +32,8 @@ import {IDisposable, dispose} from 'vs/base/common/lifecycle'; import {IPeekViewService} from 'vs/editor/contrib/zoneWidget/browser/peekViewWidget'; import {optional} from 'vs/platform/instantiation/common/instantiation'; -const ModeContextKeys = editorCommon.ModeContextKeys; +import ModeContextKeys = editorCommon.ModeContextKeys; +import EditorKbExpr = editorCommon.EditorKbExpr; export class DefinitionActionConfig { @@ -172,7 +173,7 @@ export class GoToDefinitionAction extends DefinitionAction { ); this.kbOpts = { - kbExpr: editorCommon.EditorKbExpr.TextFocus, + kbExpr: EditorKbExpr.TextFocus, primary: goToDeclarationKb }; @@ -197,7 +198,7 @@ export class OpenDefinitionToSideAction extends DefinitionAction { ); this.kbOpts = { - kbExpr: editorCommon.EditorKbExpr.TextFocus, + kbExpr: EditorKbExpr.TextFocus, primary: KeyMod.chord(KeyMod.CtrlCmd | KeyCode.KEY_K, goToDeclarationKb) }; } @@ -214,7 +215,7 @@ export class PeekDefinitionAction extends DefinitionAction { ); this.kbOpts = { - kbExpr: editorCommon.EditorKbExpr.TextFocus, + kbExpr: EditorKbExpr.TextFocus, primary: KeyMod.Alt | KeyCode.F12, linux: { primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.F10 } }; diff --git a/src/vs/editor/contrib/gotoError/browser/gotoError.ts b/src/vs/editor/contrib/gotoError/browser/gotoError.ts index 819786a553f..37d62c16de2 100644 --- a/src/vs/editor/contrib/gotoError/browser/gotoError.ts +++ b/src/vs/editor/contrib/gotoError/browser/gotoError.ts @@ -17,18 +17,20 @@ import {TPromise} from 'vs/base/common/winjs.base'; import * as dom from 'vs/base/browser/dom'; import {renderHtml} from 'vs/base/browser/htmlContentRenderer'; import {ICommandService} from 'vs/platform/commands/common/commands'; -import {IKeybindingContextKey, IKeybindingService} from 'vs/platform/keybinding/common/keybinding'; +import {KbExpr, KbCtxKey, IKeybindingContextKey, IKeybindingService} from 'vs/platform/keybinding/common/keybinding'; import {IMarker, IMarkerService} from 'vs/platform/markers/common/markers'; import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry'; import {Position} from 'vs/editor/common/core/position'; import {Range} from 'vs/editor/common/core/range'; import * as editorCommon from 'vs/editor/common/editorCommon'; -import {ServicesAccessor, EditorAction, CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions'; +import {ServicesAccessor, EditorAction, EditorCommand, CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions'; import {ICodeEditor} from 'vs/editor/browser/editorBrowser'; import {EditorBrowserRegistry} from 'vs/editor/browser/editorBrowserExtensions'; import {ZoneWidget} from 'vs/editor/contrib/zoneWidget/browser/zoneWidget'; import {getCodeActions, IQuickFix2} from 'vs/editor/contrib/quickFix/common/quickFix'; +import EditorKbExpr = editorCommon.EditorKbExpr; + class MarkerModel { private _editor: ICodeEditor; @@ -452,7 +454,7 @@ class MarkerController implements editorCommon.IEditorContribution { @ICommandService private _commandService: ICommandService ) { this._editor = editor; - this._markersNavigationVisible = this._keybindingService.createKey(CONTEXT_MARKERS_NAVIGATION_VISIBLE, false); + this._markersNavigationVisible = CONTEXT_MARKERS_NAVIGATION_VISIBLE.bindTo(this._keybindingService, false); } public getId(): string { @@ -520,7 +522,7 @@ class NextMarkerAction extends MarkerNavigationAction { ); this.kbOpts = { - kbExpr: editorCommon.EditorKbExpr.Focus, + kbExpr: EditorKbExpr.Focus, primary: KeyCode.F8 }; } @@ -536,20 +538,31 @@ class PrevMarkerAction extends MarkerNavigationAction { ); this.kbOpts = { - kbExpr: editorCommon.EditorKbExpr.Focus, + kbExpr: EditorKbExpr.Focus, primary: KeyMod.Shift | KeyCode.F8 }; } } -var CONTEXT_MARKERS_NAVIGATION_VISIBLE = 'markersNavigationVisible'; +var CONTEXT_MARKERS_NAVIGATION_VISIBLE = new KbCtxKey('markersNavigationVisible'); + +const MarkerCommand = EditorCommand.bindToContribution( + MarkerController.getMarkerController, + CommonEditorRegistry.commandWeight(50), + KbExpr.and(EditorKbExpr.Focus, CONTEXT_MARKERS_NAVIGATION_VISIBLE) +); // register actions CommonEditorRegistry.registerEditorAction(new NextMarkerAction()); CommonEditorRegistry.registerEditorAction(new PrevMarkerAction()); -CommonEditorRegistry.registerEditorCommand('closeMarkersNavigation', CommonEditorRegistry.commandWeight(50), { primary: KeyCode.Escape, secondary: [KeyMod.Shift | KeyCode.Escape] }, false, CONTEXT_MARKERS_NAVIGATION_VISIBLE, (ctx, editor, args) => { - var controller = MarkerController.getMarkerController(editor); - controller.closeMarkersNavigation(); -}); + +CommonEditorRegistry.registerEditorCommand2(new MarkerCommand( + 'closeMarkersNavigation', + x => x.closeMarkersNavigation(), + { + primary: KeyCode.Escape, + secondary: [KeyMod.Shift | KeyCode.Escape] + } +)); EditorBrowserRegistry.registerEditorContribution(MarkerController); \ No newline at end of file diff --git a/src/vs/editor/contrib/hover/browser/hover.ts b/src/vs/editor/contrib/hover/browser/hover.ts index b727266913b..a4805c15a43 100644 --- a/src/vs/editor/contrib/hover/browser/hover.ts +++ b/src/vs/editor/contrib/hover/browser/hover.ts @@ -21,6 +21,8 @@ import {ModesContentHoverWidget} from './modesContentHover'; import {ModesGlyphHoverWidget} from './modesGlyphHover'; import {IDisposable, dispose} from 'vs/base/common/lifecycle'; +import EditorKbExpr = editorCommon.EditorKbExpr; + class ModesHoverController implements editorCommon.IEditorContribution { static ID = 'editor.contrib.hover'; @@ -152,7 +154,7 @@ class ShowHoverAction extends EditorAction { ); this.kbOpts = { - kbExpr: editorCommon.EditorKbExpr.TextFocus, + kbExpr: EditorKbExpr.TextFocus, primary: KeyMod.chord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyMod.CtrlCmd | KeyCode.KEY_I) }; } diff --git a/src/vs/editor/contrib/quickFix/browser/quickFix.ts b/src/vs/editor/contrib/quickFix/browser/quickFix.ts index b61a6cf87ad..079afd1e443 100644 --- a/src/vs/editor/contrib/quickFix/browser/quickFix.ts +++ b/src/vs/editor/contrib/quickFix/browser/quickFix.ts @@ -9,12 +9,12 @@ import {onUnexpectedError} from 'vs/base/common/errors'; import {KeyCode, KeyMod} from 'vs/base/common/keyCodes'; import {IEditorService} from 'vs/platform/editor/common/editor'; import {ICommandService} from 'vs/platform/commands/common/commands'; -import {IKeybindingContextKey, IKeybindingService} from 'vs/platform/keybinding/common/keybinding'; +import {KbExpr, KbCtxKey, IKeybindingContextKey, IKeybindingService} from 'vs/platform/keybinding/common/keybinding'; import {IMarkerService} from 'vs/platform/markers/common/markers'; import {IMessageService} from 'vs/platform/message/common/message'; import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry'; import {ICommonCodeEditor, EditorKbExpr, IEditorContribution, IRange} from 'vs/editor/common/editorCommon'; -import {ServicesAccessor, EditorAction, CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions'; +import {ServicesAccessor, EditorAction, EditorCommand, CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions'; import {ICodeEditor} from 'vs/editor/browser/editorBrowser'; import {CodeActionProviderRegistry} from 'vs/editor/common/modes'; import {EditorBrowserRegistry} from 'vs/editor/browser/editorBrowserExtensions'; @@ -46,7 +46,7 @@ export class QuickFixController implements IEditorContribution { this.editor = editor; this.model = new QuickFixModel(this.editor, this._markerService, this.onAccept.bind(this)); - this.quickFixWidgetVisible = this._keybindingService.createKey(CONTEXT_QUICK_FIX_WIDGET_VISIBLE, false); + this.quickFixWidgetVisible = CONTEXT_QUICK_FIX_WIDGET_VISIBLE.bindTo(this._keybindingService, false); this.suggestWidget = new QuickFixSelectionWidget(this.editor, telemetryService,() => { this.quickFixWidgetVisible.set(true); },() => { @@ -143,35 +143,61 @@ export class QuickFixAction extends EditorAction { } } -var CONTEXT_QUICK_FIX_WIDGET_VISIBLE = 'quickFixWidgetVisible'; +var CONTEXT_QUICK_FIX_WIDGET_VISIBLE = new KbCtxKey('quickFixWidgetVisible'); -var weight = CommonEditorRegistry.commandWeight(80); +const QuickFixCommand = EditorCommand.bindToContribution( + QuickFixController.getQuickFixController, + CommonEditorRegistry.commandWeight(80), + KbExpr.and(EditorKbExpr.Focus, CONTEXT_QUICK_FIX_WIDGET_VISIBLE) +); // register action CommonEditorRegistry.registerEditorAction(new QuickFixAction()); -CommonEditorRegistry.registerEditorCommand('acceptQuickFixSuggestion', weight, { primary: KeyCode.Enter, secondary: [KeyCode.Tab] }, false, CONTEXT_QUICK_FIX_WIDGET_VISIBLE,(ctx, editor, args) => { - var controller = QuickFixController.getQuickFixController(editor); - controller.acceptSelectedSuggestion(); -}); -CommonEditorRegistry.registerEditorCommand('closeQuickFixWidget', weight, { primary: KeyCode.Escape, secondary: [KeyMod.Shift | KeyCode.Escape] }, false, CONTEXT_QUICK_FIX_WIDGET_VISIBLE,(ctx, editor, args) => { - var controller = QuickFixController.getQuickFixController(editor); - controller.closeWidget(); -}); -CommonEditorRegistry.registerEditorCommand('selectNextQuickFix', weight, { primary: KeyCode.DownArrow , mac: { primary: KeyCode.DownArrow, secondary: [KeyMod.WinCtrl | KeyCode.KEY_N] } }, false, CONTEXT_QUICK_FIX_WIDGET_VISIBLE,(ctx, editor, args) => { - var controller = QuickFixController.getQuickFixController(editor); - controller.selectNextSuggestion(); -}); -CommonEditorRegistry.registerEditorCommand('selectNextPageQuickFix', weight, { primary: KeyCode.PageDown }, false, CONTEXT_QUICK_FIX_WIDGET_VISIBLE,(ctx, editor, args) => { - var controller = QuickFixController.getQuickFixController(editor); - controller.selectNextPageSuggestion(); -}); -CommonEditorRegistry.registerEditorCommand('selectPrevQuickFix', weight, { primary: KeyCode.UpArrow , mac: { primary: KeyCode.UpArrow, secondary: [KeyMod.WinCtrl | KeyCode.KEY_P] }}, false, CONTEXT_QUICK_FIX_WIDGET_VISIBLE,(ctx, editor, args) => { - var controller = QuickFixController.getQuickFixController(editor); - controller.selectPrevSuggestion(); -}); -CommonEditorRegistry.registerEditorCommand('selectPrevPageQuickFix', weight, { primary: KeyCode.PageUp }, false, CONTEXT_QUICK_FIX_WIDGET_VISIBLE,(ctx, editor, args) => { - var controller = QuickFixController.getQuickFixController(editor); - controller.selectPrevPageSuggestion(); -}); +CommonEditorRegistry.registerEditorCommand2(new QuickFixCommand( + 'acceptQuickFixSuggestion', + x => x.acceptSelectedSuggestion(), + { + primary: KeyCode.Enter, + secondary: [KeyCode.Tab] + } +)); +CommonEditorRegistry.registerEditorCommand2(new QuickFixCommand( + 'closeQuickFixWidget', + x => x.closeWidget(), + { + primary: KeyCode.Escape, + secondary: [KeyMod.Shift | KeyCode.Escape] + } +)); +CommonEditorRegistry.registerEditorCommand2(new QuickFixCommand( + 'selectNextQuickFix', + x => x.selectNextSuggestion(), + { + primary: KeyCode.DownArrow, + mac: { primary: KeyCode.DownArrow, secondary: [KeyMod.WinCtrl | KeyCode.KEY_N] } + } +)); +CommonEditorRegistry.registerEditorCommand2(new QuickFixCommand( + 'selectNextPageQuickFix', + x => x.selectNextPageSuggestion(), + { + primary: KeyCode.PageDown + } +)); +CommonEditorRegistry.registerEditorCommand2(new QuickFixCommand( + 'selectPrevQuickFix', + x => x.selectPrevSuggestion(), + { + primary: KeyCode.UpArrow, + mac: { primary: KeyCode.UpArrow, secondary: [KeyMod.WinCtrl | KeyCode.KEY_P] } + } +)); +CommonEditorRegistry.registerEditorCommand2(new QuickFixCommand( + 'selectPrevPageQuickFix', + x => x.selectPrevPageSuggestion(), + { + primary: KeyCode.PageUp + } +)); EditorBrowserRegistry.registerEditorContribution(QuickFixController); \ No newline at end of file diff --git a/src/vs/editor/contrib/quickOpen/browser/gotoLine.ts b/src/vs/editor/contrib/quickOpen/browser/gotoLine.ts index a11ddaa7128..456816fa097 100644 --- a/src/vs/editor/contrib/quickOpen/browser/gotoLine.ts +++ b/src/vs/editor/contrib/quickOpen/browser/gotoLine.ts @@ -15,6 +15,8 @@ import {BaseEditorQuickOpenAction, IDecorator} from './editorQuickOpen'; import {ServicesAccessor} from 'vs/editor/common/editorCommonExtensions'; import {KeyCode, KeyMod} from 'vs/base/common/keyCodes'; +import EditorKbExpr = editorCommon.EditorKbExpr; + interface ParseResult { position: editorCommon.IPosition; isValid: boolean; @@ -158,7 +160,7 @@ export class GotoLineAction extends BaseEditorQuickOpenAction { ); this.kbOpts = { - kbExpr: editorCommon.EditorKbExpr.Focus, + kbExpr: EditorKbExpr.Focus, primary: KeyMod.CtrlCmd | KeyCode.KEY_G, mac: { primary: KeyMod.WinCtrl | KeyCode.KEY_G } }; diff --git a/src/vs/editor/contrib/referenceSearch/browser/referenceSearch.ts b/src/vs/editor/contrib/referenceSearch/browser/referenceSearch.ts index be06676eae3..62d3b6b7c0c 100644 --- a/src/vs/editor/contrib/referenceSearch/browser/referenceSearch.ts +++ b/src/vs/editor/contrib/referenceSearch/browser/referenceSearch.ts @@ -24,8 +24,8 @@ import {ReferenceWidget} from './referencesWidget'; import {ReferencesController, RequestOptions, ctxReferenceSearchVisible} from './referencesController'; import {ReferencesModel} from './referencesModel'; -const ModeContextKeys = editorCommon.ModeContextKeys; -const EditorKbExpr = editorCommon.EditorKbExpr; +import ModeContextKeys = editorCommon.ModeContextKeys; +import EditorKbExpr = editorCommon.EditorKbExpr; const defaultReferenceSearchOptions: RequestOptions = { getMetaTitle(model) { diff --git a/src/vs/editor/contrib/rename/browser/rename.ts b/src/vs/editor/contrib/rename/browser/rename.ts index 096af508192..2e8ddd0a035 100644 --- a/src/vs/editor/contrib/rename/browser/rename.ts +++ b/src/vs/editor/contrib/rename/browser/rename.ts @@ -12,10 +12,10 @@ import Severity from 'vs/base/common/severity'; import {TPromise} from 'vs/base/common/winjs.base'; import {IEditorService} from 'vs/platform/editor/common/editor'; import {IEventService} from 'vs/platform/event/common/event'; -import {IKeybindingContextKey, IKeybindingService, KbExpr} from 'vs/platform/keybinding/common/keybinding'; +import {KbCtxKey, IKeybindingContextKey, IKeybindingService, KbExpr} from 'vs/platform/keybinding/common/keybinding'; import {IMessageService} from 'vs/platform/message/common/message'; import {IProgressService} from 'vs/platform/progress/common/progress'; -import {ServicesAccessor, EditorAction, CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions'; +import {ServicesAccessor, EditorAction, EditorCommand, CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions'; import {EditorBrowserRegistry} from 'vs/editor/browser/editorBrowserExtensions'; import {IRange, ICommonCodeEditor, EditorKbExpr, ModeContextKeys, IEditorContribution} from 'vs/editor/common/editorCommon'; import {BulkEdit, createBulkEdit} from 'vs/editor/common/services/bulkEdit'; @@ -26,7 +26,7 @@ import RenameInputField from './renameInputField'; // --- register actions and commands -const CONTEXT_RENAME_INPUT_VISIBLE = 'renameInputVisible'; +const CONTEXT_RENAME_INPUT_VISIBLE = new KbCtxKey('renameInputVisible'); class RenameController implements IEditorContribution { @@ -48,7 +48,7 @@ class RenameController implements IEditorContribution { @IKeybindingService keybindingService: IKeybindingService ) { this._renameInputField = new RenameInputField(editor); - this._renameInputVisible = keybindingService.createKey(CONTEXT_RENAME_INPUT_VISIBLE, false); + this._renameInputVisible = CONTEXT_RENAME_INPUT_VISIBLE.bindTo(keybindingService, false); } public dispose(): void { @@ -189,14 +189,27 @@ export class RenameAction extends EditorAction { EditorBrowserRegistry.registerEditorContribution(RenameController); -const weight = CommonEditorRegistry.commandWeight(99); +const RenameCommand = EditorCommand.bindToContribution( + RenameController.get, + CommonEditorRegistry.commandWeight(99), + KbExpr.and(EditorKbExpr.Focus, CONTEXT_RENAME_INPUT_VISIBLE) +); CommonEditorRegistry.registerEditorAction(new RenameAction()); -CommonEditorRegistry.registerEditorCommand('acceptRenameInput', weight, { primary: KeyCode.Enter }, false, CONTEXT_RENAME_INPUT_VISIBLE, (ctx, editor, args) => { - RenameController.get(editor).acceptRenameInput(); -}); - -CommonEditorRegistry.registerEditorCommand('cancelRenameInput', weight, { primary: KeyCode.Escape, secondary: [KeyMod.Shift | KeyCode.Escape] }, false, CONTEXT_RENAME_INPUT_VISIBLE, (ctx, editor, args) => { - RenameController.get(editor).cancelRenameInput(); -}); +CommonEditorRegistry.registerEditorCommand2(new RenameCommand( + 'acceptRenameInput', + x => x.acceptRenameInput(), + { + primary: KeyCode.Enter + } +)); + +CommonEditorRegistry.registerEditorCommand2(new RenameCommand( + 'cancelRenameInput', + x => x.cancelRenameInput(), + { + primary: KeyCode.Escape, + secondary: [KeyMod.Shift | KeyCode.Escape] + } +)); diff --git a/src/vs/editor/contrib/snippet/common/snippet.ts b/src/vs/editor/contrib/snippet/common/snippet.ts index 8da83b01034..f27ed965121 100644 --- a/src/vs/editor/contrib/snippet/common/snippet.ts +++ b/src/vs/editor/contrib/snippet/common/snippet.ts @@ -8,14 +8,16 @@ import * as collections from 'vs/base/common/collections'; import {KeyCode, KeyMod} from 'vs/base/common/keyCodes'; import * as strings from 'vs/base/common/strings'; -import {IKeybindingContextKey, IKeybindingService} from 'vs/platform/keybinding/common/keybinding'; +import {KbExpr, KbCtxKey, IKeybindingContextKey, IKeybindingService} from 'vs/platform/keybinding/common/keybinding'; import {EditOperation} from 'vs/editor/common/core/editOperation'; import {Range} from 'vs/editor/common/core/range'; import {Selection} from 'vs/editor/common/core/selection'; import * as editorCommon from 'vs/editor/common/editorCommon'; -import {CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions'; +import {CommonEditorRegistry, EditorCommand} from 'vs/editor/common/editorCommonExtensions'; import {IDisposable, dispose} from 'vs/base/common/lifecycle'; +import EditorKbExpr = editorCommon.EditorKbExpr; + interface IParsedLinePlaceHolderInfo { id: string; value: string; @@ -742,7 +744,7 @@ class SnippetController implements ISnippetController { constructor(editor: editorCommon.ICommonCodeEditor, @IKeybindingService keybindingService: IKeybindingService) { this._editor = editor; this._currentController = null; - this._inSnippetMode = keybindingService.createKey(CONTEXT_SNIPPET_MODE, false); + this._inSnippetMode = CONTEXT_SNIPPET_MODE.bindTo(keybindingService, false); } public dispose(): void { @@ -933,20 +935,41 @@ class SnippetController implements ISnippetController { } } -export var CONTEXT_SNIPPET_MODE = 'inSnippetMode'; +export var CONTEXT_SNIPPET_MODE = new KbCtxKey('inSnippetMode'); -var weight = CommonEditorRegistry.commandWeight(30); +const SnippetCommand = EditorCommand.bindToContribution( + getSnippetController, + CommonEditorRegistry.commandWeight(30), + KbExpr.and(EditorKbExpr.TextFocus, CONTEXT_SNIPPET_MODE) +); CommonEditorRegistry.registerEditorContribution(SnippetController); -CommonEditorRegistry.registerEditorCommand('jumpToNextSnippetPlaceholder', weight, { primary: KeyCode.Tab }, true, CONTEXT_SNIPPET_MODE,(ctx, editor, args) => { - getSnippetController(editor).jumpToNextPlaceholder(); -}); -CommonEditorRegistry.registerEditorCommand('jumpToPrevSnippetPlaceholder', weight, { primary: KeyMod.Shift | KeyCode.Tab }, true, CONTEXT_SNIPPET_MODE,(ctx, editor, args) => { - getSnippetController(editor).jumpToPrevPlaceholder(); -}); -CommonEditorRegistry.registerEditorCommand('acceptSnippet', weight, { primary: KeyCode.Enter }, true, CONTEXT_SNIPPET_MODE,(ctx, editor, args) => { - getSnippetController(editor).acceptSnippet(); -}); -CommonEditorRegistry.registerEditorCommand('leaveSnippet', weight, { primary: KeyCode.Escape, secondary: [KeyMod.Shift | KeyCode.Escape] }, true, CONTEXT_SNIPPET_MODE,(ctx, editor, args) => { - getSnippetController(editor).leaveSnippet(); -}); +CommonEditorRegistry.registerEditorCommand2(new SnippetCommand( + 'jumpToNextSnippetPlaceholder', + x => x.jumpToNextPlaceholder(), + { + primary: KeyCode.Tab + } +)); +CommonEditorRegistry.registerEditorCommand2(new SnippetCommand( + 'jumpToPrevSnippetPlaceholder', + x => x.jumpToPrevPlaceholder(), + { + primary: KeyMod.Shift | KeyCode.Tab + } +)); +CommonEditorRegistry.registerEditorCommand2(new SnippetCommand( + 'acceptSnippet', + x => x.acceptSnippet(), + { + primary: KeyCode.Enter + } +)); +CommonEditorRegistry.registerEditorCommand2(new SnippetCommand( + 'leaveSnippet', + x => x.leaveSnippet(), + { + primary: KeyCode.Escape, + secondary: [KeyMod.Shift | KeyCode.Escape] + } +)); diff --git a/src/vs/editor/contrib/suggest/browser/tabCompletion.ts b/src/vs/editor/contrib/suggest/browser/tabCompletion.ts index de2f8dfa503..6cec3f85aab 100644 --- a/src/vs/editor/contrib/suggest/browser/tabCompletion.ts +++ b/src/vs/editor/contrib/suggest/browser/tabCompletion.ts @@ -6,24 +6,23 @@ 'use strict'; import {KeyCode} from 'vs/base/common/keyCodes'; -import {ICodeEditorService} from 'vs/editor/common/services/codeEditorService'; -import {IKeybindingService, KbExpr} from 'vs/platform/keybinding/common/keybinding'; +import {KbCtxKey, IKeybindingService, KbExpr} from 'vs/platform/keybinding/common/keybinding'; import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry'; import {ISnippetsRegistry, Extensions, getNonWhitespacePrefix, ISnippet} from 'vs/editor/common/modes/snippetsRegistry'; import {Registry} from 'vs/platform/platform'; import {IDisposable} from 'vs/base/common/lifecycle'; import * as editorCommon from 'vs/editor/common/editorCommon'; -import {CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions'; +import {CommonEditorRegistry, EditorCommand} from 'vs/editor/common/editorCommonExtensions'; import {CodeSnippet, ISnippetController, getSnippetController} from 'vs/editor/contrib/snippet/common/snippet'; -const EditorKbExpr = editorCommon.EditorKbExpr; +import EditorKbExpr = editorCommon.EditorKbExpr; let snippetsRegistry = Registry.as(Extensions.Snippets); class TabCompletionController implements editorCommon.IEditorContribution { static Id = 'editor.tabCompletionController'; - static ContextKey = 'hasSnippetCompletions'; + static ContextKey = new KbCtxKey('hasSnippetCompletions'); private _snippetController: ISnippetController; private _cursorChangeSubscription: IDisposable; @@ -34,7 +33,7 @@ class TabCompletionController implements editorCommon.IEditorContribution { @IKeybindingService keybindingService: IKeybindingService ) { this._snippetController = getSnippetController(editor); - const hasSnippets = keybindingService.createKey(TabCompletionController.ContextKey, undefined); + const hasSnippets = TabCompletionController.ContextKey.bindTo(keybindingService, undefined); this._cursorChangeSubscription = editor.onDidChangeCursorSelection(e => { this._currentSnippets.length = 0; @@ -76,18 +75,21 @@ class TabCompletionController implements editorCommon.IEditorContribution { CommonEditorRegistry.registerEditorContribution(TabCompletionController); -KeybindingsRegistry.registerCommandDesc({ - id: 'insertSnippet', - weight: KeybindingsRegistry.WEIGHT.editorContrib(), - primary: KeyCode.Tab, - when: KbExpr.and(KbExpr.has(TabCompletionController.ContextKey), +const TabCompletionCommand = EditorCommand.bindToContribution( + (editor) => editor.getContribution(TabCompletionController.Id), + KeybindingsRegistry.WEIGHT.editorContrib(), + KbExpr.and( + TabCompletionController.ContextKey, EditorKbExpr.TextFocus, EditorKbExpr.TabDoesNotMoveFocus, - KbExpr.has('config.editor.tabCompletion')), - handler(accessor) { - const editor = accessor.get(ICodeEditorService).getFocusedCodeEditor(); - if (editor) { - (editor.getContribution(TabCompletionController.Id)).performSnippetCompletions(); - } + KbExpr.has('config.editor.tabCompletion') + ) +); + +CommonEditorRegistry.registerEditorCommand2(new TabCompletionCommand( + 'insertSnippet', + x => x.performSnippetCompletions(), + { + primary: KeyCode.Tab } -}); +)); diff --git a/src/vs/monaco.d.ts b/src/vs/monaco.d.ts index 89537baff71..b9c7049dac3 100644 --- a/src/vs/monaco.d.ts +++ b/src/vs/monaco.d.ts @@ -900,6 +900,7 @@ declare module monaco.editor { export interface IKeybindingContextKey { set(value: T): void; reset(): void; + get(): T; } export interface IEditorOverrideServices { diff --git a/src/vs/platform/actions/common/menuService.ts b/src/vs/platform/actions/common/menuService.ts index bb2bfa14ad9..9b3ec668962 100644 --- a/src/vs/platform/actions/common/menuService.ts +++ b/src/vs/platform/actions/common/menuService.ts @@ -101,7 +101,7 @@ class Menu implements IMenu { const activeActions: MenuItemAction[] = []; for (let action of actions) { if (this._keybindingService.contextMatchesRules(action.item.when)) { - action.resource = this._keybindingService.getContextValue(ResourceContextKey.Resource); + action.resource = ResourceContextKey.Resource.getValue(this._keybindingService); activeActions.push(action); } } diff --git a/src/vs/platform/actions/common/resourceContextKey.ts b/src/vs/platform/actions/common/resourceContextKey.ts index 0c596851adf..dfb5f62830d 100644 --- a/src/vs/platform/actions/common/resourceContextKey.ts +++ b/src/vs/platform/actions/common/resourceContextKey.ts @@ -5,14 +5,15 @@ 'use strict'; import URI from 'vs/base/common/uri'; -import {IKeybindingService, IKeybindingContextKey} from 'vs/platform/keybinding/common/keybinding'; +import {KbCtxKey, IKeybindingService, IKeybindingContextKey} from 'vs/platform/keybinding/common/keybinding'; import {IModeService} from 'vs/editor/common/services/modeService'; export class ResourceContextKey implements IKeybindingContextKey { - static Scheme = 'resourceScheme'; - static LangId = 'resourceLangId'; - static Resource = 'resource'; + + static Scheme = new KbCtxKey('resourceScheme'); + static LangId = new KbCtxKey('resourceLangId'); + static Resource = new KbCtxKey('resource'); private _resourceKey: IKeybindingContextKey; private _schemeKey: IKeybindingContextKey; @@ -22,9 +23,9 @@ export class ResourceContextKey implements IKeybindingContextKey { @IKeybindingService keybindingService: IKeybindingService, @IModeService private _modeService: IModeService ) { - this._schemeKey = keybindingService.createKey(ResourceContextKey.Scheme, undefined); - this._langIdKey = keybindingService.createKey(ResourceContextKey.LangId, undefined); - this._resourceKey = keybindingService.createKey(ResourceContextKey.Resource, undefined); + this._schemeKey = ResourceContextKey.Scheme.bindTo(keybindingService, undefined); + this._langIdKey = ResourceContextKey.LangId.bindTo(keybindingService, undefined); + this._resourceKey = ResourceContextKey.Resource.bindTo(keybindingService, undefined); } set(value: URI) { @@ -38,4 +39,8 @@ export class ResourceContextKey implements IKeybindingContextKey { this._langIdKey.reset(); this._resourceKey.reset(); } + + public get(): URI { + return this._resourceKey.get(); + } } \ No newline at end of file diff --git a/src/vs/platform/keybinding/browser/keybindingServiceImpl.ts b/src/vs/platform/keybinding/browser/keybindingServiceImpl.ts index 8fef5f1334b..7aeb5a0febb 100644 --- a/src/vs/platform/keybinding/browser/keybindingServiceImpl.ts +++ b/src/vs/platform/keybinding/browser/keybindingServiceImpl.ts @@ -140,6 +140,10 @@ class KeybindingContextKey implements IKeybindingContextKey { } } + public get(): T { + return this._parent.getContextValue(this._key); + } + } export abstract class AbstractKeybindingService { diff --git a/src/vs/platform/keybinding/common/keybinding.ts b/src/vs/platform/keybinding/common/keybinding.ts index 54ea8f1daeb..397f7457706 100644 --- a/src/vs/platform/keybinding/common/keybinding.ts +++ b/src/vs/platform/keybinding/common/keybinding.ts @@ -378,6 +378,14 @@ export class KbCtxKey extends KbDefinedExpression { public bindTo(target:IKeybindingService, defaultValue:T): IKeybindingContextKey { return target.createKey(this.key, defaultValue); } + + public getValue(target:IKeybindingService): T { + return target.getContextValue(this.key); + } + + public negate(): KbExpr { + return KbExpr.not(this.key); + } } export let KbExpr = { @@ -447,6 +455,7 @@ export interface IKeybindingItem { export interface IKeybindingContextKey { set(value: T): void; reset(): void; + get(): T; } export let IKeybindingService = createDecorator('keybindingService'); diff --git a/src/vs/platform/keybinding/test/common/mockKeybindingService.ts b/src/vs/platform/keybinding/test/common/mockKeybindingService.ts index 9f5ca42975b..201137fe824 100644 --- a/src/vs/platform/keybinding/test/common/mockKeybindingService.ts +++ b/src/vs/platform/keybinding/test/common/mockKeybindingService.ts @@ -27,6 +27,10 @@ class MockKeybindingContextKey implements IKeybindingContextKey { public reset(): void { this._value = this._defaultValue; } + + public get(): T { + return this._value; + } } export class MockKeybindingService implements IKeybindingService { diff --git a/src/vs/workbench/browser/parts/editor/editorCommands.ts b/src/vs/workbench/browser/parts/editor/editorCommands.ts index 4c176f10e5b..a46020e4916 100644 --- a/src/vs/workbench/browser/parts/editor/editorCommands.ts +++ b/src/vs/workbench/browser/parts/editor/editorCommands.ts @@ -11,10 +11,8 @@ import { IConfigurationService } from 'vs/platform/configuration/common/configur import { IEditorGroupService } from 'vs/workbench/services/group/common/groupService'; import { IWorkbenchEditorConfiguration, ActiveEditorMoveArguments, ActiveEditorMovePositioning, ActiveEditorMovePositioningBy, EditorCommands } from 'vs/workbench/common/editor'; import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService'; -import * as editorCommon from 'vs/editor/common/editorCommon'; import { IEditor, Position, POSITIONS } from 'vs/platform/editor/common/editor'; - -const EditorKbExpr = editorCommon.EditorKbExpr; +import { EditorKbExpr } from 'vs/editor/common/editorCommon'; export function registerEditorComamnds() { _registerActiveEditorMoveCommand(); diff --git a/src/vs/workbench/parts/debug/browser/breakpointWidget.ts b/src/vs/workbench/parts/debug/browser/breakpointWidget.ts index 92c0e60ded1..0eb87be7d94 100644 --- a/src/vs/workbench/parts/debug/browser/breakpointWidget.ts +++ b/src/vs/workbench/parts/debug/browser/breakpointWidget.ts @@ -12,17 +12,18 @@ import platform = require('vs/base/common/platform'); import lifecycle = require('vs/base/common/lifecycle'); import dom = require('vs/base/browser/dom'); import {InputBox} from 'vs/base/browser/ui/inputbox/inputBox'; -import {CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions'; +import {CommonEditorRegistry, ServicesAccessor, EditorCommand} from 'vs/editor/common/editorCommonExtensions'; +import {EditorKbExpr, ICommonCodeEditor} from 'vs/editor/common/editorCommon'; import editorbrowser = require('vs/editor/browser/editorBrowser'); import {ZoneWidget} from 'vs/editor/contrib/zoneWidget/browser/zoneWidget'; import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation'; import {IContextViewService} from 'vs/platform/contextview/browser/contextView'; -import {IKeybindingService, IKeybindingContextKey} from 'vs/platform/keybinding/common/keybinding'; +import {KbCtxKey, KbExpr, IKeybindingService, IKeybindingContextKey} from 'vs/platform/keybinding/common/keybinding'; import debug = require('vs/workbench/parts/debug/common/debug'); import {IKeyboardEvent} from 'vs/base/browser/keyboardEvent'; const $ = dom.emmet; -const CONTEXT_BREAKPOINT_WIDGET_VISIBLE = 'breakpointWidgetVisible'; +const CONTEXT_BREAKPOINT_WIDGET_VISIBLE = new KbCtxKey('breakpointWidgetVisible'); const CLOSE_BREAKPOINT_WIDGET_COMMAND_ID = 'closeBreakpointWidget'; export class BreakpointWidget extends ZoneWidget { @@ -42,7 +43,7 @@ export class BreakpointWidget extends ZoneWidget { this.toDispose = []; this.create(); - this.breakpointWidgetVisible = keybindingService.createKey(CONTEXT_BREAKPOINT_WIDGET_VISIBLE, false); + this.breakpointWidgetVisible = CONTEXT_BREAKPOINT_WIDGET_VISIBLE.bindTo(keybindingService, false); this.breakpointWidgetVisible.set(true); BreakpointWidget.INSTANCE = this; this.toDispose.push(editor.onDidChangeModel(() => this.dispose())); @@ -119,8 +120,23 @@ export class BreakpointWidget extends ZoneWidget { } } -CommonEditorRegistry.registerEditorCommand(CLOSE_BREAKPOINT_WIDGET_COMMAND_ID, CommonEditorRegistry.commandWeight(8), { primary: KeyCode.Escape, secondary: [KeyMod.Shift | KeyCode.Escape] }, false, CONTEXT_BREAKPOINT_WIDGET_VISIBLE, (ctx, editor, args) => { - if (BreakpointWidget.INSTANCE) { - BreakpointWidget.INSTANCE.dispose(); +class CloseBreakpointWidgetCommand extends EditorCommand { + + constructor() { + super(CLOSE_BREAKPOINT_WIDGET_COMMAND_ID); + + this.kbOpts = { + weight: CommonEditorRegistry.commandWeight(8), + kbExpr: KbExpr.and(EditorKbExpr.Focus, CONTEXT_BREAKPOINT_WIDGET_VISIBLE), + primary: KeyCode.Escape, + secondary: [KeyMod.Shift | KeyCode.Escape] + }; + } + + protected runEditorCommand(accessor:ServicesAccessor, editor: ICommonCodeEditor, args: any): void { + if (BreakpointWidget.INSTANCE) { + BreakpointWidget.INSTANCE.dispose(); + } } -}); +} +CommonEditorRegistry.registerEditorCommand2(new CloseBreakpointWidgetCommand()); diff --git a/src/vs/workbench/parts/debug/browser/debugActions.ts b/src/vs/workbench/parts/debug/browser/debugActions.ts index 8ba12523747..d23978b99c4 100644 --- a/src/vs/workbench/parts/debug/browser/debugActions.ts +++ b/src/vs/workbench/parts/debug/browser/debugActions.ts @@ -22,7 +22,7 @@ import {ServicesAccessor, EditorAction} from 'vs/editor/common/editorCommonExten import {KeyMod, KeyCode} from 'vs/base/common/keyCodes'; import IDebugService = debug.IDebugService; -const EditorKbExpr = editorCommon.EditorKbExpr; +import EditorKbExpr = editorCommon.EditorKbExpr; export class AbstractDebugAction extends actions.Action { @@ -528,7 +528,7 @@ export class ToggleBreakpointAction extends EditorAction { ); this.kbOpts = { - kbExpr: editorCommon.EditorKbExpr.TextFocus, + kbExpr: EditorKbExpr.TextFocus, primary: KeyCode.F9 }; } @@ -603,7 +603,7 @@ export class RunToCursorAction extends EditorAction { ); this.menuOpts = { - kbExpr: KbExpr.has(debug.CONTEXT_IN_DEBUG_MODE), + kbExpr: debug.CONTEXT_IN_DEBUG_MODE, group: 'debug' }; } @@ -673,7 +673,7 @@ export class SelectionToReplAction extends EditorAction { ); this.menuOpts = { - kbExpr: KbExpr.and(EditorKbExpr.HasNonEmptySelection, KbExpr.has(debug.CONTEXT_IN_DEBUG_MODE)), + kbExpr: KbExpr.and(EditorKbExpr.HasNonEmptySelection, debug.CONTEXT_IN_DEBUG_MODE), group: 'debug' }; } @@ -711,7 +711,7 @@ export class ShowDebugHoverAction extends EditorAction { ); this.kbOpts = { - kbExpr: KbExpr.and(KbExpr.has(debug.CONTEXT_IN_DEBUG_MODE), EditorKbExpr.TextFocus), + kbExpr: KbExpr.and(debug.CONTEXT_IN_DEBUG_MODE, EditorKbExpr.TextFocus), primary: KeyMod.chord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyMod.CtrlCmd | KeyCode.KEY_I) }; } diff --git a/src/vs/workbench/parts/debug/common/debug.ts b/src/vs/workbench/parts/debug/common/debug.ts index ed285b6536e..4eca805fa12 100644 --- a/src/vs/workbench/parts/debug/common/debug.ts +++ b/src/vs/workbench/parts/debug/common/debug.ts @@ -11,11 +11,13 @@ import {createDecorator} from 'vs/platform/instantiation/common/instantiation'; import editor = require('vs/editor/common/editorCommon'); import {Source} from 'vs/workbench/parts/debug/common/debugSource'; import {Range} from 'vs/editor/common/core/range'; +import {KbCtxKey, KbExpr} from 'vs/platform/keybinding/common/keybinding'; export const VIEWLET_ID = 'workbench.view.debug'; export const REPL_ID = 'workbench.panel.repl'; export const DEBUG_SERVICE_ID = 'debugService'; -export const CONTEXT_IN_DEBUG_MODE = 'inDebugMode'; +export const CONTEXT_IN_DEBUG_MODE = new KbCtxKey('inDebugMode'); +export const CONTEXT_NOT_IN_DEBUG_MODE:KbExpr = CONTEXT_IN_DEBUG_MODE.negate(); export const EDITOR_CONTRIBUTION_ID = 'editor.contrib.debug'; // raw diff --git a/src/vs/workbench/parts/debug/electron-browser/debug.contribution.ts b/src/vs/workbench/parts/debug/electron-browser/debug.contribution.ts index cd51e236d28..ef236894ad3 100644 --- a/src/vs/workbench/parts/debug/electron-browser/debug.contribution.ts +++ b/src/vs/workbench/parts/debug/electron-browser/debug.contribution.ts @@ -14,7 +14,7 @@ import {SyncActionDescriptor} from 'vs/platform/actions/common/actions'; import platform = require('vs/platform/platform'); import {registerSingleton} from 'vs/platform/instantiation/common/extensions'; import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry'; -import {KbExpr, IKeybindings} from 'vs/platform/keybinding/common/keybinding'; +import {IKeybindings} from 'vs/platform/keybinding/common/keybinding'; import {ServicesAccessor} from 'vs/platform/instantiation/common/instantiation'; import wbaregistry = require('vs/workbench/common/actionRegistry'); import viewlet = require('vs/workbench/browser/viewlet'); @@ -95,20 +95,20 @@ registry.registerWorkbenchAction(new SyncActionDescriptor(OpenDebugViewletAction const debugCategory = nls.localize('debugCategory', "Debug"); registry.registerWorkbenchAction(new SyncActionDescriptor( - StartDebugAction, StartDebugAction.ID, StartDebugAction.LABEL, { primary: KeyCode.F5 }, KbExpr.not(debug.CONTEXT_IN_DEBUG_MODE)), 'Debug: Start Debugging', debugCategory); -registry.registerWorkbenchAction(new SyncActionDescriptor(StepOverDebugAction, StepOverDebugAction.ID, StepOverDebugAction.LABEL, { primary: KeyCode.F10 }, KbExpr.has(debug.CONTEXT_IN_DEBUG_MODE)), 'Debug: Step Over', debugCategory); -registry.registerWorkbenchAction(new SyncActionDescriptor(StepIntoDebugAction, StepIntoDebugAction.ID, StepIntoDebugAction.LABEL, { primary: KeyCode.F11 }, KbExpr.has(debug.CONTEXT_IN_DEBUG_MODE), KeybindingsRegistry.WEIGHT.workbenchContrib(1)), 'Debug: Step Into', debugCategory); -registry.registerWorkbenchAction(new SyncActionDescriptor(StepOutDebugAction, StepOutDebugAction.ID, StepOutDebugAction.LABEL, { primary: KeyMod.Shift | KeyCode.F11 }, KbExpr.has(debug.CONTEXT_IN_DEBUG_MODE)), 'Debug: Step Out', debugCategory); -registry.registerWorkbenchAction(new SyncActionDescriptor(StepBackDebugAction, StepBackDebugAction.ID, StepBackDebugAction.LABEL, { primary: KeyMod.Shift | KeyCode.F10 }, KbExpr.has(debug.CONTEXT_IN_DEBUG_MODE)), 'Debug: Step Back', debugCategory); -registry.registerWorkbenchAction(new SyncActionDescriptor(RestartDebugAction, RestartDebugAction.ID, RestartDebugAction.LABEL, { primary: KeyMod.Shift | KeyMod.CtrlCmd | KeyCode.F5 }, KbExpr.has(debug.CONTEXT_IN_DEBUG_MODE)), 'Debug: Restart', debugCategory); -registry.registerWorkbenchAction(new SyncActionDescriptor(StopDebugAction, StopDebugAction.ID, StopDebugAction.LABEL, { primary: KeyMod.Shift | KeyCode.F5 }, KbExpr.has(debug.CONTEXT_IN_DEBUG_MODE)), 'Debug: Stop', debugCategory); -registry.registerWorkbenchAction(new SyncActionDescriptor(ContinueAction, ContinueAction.ID, ContinueAction.LABEL, { primary: KeyCode.F5 }, KbExpr.has(debug.CONTEXT_IN_DEBUG_MODE)), 'Debug: Continue', debugCategory); + StartDebugAction, StartDebugAction.ID, StartDebugAction.LABEL, { primary: KeyCode.F5 }, debug.CONTEXT_NOT_IN_DEBUG_MODE), 'Debug: Start Debugging', debugCategory); +registry.registerWorkbenchAction(new SyncActionDescriptor(StepOverDebugAction, StepOverDebugAction.ID, StepOverDebugAction.LABEL, { primary: KeyCode.F10 }, debug.CONTEXT_IN_DEBUG_MODE), 'Debug: Step Over', debugCategory); +registry.registerWorkbenchAction(new SyncActionDescriptor(StepIntoDebugAction, StepIntoDebugAction.ID, StepIntoDebugAction.LABEL, { primary: KeyCode.F11 }, debug.CONTEXT_IN_DEBUG_MODE, KeybindingsRegistry.WEIGHT.workbenchContrib(1)), 'Debug: Step Into', debugCategory); +registry.registerWorkbenchAction(new SyncActionDescriptor(StepOutDebugAction, StepOutDebugAction.ID, StepOutDebugAction.LABEL, { primary: KeyMod.Shift | KeyCode.F11 }, debug.CONTEXT_IN_DEBUG_MODE), 'Debug: Step Out', debugCategory); +registry.registerWorkbenchAction(new SyncActionDescriptor(StepBackDebugAction, StepBackDebugAction.ID, StepBackDebugAction.LABEL, { primary: KeyMod.Shift | KeyCode.F10 }, debug.CONTEXT_IN_DEBUG_MODE), 'Debug: Step Back', debugCategory); +registry.registerWorkbenchAction(new SyncActionDescriptor(RestartDebugAction, RestartDebugAction.ID, RestartDebugAction.LABEL, { primary: KeyMod.Shift | KeyMod.CtrlCmd | KeyCode.F5 }, debug.CONTEXT_IN_DEBUG_MODE), 'Debug: Restart', debugCategory); +registry.registerWorkbenchAction(new SyncActionDescriptor(StopDebugAction, StopDebugAction.ID, StopDebugAction.LABEL, { primary: KeyMod.Shift | KeyCode.F5 }, debug.CONTEXT_IN_DEBUG_MODE), 'Debug: Stop', debugCategory); +registry.registerWorkbenchAction(new SyncActionDescriptor(ContinueAction, ContinueAction.ID, ContinueAction.LABEL, { primary: KeyCode.F5 }, debug.CONTEXT_IN_DEBUG_MODE), 'Debug: Continue', debugCategory); registry.registerWorkbenchAction(new SyncActionDescriptor(PauseAction, PauseAction.ID, PauseAction.LABEL), 'Debug: Pause', debugCategory); registry.registerWorkbenchAction(new SyncActionDescriptor(ConfigureAction, ConfigureAction.ID, ConfigureAction.LABEL), 'Debug: Open launch.json', debugCategory); registry.registerWorkbenchAction(new SyncActionDescriptor(ToggleReplAction, ToggleReplAction.ID, ToggleReplAction.LABEL, { primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_Y, }), 'Debug: Debug Console', debugCategory); registry.registerWorkbenchAction(new SyncActionDescriptor(AddFunctionBreakpointAction, AddFunctionBreakpointAction.ID, AddFunctionBreakpointAction.LABEL), 'Debug: Add Function Breakpoint', debugCategory); registry.registerWorkbenchAction(new SyncActionDescriptor(ReapplyBreakpointsAction, ReapplyBreakpointsAction.ID, ReapplyBreakpointsAction.LABEL), 'Debug: Reapply All Breakpoints', debugCategory); -registry.registerWorkbenchAction(new SyncActionDescriptor(RunAction, RunAction.ID, RunAction.LABEL, { primary: KeyMod.CtrlCmd | KeyCode.F5 }, KbExpr.not(debug.CONTEXT_IN_DEBUG_MODE)), 'Debug: Start Without Debugging', debugCategory); +registry.registerWorkbenchAction(new SyncActionDescriptor(RunAction, RunAction.ID, RunAction.LABEL, { primary: KeyMod.CtrlCmd | KeyCode.F5 }, debug.CONTEXT_NOT_IN_DEBUG_MODE), 'Debug: Start Without Debugging', debugCategory); registry.registerWorkbenchAction(new SyncActionDescriptor(RemoveAllBreakpointsAction, RemoveAllBreakpointsAction.ID, RemoveAllBreakpointsAction.LABEL), 'Debug: Remove All Breakpoints', debugCategory); registry.registerWorkbenchAction(new SyncActionDescriptor(EnableAllBreakpointsAction, EnableAllBreakpointsAction.ID, EnableAllBreakpointsAction.LABEL), 'Debug: Enable All Breakpoints', debugCategory); registry.registerWorkbenchAction(new SyncActionDescriptor(DisableAllBreakpointsAction, DisableAllBreakpointsAction.ID, DisableAllBreakpointsAction.LABEL), 'Debug: Disable All Breakpoints', debugCategory); @@ -130,7 +130,7 @@ KeybindingsRegistry.registerCommandDesc({ const noDebug = configuration && !!configuration.noDebug; return debugService.createSession(noDebug, configuration); }, - when: KbExpr.not(debug.CONTEXT_IN_DEBUG_MODE), + when: debug.CONTEXT_NOT_IN_DEBUG_MODE, primary: undefined }); diff --git a/src/vs/workbench/parts/debug/electron-browser/debugService.ts b/src/vs/workbench/parts/debug/electron-browser/debugService.ts index d6d6f3851a9..63aaab9ba12 100644 --- a/src/vs/workbench/parts/debug/electron-browser/debugService.ts +++ b/src/vs/workbench/parts/debug/electron-browser/debugService.ts @@ -109,7 +109,7 @@ export class DebugService implements debug.IDebugService { this._state = debug.State.Disabled; } this.configurationManager = this.instantiationService.createInstance(ConfigurationManager, this.storageService.get(DEBUG_SELECTED_CONFIG_NAME_KEY, StorageScope.WORKSPACE, 'null')); - this.inDebugMode = keybindingService.createKey(debug.CONTEXT_IN_DEBUG_MODE, false); + this.inDebugMode = debug.CONTEXT_IN_DEBUG_MODE.bindTo(keybindingService, false); this.model = new model.Model(this.loadBreakpoints(), this.storageService.getBoolean(DEBUG_BREAKPOINTS_ACTIVATED_KEY, StorageScope.WORKSPACE, true), this.loadFunctionBreakpoints(), this.loadExceptionBreakpoints(), this.loadWatchExpressions()); diff --git a/src/vs/workbench/parts/search/browser/searchWidget.ts b/src/vs/workbench/parts/search/browser/searchWidget.ts index a759e3ee490..4d88088f1e6 100644 --- a/src/vs/workbench/parts/search/browser/searchWidget.ts +++ b/src/vs/workbench/parts/search/browser/searchWidget.ts @@ -14,8 +14,7 @@ import { InputBox } from 'vs/base/browser/ui/inputbox/inputBox'; import { Button } from 'vs/base/browser/ui/button/button'; import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent'; import { KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRegistry'; -import { IKeybindingService, IKeybindingContextKey } from 'vs/platform/keybinding/common/keybinding'; -import { KbExpr } from 'vs/platform/keybinding/common/keybinding'; +import { KbExpr, KbCtxKey, IKeybindingService, IKeybindingContextKey } from 'vs/platform/keybinding/common/keybinding'; import { IContextViewService } from 'vs/platform/contextview/browser/contextView'; import { KeyCode, KeyMod } from 'vs/base/common/keyCodes'; import Event, { Emitter } from 'vs/base/common/event'; @@ -23,7 +22,7 @@ import { Builder } from 'vs/base/browser/builder'; import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; import { IViewletService } from 'vs/workbench/services/viewlet/common/viewletService'; import { isSearchViewletFocussed, appendKeyBindingLabel } from 'vs/workbench/parts/search/browser/searchActions'; -import { CONTEXT_FIND_WIDGET_VISIBLE } from 'vs/editor/contrib/find/common/findController'; +import { CONTEXT_FIND_WIDGET_NOT_VISIBLE } from 'vs/editor/contrib/find/common/findController'; export interface ISearchWidgetOptions { value?:string; @@ -64,7 +63,7 @@ class ReplaceAllAction extends Action { export class SearchWidget extends Widget { - static REPLACE_ACTIVE_CONTEXT_KEY= 'replaceActive'; + static REPLACE_ACTIVE_CONTEXT_KEY= new KbCtxKey('replaceActive'); private static REPLACE_ALL_DISABLED_LABEL= nls.localize('search.action.replaceAll.disabled.label', "Replace All (Submit Search to Enable)"); private static REPLACE_ALL_ENABLED_LABEL=(keyBindingService: IKeybindingService):string=>{ let keybindings = keyBindingService.lookupKeybindings(ReplaceAllAction.ID); @@ -104,7 +103,7 @@ export class SearchWidget extends Widget { constructor(container: Builder, private contextViewService: IContextViewService, options: ISearchWidgetOptions= Object.create(null), private keyBindingService: IKeybindingService, private instantiationService: IInstantiationService) { super(); - this.replaceActive = this.keyBindingService.createKey(SearchWidget.REPLACE_ACTIVE_CONTEXT_KEY, false); + this.replaceActive = SearchWidget.REPLACE_ACTIVE_CONTEXT_KEY.bindTo(this.keyBindingService, false); this.render(container, options); } @@ -222,7 +221,7 @@ export class SearchWidget extends Widget { } private isReplaceActive(): boolean { - return this.keyBindingService.getContextValue(SearchWidget.REPLACE_ACTIVE_CONTEXT_KEY); + return this.replaceActive.get(); } private updateReplaceActiveState(): void { @@ -334,7 +333,7 @@ export class SearchWidget extends Widget { export function registerContributions() { KeybindingsRegistry.registerCommandDesc({id: ReplaceAllAction.ID, weight: KeybindingsRegistry.WEIGHT.workbenchContrib(), - when: KbExpr.and(KbExpr.has('searchViewletVisible'), KbExpr.has(SearchWidget.REPLACE_ACTIVE_CONTEXT_KEY), KbExpr.not(CONTEXT_FIND_WIDGET_VISIBLE)), + when: KbExpr.and(KbExpr.has('searchViewletVisible'), SearchWidget.REPLACE_ACTIVE_CONTEXT_KEY, CONTEXT_FIND_WIDGET_NOT_VISIBLE), primary: KeyMod.Alt | KeyMod.CtrlCmd | KeyCode.Enter, handler: accessor => { if (isSearchViewletFocussed(accessor.get(IViewletService))) { @@ -342,4 +341,4 @@ export function registerContributions() { } } }); -} \ No newline at end of file +} -- GitLab