Skip to content

Commit 55ac452

Browse files
committed
webview id attached to notebook editor
1 parent a14736c commit 55ac452

13 files changed

Lines changed: 122 additions & 106 deletions

src/vs/workbench/api/browser/mainThreadNotebook.ts

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { extHostNamedCustomer } from 'vs/workbench/api/common/extHostCustomers';
7-
import { MainContext, MainThreadNotebookShape, NotebookExtensionDescription, IExtHostContext, ExtHostNotebookShape, ExtHostContext, INotebookDocumentsAndEditorsDelta } from '../common/extHost.protocol';
7+
import { MainContext, MainThreadNotebookShape, NotebookExtensionDescription, IExtHostContext, ExtHostNotebookShape, ExtHostContext, INotebookDocumentsAndEditorsDelta, INotebookModelAddedData } from '../common/extHost.protocol';
88
import { Disposable, IDisposable, combinedDisposable } from 'vs/base/common/lifecycle';
99
import { URI, UriComponents } from 'vs/base/common/uri';
1010
import { INotebookService, IMainNotebookController } from 'vs/workbench/contrib/notebook/common/notebookService';
@@ -17,7 +17,6 @@ import { CancellationToken } from 'vs/base/common/cancellation';
1717
import { IAccessibilityService } from 'vs/platform/accessibility/common/accessibility';
1818
import { IRelativePattern } from 'vs/base/common/glob';
1919
import { ExtensionIdentifier } from 'vs/platform/extensions/common/extensions';
20-
import { generateUuid } from 'vs/base/common/uuid';
2120

2221
export class MainThreadNotebookDocument extends Disposable {
2322
private _textModel: NotebookTextModel;
@@ -30,11 +29,10 @@ export class MainThreadNotebookDocument extends Disposable {
3029
private readonly _proxy: ExtHostNotebookShape,
3130
public handle: number,
3231
public viewType: string,
33-
public uri: URI,
34-
public webviewId: string,
32+
public uri: URI
3533
) {
3634
super();
37-
this._textModel = new NotebookTextModel(handle, viewType, uri, webviewId);
35+
this._textModel = new NotebookTextModel(handle, viewType, uri);
3836
this._register(this._textModel.onDidModelChange(e => {
3937
this._proxy.$acceptModelChanged(this.uri, e);
4038
}));
@@ -189,6 +187,12 @@ export class MainThreadNotebooks extends Disposable implements MainThreadNoteboo
189187
}));
190188
}
191189

190+
async addNotebookDocument(data: INotebookModelAddedData) {
191+
this._proxy.$acceptDocumentAndEditorsDelta({
192+
addedDocuments: [data]
193+
});
194+
}
195+
192196
private _addNotebookEditor(e: IEditor) {
193197
this._toDisposeOnEditorRemove.set(e.getId(), combinedDisposable(
194198
e.onDidChangeModel(() => this._updateState()),
@@ -335,7 +339,7 @@ export class MainThreadNotebookController implements IMainNotebookController {
335339
) {
336340
}
337341

338-
async createNotebook(viewType: string, uri: URI, backup: INotebookTextModelBackup | undefined, forceReload: boolean): Promise<NotebookTextModel | undefined> {
342+
async createNotebook(viewType: string, uri: URI, backup: INotebookTextModelBackup | undefined, forceReload: boolean, editorId?: string): Promise<NotebookTextModel | undefined> {
339343
let mainthreadNotebook = this._mapping.get(URI.from(uri).toString());
340344

341345
if (mainthreadNotebook) {
@@ -355,7 +359,7 @@ export class MainThreadNotebookController implements IMainNotebookController {
355359
return mainthreadNotebook.textModel;
356360
}
357361

358-
let document = new MainThreadNotebookDocument(this._proxy, MainThreadNotebookController.documentHandle++, viewType, uri, generateUuid());
362+
let document = new MainThreadNotebookDocument(this._proxy, MainThreadNotebookController.documentHandle++, viewType, uri);
359363
this._mapping.set(document.uri.toString(), document);
360364

361365
if (backup) {
@@ -367,28 +371,29 @@ export class MainThreadNotebookController implements IMainNotebookController {
367371
{
368372
editType: CellEditType.Insert,
369373
index: 0,
370-
cells: backup.cells
374+
cells: backup.cells || []
371375
}
372376
]);
373377

374-
await this._proxy.$acceptDocumentAndEditorsDelta({
375-
addedDocuments: [{
376-
viewType: document.viewType,
377-
handle: document.handle,
378-
webviewId: document.webviewId,
379-
uri: document.uri,
380-
metadata: document.textModel.metadata,
381-
versionId: document.textModel.versionId,
382-
cells: document.textModel.cells.map(cell => ({
383-
handle: cell.handle,
384-
uri: cell.uri,
385-
source: cell.textBuffer.getLinesContent(),
386-
language: cell.language,
387-
cellKind: cell.cellKind,
388-
outputs: cell.outputs,
389-
metadata: cell.metadata
390-
}))
391-
}]
378+
this._mainThreadNotebook.addNotebookDocument({
379+
viewType: document.viewType,
380+
handle: document.handle,
381+
uri: document.uri,
382+
metadata: document.textModel.metadata,
383+
versionId: document.textModel.versionId,
384+
cells: document.textModel.cells.map(cell => ({
385+
handle: cell.handle,
386+
uri: cell.uri,
387+
source: cell.textBuffer.getLinesContent(),
388+
language: cell.language,
389+
cellKind: cell.cellKind,
390+
outputs: cell.outputs,
391+
metadata: cell.metadata
392+
})),
393+
attachedEditor: editorId ? {
394+
id: editorId,
395+
selections: document.textModel.selections
396+
} : undefined
392397
});
393398

394399
return document.textModel;
@@ -410,24 +415,25 @@ export class MainThreadNotebookController implements IMainNotebookController {
410415
document.textModel.insertTemplateCell(mainCell);
411416
}
412417

413-
await this._proxy.$acceptDocumentAndEditorsDelta({
414-
addedDocuments: [{
415-
viewType: document.viewType,
416-
handle: document.handle,
417-
webviewId: document.webviewId,
418-
uri: document.uri,
419-
metadata: document.textModel.metadata,
420-
versionId: document.textModel.versionId,
421-
cells: document.textModel.cells.map(cell => ({
422-
handle: cell.handle,
423-
uri: cell.uri,
424-
source: cell.textBuffer.getLinesContent(),
425-
language: cell.language,
426-
cellKind: cell.cellKind,
427-
outputs: cell.outputs,
428-
metadata: cell.metadata
429-
}))
430-
}]
418+
await this._mainThreadNotebook.addNotebookDocument({
419+
viewType: document.viewType,
420+
handle: document.handle,
421+
uri: document.uri,
422+
metadata: document.textModel.metadata,
423+
versionId: document.textModel.versionId,
424+
cells: document.textModel.cells.map(cell => ({
425+
handle: cell.handle,
426+
uri: cell.uri,
427+
source: cell.textBuffer.getLinesContent(),
428+
language: cell.language,
429+
cellKind: cell.cellKind,
430+
outputs: cell.outputs,
431+
metadata: cell.metadata
432+
})),
433+
attachedEditor: editorId ? {
434+
id: editorId,
435+
selections: document.textModel.selections
436+
} : undefined
431437
});
432438

433439
this._proxy.$acceptEditorPropertiesChanged(uri, { selections: null, metadata: document.textModel.metadata });

src/vs/workbench/api/common/extHost.protocol.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1546,11 +1546,11 @@ export interface INotebookEditorPropertiesChangeData {
15461546
export interface INotebookModelAddedData {
15471547
uri: UriComponents;
15481548
handle: number;
1549-
webviewId: string;
15501549
versionId: number;
15511550
cells: IMainCellDto[],
15521551
viewType: string;
15531552
metadata?: NotebookDocumentMetadata;
1553+
attachedEditor?: { id: string; selections: number[]; }
15541554
}
15551555

15561556
export interface INotebookEditorAddData {

src/vs/workbench/api/common/extHostNotebook.ts

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,6 @@ export class ExtHostNotebookDocument extends Disposable implements vscode.Notebo
219219
return this._versionId;
220220
}
221221

222-
webviewId: string = '';
223-
224222
constructor(
225223
private readonly _proxy: MainThreadNotebookShape,
226224
private _documentsAndEditors: ExtHostDocumentsAndEditors,
@@ -501,7 +499,6 @@ export class ExtHostNotebookEditor extends Disposable implements vscode.Notebook
501499
public uri: URI,
502500
private _proxy: MainThreadNotebookShape,
503501
private _onDidReceiveMessage: Emitter<any>,
504-
public _webviewId: string,
505502
private _webviewInitData: WebviewInitData,
506503
public document: ExtHostNotebookDocument,
507504
private _documentsAndEditors: ExtHostDocumentsAndEditors
@@ -591,7 +588,7 @@ export class ExtHostNotebookEditor extends Disposable implements vscode.Notebook
591588
}
592589

593590
asWebviewUri(localResource: vscode.Uri): vscode.Uri {
594-
return asWebviewUri(this._webviewInitData, this._webviewId, localResource);
591+
return asWebviewUri(this._webviewInitData, this.id, localResource);
595592
}
596593
}
597594

@@ -997,7 +994,38 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
997994
}
998995
}
999996

997+
private _createExtHostEditor(document: ExtHostNotebookDocument, editorId: string, selections: number[]) {
998+
const onDidReceiveMessage = new Emitter<any>();
999+
const revivedUri = document.uri;
1000+
1001+
let editor = new ExtHostNotebookEditor(
1002+
document.viewType,
1003+
editorId,
1004+
revivedUri,
1005+
this._proxy,
1006+
onDidReceiveMessage,
1007+
this._webviewInitData,
1008+
document,
1009+
this._documentsAndEditors
1010+
);
1011+
1012+
const cells = editor.document.cells;
1013+
1014+
if (selections.length) {
1015+
const firstCell = selections[0];
1016+
editor.selection = cells.find(cell => cell.handle === firstCell);
1017+
} else {
1018+
editor.selection = undefined;
1019+
}
1020+
1021+
this._editors.get(editorId)?.editor.dispose();
1022+
1023+
this._editors.set(editorId, { editor, onDidReceiveMessage });
1024+
}
1025+
10001026
async $acceptDocumentAndEditorsDelta(delta: INotebookDocumentsAndEditorsDelta) {
1027+
let editorChanged = false;
1028+
10011029
if (delta.removedDocuments) {
10021030
delta.removedDocuments.forEach((uri) => {
10031031
let document = this._documents.get(URI.revive(uri).toString());
@@ -1045,49 +1073,32 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
10451073
]
10461074
});
10471075

1048-
document.webviewId = modelData.webviewId;
10491076
this._documents.set(revivedUriStr, document);
1077+
1078+
// create editor if populated
1079+
if (modelData.attachedEditor) {
1080+
this._createExtHostEditor(document, modelData.attachedEditor.id, modelData.attachedEditor.selections);
1081+
editorChanged = true;
1082+
}
10501083
}
10511084

10521085
const document = this._documents.get(revivedUriStr)!;
10531086
this._onDidOpenNotebookDocument.fire(document);
10541087
});
10551088
}
10561089

1057-
let editorChanged = false;
1058-
10591090
if (delta.addedEditors) {
10601091
delta.addedEditors.forEach(editorModelData => {
1092+
if (this._editors.has(editorModelData.id)) {
1093+
return;
1094+
}
1095+
10611096
const revivedUri = URI.revive(editorModelData.documentUri);
10621097
const document = this._documents.get(revivedUri.toString());
10631098

10641099
if (document) {
1065-
const onDidReceiveMessage = new Emitter<any>();
1066-
1067-
let editor = new ExtHostNotebookEditor(
1068-
document.viewType,
1069-
editorModelData.id,
1070-
revivedUri,
1071-
this._proxy,
1072-
onDidReceiveMessage,
1073-
document.webviewId,
1074-
this._webviewInitData,
1075-
document,
1076-
this._documentsAndEditors
1077-
);
1078-
1079-
const cells = editor.document.cells;
1080-
1081-
if (editorModelData.selections.length) {
1082-
const firstCell = editorModelData.selections[0];
1083-
editor.selection = cells.find(cell => cell.handle === firstCell);
1084-
} else {
1085-
editor.selection = undefined;
1086-
}
1087-
1100+
this._createExtHostEditor(document, editorModelData.id, editorModelData.selections);
10881101
editorChanged = true;
1089-
1090-
this._editors.set(editorModelData.id, { editor, onDidReceiveMessage });
10911102
}
10921103
});
10931104
}

src/vs/workbench/contrib/notebook/browser/notebookEditor.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,6 @@ export class NotebookEditor extends BaseEditor {
139139
});
140140

141141

142-
const model = await input.resolve();
143-
const viewState = this.loadTextEditorViewState(input);
144142
const existingEditorWidgetForInput = NotebookRegistry.getNotebookEditorWidget(input);
145143
if (existingEditorWidgetForInput) {
146144
// hide current widget
@@ -161,6 +159,9 @@ export class NotebookEditor extends BaseEditor {
161159
this._widget.layout(this.dimension, this._rootElement);
162160
}
163161

162+
const model = await input.resolve(this._widget!.getId());
163+
const viewState = this.loadTextEditorViewState(input);
164+
164165
this._widget.setModel(model.notebook, viewState, options);
165166
}
166167

src/vs/workbench/contrib/notebook/browser/notebookEditorInput.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,12 @@ export class NotebookEditorInput extends EditorInput {
129129
return;
130130
}
131131

132-
async resolve(): Promise<NotebookEditorModel> {
132+
async resolve(editorId?: string): Promise<NotebookEditorModel> {
133133
if (!await this.notebookService.canResolve(this.viewType!)) {
134134
throw new Error(`Cannot open notebook of type '${this.viewType}'`);
135135
}
136136

137-
this.textModel = await this.notebookService.modelManager.resolve(this.resource, this.viewType!);
137+
this.textModel = await this.notebookService.modelManager.resolve(this.resource, this.viewType!, editorId);
138138

139139
this._register(this.textModel.onDidChangeDirty(() => {
140140
this._onDidChangeDirty.fire();

src/vs/workbench/contrib/notebook/browser/notebookEditorWidget.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditor
471471
}
472472

473473
private async attachModel(textModel: NotebookTextModel, viewState: INotebookEditorViewState | undefined) {
474-
this.createWebview(textModel.webviewId);
474+
this.createWebview(this.getId());
475475
await this.webview!.waitForInitialization();
476476

477477
this.eventDispatcher = new NotebookEventDispatcher();

src/vs/workbench/contrib/notebook/browser/notebookServiceImpl.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,13 @@ export class NotebookService extends Disposable implements INotebookService, ICu
265265
return;
266266
}
267267

268-
async createNotebookFromBackup(viewType: string, uri: URI, metadata: NotebookDocumentMetadata, languages: string[], cells: ICellDto2[]): Promise<NotebookTextModel | undefined> {
268+
async createNotebookFromBackup(viewType: string, uri: URI, metadata: NotebookDocumentMetadata, languages: string[], cells: ICellDto2[], editorId?: string): Promise<NotebookTextModel | undefined> {
269269
const provider = this._notebookProviders.get(viewType);
270270
if (!provider) {
271271
return undefined;
272272
}
273273

274-
const notebookModel = await provider.controller.createNotebook(viewType, uri, { metadata, languages, cells }, false);
274+
const notebookModel = await provider.controller.createNotebook(viewType, uri, { metadata, languages, cells }, false, editorId);
275275
if (!notebookModel) {
276276
return undefined;
277277
}
@@ -286,15 +286,15 @@ export class NotebookService extends Disposable implements INotebookService, ICu
286286
return modelData.model;
287287
}
288288

289-
async resolveNotebook(viewType: string, uri: URI, forceReload: boolean): Promise<NotebookTextModel | undefined> {
289+
async resolveNotebook(viewType: string, uri: URI, forceReload: boolean, editorId?: string): Promise<NotebookTextModel | undefined> {
290290
const provider = this._notebookProviders.get(viewType);
291291
if (!provider) {
292292
return undefined;
293293
}
294294

295295
let notebookModel: NotebookTextModel | undefined;
296296

297-
notebookModel = await provider.controller.createNotebook(viewType, uri, undefined, forceReload);
297+
notebookModel = await provider.controller.createNotebook(viewType, uri, undefined, forceReload, editorId);
298298

299299
// new notebook model created
300300
const modelId = MODEL_ID(uri);

src/vs/workbench/contrib/notebook/common/model/notebookTextModel.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
104104
constructor(
105105
public handle: number,
106106
public viewType: string,
107-
public uri: URI,
108-
public webviewId: string
107+
public uri: URI
109108
) {
110109
super();
111110
this.cells = [];

0 commit comments

Comments
 (0)