From 7d46e7792220ce9ebbb43393cd4fcce89b1d038d Mon Sep 17 00:00:00 2001 From: Chris LaFreniere <40371649+chlafreniere@users.noreply.github.com> Date: Tue, 23 Apr 2019 17:33:15 -0700 Subject: [PATCH] Notebooks: Show keyboard shortcut for run cell (#5097) * Show keyboard shortcut for run cell * PR comment --- .../parts/notebook/cellViews/codeActions.ts | 12 ++++++----- .../parts/notebook/notebookActions.ts | 20 +++++++++++++++++-- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/sql/workbench/parts/notebook/cellViews/codeActions.ts b/src/sql/workbench/parts/notebook/cellViews/codeActions.ts index 9ebc49aad1..6a933e4dab 100644 --- a/src/sql/workbench/parts/notebook/cellViews/codeActions.ts +++ b/src/sql/workbench/parts/notebook/cellViews/codeActions.ts @@ -14,6 +14,7 @@ import { getErrorMessage } from 'sql/workbench/parts/notebook/notebookUtils'; import { ICellModel, CellExecutionState } from 'sql/workbench/parts/notebook/models/modelInterfaces'; import { IConnectionManagementService } from 'sql/platform/connection/common/connectionManagement'; import { MultiStateAction, IMultiStateData } from 'sql/workbench/parts/notebook/notebookActions'; +import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding'; let notebookMoreActionMsg = localize('notebook.failed', "Please select active cell and try again"); const emptyExecutionCountLabel = '[ ]'; @@ -67,13 +68,14 @@ export class RunCellAction extends MultiStateAction { private _executionChangedDisposable: IDisposable; private _context: CellContext; constructor(context: CellContext, @INotificationService private notificationService: INotificationService, - @IConnectionManagementService private connectionManagementService: IConnectionManagementService) { + @IConnectionManagementService private connectionManagementService: IConnectionManagementService, + @IKeybindingService private keybindingService: IKeybindingService) { super(RunCellAction.ID, new IMultiStateData([ { key: CellExecutionState.Hidden, value: { label: emptyExecutionCountLabel, className: '', tooltip: '', hideIcon: true } }, - { key: CellExecutionState.Stopped, value: { label: '', className: 'toolbarIconRun', tooltip: localize('runCell', 'Run cell') } }, - { key: CellExecutionState.Running, value: { label: '', className: 'toolbarIconStop', tooltip: localize('stopCell', 'Cancel execution') } }, - { key: CellExecutionState.Error, value: { label: '', className: 'toolbarIconRunError', tooltip: localize('errorRunCell', 'Error on last run. Click to run again') } }, - ], CellExecutionState.Hidden)); + { key: CellExecutionState.Stopped, value: { label: '', className: 'toolbarIconRun', tooltip: localize('runCell', "Run cell"), commandId: 'notebook.command.runactivecell' } }, + { key: CellExecutionState.Running, value: { label: '', className: 'toolbarIconStop', tooltip: localize('stopCell', "Cancel execution") } }, + { key: CellExecutionState.Error, value: { label: '', className: 'toolbarIconRunError', tooltip: localize('errorRunCell', "Error on last run. Click to run again") } }, + ], CellExecutionState.Hidden), keybindingService); this.ensureContextIsUpdated(context); } diff --git a/src/sql/workbench/parts/notebook/notebookActions.ts b/src/sql/workbench/parts/notebook/notebookActions.ts index 90cc580ff1..c80780a364 100644 --- a/src/sql/workbench/parts/notebook/notebookActions.ts +++ b/src/sql/workbench/parts/notebook/notebookActions.ts @@ -22,6 +22,7 @@ import { noKernel } from 'sql/workbench/services/notebook/common/sessionManager' import { IConnectionDialogService } from 'sql/workbench/services/connection/common/connectionDialogService'; import { NotebookModel } from 'sql/workbench/parts/notebook/models/notebookModel'; import { generateUri } from 'sql/platform/connection/common/utils'; +import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding'; const msgLoading = localize('loading', "Loading kernels..."); const msgChanging = localize('changing', "Changing kernel..."); @@ -107,6 +108,7 @@ export interface IActionStateData { label?: string; tooltip?: string; hideIcon?: boolean; + commandId?: string; } export class IMultiStateData { @@ -149,6 +151,10 @@ export class IMultiStateData { return this.getStateValueOrDefault((data) => data.tooltip, ''); } + public get commandId(): string { + return this.getStateValueOrDefault((data) => data.commandId, ''); + } + private getStateValueOrDefault(getter: (data: IActionStateData) => U, defaultVal?: U): U { let data = this._stateMap.get(this._state); return data ? getter(data) : defaultVal; @@ -158,14 +164,24 @@ export class IMultiStateData { export abstract class MultiStateAction extends Action { - constructor(id: string, protected states: IMultiStateData) { + constructor(id: string, protected states: IMultiStateData, private _keybindingService: IKeybindingService) { super(id, ''); this.updateLabelAndIcon(); } private updateLabelAndIcon() { + let keyboardShortcut: string; + try { + // If a keyboard shortcut exists for the command id passed in, append that to the label + if (this.states.commandId !== '') { + let binding = this._keybindingService.lookupKeybinding(this.states.commandId); + keyboardShortcut = binding ? binding.getLabel() : undefined; + } + } catch (error) { + console.log(error); + } this.label = this.states.label; - this.tooltip = this.states.tooltip; + this.tooltip = keyboardShortcut ? this.states.tooltip + ` (${keyboardShortcut})` : this.states.tooltip; this.class = this.states.classes; }