Skip to content

Commit

Permalink
Refactor preferred controller for interactive
Browse files Browse the repository at this point in the history
  • Loading branch information
DonJayamanne committed Jul 12, 2021
1 parent d20d44f commit d4c1a5d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'use strict';
import { inject, injectable, named } from 'inversify';
import { ConfigurationTarget, Event, EventEmitter, Memento, workspace, window, ViewColumn } from 'vscode';
import { IPythonApiProvider, IPythonExtensionChecker } from '../../api/types';
import { IPythonExtensionChecker } from '../../api/types';

import {
IApplicationShell,
Expand All @@ -30,7 +30,6 @@ import { noop } from '../../common/utils/misc';
import { IServiceContainer } from '../../ioc/types';
import { IExportDialog } from '../export/types';
import { IKernelProvider } from '../jupyter/kernels/types';
import { InteractiveWindowView } from '../notebook/constants';
import { INotebookControllerManager } from '../notebook/types';
import {
IInteractiveWindow,
Expand Down Expand Up @@ -77,8 +76,7 @@ export class NativeInteractiveWindowProvider implements IInteractiveWindowProvid
@inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService,
@inject(IKernelProvider) private readonly kernelProvider: IKernelProvider,
@inject(INotebookControllerManager) private readonly notebookControllerManager: INotebookControllerManager,
@inject(ICommandManager) private readonly commandManager: ICommandManager,
@inject(IPythonApiProvider) private readonly pythonApi: IPythonApiProvider
@inject(ICommandManager) private readonly commandManager: ICommandManager
) {
asyncRegistry.push(this);
}
Expand Down Expand Up @@ -238,19 +236,8 @@ export class NativeInteractiveWindowProvider implements IInteractiveWindowProvid
}

private async getControllerForInteractiveWindow(): Promise<string | undefined> {
// Fetch the active interpreter and use the matching controller
const api = await this.pythonApi.getApi();
const activeInterpreter = await api.getActiveInterpreter();

if (!activeInterpreter) {
return;
}
const preferredController = this.notebookControllerManager.getOrCreateController(
activeInterpreter,
InteractiveWindowView
);

return preferredController !== undefined ? `${JVSC_EXTENSION_ID}/${preferredController.id}` : undefined;
const preferredController = await this.notebookControllerManager.getInteractiveController();
return preferredController ? `${JVSC_EXTENSION_ID}/${preferredController.id}` : undefined;
}

// TODO: we don't currently have a way to know when the VS Code InteractiveEditor
Expand Down
15 changes: 13 additions & 2 deletions src/client/datascience/notebook/notebookControllerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { sendTelemetryEvent } from '../../telemetry';
import { NotebookCellLanguageService } from './cellLanguageService';
import { sendKernelListTelemetry } from '../telemetry/kernelTelemetry';
import { noop } from '../../common/utils/misc';
import { IPythonExtensionChecker } from '../../api/types';
import { IPythonApiProvider, IPythonExtensionChecker } from '../../api/types';
import { PythonEnvironment } from '../../pythonEnvironments/info';
import { isCI } from '../../common/constants';
/**
Expand Down Expand Up @@ -77,7 +77,8 @@ export class NotebookControllerManager implements INotebookControllerManager, IE
@inject(NotebookCellLanguageService) private readonly languageService: NotebookCellLanguageService,
@inject(IWorkspaceService) private readonly workspace: IWorkspaceService,
@inject(IPythonExtensionChecker) private readonly extensionChecker: IPythonExtensionChecker,
@inject(IDocumentManager) private readonly docManager: IDocumentManager
@inject(IDocumentManager) private readonly docManager: IDocumentManager,
@inject(IPythonApiProvider) private readonly pythonApi: IPythonApiProvider
) {
this._onNotebookControllerSelected = new EventEmitter<{
notebook: NotebookDocument;
Expand All @@ -86,6 +87,16 @@ export class NotebookControllerManager implements INotebookControllerManager, IE
this.disposables.push(this._onNotebookControllerSelected);
this.isLocalLaunch = isLocalLaunch(this.configuration);
}
public async getInteractiveController(): Promise<VSCodeNotebookController | undefined> {
// Fetch the active interpreter and use the matching controller
const api = await this.pythonApi.getApi();
const activeInterpreter = await api.getActiveInterpreter();

if (!activeInterpreter) {
return;
}
this.getOrCreateController(activeInterpreter, InteractiveWindowView)?.id;
}

get onNotebookControllerSelected() {
return this._onNotebookControllerSelected.event;
Expand Down
6 changes: 1 addition & 5 deletions src/client/datascience/notebook/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { Event, NotebookDocument, NotebookEditor, Uri } from 'vscode';
import { PythonEnvironment } from '../../pythonEnvironments/info';
import { VSCodeNotebookController } from './vscodeNotebookController';

export const INotebookKernelResolver = Symbol('INotebookKernelResolver');
Expand All @@ -13,10 +12,7 @@ export interface INotebookControllerManager {
getSelectedNotebookController(document: NotebookDocument): VSCodeNotebookController | undefined;
// Marked test only, just for tests to access registered controllers
registeredNotebookControllers(): VSCodeNotebookController[];
getOrCreateController(
pythonInterpreter: PythonEnvironment,
notebookType: 'interactive' | 'jupyter-notebook'
): VSCodeNotebookController | undefined;
getInteractiveController(): Promise<VSCodeNotebookController | undefined>;
}
export enum CellOutputMimeTypes {
error = 'application/vnd.code.notebook.error',
Expand Down

0 comments on commit d4c1a5d

Please sign in to comment.