|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +import { inject, injectable } from 'inversify'; |
| 7 | +import { Event, EventEmitter, notebook, NotebookDocument, Uri } from 'vscode'; |
| 8 | +import { IExtensionSingleActivationService } from '../../activation/types'; |
| 9 | +import { ICommandManager, IWorkspaceService } from '../../common/application/types'; |
| 10 | +import '../../common/extensions'; |
| 11 | +import { IDisposableRegistry } from '../../common/types'; |
| 12 | +import { createDeferred, Deferred } from '../../common/utils/async'; |
| 13 | +import { isUri } from '../../common/utils/misc'; |
| 14 | +import { sendTelemetryEvent } from '../../telemetry'; |
| 15 | +import { Telemetry } from '../constants'; |
| 16 | +import { INotebookStorageProvider } from '../interactive-ipynb/notebookStorageProvider'; |
| 17 | +import { INotebookEditor, INotebookEditorProvider } from '../types'; |
| 18 | +import { monitorModelCellOutputChangesAndUpdateNotebookDocument } from './executionHelpers'; |
| 19 | +import { NotebookEditor } from './notebookEditor'; |
| 20 | + |
| 21 | +/** |
| 22 | + * Class responsbile for activating an registering the necessary event handlers in NotebookEditorProvider. |
| 23 | + */ |
| 24 | +@injectable() |
| 25 | +export class NotebookEditorProviderActivation implements IExtensionSingleActivationService { |
| 26 | + constructor(@inject(INotebookEditorProvider) private readonly provider: INotebookEditorProvider) {} |
| 27 | + public async activate(): Promise<void> { |
| 28 | + // The whole purpose is to ensure the NotebookEditorProvider class activates as soon as extension loads. |
| 29 | + // tslint:disable-next-line: no-use-before-declare |
| 30 | + if (this.provider instanceof NotebookEditorProvider) { |
| 31 | + this.provider.activate(); |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Notebook Editor provider used by other parts of DS code. |
| 38 | + * This is an adapter, that takes the VSCode api for editors (did notebook editors open, close save, etc) and |
| 39 | + * then exposes them in a manner we expect - i.e. INotebookEditorProvider. |
| 40 | + * This is also responsible for tracking all notebooks that open and then keeping the VS Code notebook models updated with changes we made to our underlying model. |
| 41 | + * E.g. when cells are executed the results in our model is updated, this tracks those changes and syncs VSC cells with those updates. |
| 42 | + */ |
| 43 | +@injectable() |
| 44 | +export class NotebookEditorProvider implements INotebookEditorProvider { |
| 45 | + public get onDidChangeActiveNotebookEditor(): Event<INotebookEditor | undefined> { |
| 46 | + return this._onDidChangeActiveNotebookEditor.event; |
| 47 | + } |
| 48 | + public get onDidCloseNotebookEditor(): Event<INotebookEditor> { |
| 49 | + return this._onDidCloseNotebookEditor.event; |
| 50 | + } |
| 51 | + public get onDidOpenNotebookEditor(): Event<INotebookEditor> { |
| 52 | + return this._onDidOpenNotebookEditor.event; |
| 53 | + } |
| 54 | + public get activeEditor(): INotebookEditor | undefined { |
| 55 | + return this.editors.find((e) => e.visible && e.active); |
| 56 | + } |
| 57 | + public get editors(): INotebookEditor[] { |
| 58 | + return [...this.openedEditors]; |
| 59 | + } |
| 60 | + // Note, this constant has to match the value used in the package.json to register the webview custom editor. |
| 61 | + public static readonly customEditorViewType = 'NativeEditorProvider.ipynb'; |
| 62 | + protected readonly _onDidChangeActiveNotebookEditor = new EventEmitter<INotebookEditor | undefined>(); |
| 63 | + protected readonly _onDidOpenNotebookEditor = new EventEmitter<INotebookEditor>(); |
| 64 | + private readonly _onDidCloseNotebookEditor = new EventEmitter<INotebookEditor>(); |
| 65 | + private openedEditors: Set<INotebookEditor> = new Set<INotebookEditor>(); |
| 66 | + private executedEditors: Set<string> = new Set<string>(); |
| 67 | + private notebookCount: number = 0; |
| 68 | + private openedNotebookCount: number = 0; |
| 69 | + private readonly notebookEditors = new Map<NotebookDocument, INotebookEditor>(); |
| 70 | + private readonly notebookEditorsByUri = new Map<string, INotebookEditor>(); |
| 71 | + private readonly notebooksWaitingToBeOpenedByUri = new Map<string, Deferred<INotebookEditor>>(); |
| 72 | + constructor( |
| 73 | + @inject(INotebookStorageProvider) private readonly storage: INotebookStorageProvider, |
| 74 | + @inject(IWorkspaceService) private readonly workspace: IWorkspaceService, |
| 75 | + @inject(ICommandManager) private readonly commandManager: ICommandManager, |
| 76 | + @inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry |
| 77 | + ) { |
| 78 | + disposables.push(this); |
| 79 | + } |
| 80 | + public activate() { |
| 81 | + this.disposables.push(notebook.onDidOpenNotebookDocument(this.onDidOpenNotebookDocument, this)); |
| 82 | + this.disposables.push(notebook.onDidCloseNotebookDocument(this.onDidCloseNotebookDocument, this)); |
| 83 | + |
| 84 | + // Look through the file system for ipynb files to see how many we have in the workspace. Don't wait |
| 85 | + // on this though. |
| 86 | + const findFilesPromise = this.workspace.findFiles('**/*.ipynb'); |
| 87 | + if (findFilesPromise && findFilesPromise.then) { |
| 88 | + findFilesPromise.then((r) => (this.notebookCount += r.length)); |
| 89 | + } |
| 90 | + } |
| 91 | + public dispose() { |
| 92 | + // Send a bunch of telemetry |
| 93 | + if (this.openedNotebookCount) { |
| 94 | + sendTelemetryEvent(Telemetry.NotebookOpenCount, undefined, { count: this.openedNotebookCount }); |
| 95 | + } |
| 96 | + if (this.executedEditors.size) { |
| 97 | + sendTelemetryEvent(Telemetry.NotebookRunCount, undefined, { count: this.executedEditors.size }); |
| 98 | + } |
| 99 | + if (this.notebookCount) { |
| 100 | + sendTelemetryEvent(Telemetry.NotebookWorkspaceCount, undefined, { count: this.notebookCount }); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public async open(file: Uri): Promise<INotebookEditor> { |
| 105 | + // Wait for editor to get opened up, vscode will notify when it is opened. |
| 106 | + // Further below. |
| 107 | + const deferred = createDeferred<INotebookEditor>(); |
| 108 | + this.notebooksWaitingToBeOpenedByUri.set(file.toString(), deferred); |
| 109 | + deferred.promise.then(() => this.notebooksWaitingToBeOpenedByUri.delete(file.toString())).ignoreErrors(); |
| 110 | + await this.commandManager.executeCommand('vscode.open', file); |
| 111 | + return deferred.promise; |
| 112 | + } |
| 113 | + public async show(_file: Uri): Promise<INotebookEditor | undefined> { |
| 114 | + // We do not need this. |
| 115 | + throw new Error('Not supported'); |
| 116 | + } |
| 117 | + public async createNew(_contents?: string): Promise<INotebookEditor> { |
| 118 | + // tslint:disable-next-line: no-suspicious-comment |
| 119 | + // TODO: In another branch. |
| 120 | + // const model = await this.storage.createNew(contents); |
| 121 | + // await this.onDidOpenNotebookDocument(model.file); |
| 122 | + // tslint:disable-next-line: no-suspicious-comment |
| 123 | + // TODO: Need to do this. |
| 124 | + // Update number of notebooks in the workspace |
| 125 | + // this.notebookCount += 1; |
| 126 | + // return this.open(model.file); |
| 127 | + // tslint:disable-next-line: no-any |
| 128 | + return undefined as any; |
| 129 | + } |
| 130 | + protected openedEditor(editor: INotebookEditor): void { |
| 131 | + this.openedNotebookCount += 1; |
| 132 | + if (!this.executedEditors.has(editor.file.fsPath)) { |
| 133 | + editor.executed(this.onExecuted.bind(this)); |
| 134 | + } |
| 135 | + this.disposables.push(editor.onDidChangeViewState(this.onChangedViewState, this)); |
| 136 | + this.openedEditors.add(editor); |
| 137 | + editor.closed(this.closedEditor.bind(this)); |
| 138 | + this._onDidOpenNotebookEditor.fire(editor); |
| 139 | + } |
| 140 | + |
| 141 | + private closedEditor(editor: INotebookEditor): void { |
| 142 | + this.openedEditors.delete(editor); |
| 143 | + this._onDidCloseNotebookEditor.fire(editor); |
| 144 | + } |
| 145 | + private onChangedViewState(): void { |
| 146 | + this._onDidChangeActiveNotebookEditor.fire(this.activeEditor); |
| 147 | + } |
| 148 | + |
| 149 | + private onExecuted(editor: INotebookEditor): void { |
| 150 | + if (editor) { |
| 151 | + this.executedEditors.add(editor.file.fsPath); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private async onDidOpenNotebookDocument(doc: NotebookDocument | Uri): Promise<void> { |
| 156 | + const uri = isUri(doc) ? doc : doc.uri; |
| 157 | + const model = await this.storage.load(uri); |
| 158 | + // In open method we might be waiting. |
| 159 | + const editor = this.notebookEditorsByUri.get(uri.toString()) || new NotebookEditor(model); |
| 160 | + const deferred = this.notebooksWaitingToBeOpenedByUri.get(uri.toString()); |
| 161 | + deferred?.resolve(editor); // NOSONAR |
| 162 | + if (!isUri(doc)) { |
| 163 | + // This is where we ensure changes to our models are propagated back to the VSCode model. |
| 164 | + this.disposables.push(monitorModelCellOutputChangesAndUpdateNotebookDocument(doc, model)); |
| 165 | + this.notebookEditors.set(doc, editor); |
| 166 | + } |
| 167 | + this.notebookEditorsByUri.set(uri.toString(), editor); |
| 168 | + } |
| 169 | + private async onDidCloseNotebookDocument(doc: NotebookDocument | Uri): Promise<void> { |
| 170 | + const editor = isUri(doc) ? this.notebookEditorsByUri.get(doc.toString()) : this.notebookEditors.get(doc); |
| 171 | + if (editor) { |
| 172 | + editor.dispose(); |
| 173 | + if (editor.model) { |
| 174 | + editor.model.dispose(); |
| 175 | + } |
| 176 | + } |
| 177 | + if (isUri(doc)) { |
| 178 | + this.notebookEditorsByUri.delete(doc.toString()); |
| 179 | + } else { |
| 180 | + this.notebookEditors.delete(doc); |
| 181 | + this.notebookEditorsByUri.delete(doc.uri.toString()); |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments