From cf7dc8b7f05a3a28761e07ac8cdd769e4bb00c04 Mon Sep 17 00:00:00 2001 From: Ian Rose Date: Fri, 12 Jul 2019 17:42:16 -0700 Subject: [PATCH] Auto-generate a help string for the setting with available viewers and file types. --- packages/docmanager-extension/package.json | 1 + .../docmanager-extension/schema/plugin.json | 1 + packages/docmanager-extension/src/index.ts | 49 +++++++++++++++++-- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/packages/docmanager-extension/package.json b/packages/docmanager-extension/package.json index df718748513e..b52087e6cd7f 100644 --- a/packages/docmanager-extension/package.json +++ b/packages/docmanager-extension/package.json @@ -45,6 +45,7 @@ "@jupyterlab/services": "^4.1.0-alpha.2", "@jupyterlab/statusbar": "^1.1.0-alpha.2", "@phosphor/algorithm": "^1.1.3", + "@phosphor/coreutils": "^1.3.1", "@phosphor/disposable": "^1.2.0", "@phosphor/widgets": "^1.8.0" }, diff --git a/packages/docmanager-extension/schema/plugin.json b/packages/docmanager-extension/schema/plugin.json index f5e4a8c4c93f..bae7f81c7a3d 100644 --- a/packages/docmanager-extension/schema/plugin.json +++ b/packages/docmanager-extension/schema/plugin.json @@ -3,6 +3,7 @@ "description": "Document Manager settings.", "jupyter.lab.setting-icon-class": "jp-FileIcon", "jupyter.lab.setting-icon-label": "Document Manager", + "jupyter.lab.transform": true, "jupyter.lab.shortcuts": [ { "command": "docmanager:save", diff --git a/packages/docmanager-extension/src/index.ts b/packages/docmanager-extension/src/index.ts index 611071f8cc1e..6e0e69e9598a 100644 --- a/packages/docmanager-extension/src/index.ts +++ b/packages/docmanager-extension/src/index.ts @@ -1,10 +1,6 @@ // Copyright (c) Jupyter Development Team. // Distributed under the terms of the Modified BSD License. -import { some, map, each } from '@phosphor/algorithm'; - -import { Widget } from '@phosphor/widgets'; - import { ILabShell, ILabStatus, @@ -37,8 +33,14 @@ import { Contents, Kernel } from '@jupyterlab/services'; import { IStatusBar } from '@jupyterlab/statusbar'; +import { each, map, some, toArray } from '@phosphor/algorithm'; + +import { JSONExt } from '@phosphor/coreutils'; + import { IDisposable } from '@phosphor/disposable'; +import { Widget } from '@phosphor/widgets'; + /** * The command IDs used by the document manager plugin. */ @@ -189,6 +191,45 @@ const docManagerPlugin: JupyterFrontEndPlugin = { console.error(reason.message); }); + // Register a fetch transformer for the settings registry, + // allowing us to dynamically populate a help string with the + // available document viewers and file types for the default + // viewer overrides. + settingRegistry.transform(pluginId, { + fetch: plugin => { + // Get the available file types. + const fileTypes = toArray(registry.fileTypes()) + .map(ft => ft.name) + .join(' \n'); + // Get the available widget factories. + const factories = toArray(registry.widgetFactories()) + .map(f => f.name) + .join(' \n'); + // Generate the help string. + const description = `Overrides for the default viewers for file types. +Specify a mapping from file type name to document viewer name, for example: + +defaultViewers: { + markdown: "Markdown Preview" +} + +If you specify non-existent file types or viewers, or if a viewer cannot +open a given file type, the override will not function. + +Available viewers: +${factories} + +Available file types: +${fileTypes}`; + const schema = JSONExt.deepCopy(plugin.schema); + schema.properties.defaultViewers.description = description; + return { ...plugin, schema }; + } + }); + // If the document registry gains or loses a factory or file type, + // regenerate the settings description with the available options. + registry.changed.connect(() => settingRegistry.reload(pluginId)); + return docManager; } };