浏览代码

Handle mimeType

Jeremy Tuloup 5 年之前
父节点
当前提交
e87da66a5f
共有 6 个文件被更改,包括 37 次插入18 次删除
  1. 3 3
      src/debugger.ts
  2. 18 6
      src/editors/index.ts
  3. 1 2
      src/index.ts
  4. 2 2
      src/service.ts
  5. 3 1
      src/tokens.ts
  6. 10 4
      tests/src/debugger.spec.ts

+ 3 - 3
src/debugger.ts

@@ -1,7 +1,7 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
-import { CodeEditor } from '@jupyterlab/codeeditor';
+import { IEditorServices } from '@jupyterlab/codeeditor';
 
 import { IDataConnector } from '@jupyterlab/coreutils';
 
@@ -51,7 +51,7 @@ export class Debugger extends SplitPanel {
     this.editors = new DebuggerEditors({
       model: this.model,
       service: this.service,
-      editorFactory: options.editorFactory
+      editorServices: options.editorServices
     });
     this.addWidget(this.editors);
 
@@ -84,7 +84,7 @@ export class Debugger extends SplitPanel {
 export namespace Debugger {
   export interface IOptions {
     debugService: DebugService;
-    editorFactory: CodeEditor.Factory;
+    editorServices: IEditorServices;
     callstackCommands: Callstack.ICommands;
     connector?: IDataConnector<ReadonlyJSONValue>;
   }

+ 18 - 6
src/editors/index.ts

@@ -3,7 +3,12 @@
 | Distributed under the terms of the Modified BSD License.
 |----------------------------------------------------------------------------*/
 
-import { CodeEditor, CodeEditorWrapper } from '@jupyterlab/codeeditor';
+import {
+  CodeEditor,
+  CodeEditorWrapper,
+  IEditorMimeTypeService,
+  IEditorServices
+} from '@jupyterlab/codeeditor';
 
 import { find } from '@phosphor/algorithm';
 
@@ -28,7 +33,8 @@ export class DebuggerEditors extends TabPanel {
 
     this.debuggerModel = options.model;
     this.debuggerService = options.service;
-    this.editorFactory = options.editorFactory;
+    this.editorFactory = options.editorServices.factoryService.newInlineEditor;
+    this.mimeTypeService = options.editorServices.mimeTypeService;
 
     this.debuggerModel.callstackModel.currentFrameChanged.connect(
       this.onCurrentFrameChanged,
@@ -60,7 +66,7 @@ export class DebuggerEditors extends TabPanel {
   }
 
   private async onCurrentFrameChanged(
-    callstackModel: Callstack.Model,
+    _: Callstack.Model,
     frame: Callstack.IFrame
   ) {
     if (!frame) {
@@ -68,15 +74,20 @@ export class DebuggerEditors extends TabPanel {
     }
 
     const path = frame.source.path;
-    const content = await this.debuggerService.getSource({
+    const source = await this.debuggerService.getSource({
       sourceReference: 0,
       path
     });
 
+    if (!source.success) {
+      return;
+    }
+
+    const { content, mimeType } = source.body;
     this.model.addEditor({
       path,
       code: content,
-      mimeType: 'text/x-python'
+      mimeType: mimeType || this.mimeTypeService.getMimeTypeByFilePath(path)
     });
   }
 
@@ -117,6 +128,7 @@ export class DebuggerEditors extends TabPanel {
   private debuggerModel: Debugger.Model;
   private debuggerService: IDebugger;
   private editorFactory: CodeEditor.Factory;
+  private mimeTypeService: IEditorMimeTypeService;
 }
 
 /**
@@ -129,7 +141,7 @@ export namespace DebuggerEditors {
   export interface IOptions {
     service: IDebugger;
     model: Debugger.Model;
-    editorFactory: CodeEditor.Factory;
+    editorServices: IEditorServices;
   }
 
   /**

+ 1 - 2
src/index.ts

@@ -242,7 +242,6 @@ const main: JupyterFrontEndPlugin<IDebugger> = {
     palette: ICommandPalette | null
   ): IDebugger => {
     const { commands, shell } = app;
-    const editorFactory = editorServices.factoryService.newInlineEditor;
 
     const service = new DebugService();
 
@@ -417,7 +416,7 @@ const main: JupyterFrontEndPlugin<IDebugger> = {
               debugService: service,
               connector: state,
               callstackCommands,
-              editorFactory
+              editorServices
             })
           });
           widget.id = id;

+ 2 - 2
src/service.ts

@@ -361,12 +361,12 @@ export class DebugService implements IDebugger {
    * Retrieve the content of a source file.
    * @param source The source object containing the path to the file.
    */
-  async getSource(source: DebugProtocol.Source): Promise<string> {
+  async getSource(source: DebugProtocol.Source) {
     const reply = await this.session.sendRequest('source', {
       source,
       sourceReference: source.sourceReference
     });
-    return reply.body.content;
+    return reply;
   }
 
   getAllFrames = async () => {

+ 3 - 1
src/tokens.ts

@@ -140,7 +140,9 @@ export interface IDebugger extends IDisposable {
    * Retrieve the content of a source file.
    * @param source The source object containing the path to the file.
    */
-  getSource(source: DebugProtocol.Source): Promise<string>;
+  getSource(
+    source: DebugProtocol.Source
+  ): Promise<DebugProtocol.SourceResponse>;
 }
 
 /**

+ 10 - 4
tests/src/debugger.spec.ts

@@ -1,6 +1,9 @@
 import { expect } from 'chai';
 
-import { CodeMirrorEditorFactory } from '@jupyterlab/codemirror';
+import {
+  CodeMirrorEditorFactory,
+  CodeMirrorMimeTypeService
+} from '@jupyterlab/codemirror';
 
 import { CommandRegistry } from '@phosphor/commands';
 
@@ -13,8 +16,8 @@ class TestPanel extends Debugger {}
 describe('Debugger', () => {
   const service = new DebugService();
   const registry = new CommandRegistry();
-  const editorServices = new CodeMirrorEditorFactory();
-  const editorFactory = editorServices.newInlineEditor;
+  const factoryService = new CodeMirrorEditorFactory();
+  const mimeTypeService = new CodeMirrorMimeTypeService();
 
   let panel: TestPanel;
 
@@ -29,7 +32,10 @@ describe('Debugger', () => {
         stepIn: '',
         stepOut: ''
       },
-      editorFactory: editorFactory
+      editorServices: {
+        factoryService,
+        mimeTypeService
+      }
     });
   });