Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Work on easy-configuring of the console execute shortcut. #4054

Merged
merged 3 commits into from May 15, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
109 changes: 104 additions & 5 deletions packages/console-extension/src/index.ts
Expand Up @@ -18,7 +18,7 @@ import {
} from '@jupyterlab/console';

import {
PageConfig
ISettingRegistry, PageConfig
} from '@jupyterlab/coreutils';

import {
Expand All @@ -42,11 +42,11 @@ import {
} from '@phosphor/algorithm';

import {
ReadonlyJSONObject
JSONExt, ReadonlyJSONObject
} from '@phosphor/coreutils';

import {
DockLayout
DockLayout, Menu
} from '@phosphor/widgets';


Expand Down Expand Up @@ -89,6 +89,12 @@ namespace CommandIDs {

export
const toggleShowAllActivity = 'console:toggle-show-all-kernel-activity';

export
const enterToExecute = 'console:enter-to-execute';

export
const shiftEnterToExecute = 'console:shift-enter-to-execute';
}


Expand All @@ -105,7 +111,8 @@ const tracker: JupyterLabPlugin<IConsoleTracker> = {
IEditorServices,
ILayoutRestorer,
IFileBrowserFactory,
IRenderMimeRegistry
IRenderMimeRegistry,
ISettingRegistry
],
optional: [ILauncher],
activate: activateConsole,
Expand Down Expand Up @@ -138,7 +145,7 @@ export default plugins;
/**
* Activate the console extension.
*/
function activateConsole(app: JupyterLab, mainMenu: IMainMenu, palette: ICommandPalette, contentFactory: ConsolePanel.IContentFactory, editorServices: IEditorServices, restorer: ILayoutRestorer, browserFactory: IFileBrowserFactory, rendermime: IRenderMimeRegistry, launcher: ILauncher | null): IConsoleTracker {
function activateConsole(app: JupyterLab, mainMenu: IMainMenu, palette: ICommandPalette, contentFactory: ConsolePanel.IContentFactory, editorServices: IEditorServices, restorer: ILayoutRestorer, browserFactory: IFileBrowserFactory, rendermime: IRenderMimeRegistry, settingRegistry: ISettingRegistry, launcher: ILauncher | null): IConsoleTracker {
const manager = app.serviceManager;
const { commands, shell } = app;
const category = 'Console';
Expand Down Expand Up @@ -416,6 +423,89 @@ function activateConsole(app: JupyterLab, mainMenu: IMainMenu, palette: ICommand
false,
isEnabled
});

// Constants for setting the shortcuts for executing console cells.
const shortcutPlugin = '@jupyterlab/shortcuts-extension:plugin';
const selector = '.jp-CodeConsole-promptCell';

// Keep updated keybindings for the console commands related to execution.
let linebreak = find(commands.keyBindings,
kb => kb.command === CommandIDs.linebreak);
let runUnforced = find(commands.keyBindings,
kb => kb.command === CommandIDs.runUnforced);
let runForced = find(commands.keyBindings,
kb => kb.command === CommandIDs.runForced);
commands.keyBindingChanged.connect((s, args) => {
if (args.binding.command === CommandIDs.linebreak) {
linebreak = args.type === 'added' ? args.binding : undefined;
return;
}
if (args.binding.command === CommandIDs.runUnforced) {
runUnforced = args.type === 'added' ? args.binding : undefined;
return;
}
if (args.binding.command === CommandIDs.runForced) {
runForced = args.type === 'added' ? args.binding : undefined;
return;
}
});

commands.addCommand(CommandIDs.shiftEnterToExecute, {
label: 'Execute with Shift+Enter',
isToggled: () => {
// Only show as toggled if the shortcuts are strictly
// The Shift+Enter ones.
return linebreak && JSONExt.deepEqual(linebreak.keys, ['Enter']) &&
runUnforced === undefined &&
runForced && JSONExt.deepEqual(runForced.keys, ['Shift Enter']);
},
execute: () => {
const first = settingRegistry.set(shortcutPlugin, CommandIDs.linebreak, {
command: CommandIDs.linebreak,
keys: ['Enter'],
selector
});
const second = settingRegistry.remove(shortcutPlugin, CommandIDs.runUnforced);
const third = settingRegistry.set(shortcutPlugin, CommandIDs.runForced, {
command: CommandIDs.runForced,
keys: ['Shift Enter'],
selector
});

return Promise.all([first, second, third]);
}
});

commands.addCommand(CommandIDs.enterToExecute, {
label: 'Execute with Enter',
isToggled: () => {
// Only show as toggled if the shortcuts are strictly
// The Enter ones.
return linebreak && JSONExt.deepEqual(linebreak.keys, ['Ctrl Enter']) &&
runUnforced && JSONExt.deepEqual(runUnforced.keys, ['Enter']) &&
runForced && JSONExt.deepEqual(runForced.keys, ['Shift Enter']);
},
execute: () => {
const first = settingRegistry.set(shortcutPlugin, CommandIDs.linebreak, {
command: CommandIDs.linebreak,
keys: ['Ctrl Enter'],
selector
});
const second = settingRegistry.set(shortcutPlugin, CommandIDs.runUnforced, {
command: CommandIDs.runUnforced,
keys: ['Enter'],
selector
});
const third = settingRegistry.set(shortcutPlugin, CommandIDs.runForced, {
command: CommandIDs.runForced,
keys: ['Shift Enter'],
selector
});

return Promise.all([first, second, third]);
}
});

// Add command palette items
[
CommandIDs.create,
Expand Down Expand Up @@ -497,6 +587,15 @@ function activateConsole(app: JupyterLab, mainMenu: IMainMenu, palette: ICommand
clearCurrent: (current: ConsolePanel) => { return current.console.clear(); }
} as IEditMenu.IClearer<ConsolePanel>);

// Add the execute keystroke setting submenu.
const executeMenu = new Menu({ commands });
executeMenu.title.label = 'Console Run Keystroke';
executeMenu.addItem({ command: CommandIDs.enterToExecute });
executeMenu.addItem({ command: CommandIDs.shiftEnterToExecute });
mainMenu.settingsMenu.addGroup([{
type: 'submenu' as Menu.ItemType, submenu: executeMenu
}], 10);

// Add kernel information to the application help menu.
mainMenu.helpMenu.kernelUsers.add({
tracker,
Expand Down