forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
nativeEditorProvider.ts
320 lines (287 loc) · 13.2 KB
/
nativeEditorProvider.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable } from 'inversify';
import * as uuid from 'uuid/v4';
import { CancellationToken, Disposable, Event, EventEmitter, Uri, WebviewPanel } from 'vscode';
import { arePathsSame } from '../../../datascience-ui/react-common/arePathsSame';
import {
CustomDocument,
CustomDocumentEditEvent,
CustomDocumentRevert,
CustomEditorEditingDelegate,
CustomEditorProvider,
ICustomEditorService,
IWorkspaceService
} from '../../common/application/types';
import { traceInfo } from '../../common/logger';
import {
IAsyncDisposable,
IAsyncDisposableRegistry,
IConfigurationService,
IDisposableRegistry
} from '../../common/types';
import { createDeferred } from '../../common/utils/async';
import * as localize from '../../common/utils/localize';
import { noop } from '../../common/utils/misc';
import { IServiceContainer } from '../../ioc/types';
import { captureTelemetry, sendTelemetryEvent } from '../../telemetry';
import { Telemetry } from '../constants';
import { NotebookModelChange } from '../interactive-common/interactiveWindowTypes';
import { INotebookEditor, INotebookEditorProvider, INotebookModel, INotebookStorage } from '../types';
// Class that is registered as the custom editor provider for notebooks. VS code will call into this class when
// opening an ipynb file. This class then creates a backing storage, model, and opens a view for the file.
@injectable()
export class NativeEditorProvider
implements
INotebookEditorProvider,
CustomEditorProvider,
IAsyncDisposable,
CustomEditorEditingDelegate<NotebookModelChange> {
public get onDidChangeActiveNotebookEditor(): Event<INotebookEditor | undefined> {
return this._onDidChangeActiveNotebookEditor.event;
}
public get onDidCloseNotebookEditor(): Event<INotebookEditor> {
return this._onDidCloseNotebookEditor.event;
}
public get onDidOpenNotebookEditor(): Event<INotebookEditor> {
return this._onDidOpenNotebookEditor.event;
}
public get activeEditor(): INotebookEditor | undefined {
return this.editors.find((e) => e.visible && e.active);
}
public get editingDelegate(): CustomEditorEditingDelegate<NotebookModelChange> {
return this;
}
public get onDidEdit(): Event<CustomDocumentEditEvent<NotebookModelChange>> {
return this._onDidEdit.event;
}
public get editors(): INotebookEditor[] {
return [...this.openedEditors];
}
// Note, this constant has to match the value used in the package.json to register the webview custom editor.
public static readonly customEditorViewType = 'NativeEditorProvider.ipynb';
protected readonly _onDidChangeActiveNotebookEditor = new EventEmitter<INotebookEditor | undefined>();
protected readonly _onDidOpenNotebookEditor = new EventEmitter<INotebookEditor>();
protected readonly _onDidEdit = new EventEmitter<CustomDocumentEditEvent<NotebookModelChange>>();
protected customDocuments = new Map<string, CustomDocument>();
private readonly _onDidCloseNotebookEditor = new EventEmitter<INotebookEditor>();
private openedEditors: Set<INotebookEditor> = new Set<INotebookEditor>();
private storageAndModels = new Map<string, Promise<{ model: INotebookModel; storage: INotebookStorage }>>();
private executedEditors: Set<string> = new Set<string>();
private models = new WeakSet<INotebookModel>();
private notebookCount: number = 0;
private openedNotebookCount: number = 0;
private _id = uuid();
constructor(
@inject(IServiceContainer) protected readonly serviceContainer: IServiceContainer,
@inject(IAsyncDisposableRegistry) protected readonly asyncRegistry: IAsyncDisposableRegistry,
@inject(IDisposableRegistry) protected readonly disposables: IDisposableRegistry,
@inject(IWorkspaceService) protected readonly workspace: IWorkspaceService,
@inject(IConfigurationService) protected readonly configuration: IConfigurationService,
@inject(ICustomEditorService) private customEditorService: ICustomEditorService
) {
traceInfo(`id is ${this._id}`);
asyncRegistry.push(this);
// Look through the file system for ipynb files to see how many we have in the workspace. Don't wait
// on this though.
const findFilesPromise = workspace.findFiles('**/*.ipynb');
if (findFilesPromise && findFilesPromise.then) {
findFilesPromise.then((r) => (this.notebookCount += r.length));
}
// Register for the custom editor service.
customEditorService.registerCustomEditorProvider(NativeEditorProvider.customEditorViewType, this, {
enableFindWidget: true,
retainContextWhenHidden: true
});
}
public save(document: CustomDocument, cancellation: CancellationToken): Promise<void> {
return this.loadStorage(document.uri).then(async (s) => {
if (s) {
await s.save(cancellation);
}
});
}
public saveAs(document: CustomDocument, targetResource: Uri): Promise<void> {
return this.loadStorage(document.uri).then(async (s) => {
if (s) {
await s.saveAs(targetResource);
}
});
}
public applyEdits(document: CustomDocument, edits: readonly NotebookModelChange[]): Promise<void> {
return this.loadModel(document.uri).then((s) => {
if (s) {
edits.forEach((e) => s.update({ ...e, source: 'redo' }));
}
});
}
public undoEdits(document: CustomDocument, edits: readonly NotebookModelChange[]): Promise<void> {
return this.loadModel(document.uri).then((s) => {
if (s) {
edits.forEach((e) => s.update({ ...e, source: 'undo' }));
}
});
}
public async revert(_document: CustomDocument, _edits: CustomDocumentRevert<NotebookModelChange>): Promise<void> {
noop();
}
public async backup(document: CustomDocument, cancellation: CancellationToken): Promise<void> {
return this.loadStorage(document.uri).then(async (s) => {
if (s) {
await s.backup(cancellation);
}
});
}
public async resolveCustomEditor(document: CustomDocument, panel: WebviewPanel) {
this.customDocuments.set(document.uri.fsPath, document);
await this.createNotebookEditor(document.uri, panel);
}
public async resolveCustomDocument(document: CustomDocument): Promise<void> {
this.customDocuments.set(document.uri.fsPath, document);
await this.loadStorage(document.uri);
}
public async resolveNativeEditorStorage(document: CustomDocument): Promise<INotebookStorage> {
this.customDocuments.set(document.uri.fsPath, document);
return this.loadStorage(document.uri);
}
public async dispose(): Promise<void> {
// Send a bunch of telemetry
if (this.openedNotebookCount) {
sendTelemetryEvent(Telemetry.NotebookOpenCount, undefined, { count: this.openedNotebookCount });
}
if (this.executedEditors.size) {
sendTelemetryEvent(Telemetry.NotebookRunCount, undefined, { count: this.executedEditors.size });
}
if (this.notebookCount) {
sendTelemetryEvent(Telemetry.NotebookWorkspaceCount, undefined, { count: this.notebookCount });
}
}
public async open(file: Uri): Promise<INotebookEditor> {
// Create a deferred promise that will fire when the notebook
// actually opens
const deferred = createDeferred<INotebookEditor>();
// Sign up for open event once it does open
let disposable: Disposable | undefined;
const handler = (e: INotebookEditor) => {
if (arePathsSame(e.file.fsPath, file.fsPath)) {
if (disposable) {
disposable.dispose();
}
deferred.resolve(e);
}
};
disposable = this._onDidOpenNotebookEditor.event(handler);
// Send an open command.
this.customEditorService.openEditor(file).ignoreErrors();
// Promise should resolve when the file opens.
return deferred.promise;
}
public async show(file: Uri): Promise<INotebookEditor | undefined> {
return this.open(file);
}
@captureTelemetry(Telemetry.CreateNewNotebook, undefined, false)
public async createNew(contents?: string): Promise<INotebookEditor> {
// Create a new URI for the dummy file using our root workspace path
const uri = await this.getNextNewNotebookUri();
// Update number of notebooks in the workspace
this.notebookCount += 1;
// Set these contents into the storage before the file opens
await this.loadStorage(uri, contents);
return this.open(uri);
}
protected async createNotebookEditor(resource: Uri, panel?: WebviewPanel) {
try {
// Get the model
const model = await this.loadModel(resource);
// Create a new editor
const editor = this.serviceContainer.get<INotebookEditor>(INotebookEditor);
// Load it (should already be visible)
return editor
.load(model, panel)
.then(() => this.openedEditor(editor))
.then(() => editor);
} catch (exc) {
// Send telemetry indicating a failure
sendTelemetryEvent(Telemetry.OpenNotebookFailure);
throw exc;
}
}
protected openedEditor(editor: INotebookEditor): void {
this.openedNotebookCount += 1;
if (!this.executedEditors.has(editor.file.fsPath)) {
editor.executed(this.onExecuted.bind(this));
}
this.disposables.push(editor.onDidChangeViewState(this.onChangedViewState, this));
this.openedEditors.add(editor);
editor.closed(this.closedEditor.bind(this));
this._onDidOpenNotebookEditor.fire(editor);
}
private closedEditor(editor: INotebookEditor): void {
this.openedEditors.delete(editor);
// If last editor, dispose of the storage
const key = editor.file.toString();
if (![...this.openedEditors].find((e) => e.file.toString() === key)) {
this.storageAndModels.delete(key);
}
this._onDidCloseNotebookEditor.fire(editor);
}
private onChangedViewState(): void {
this._onDidChangeActiveNotebookEditor.fire(this.activeEditor);
}
private onExecuted(editor: INotebookEditor): void {
if (editor) {
this.executedEditors.add(editor.file.fsPath);
}
}
private async modelChanged(change: NotebookModelChange) {
if (change.source === 'user' && change.kind === 'file') {
// Update our storage
const promise = this.storageAndModels.get(change.oldFile.toString());
this.storageAndModels.delete(change.oldFile.toString());
this.storageAndModels.set(change.newFile.toString(), promise!);
}
}
private async modelEdited(model: INotebookModel, change: NotebookModelChange) {
// Find the document associated with this edit.
const document = this.customDocuments.get(model.file.fsPath);
if (document) {
this._onDidEdit.fire({ document, edit: change });
}
}
private async loadModel(file: Uri, contents?: string): Promise<INotebookModel> {
const modelAndStorage = await this.loadModelAndStorage(file, contents);
return modelAndStorage.model;
}
private async loadStorage(file: Uri, contents?: string): Promise<INotebookStorage> {
const modelAndStorage = await this.loadModelAndStorage(file, contents);
return modelAndStorage.storage;
}
private loadModelAndStorage(file: Uri, contents?: string) {
const key = file.toString();
let modelPromise = this.storageAndModels.get(key);
if (!modelPromise) {
const storage = this.serviceContainer.get<INotebookStorage>(INotebookStorage);
modelPromise = storage.load(file, contents).then((m) => {
if (!this.models.has(m)) {
this.models.add(m);
this.disposables.push(m.changed(this.modelChanged.bind(this)));
this.disposables.push(storage.onDidEdit(this.modelEdited.bind(this, m)));
}
return { model: m, storage };
});
this.storageAndModels.set(key, modelPromise);
}
return modelPromise;
}
private async getNextNewNotebookUri(): Promise<Uri> {
// See if we have any untitled storage already
const untitledStorage = [...this.storageAndModels.keys()].filter((k) => Uri.parse(k).scheme === 'untitled');
// Just use the length (don't bother trying to fill in holes). We never remove storage objects from
// our map, so we'll keep creating new untitled notebooks.
const fileName = `${localize.DataScience.untitledNotebookFileName()}-${untitledStorage.length + 1}.ipynb`;
const fileUri = Uri.file(fileName);
// Turn this back into an untitled
return fileUri.with({ scheme: 'untitled', path: fileName });
}
}