Browse Source

Conditionally enable run text (#3476)

* Allow menu extenders to provide more contextual information about
whether they are enabled.

* Only enable run code for text files if they have an attached console.
Ian Rose 7 years ago
parent
commit
bdff1a5bad

+ 1 - 0
packages/fileeditor-extension/package.json

@@ -33,6 +33,7 @@
     "@jupyterlab/application": "^0.13.1",
     "@jupyterlab/apputils": "^0.13.1",
     "@jupyterlab/codeeditor": "^0.13.0",
+    "@jupyterlab/console": "^0.13.0",
     "@jupyterlab/coreutils": "^0.13.0",
     "@jupyterlab/filebrowser": "^0.13.2",
     "@jupyterlab/fileeditor": "^0.13.0",

+ 17 - 4
packages/fileeditor-extension/src/index.ts

@@ -13,6 +13,10 @@ import {
   CodeEditor, IEditorServices
 } from '@jupyterlab/codeeditor';
 
+import {
+  IConsoleTracker
+} from '@jupyterlab/console';
+
 import {
   ISettingRegistry, MarkdownCodeBlocks, PathExt
 } from '@jupyterlab/coreutils';
@@ -30,7 +34,7 @@ import {
 } from '@jupyterlab/launcher';
 
 import {
-  IEditMenu, IFileMenu, IMainMenu, IViewMenu
+  IEditMenu, IFileMenu, IMainMenu, IRunMenu, IViewMenu
 } from '@jupyterlab/mainmenu';
 
 import {
@@ -91,7 +95,7 @@ namespace CommandIDs {
 const plugin: JupyterLabPlugin<IEditorTracker> = {
   activate,
   id: '@jupyterlab/fileeditor-extension:plugin',
-  requires: [IEditorServices, IFileBrowserFactory, ILayoutRestorer, ISettingRegistry],
+  requires: [IConsoleTracker, IEditorServices, IFileBrowserFactory, ILayoutRestorer, ISettingRegistry],
   optional: [ICommandPalette, ILauncher, IMainMenu],
   provides: IEditorTracker,
   autoStart: true
@@ -107,7 +111,7 @@ export default plugin;
 /**
  * Activate the editor tracker plugin.
  */
-function activate(app: JupyterLab, editorServices: IEditorServices, browserFactory: IFileBrowserFactory, restorer: ILayoutRestorer, settingRegistry: ISettingRegistry, palette: ICommandPalette, launcher: ILauncher | null, menu: IMainMenu | null): IEditorTracker {
+function activate(app: JupyterLab, consoleTracker: IConsoleTracker, editorServices: IEditorServices, browserFactory: IFileBrowserFactory, restorer: ILayoutRestorer, settingRegistry: ISettingRegistry, palette: ICommandPalette, launcher: ILauncher | null, menu: IMainMenu | null): IEditorTracker {
   const id = plugin.id;
   const namespace = 'editor';
   const factory = new FileEditorFactory({
@@ -465,8 +469,17 @@ function activate(app: JupyterLab, editorServices: IEditorServices, browserFacto
     menu.runMenu.codeRunners.add({
       tracker,
       noun: 'Code',
+      isEnabled: current => {
+        let found = false;
+        consoleTracker.forEach(console => {
+          if (console.console.session.path === current.context.path) {
+            found = true;
+          }
+        });
+        return found;
+      },
       run: () => commands.execute(CommandIDs.runCode)
-    });
+    } as IRunMenu.ICodeRunner<FileEditor>);
   }
 
   app.contextMenu.addItem({

+ 2 - 1
packages/mainmenu-extension/src/index.ts

@@ -516,7 +516,8 @@ namespace Private {
     return () => {
       let widget = app.shell.currentWidget;
       const extender = findExtender(widget, s);
-      return !!extender && !!extender[executor];
+      return !!extender && !!extender[executor] &&
+        (extender.isEnabled ? extender.isEnabled(widget) : true);
     };
   }
 

+ 9 - 0
packages/mainmenu/src/labmenu.ts

@@ -48,6 +48,15 @@ interface IMenuExtender<T extends Widget> {
    * A widget tracker for identifying the appropriate extender.
    */
   tracker: IInstanceTracker<T>;
+
+  /**
+   * An additional function that determines whether the extender
+   * is enabled. By default it is considered enabled if the application
+   * active widget is contained in the `tracker`. If this is also
+   * provided, the critereon is equivalent to
+   * `tracker.has(widget) && extender.isEnabled(widget)`
+   */
+  isEnabled?: (widget: T) => boolean;
 }
 
 /**