Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor preferred controller for interactive #6642

Merged
merged 2 commits into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for what's the preferred should be in the manager (that's where it is today for native notebooks).

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();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change also makes it easier for #5861
Else we have to change a few places, when ideally we should have the controllers created & return from a single place.


if (!activeInterpreter) {
return;
}
this.getOrCreateController(activeInterpreter, InteractiveWindowView)?.id;
DonJayamanne marked this conversation as resolved.
Show resolved Hide resolved
}

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