Skip to content

Commit 946d994

Browse files
committed
💂 notebook tab switching and pick editor types.
1 parent 9efb82e commit 946d994

3 files changed

Lines changed: 93 additions & 0 deletions

File tree

extensions/vscode-notebook-tests/src/notebook.test.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,70 @@ suite('notebook working copy', () => {
319319

320320
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
321321
});
322+
323+
test('multiple tabs: dirty + clean', async function () {
324+
const resource = vscode.Uri.parse(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
325+
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
326+
327+
await waitFor(500);
328+
await vscode.commands.executeCommand('notebook.cell.insertCodeCellBelow');
329+
assert.equal(vscode.notebook.activeNotebookEditor!.selection?.source, '');
330+
331+
await vscode.commands.executeCommand('notebook.cell.insertCodeCellAbove');
332+
await vscode.commands.executeCommand('default:type', { text: 'var abc = 0;' });
333+
334+
const secondResource = vscode.Uri.parse(join(vscode.workspace.rootPath || '', './second.vsctestnb'));
335+
await vscode.commands.executeCommand('vscode.openWith', secondResource, 'notebookCoreTest');
336+
await waitFor(500);
337+
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
338+
339+
// make sure that the previous dirty editor is still restored in the extension host and no data loss
340+
assert.equal(vscode.notebook.activeNotebookEditor !== undefined, true);
341+
assert.equal(vscode.notebook.activeNotebookEditor?.selection !== undefined, true);
342+
assert.deepEqual(vscode.notebook.activeNotebookEditor?.document.cells[1], vscode.notebook.activeNotebookEditor?.selection);
343+
assert.deepEqual(vscode.notebook.activeNotebookEditor?.document.cells.length, 3);
344+
assert.equal(vscode.notebook.activeNotebookEditor?.selection?.source, 'var abc = 0;');
345+
346+
await vscode.commands.executeCommand('workbench.action.files.save');
347+
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
348+
});
349+
350+
test('multiple tabs: two dirty tabs and switching', async function () {
351+
const resource = vscode.Uri.parse(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
352+
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
353+
354+
await waitFor(500);
355+
await vscode.commands.executeCommand('notebook.cell.insertCodeCellBelow');
356+
assert.equal(vscode.notebook.activeNotebookEditor!.selection?.source, '');
357+
358+
await vscode.commands.executeCommand('notebook.cell.insertCodeCellAbove');
359+
await vscode.commands.executeCommand('default:type', { text: 'var abc = 0;' });
360+
361+
const secondResource = vscode.Uri.parse(join(vscode.workspace.rootPath || '', './second.vsctestnb'));
362+
await vscode.commands.executeCommand('vscode.openWith', secondResource, 'notebookCoreTest');
363+
await waitFor(500);
364+
await vscode.commands.executeCommand('notebook.cell.insertCodeCellBelow');
365+
assert.equal(vscode.notebook.activeNotebookEditor!.selection?.source, '');
366+
367+
// switch to the first editor
368+
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
369+
assert.equal(vscode.notebook.activeNotebookEditor !== undefined, true);
370+
assert.equal(vscode.notebook.activeNotebookEditor?.selection !== undefined, true);
371+
assert.deepEqual(vscode.notebook.activeNotebookEditor?.document.cells[1], vscode.notebook.activeNotebookEditor?.selection);
372+
assert.deepEqual(vscode.notebook.activeNotebookEditor?.document.cells.length, 3);
373+
assert.equal(vscode.notebook.activeNotebookEditor?.selection?.source, 'var abc = 0;');
374+
375+
// switch to the second editor
376+
await vscode.commands.executeCommand('vscode.openWith', secondResource, 'notebookCoreTest');
377+
assert.equal(vscode.notebook.activeNotebookEditor !== undefined, true);
378+
assert.equal(vscode.notebook.activeNotebookEditor?.selection !== undefined, true);
379+
assert.deepEqual(vscode.notebook.activeNotebookEditor?.document.cells[1], vscode.notebook.activeNotebookEditor?.selection);
380+
assert.deepEqual(vscode.notebook.activeNotebookEditor?.document.cells.length, 2);
381+
assert.equal(vscode.notebook.activeNotebookEditor?.selection?.source, '');
382+
383+
await vscode.commands.executeCommand('workbench.action.files.saveAll');
384+
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
385+
});
322386
});
323387

324388
suite('metadata', () => {
@@ -344,6 +408,25 @@ suite('regression', () => {
344408
assert.equal(vscode.notebook.activeNotebookEditor!.selection?.source, '');
345409
assert.equal(vscode.notebook.activeNotebookEditor!.selection?.language, 'typescript');
346410
});
411+
412+
test('#97830, #97764. Support switch to other editor types', async function () {
413+
const resource = vscode.Uri.parse(join(vscode.workspace.rootPath || '', './empty.vsctestnb'));
414+
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
415+
416+
await waitFor(500);
417+
await vscode.commands.executeCommand('notebook.cell.insertCodeCellBelow');
418+
await vscode.commands.executeCommand('default:type', { text: 'var abc = 0;' });
419+
420+
assert.equal(vscode.notebook.activeNotebookEditor !== undefined, true, 'notebook first');
421+
assert.equal(vscode.notebook.activeNotebookEditor!.selection?.source, 'var abc = 0;');
422+
assert.equal(vscode.notebook.activeNotebookEditor!.selection?.language, 'typescript');
423+
424+
await vscode.commands.executeCommand('vscode.openWith', resource, 'default');
425+
assert.equal(vscode.window.activeTextEditor?.document.uri.path, resource.path);
426+
427+
await vscode.commands.executeCommand('workbench.action.files.saveAll');
428+
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
429+
});
347430
});
348431

349432
suite('webview resource uri', () => {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditor
9292
protected readonly _contributions: { [key: string]: INotebookEditorContribution; };
9393
private scrollBeyondLastLine: boolean;
9494
private readonly memento: Memento;
95+
private _isDisposed: boolean = false;
9596

9697

9798
constructor(
@@ -747,6 +748,10 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditor
747748

748749
let r: () => void;
749750
DOM.scheduleAtNextAnimationFrame(() => {
751+
if (this._isDisposed) {
752+
return;
753+
}
754+
750755
relayout(cell, height);
751756
r();
752757
});
@@ -1226,6 +1231,7 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditor
12261231
//#endregion
12271232

12281233
dispose() {
1234+
this._isDisposed = true;
12291235
const keys = Object.keys(this._contributions);
12301236
for (let i = 0, len = keys.length; i < len; i++) {
12311237
const contributionId = keys[i];

0 commit comments

Comments
 (0)