forked from microsoft/vscode-python
/
interactiveWindow.ts
441 lines (395 loc) · 17.1 KB
/
interactiveWindow.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import type { nbformat } from '@jupyterlab/coreutils';
import { inject, injectable, multiInject, named } from 'inversify';
import * as path from 'path';
import { Event, EventEmitter, Memento, Uri, ViewColumn } from 'vscode';
import {
IApplicationShell,
ICommandManager,
IDocumentManager,
ILiveShareApi,
IWebPanelProvider,
IWorkspaceService
} from '../../common/application/types';
import { UseCustomEditorApi } from '../../common/constants';
import { ContextKey } from '../../common/contextKey';
import '../../common/extensions';
import { traceError } from '../../common/logger';
import { IFileSystem } from '../../common/platform/types';
import {
GLOBAL_MEMENTO,
IConfigurationService,
IDisposableRegistry,
IExperimentService,
IExperimentsManager,
IMemento,
IPersistentStateFactory,
Resource,
WORKSPACE_MEMENTO
} from '../../common/types';
import * as localize from '../../common/utils/localize';
import { EXTENSION_ROOT_DIR } from '../../constants';
import { PythonInterpreter } from '../../pythonEnvironments/info';
import { captureTelemetry, sendTelemetryEvent } from '../../telemetry';
import { EditorContexts, Identifiers, Telemetry } from '../constants';
import { IDataViewerFactory } from '../data-viewing/types';
import { InteractiveBase } from '../interactive-common/interactiveBase';
import {
INotebookIdentity,
InteractiveWindowMessages,
ISubmitNewCell,
NotebookModelChange,
SysInfoReason
} from '../interactive-common/interactiveWindowTypes';
import { KernelSwitcher } from '../jupyter/kernels/kernelSwitcher';
import {
ICell,
ICodeCssGenerator,
IDataScienceErrorHandler,
IInteractiveWindow,
IInteractiveWindowInfo,
IInteractiveWindowListener,
IInteractiveWindowProvider,
IJupyterDebugger,
IJupyterKernelSpec,
IJupyterVariableDataProviderFactory,
IJupyterVariables,
INotebookExporter,
INotebookProvider,
IStatusProvider,
IThemeFinder,
WebViewViewChangeEventArgs
} from '../types';
const historyReactDir = path.join(EXTENSION_ROOT_DIR, 'out', 'datascience-ui', 'notebook');
@injectable()
export class InteractiveWindow extends InteractiveBase implements IInteractiveWindow {
public get onDidChangeViewState(): Event<void> {
return this._onDidChangeViewState.event;
}
public get visible(): boolean {
return this.viewState.visible;
}
public get active(): boolean {
return this.viewState.active;
}
public get closed(): Event<IInteractiveWindow> {
return this.closedEvent.event;
}
private _onDidChangeViewState = new EventEmitter<void>();
private closedEvent: EventEmitter<IInteractiveWindow> = new EventEmitter<IInteractiveWindow>();
private waitingForExportCells: boolean = false;
private trackedJupyterStart: boolean = false;
private lastFile: string | undefined;
constructor(
@multiInject(IInteractiveWindowListener) listeners: IInteractiveWindowListener[],
@inject(ILiveShareApi) liveShare: ILiveShareApi,
@inject(IApplicationShell) applicationShell: IApplicationShell,
@inject(IDocumentManager) documentManager: IDocumentManager,
@inject(IStatusProvider) statusProvider: IStatusProvider,
@inject(IWebPanelProvider) provider: IWebPanelProvider,
@inject(IDisposableRegistry) disposables: IDisposableRegistry,
@inject(ICodeCssGenerator) cssGenerator: ICodeCssGenerator,
@inject(IThemeFinder) themeFinder: IThemeFinder,
@inject(IFileSystem) fileSystem: IFileSystem,
@inject(IConfigurationService) configuration: IConfigurationService,
@inject(ICommandManager) commandManager: ICommandManager,
@inject(INotebookExporter) jupyterExporter: INotebookExporter,
@inject(IWorkspaceService) workspaceService: IWorkspaceService,
@inject(IInteractiveWindowProvider) private interactiveWindowProvider: IInteractiveWindowProvider,
@inject(IDataViewerFactory) dataExplorerFactory: IDataViewerFactory,
@inject(IJupyterVariableDataProviderFactory)
jupyterVariableDataProviderFactory: IJupyterVariableDataProviderFactory,
@inject(IJupyterVariables) @named(Identifiers.ALL_VARIABLES) jupyterVariables: IJupyterVariables,
@inject(IJupyterDebugger) jupyterDebugger: IJupyterDebugger,
@inject(IDataScienceErrorHandler) errorHandler: IDataScienceErrorHandler,
@inject(IPersistentStateFactory) private readonly stateFactory: IPersistentStateFactory,
@inject(IMemento) @named(GLOBAL_MEMENTO) globalStorage: Memento,
@inject(IMemento) @named(WORKSPACE_MEMENTO) workspaceStorage: Memento,
@inject(IExperimentsManager) experimentsManager: IExperimentsManager,
@inject(KernelSwitcher) switcher: KernelSwitcher,
@inject(INotebookProvider) notebookProvider: INotebookProvider,
@inject(UseCustomEditorApi) useCustomEditorApi: boolean,
@inject(IExperimentService) expService: IExperimentService
) {
super(
listeners,
liveShare,
applicationShell,
documentManager,
provider,
disposables,
cssGenerator,
themeFinder,
statusProvider,
fileSystem,
configuration,
jupyterExporter,
workspaceService,
dataExplorerFactory,
jupyterVariableDataProviderFactory,
jupyterVariables,
jupyterDebugger,
errorHandler,
commandManager,
globalStorage,
workspaceStorage,
historyReactDir,
[
path.join(historyReactDir, 'require.js'),
path.join(historyReactDir, 'ipywidgets.js'),
path.join(historyReactDir, 'monaco.bundle.js'),
path.join(historyReactDir, 'commons.initial.bundle.js'),
path.join(historyReactDir, 'interactiveWindow.js')
],
localize.DataScience.historyTitle(),
ViewColumn.Two,
experimentsManager,
switcher,
notebookProvider,
useCustomEditorApi,
expService
);
// Send a telemetry event to indicate window is opening
sendTelemetryEvent(Telemetry.OpenedInteractiveWindow);
}
public dispose() {
const promise = super.dispose();
if (this.closedEvent) {
this.closedEvent.fire(this);
}
return promise;
}
public addMessage(message: string): Promise<void> {
this.addMessageImpl(message);
return Promise.resolve();
}
public async show(): Promise<void> {
// Start the server as soon as we open
this.ensureConnectionAndNotebook().ignoreErrors();
// When showing we have to load the web panel. Make sure
// we use the last file sent through add code.
await this.loadWebPanel(this.lastFile ? path.dirname(this.lastFile) : process.cwd());
// Make sure we're loaded first. InteractiveWindow doesn't makes sense without an active server.
await this.ensureConnectionAndNotebook();
// Make sure we have at least the initial sys info
await this.addSysInfo(SysInfoReason.Start);
// Then show our web panel.
return super.show();
}
public async addCode(code: string, file: string, line: number): Promise<boolean> {
return this.addOrDebugCode(code, file, line, false);
}
public exportCells() {
// First ask for all cells. Set state to indicate waiting for result
this.waitingForExportCells = true;
// Telemetry will fire when the export function is called.
this.postMessage(InteractiveWindowMessages.GetAllCells).ignoreErrors();
}
// tslint:disable-next-line: no-any
public onMessage(message: string, payload: any) {
super.onMessage(message, payload);
switch (message) {
case InteractiveWindowMessages.Export:
this.handleMessage(message, payload, this.export);
break;
case InteractiveWindowMessages.ReturnAllCells:
this.handleMessage(message, payload, this.handleReturnAllCells);
break;
case InteractiveWindowMessages.UpdateModel:
this.handleMessage(message, payload, this.handleModelChange);
break;
default:
break;
}
}
public async debugCode(code: string, file: string, line: number): Promise<boolean> {
let saved = true;
// Make sure the file is saved before debugging
const doc = this.documentManager.textDocuments.find((d) => this.fileSystem.arePathsSame(d.fileName, file));
if (doc && doc.isUntitled) {
// Before we start, get the list of documents
const beforeSave = [...this.documentManager.textDocuments];
saved = await doc.save();
// If that worked, we have to open the new document. It should be
// the new entry in the list
if (saved) {
const diff = this.documentManager.textDocuments.filter((f) => beforeSave.indexOf(f) === -1);
if (diff && diff.length > 0) {
file = diff[0].fileName;
// Open the new document
await this.documentManager.openTextDocument(file);
}
}
}
// Call the internal method if we were able to save
if (saved) {
return this.addOrDebugCode(code, file, line, true);
}
return false;
}
@captureTelemetry(Telemetry.ExpandAll)
public expandAllCells() {
this.postMessage(InteractiveWindowMessages.ExpandAll).ignoreErrors();
}
@captureTelemetry(Telemetry.CollapseAll)
public collapseAllCells() {
this.postMessage(InteractiveWindowMessages.CollapseAll).ignoreErrors();
}
@captureTelemetry(Telemetry.ScrolledToCell)
public scrollToCell(id: string): void {
this.postMessage(InteractiveWindowMessages.ScrollToCell, { id }).ignoreErrors();
}
public async getOwningResource(): Promise<Resource> {
if (this.lastFile) {
return Uri.file(this.lastFile);
}
const root = this.workspaceService.rootPath;
if (root) {
return Uri.file(root);
}
return undefined;
}
protected async addSysInfo(reason: SysInfoReason): Promise<void> {
await super.addSysInfo(reason);
// If `reason == Start`, then this means UI has been updated with the last
// pience of informaiotn (which was sys info), and now UI can be deemed as having been loaded.
// Marking a UI as having been loaded is done by sending a message `LoadAllCells`, even though we're not loading any cells.
// We're merely using existing messages (from NativeEditor).
if (reason === SysInfoReason.Start) {
this.postMessage(InteractiveWindowMessages.LoadAllCells, { cells: [] }).ignoreErrors();
}
}
protected async onViewStateChanged(args: WebViewViewChangeEventArgs) {
super.onViewStateChanged(args);
this._onDidChangeViewState.fire();
}
@captureTelemetry(Telemetry.SubmitCellThroughInput, undefined, false)
// tslint:disable-next-line:no-any
protected submitNewCell(info: ISubmitNewCell) {
// If there's any payload, it has the code and the id
if (info && info.code && info.id) {
// Send to ourselves.
this.submitCode(info.code, Identifiers.EmptyFileName, 0, info.id).ignoreErrors();
// Activate the other side, and send as if came from a file
this.interactiveWindowProvider
.getOrCreateActive()
.then((_v) => {
this.shareMessage(InteractiveWindowMessages.RemoteAddCode, {
code: info.code,
file: Identifiers.EmptyFileName,
line: 0,
id: info.id,
originator: this.id,
debug: false
});
})
.ignoreErrors();
}
}
protected async getNotebookMetadata(): Promise<nbformat.INotebookMetadata | undefined> {
return undefined;
}
protected async updateNotebookOptions(
_kernelSpec: IJupyterKernelSpec,
_interpreter: PythonInterpreter | undefined
): Promise<void> {
// Do nothing as this data isn't stored in our options.
}
protected async getNotebookIdentity(): Promise<INotebookIdentity> {
// Always the same identity (for now)
return {
resource: Uri.parse(Identifiers.InteractiveWindowIdentity),
type: 'interactive'
};
}
protected updateContexts(info: IInteractiveWindowInfo | undefined) {
// This should be called by the python interactive window every
// time state changes. We use this opportunity to update our
// extension contexts
if (this.commandManager && this.commandManager.executeCommand) {
const interactiveContext = new ContextKey(EditorContexts.HaveInteractive, this.commandManager);
interactiveContext.set(!this.isDisposed).catch();
const interactiveCellsContext = new ContextKey(EditorContexts.HaveInteractiveCells, this.commandManager);
const redoableContext = new ContextKey(EditorContexts.HaveRedoableCells, this.commandManager);
const hasCellSelectedContext = new ContextKey(EditorContexts.HaveCellSelected, this.commandManager);
if (info) {
interactiveCellsContext.set(info.cellCount > 0).catch();
redoableContext.set(info.redoCount > 0).catch();
hasCellSelectedContext.set(info.selectedCell ? true : false).catch();
} else {
interactiveCellsContext.set(false).catch();
redoableContext.set(false).catch();
hasCellSelectedContext.set(false).catch();
}
}
}
protected async closeBecauseOfFailure(_exc: Error): Promise<void> {
this.dispose();
}
protected ensureConnectionAndNotebook(): Promise<void> {
// Keep track of users who have used interactive window in a worksapce folder.
// To be used if/when changing workflows related to startup of jupyter.
if (!this.trackedJupyterStart) {
this.trackedJupyterStart = true;
const store = this.stateFactory.createGlobalPersistentState('INTERACTIVE_WINDOW_USED', false);
store.updateValue(true).ignoreErrors();
}
return super.ensureConnectionAndNotebook();
}
private async addOrDebugCode(code: string, file: string, line: number, debug: boolean): Promise<boolean> {
if (this.lastFile && !this.fileSystem.arePathsSame(file, this.lastFile)) {
sendTelemetryEvent(Telemetry.NewFileForInteractiveWindow);
}
// Save the last file we ran with.
this.lastFile = file;
// Make sure our web panel opens.
await this.show();
// Tell the webpanel about the new directory.
this.updateCwd(path.dirname(file));
// Call the internal method.
return this.submitCode(code, file, line, undefined, undefined, debug ? { runByLine: false } : undefined);
}
@captureTelemetry(Telemetry.ExportNotebookInteractive, undefined, false)
// tslint:disable-next-line: no-any no-empty
private async export(cells: ICell[]) {
// Should be an array of cells
if (cells && this.applicationShell) {
// Indicate busy
this.startProgress();
try {
const filtersKey = localize.DataScience.exportDialogFilter();
const filtersObject: Record<string, string[]> = {};
filtersObject[filtersKey] = ['ipynb'];
// Bring up the open file dialog box
const uri = await this.applicationShell.showSaveDialog({
saveLabel: localize.DataScience.exportDialogTitle(),
filters: filtersObject
});
if (uri) {
await this.jupyterExporter.exportToFile(cells, uri.fsPath);
}
} finally {
this.stopProgress();
}
}
}
private handleModelChange(update: NotebookModelChange) {
// Send telemetry for delete and delete all. We don't send telemetry for the other updates yet
if (update.source === 'user') {
if (update.kind === 'remove_all') {
sendTelemetryEvent(Telemetry.DeleteAllCells);
} else if (update.kind === 'remove') {
sendTelemetryEvent(Telemetry.DeleteCell);
}
}
}
// tslint:disable-next-line:no-any
private handleReturnAllCells(cells: ICell[]) {
// See what we're waiting for.
if (this.waitingForExportCells) {
this.export(cells).catch((ex) => traceError('Error exporting:', ex));
}
}
}