瀏覽代碼

Fix codemirror typings

This commit is largely just working around and ignoring typing issues using type casts. We should come back and fix the typings so we don’t need so many typecasts.
Jason Grout 4 年之前
父節點
當前提交
4f724f0843

+ 18 - 17
packages/codemirror/src/editor.ts

@@ -737,14 +737,14 @@ export class CodeMirrorEditor implements CodeEditor.IEditor {
     void Mode.ensure(mime).then(spec => {
       editor.setOption('mode', spec?.mime ?? 'null');
     });
-    const extraKeys = editor.getOption('extraKeys') || {};
+    const extraKeys = (editor.getOption('extraKeys' as any) || {}) as CodeMirror.KeyMap;
     const isCode = mime !== 'text/plain' && mime !== 'text/x-ipythongfm';
     if (isCode) {
       extraKeys['Backspace'] = 'delSpaceToPrevTabStop';
     } else {
       delete extraKeys['Backspace'];
     }
-    editor.setOption('extraKeys', extraKeys);
+    editor.setOption('extraKeys' as any, extraKeys);
   }
 
   /**
@@ -1205,7 +1205,7 @@ export namespace CodeMirrorEditor {
      * Can be used to specify extra keybindings for the editor, alongside the
      * ones defined by keyMap. Should be either null, or a valid keymap value.
      */
-    extraKeys?: any;
+    extraKeys?: CodeMirror.KeyMap | null;
 
     /**
      * Can be used to add extra gutters (beyond or instead of the line number
@@ -1372,7 +1372,7 @@ namespace Private {
       lineWrapping: lineWrap === 'off' ? false : true,
       readOnly,
       ...otherOptions
-    };
+    } as CodeMirror.EditorConfiguration;
     return CodeMirror(el => {
       if (fontFamily) {
         el.style.fontFamily = fontFamily;
@@ -1429,7 +1429,8 @@ namespace Private {
    */
   export function delSpaceToPrevTabStop(cm: CodeMirror.Editor): void {
     const doc = cm.getDoc();
-    const tabSize = cm.getOption('indentUnit');
+    // default tabsize is 2, according to codemirror docs: https://codemirror.net/doc/manual.html#config
+    const tabSize = cm.getOption('indentUnit') ?? 2;
     const ranges = doc.listSelections(); // handle multicursor
     for (let i = ranges.length - 1; i >= 0; i--) {
       // iterate reverse so any deletions don't overlap
@@ -1548,17 +1549,17 @@ namespace Private {
         break;
       }
       case 'tabSize':
-        editor.setOption('indentUnit', value);
+        editor.setOption('indentUnit', value as CodeMirror.EditorConfiguration['tabSize']);
         break;
       case 'insertSpaces':
         editor.setOption('indentWithTabs', !value);
         break;
       case 'autoClosingBrackets':
-        editor.setOption('autoCloseBrackets', value);
+        editor.setOption('autoCloseBrackets', value as any);
         break;
       case 'rulers': {
         const rulers = value as Array<number>;
-        editor.setOption(
+        (editor.setOption as any)(
           'rulers',
           rulers.map(column => {
             return {
@@ -1570,34 +1571,34 @@ namespace Private {
         break;
       }
       case 'readOnly':
-        el.classList.toggle(READ_ONLY_CLASS, value);
-        editor.setOption(option, value);
+        el.classList.toggle(READ_ONLY_CLASS, value as boolean);
+        (editor.setOption as any)(option, value);
         break;
       case 'fontFamily':
-        el.style.fontFamily = value;
+        el.style.fontFamily = value as string;
         break;
       case 'fontSize':
         el.style.setProperty('font-size', value ? value + 'px' : null);
         break;
       case 'lineHeight':
-        el.style.lineHeight = value ? value.toString() : null;
+        el.style.lineHeight = (value ? value.toString() : null) as any;
         break;
       case 'gutters':
-        editor.setOption(option, getActiveGutters(config));
+        (editor.setOption as any)(option, getActiveGutters(config));
         break;
       case 'lineNumbers':
-        editor.setOption(option, value);
+        (editor.setOption as any)(option, value);
         editor.setOption('gutters', getActiveGutters(config));
         break;
       case 'codeFolding':
-        editor.setOption('foldGutter', value);
+        (editor.setOption as any)('foldGutter', value);
         editor.setOption('gutters', getActiveGutters(config));
         break;
       case 'theme':
-        void setTheme(editor, shadowRoot, value);
+        void setTheme(editor, shadowRoot, value as string);
         break;
       default:
-        editor.setOption(option, value);
+        (editor.setOption as any)(option, value);
         break;
     }
   }

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

@@ -27,7 +27,7 @@ export class CodeMirrorMimeTypeService implements IEditorMimeTypeService {
         name: info.name,
         ext: [ext.split('.').slice(-1)[0]]
       }
-    ).mime;
+    ).mime as string;
   }
 
   /**
@@ -44,6 +44,6 @@ export class CodeMirrorMimeTypeService implements IEditorMimeTypeService {
       return 'text/x-ipythongfm';
     }
     const mode = Mode.findByFileName(path) || Mode.findBest('');
-    return mode.mime;
+    return mode.mime as string;
   }
 }

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

@@ -88,7 +88,7 @@ export namespace Mode {
    * Get the raw list of available modes specs.
    */
   export function getModeInfo(): ISpec[] {
-    return CodeMirror.modeInfo;
+    return CodeMirror.modeInfo as ISpec[];
   }
 
   /**

+ 1 - 1
packages/codemirror/src/typings.d.ts

@@ -2,4 +2,4 @@
 // Distributed under the terms of the Modified BSD License.
 
 /// <reference path="../typings/codemirror/codemirror.d.ts"/>
-/// <reference types="@types/codemirror/searchcursor"/>
+/// <reference types="@types/codemirror/addon/search/searchcursor"/>

+ 4 - 8
packages/codemirror/typings/codemirror/codemirror.d.ts

@@ -25,14 +25,6 @@ declare module 'codemirror' {
    */
   function defineMIME(mimetype: string, mode: any): void;
 
-  interface modeinfo {
-    ext: string[];
-    mime: string;
-    mode: string;
-    name: string;
-  }
-  var modeInfo: modeinfo[];
-
   /**
    * A mode that encompasses many mode types.
    */
@@ -108,4 +100,8 @@ declare module 'codemirror' {
   interface StringStream {
     lineOracle: Context;
   }
+
+  interface EditorConfiguration {
+    lineSeparator?: string | null;
+  }
 }

+ 2 - 2
packages/statusbar/src/style/variables.ts

@@ -1,6 +1,6 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
-import { WhiteSpaceProperty } from 'csstype';
+import { Property } from 'csstype';
 
 export default {
   hoverColor: 'var(--jp-layout-color3)',
@@ -15,6 +15,6 @@ export default {
   itemPadding: '6px',
   statusBarPadding: '10px',
   interItemHalfSpacing: '2px', // this amount accounts for half the spacing between items
-  whiteSpace: 'nowrap' as WhiteSpaceProperty,
+  whiteSpace: 'nowrap' as Property.WhiteSpace,
   textOverflow: 'ellipsis'
 };