浏览代码

Merge pull request #2804 from blink1073/codemirror-updates

Codemirror Updates
Afshin Darian 7 年之前
父节点
当前提交
e0c1ecd853
共有 3 个文件被更改,包括 36 次插入30 次删除
  1. 9 5
      packages/codemirror-extension/src/index.ts
  2. 8 21
      packages/codemirror/src/mimetype.ts
  3. 19 4
      packages/codemirror/src/mode.ts

+ 9 - 5
packages/codemirror-extension/src/index.ts

@@ -230,10 +230,10 @@ function activateEditorCommands(app: JupyterLab, tracker: IEditorTracker, mainMe
     commands.addCommand(CommandIDs.changeMode, {
       label: args => args['name'] as string,
       execute: args => {
-        let mode = args['mode'] as string;
+        let name = args['name'] as string;
         let widget = tracker.currentWidget;
-        if (mode && widget) {
-          let spec = Mode.findByName(mode);
+        if (name && widget) {
+          let spec = Mode.findByName(name);
           if (spec) {
             widget.model.mimeType = spec.mime;
           }
@@ -247,8 +247,8 @@ function activateEditorCommands(app: JupyterLab, tracker: IEditorTracker, mainMe
         }
         let mime = widget.model.mimeType;
         let spec = Mode.findByMIME(mime);
-        let mode = spec && spec.mode;
-        return args['mode'] === mode;
+        let name = spec && spec.name;
+        return args['name'] === name;
       }
     });
 
@@ -257,6 +257,10 @@ function activateEditorCommands(app: JupyterLab, tracker: IEditorTracker, mainMe
       let bName = b.name || '';
       return aName.localeCompare(bName);
     }).forEach(spec => {
+      // Avoid mode name with a curse word.
+      if (spec.mode.indexOf('brainf') === 0) {
+        return;
+      }
       modeMenu.addItem({
         command: CommandIDs.changeMode,
         args: {...spec}

+ 8 - 21
packages/codemirror/src/mimetype.ts

@@ -25,38 +25,25 @@ class CodeMirrorMimeTypeService implements IEditorMimeTypeService {
    * If a mime type cannot be found returns the defaul mime type `text/plain`, never `null`.
    */
   getMimeTypeByLanguage(info: nbformat.ILanguageInfoMetadata): string {
-    let mode: Mode.ISpec;
-    if (info.codemirror_mode) {
-      mode = Mode.findBest(info.codemirror_mode as any);
-      if (mode) {
-        return mode.mime;
-      }
-    }
-    mode = Mode.findByMIME(info.mimetype || '');
-    if (mode) {
-      return info.mimetype!;
-    }
     let ext = info.file_extension || '';
-    ext = ext.split('.').slice(-1)[0];
-    mode = Mode.findByExtension(ext || '');
-    if (mode) {
-      return mode.mime;
-    }
-    mode = Mode.findByName(info.name || '');
-    return mode ? mode.mime : IEditorMimeTypeService.defaultMimeType;
+    return Mode.findBest(info.codemirror_mode as any || {
+      mimetype: info.mimetype,
+      name: info.name,
+      ext: [ext.split('.').slice(-1)[0]]
+    }).mime;
   }
 
   /**
    * Returns a mime type for the given file path.
    *
    * #### Notes
-   * If a mime type cannot be found returns the defaul mime type `text/plain`, never `null`.
+   * If a mime type cannot be found returns the default mime type `text/plain`, never `null`.
    */
   getMimeTypeByFilePath(path: string): string {
     if (PathExt.extname(path) === '.ipy') {
       return 'text/x-python';
     }
-    const mode = Mode.findByFileName(path);
-    return mode ? mode.mime : IEditorMimeTypeService.defaultMimeType;
+    let mode = Mode.findByFileName(path) || Mode.findBest('');
+    return mode.mime;
   }
 }

+ 19 - 4
packages/codemirror/src/mode.ts

@@ -1,6 +1,10 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
+import {
+  IEditorMimeTypeService
+} from '@jupyterlab/codeeditor';
+
 import * as CodeMirror
   from 'codemirror';
 
@@ -89,10 +93,13 @@ namespace Mode {
     let modename = (typeof mode === 'string') ? mode :
         mode.mode || mode.name;
     let mimetype = (typeof mode !== 'string') ? mode.mime : modename;
+    let ext = (typeof mode !== 'string') ? mode.ext : [];
 
     return (
       CodeMirror.findModeByName(modename || '') ||
       CodeMirror.findModeByMIME(mimetype || '') ||
+      findByExtension(ext) ||
+      CodeMirror.findModeByMIME(IEditorMimeTypeService.defaultMimeType) ||
       CodeMirror.findModeByMIME('text/plain')
     );
   }
@@ -109,8 +116,8 @@ namespace Mode {
    * Find a codemirror mode by name.
    */
   export
-  function findByName(mime: string): ISpec {
-    return CodeMirror.findModeByName(mime);
+  function findByName(name: string): ISpec {
+    return CodeMirror.findModeByName(name);
   }
 
   /**
@@ -125,7 +132,15 @@ namespace Mode {
    * Find a codemirror mode by extension.
    */
   export
-  function findByExtension(name: string): ISpec {
-    return CodeMirror.findModeByExtension(name);
+  function findByExtension(ext: string | string[]): ISpec {
+    if (typeof ext === 'string') {
+      return CodeMirror.findModeByExtension(name);
+    }
+    for (let i = 0; i < ext.length; i++) {
+      let mode = CodeMirror.findModeByExtension(ext[i]);
+      if (mode) {
+        return mode;
+      }
+    }
   }
 }