Skip to content

Commit

Permalink
recover notebook backed Interactive windows (#16166)
Browse files Browse the repository at this point in the history
* recover notebook backed IWs to be re-used

* create correct type on recovery

* lint
  • Loading branch information
amunger authored Oct 28, 2024
1 parent fee0a06 commit 5c9fb46
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 60 deletions.
12 changes: 0 additions & 12 deletions src/interactive-window/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import { NotebookCell } from 'vscode';
import { dedentCode, splitLines } from '../platform/common/helpers';
import { IJupyterSettings } from '../platform/common/types';
import { appendLineFeed, removeLinesFromFrontAndBackNoConcat } from '../platform/common/utils';
import { isUri } from '../platform/common/utils/misc';
import { uncommentMagicCommands } from './editor-integration/cellFactory';
import { CellMatcher } from './editor-integration/cellMatcher';
import { InteractiveCellMetadata } from './editor-integration/types';
import { InteractiveTab } from './types';

export function getInteractiveCellMetadata(cell: NotebookCell): InteractiveCellMetadata | undefined {
if (cell.metadata.interactive !== undefined) {
Expand Down Expand Up @@ -38,13 +36,3 @@ export function generateInteractiveCode(code: string, settings: IJupyterSettings

return dedentCode(withMagicsAndLinefeeds.join(''));
}

export function isInteractiveInputTab(tab: unknown): tab is InteractiveTab {
let interactiveTab = tab as InteractiveTab;
return (
interactiveTab &&
interactiveTab.input &&
isUri(interactiveTab.input.uri) &&
isUri(interactiveTab.input.inputBoxUri)
);
}
19 changes: 8 additions & 11 deletions src/interactive-window/interactiveWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,8 @@ import { INotebookExporter } from '../kernels/jupyter/types';
import { ExportFormat } from '../notebooks/export/types';
import { generateCellsFromNotebookDocument } from './editor-integration/cellFactory';
import { CellMatcher } from './editor-integration/cellMatcher';
import {
IInteractiveWindow,
IInteractiveWindowDebugger,
IInteractiveWindowDebuggingManager,
InteractiveTab
} from './types';
import { generateInteractiveCode, isInteractiveInputTab } from './helpers';
import { IInteractiveWindow, IInteractiveWindowDebugger, IInteractiveWindowDebuggingManager } from './types';
import { generateInteractiveCode } from './helpers';
import { getInteractiveCellMetadata } from './helpers';
import { getFilePath } from '../platform/common/platform/fs-paths';
import {
Expand Down Expand Up @@ -120,7 +115,7 @@ export class InteractiveWindow implements IInteractiveWindow {
private readonly serviceContainer: IServiceContainer,
private _owner: Resource,
private readonly controllerFactory: InteractiveControllerFactory,
notebookEditorOrTab: NotebookEditor | InteractiveTab,
notebookEditorOrTabInput: NotebookEditor | TabInputNotebook | TabInputInteractiveWindow,
public readonly inputUri: Uri
) {
this.fs = this.serviceContainer.get<IFileSystem>(IFileSystem);
Expand All @@ -135,9 +130,11 @@ export class InteractiveWindow implements IInteractiveWindow {
this.debuggingManager = this.serviceContainer.get<IInteractiveWindowDebuggingManager>(
IInteractiveWindowDebuggingManager
);
this.notebookUri = isInteractiveInputTab(notebookEditorOrTab)
? notebookEditorOrTab.input.uri
: notebookEditorOrTab.notebook.uri;
this.notebookUri =
notebookEditorOrTabInput instanceof TabInputInteractiveWindow ||
notebookEditorOrTabInput instanceof TabInputNotebook
? notebookEditorOrTabInput.uri
: notebookEditorOrTabInput.notebook.uri;

// Set our owner and first submitter
if (this._owner) {
Expand Down
46 changes: 28 additions & 18 deletions src/interactive-window/interactiveWindowProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
NotebookControllerAffinity,
NotebookDocument,
NotebookEditor,
TabInputInteractiveWindow,
TabInputNotebook,
Uri,
ViewColumn,
commands,
Expand Down Expand Up @@ -48,14 +50,12 @@ import {
IInteractiveWindow,
IInteractiveWindowCache,
IInteractiveWindowProvider,
INativeInteractiveWindow,
InteractiveTab
INativeInteractiveWindow
} from './types';
import { getInteractiveWindowTitle } from './identity';
import { createDeferred } from '../platform/common/utils/async';
import { getDisplayPath } from '../platform/common/platform/fs-paths';
import { IVSCodeNotebookController } from '../notebooks/controllers/types';
import { isInteractiveInputTab } from './helpers';
import { sendTelemetryEvent } from '../telemetry';
import { InteractiveControllerFactory } from './InteractiveWindowController';
import { NotebookInteractiveWindow } from './notebookInteractiveWindow';
Expand Down Expand Up @@ -116,11 +116,12 @@ export class InteractiveWindowProvider implements IInteractiveWindowProvider, IE

private restoreWindows() {
// VS Code controls if interactive windows are restored.
const interactiveWindowMapping = new Map<string, InteractiveTab>();
const interactiveWindowMapping = new Map<string, TabInputInteractiveWindow | TabInputNotebook>();
window.tabGroups.all.forEach((group) => {
group.tabs.forEach((tab) => {
if (isInteractiveInputTab(tab) && tab.input.uri) {
interactiveWindowMapping.set(tab.input.uri.toString(), tab);
const input = tab.input;
if (input instanceof TabInputInteractiveWindow || input instanceof TabInputNotebook) {
interactiveWindowMapping.set(input.uri.toString(), input);
}
});
});
Expand All @@ -130,21 +131,30 @@ export class InteractiveWindowProvider implements IInteractiveWindowProvider, IE
return;
}

const tab = interactiveWindowMapping.get(iw.uriString);
const tabInput = interactiveWindowMapping.get(iw.uriString);

if (!tab) {
if (!tabInput) {
return;
}

const mode = this.configService.getSettings(tab.input.uri).interactiveWindowMode;

const result = new InteractiveWindow(
this.serviceContainer,
Uri.parse(iw.owner),
new InteractiveControllerFactory(this.controllerHelper, mode),
tab,
Uri.parse(iw.inputBoxUriString)
);
const mode = this.configService.getSettings(tabInput.uri).interactiveWindowMode;

const result =
tabInput instanceof TabInputInteractiveWindow
? new InteractiveWindow(
this.serviceContainer,
Uri.parse(iw.owner),
new InteractiveControllerFactory(this.controllerHelper, mode),
tabInput,
Uri.parse(iw.inputBoxUriString)
)
: new NotebookInteractiveWindow(
this.serviceContainer,
Uri.parse(iw.owner),
new InteractiveControllerFactory(this.controllerHelper, mode),
tabInput,
Uri.parse(iw.inputBoxUriString)
);

result.notifyConnectionReset().catch(noop);

Expand Down Expand Up @@ -315,7 +325,7 @@ export class InteractiveWindowProvider implements IInteractiveWindowProvider, IE

const editor = await window.showNotebookDocument(notebookDocument, {
// currently, to set the controller, we need to focus the editor
preserveFocus: !!controller ? true : preserveFocus,
preserveFocus: !!controller ? false : preserveFocus,
viewColumn,
asRepl: title
});
Expand Down
20 changes: 1 addition & 19 deletions src/interactive-window/types.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import {
Disposable,
Event,
NotebookCell,
NotebookController,
NotebookDocument,
NotebookEditor,
Tab,
Uri
} from 'vscode';
import { Disposable, Event, NotebookCell, NotebookController, NotebookDocument, NotebookEditor, Uri } from 'vscode';
import { IDebuggingManager } from '../notebooks/debugger/debuggingTypes';
import { IKernel, KernelConnectionMetadata } from '../kernels/types';
import { Resource, InteractiveWindowMode } from '../platform/common/types';
Expand Down Expand Up @@ -117,15 +108,6 @@ export interface IInteractiveWindowCache {
inputBoxUriString: string;
}

export interface TabInputInteractiveWindow {
readonly uri: Uri;
readonly inputBoxUri: Uri;
}

export interface InteractiveTab extends Tab {
readonly input: TabInputInteractiveWindow;
}

export const IInteractiveWindowDebuggingManager = Symbol('IInteractiveWindowDebuggingManager');
export interface IInteractiveWindowDebuggingManager extends IDebuggingManager {
start(notebook: NotebookDocument, cell: NotebookCell): Promise<void>;
Expand Down

0 comments on commit 5c9fb46

Please sign in to comment.