Prechádzať zdrojové kódy

More cleanup of codemirror mode handling

Steven Silvester 8 rokov pred
rodič
commit
036ac39ac9

+ 1 - 1
packages/codemirror/src/editor.ts

@@ -803,6 +803,6 @@ namespace Private {
  * character or first multiple of tabsize tabstop.
  */
 CodeMirrorEditor.addCommand(
-  'delSpaceToPrevTabStop'], Private.delSpaceToPrevTabStop
+  'delSpaceToPrevTabStop', Private.delSpaceToPrevTabStop
 );
 

+ 7 - 2
packages/codemirror/src/mimetype.ts

@@ -25,10 +25,14 @@ 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) {
-      return Mode.find(info.codemirror_mode as any).mime;
+      mode = Mode.findBest(info.codemirror_mode as any);
+      if (mode) {
+        return mode.mime;
+      }
     }
-    let mode = Mode.findByMIME(info.mimetype || '');
+    mode = Mode.findByMIME(info.mimetype || '');
     if (mode) {
       return info.mimetype!;
     }
@@ -41,6 +45,7 @@ class CodeMirrorMimeTypeService implements IEditorMimeTypeService {
     mode = Mode.findByName(info.name || '');
     return mode ? mode.mime : IEditorMimeTypeService.defaultMimeType;
   }
+
   /**
    * Returns a mime type for the given file path.
    *

+ 5 - 8
packages/codemirror/src/mode.ts

@@ -50,17 +50,14 @@ namespace Mode {
   /**
    * Ensure a codemirror mode is available by name or Codemirror spec.
    *
-   * @param mode - The mode to ensure.  If it is a string, uses [find]
+   * @param mode - The mode to ensure.  If it is a string, uses [findBest]
    *   to get the appropriate spec.
    *
    * @returns A promise that resolves when the mode is available.
    */
   export
   function ensure(mode: string | ISpec): Promise<ISpec> {
-    let spec = find(mode);
-    if (!spec) {
-      return CodeMirror.modes['null'];
-    }
+    let spec = findBest(mode);
 
     // Simplest, cheapest check by mode name.
     if (CodeMirror.modes.hasOwnProperty(spec.mode)) {
@@ -79,15 +76,15 @@ namespace Mode {
    * Find a codemirror mode by name or CodeMirror spec.
    */
   export
-  function find(mode: string | ISpec): ISpec {
+  function findBest(mode: string | ISpec): ISpec {
     let modename = (typeof mode === 'string') ? mode :
         mode.mode || mode.name;
-    let mimetype = (typeof mode !== 'string') ? mode.mime : '';
+    let mimetype = (typeof mode !== 'string') ? mode.mime : modename;
 
     return (
       CodeMirror.findModeByName(modename) ||
       CodeMirror.findModeByMIME(mimetype) ||
-      CodeMirror.modes['null']
+      CodeMirror.findModeByMIME('text/plain')
     );
   }