Skip to content

Commit a4c6cea

Browse files
authored
No empty cell for native notebooks (#13524)
1 parent 151720a commit a4c6cea

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

src/client/datascience/jupyter/kernels/kernel.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,6 @@ export class Kernel implements IKernel {
138138
} else {
139139
await this.validate(this.uri);
140140
const metadata = ((getDefaultNotebookContent().metadata || {}) as unknown) as nbformat.INotebookMetadata;
141-
// tslint:disable-next-line: no-suspicious-comment
142-
// TODO: Just pass the `this.metadata` into the func.
143141
updateNotebookMetadata(metadata, this.metadata);
144142

145143
this._notebookPromise = this.notebookProvider.getOrCreateNotebook({

src/client/datascience/notebookStorage/nativeEditorStorage.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,11 @@ export class NativeEditorStorage implements INotebookStorage {
348348
};
349349
});
350350

351-
// Make sure at least one
352-
if (remapped.length === 0) {
353-
remapped.splice(0, 0, this.createEmptyCell(uuid()));
351+
if (!forVSCodeNotebook) {
352+
// Make sure at least one
353+
if (remapped.length === 0) {
354+
remapped.splice(0, 0, this.createEmptyCell(uuid()));
355+
}
354356
}
355357
const pythonNumber = json ? await this.extractPythonMainVersion(json) : 3;
356358

src/client/datascience/notebookStorage/notebookStorageProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export class NotebookStorageProvider implements INotebookStorageProvider {
110110
const uri = this.getNextNewNotebookUri(forVSCodeNotebooks);
111111

112112
// Always skip loading from the hot exit file. When creating a new file we want a new file.
113-
return this.getOrCreateModel(uri, contents, true);
113+
return this.getOrCreateModel(uri, contents, true, forVSCodeNotebooks);
114114
}
115115

116116
private getNextNewNotebookUri(forVSCodeNotebooks?: boolean): Uri {

src/client/datascience/notebookStorage/vscNotebookModel.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import type { nbformat } from '@jupyterlab/coreutils';
55
import { Memento, Uri } from 'vscode';
66
import { NotebookDocument } from '../../../../types/vscode-proposed';
7+
import { IVSCodeNotebook } from '../../common/application/types';
78
import { ICryptoUtils } from '../../common/types';
89
import { NotebookModelChange } from '../interactive-common/interactiveWindowTypes';
910
import {
@@ -41,12 +42,29 @@ export class VSCodeNotebookModel extends BaseNotebookModel {
4142
return this.document?.isDirty === true;
4243
}
4344
public get cells(): ICell[] {
45+
// Possible the document has been closed/disposed
46+
if (this.isDisposed) {
47+
return [];
48+
}
49+
4450
// When a notebook is not trusted, return original cells.
4551
// This is because the VSCode NotebookDocument object will not have any output in the cells.
4652
return this.document && this.isTrusted
4753
? this.document.cells.map((cell) => createCellFromVSCNotebookCell(cell, this))
4854
: this._cells;
4955
}
56+
public get isDisposed() {
57+
// Possible the document has been closed/disposed
58+
if (
59+
this.document &&
60+
this.vscodeNotebook &&
61+
!this.vscodeNotebook?.notebookDocuments.find((doc) => doc === this.document)
62+
) {
63+
return true;
64+
}
65+
return this._isDisposed === true;
66+
}
67+
5068
private document?: NotebookDocument;
5169
public get notebookContentWithoutCells(): Partial<nbformat.INotebookContent> {
5270
return {
@@ -63,7 +81,8 @@ export class VSCodeNotebookModel extends BaseNotebookModel {
6381
crypto: ICryptoUtils,
6482
json: Partial<nbformat.INotebookContent> = {},
6583
indentAmount: string = ' ',
66-
pythonNumber: number = 3
84+
pythonNumber: number = 3,
85+
private readonly vscodeNotebook?: IVSCodeNotebook
6786
) {
6887
super(isTrusted, file, cells, globalMemento, crypto, json, indentAmount, pythonNumber);
6988
}

0 commit comments

Comments
 (0)