Skip to content

Instantly share code, notes, and snippets.

@jasongrout
Created December 1, 2018 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasongrout/51f8ac587527af70e8beef53f8af62d6 to your computer and use it in GitHub Desktop.
Save jasongrout/51f8ac587527af70e8beef53f8af62d6 to your computer and use it in GitHub Desktop.
diff --git a/packages/console-extension/schema/tracker.json b/packages/console-extension/schema/tracker.json
index b57152070..5cd7fe206 100644
--- a/packages/console-extension/schema/tracker.json
+++ b/packages/console-extension/schema/tracker.json
@@ -13,7 +13,16 @@
"selector": ".jp-CodeConsole-promptCell"
}
],
- "properties": {},
+ "jupyter.lab.transform": true,
+ "properties": {
+ "runKey": {
+ "title": "Run Code Shortcut",
+ "description": "Whether to use Enter or Shift Enter as the run code shortcut",
+ "type": "string",
+ "enum": ["Enter", "Shift Enter"],
+ "default": "Enter"
+ }
+ },
"additionalProperties": false,
"type": "object"
}
diff --git a/packages/console-extension/src/index.ts b/packages/console-extension/src/index.ts
index a71328cb9..0172baa05 100644
--- a/packages/console-extension/src/index.ts
+++ b/packages/console-extension/src/index.ts
@@ -457,9 +457,49 @@ function activateConsole(
});
// Constants for setting the shortcuts for executing console cells.
- const shortcutPlugin = '@jupyterlab/shortcuts-extension:plugin';
+ const shortcutPlugin = '@jupyterlab/console-extension:tracker';
const selector = '.jp-CodeConsole-promptCell';
+ // Transform the plugin object to return different schema than the default.
+ settingRegistry.transform(shortcutPlugin, {
+ fetch: plugin => {
+ if (plugin.data['runKey'] === 'Enter') {
+ // Change the default keyboard shortcut, depending on the run setting.
+ plugin.schema['jupyter.lab.shortcuts'] = [
+ {
+ command: 'console:linebreak',
+ keys: ['Enter'],
+ selector: '.jp-CodeConsole-promptCell'
+ },
+ {
+ command: 'console:run-forced',
+ keys: ['Shift Enter'],
+ selector: '.jp-CodeConsole-promptCell'
+ }
+ ];
+ } else {
+ plugin.schema['jupyter.lab.shortcuts'] = [
+ {
+ command: 'console:linebreak',
+ keys: ['Ctrl Enter'],
+ selector: '.jp-CodeConsole-promptCell'
+ },
+ {
+ command: 'console:run-forced',
+ keys: ['Shift Enter'],
+ selector: '.jp-CodeConsole-promptCell'
+ },
+ {
+ command: 'console:run-unforced',
+ keys: ['Enter'],
+ selector: '.jp-CodeConsole-promptCell'
+ }
+ ];
+ }
+ return plugin;
+ }
+ });
+
// Keep updated keybindings for the console commands related to execution.
let linebreak = find(
commands.keyBindings,
diff --git a/packages/coreutils/src/settingregistry.ts b/packages/coreutils/src/settingregistry.ts
index 797ae7582..412cdcc22 100644
--- a/packages/coreutils/src/settingregistry.ts
+++ b/packages/coreutils/src/settingregistry.ts
@@ -830,15 +830,17 @@ export class SettingRegistry {
* Preload a list of plugins and fail gracefully.
*/
private async _preload(plugins: ISettingRegistry.IPlugin[]): Promise<void> {
- plugins.forEach(async plugin => {
- try {
- // Apply a transformation to the plugin if necessary.
- await this._load(await this._transform('fetch', plugin));
- } catch (errors) {
- /* Ignore preload errors. */
- console.log('Ignored setting registry preload errors.', errors);
- }
- });
+ await Promise.all(
+ plugins.map(async plugin => {
+ try {
+ // Apply a transformation to the plugin if necessary.
+ await this._load(await this._transform('fetch', plugin));
+ } catch (errors) {
+ /* Ignore preload errors. */
+ console.log('Ignored setting registry preload errors.', errors);
+ }
+ })
+ );
}
/**
diff --git a/packages/services/src/setting/index.ts b/packages/services/src/setting/index.ts
index 3de7d33b0..8214fd5b7 100644
--- a/packages/services/src/setting/index.ts
+++ b/packages/services/src/setting/index.ts
@@ -76,6 +76,8 @@ export class SettingManager extends DataConnector<
}
const json = await response.json();
+ // TODO: this cast is an error. The json is not the right structure.
+ // Instead of the expected "data" key, the values have a "settings" key.
const values: ISettingRegistry.IPlugin[] = (json || {})['settings'] || [];
const ids = values.map(value => value.id);
diff --git a/packages/shortcuts-extension/src/index.ts b/packages/shortcuts-extension/src/index.ts
index c933d4149..c589bc06e 100644
--- a/packages/shortcuts-extension/src/index.ts
+++ b/packages/shortcuts-extension/src/index.ts
@@ -161,6 +161,8 @@ const shortcuts: JupyterLabPlugin<void> = {
}
registry.pluginChanged.connect((sender, plugin) => {
+ // TODO: we probably want to reload *any* plugin, since the plugin may
+ // change its keyboard shortcuts with a fetch transform.
if (!(plugin in loaded)) {
populate(canonical);
}
@@ -208,7 +210,11 @@ const shortcuts: JupyterLabPlugin<void> = {
});
try {
- const settings = await registry.load(shortcuts.id);
+ // Repopulate the canonical variable after the setting registry has
+ // preloaded all initial plugins.
+ canonical = null;
+
+ let settings = await registry.load(shortcuts.id);
Private.loadShortcuts(commands, settings.composite);
settings.changed.connect(() => {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment