Skip to content

Commit b5332bc

Browse files
authored
Ignore other notebooks (avoid conflicts with non jupyter nbs) (#12335)
For #10496 Ensure we do not treat notebooks such as Github Issues NB as one of ours.
1 parent 99c6f91 commit b5332bc

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed

src/client/datascience/notebook/cellEditSyncService.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { getOriginalCellId } from './helpers/cellMappers';
1818
export class CellEditSyncService implements IExtensionSingleActivationService, IDisposable {
1919
private readonly disposables: IDisposable[] = [];
2020
private mappedDocuments = new WeakMap<TextDocument, { cellId: string; model: INotebookModel }>();
21+
private nonJupyterNotebookDocuments = new WeakSet<TextDocument>();
2122
constructor(
2223
@inject(IDocumentManager) private readonly documentManager: IDocumentManager,
2324
@inject(IDisposableRegistry) disposableRegistry: IDisposableRegistry,
@@ -65,6 +66,9 @@ export class CellEditSyncService implements IExtensionSingleActivationService, I
6566
if (this.mappedDocuments.has(cellDocument)) {
6667
return this.mappedDocuments.get(cellDocument)!;
6768
}
69+
if (this.nonJupyterNotebookDocuments.has(cellDocument)) {
70+
return;
71+
}
6872

6973
let document: NotebookDocument | undefined;
7074
let cell: NotebookCell | undefined;
@@ -77,20 +81,17 @@ export class CellEditSyncService implements IExtensionSingleActivationService, I
7781
return !!found;
7882
});
7983

80-
if (!document) {
84+
if (!document || !cell) {
85+
if (this.isNonJupyterTextDocument(cellDocument)) {
86+
return;
87+
}
88+
8189
traceError(
8290
`Syncing Cell Editor aborted, Unable to find corresponding Notebook for ${cellDocument.uri.toString()}`,
8391
new Error('Unable to find corresponding Notebook')
8492
);
8593
return;
8694
}
87-
if (!cell) {
88-
traceError(
89-
`Syncing Cell Editor aborted, Unable to find corresponding NotebookCell for ${cellDocument.uri.toString()}`,
90-
new Error('Unable to find corresponding NotebookCell')
91-
);
92-
return;
93-
}
9495

9596
// Check if we have an editor associated with this document.
9697
const editor = this.editorProvider.editors.find((item) => item.file.toString() === document?.uri.toString());
@@ -112,4 +113,19 @@ export class CellEditSyncService implements IExtensionSingleActivationService, I
112113
this.mappedDocuments.set(cellDocument, { model: editor.model, cellId: getOriginalCellId(cell)! });
113114
return this.mappedDocuments.get(cellDocument);
114115
}
116+
/**
117+
* Check if the text document belongs to a notebook that's not ours.
118+
*/
119+
private isNonJupyterTextDocument(cellDocument: TextDocument) {
120+
// If this text document is for a note book thats
121+
if (
122+
this.vscNotebook.notebookDocuments.find(
123+
(doc) => doc.cells.findIndex((cell) => cell.document === cellDocument) >= 0
124+
)
125+
) {
126+
this.nonJupyterNotebookDocuments.add(cellDocument);
127+
return true;
128+
}
129+
return false;
130+
}
115131
}

src/client/datascience/notebook/helpers/helpers.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import type {
1313
NotebookCell,
1414
NotebookCellData,
1515
NotebookCellMetadata,
16-
NotebookData
16+
NotebookData,
17+
NotebookDocument
1718
} from 'vscode-proposed';
1819
import { NotebookCellRunState } from '../../../../../typings/vscode-proposed';
1920
import {
@@ -30,13 +31,22 @@ import { mapVSCNotebookCellToCellModel } from './cellMappers';
3031
const vscodeNotebookEnums = require('vscode') as typeof import('vscode-proposed');
3132
// tslint:disable-next-line: no-require-imports
3233
import cloneDeep = require('lodash/cloneDeep');
34+
import { JupyterNotebookView } from '../constants';
3335

3436
// This is the custom type we are adding into nbformat.IBaseCellMetadata
3537
interface IBaseCellVSCodeMetadata {
3638
end_execution_time?: string;
3739
start_execution_time?: string;
3840
}
3941

42+
/**
43+
* Whether this is a Notebook we created/manage/use.
44+
* Remember, there could be other notebooks such as GitHub Issues nb by VS Code.
45+
*/
46+
export function isJupyterNotebook(notebook: NotebookDocument) {
47+
return notebook.viewType === JupyterNotebookView;
48+
}
49+
4050
/**
4151
* Converts a NotebookModel into VSCode friendly format.
4252
*/

src/client/datascience/notebook/integration.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { NotebookEditorSupport } from '../../common/experiments/groups';
99
import { IFileSystem } from '../../common/platform/types';
1010
import { IDisposableRegistry, IExperimentsManager, IExtensionContext } from '../../common/types';
1111
import { noop } from '../../common/utils/misc';
12+
import { JupyterNotebookView } from './constants';
1213
import { NotebookContentProvider } from './contentProvider';
1314
import { NotebookKernel } from './notebookKernel';
1415
import { NotebookOutputRenderer } from './renderer';
@@ -75,7 +76,7 @@ export class NotebookIntegration implements IExtensionSingleActivationService {
7576
];
7677
content.contributes.notebookProvider = [
7778
{
78-
viewType: 'jupyter-notebook',
79+
viewType: JupyterNotebookView,
7980
displayName: 'Jupyter Notebook',
8081
selector: [
8182
{
@@ -92,10 +93,10 @@ export class NotebookIntegration implements IExtensionSingleActivationService {
9293
}
9394

9495
this.disposables.push(
95-
this.vscNotebook.registerNotebookContentProvider('jupyter-notebook', this.notebookContentProvider)
96+
this.vscNotebook.registerNotebookContentProvider(JupyterNotebookView, this.notebookContentProvider)
9697
);
9798
this.disposables.push(
98-
this.vscNotebook.registerNotebookKernel('jupyter-notebook', ['**/*.ipynb'], this.notebookKernel)
99+
this.vscNotebook.registerNotebookKernel(JupyterNotebookView, ['**/*.ipynb'], this.notebookKernel)
99100
);
100101
this.disposables.push(
101102
this.vscNotebook.registerNotebookOutputRenderer(

src/client/datascience/notebook/notebookEditorProvider.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { INotebookEditor, INotebookEditorProvider, INotebookProvider, IStatusPro
2727
import { JupyterNotebookView } from './constants';
2828
import { mapVSCNotebookCellsToNotebookCellModels } from './helpers/cellMappers';
2929
import { updateCellModelWithChangesToVSCCell } from './helpers/cellUpdateHelpers';
30+
import { isJupyterNotebook } from './helpers/helpers';
3031
import { NotebookEditor } from './notebookEditor';
3132
import { INotebookExecutionService } from './types';
3233

@@ -186,6 +187,9 @@ export class NotebookEditorProvider implements INotebookEditorProvider {
186187
}
187188

188189
private async onDidOpenNotebookDocument(doc: NotebookDocument): Promise<void> {
190+
if (!isJupyterNotebook(doc)) {
191+
return;
192+
}
189193
const uri = doc.uri;
190194
const model = await this.storage.load(uri);
191195
// tslint:disable-next-line: no-suspicious-comment
@@ -256,6 +260,9 @@ export class NotebookEditorProvider implements INotebookEditorProvider {
256260
private async onDidChangeNotebookDocument(
257261
e: NotebookCellsChangeEvent | NotebookCellOutputsChangeEvent | NotebookCellLanguageChangeEvent
258262
): Promise<void> {
263+
if (!isJupyterNotebook(e.document)) {
264+
return;
265+
}
259266
const model = await this.storage.load(e.document.uri);
260267
updateCellModelWithChangesToVSCCell(e, model);
261268
}

0 commit comments

Comments
 (0)