Skip to content

Commit 868d389

Browse files
authored
Better handling of notebook diagnostics (microsoft#16529)
1 parent 151a0ef commit 868d389

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

news/2 Fixes/16528.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Clear Notebook Cell diagnostics when deleting a cell or closing a notebook.

src/client/jupyter/languageserver/notebookConverter.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ export class NotebookConverter implements Disposable {
5959

6060
private onDidChangeCellsEmitter = new EventEmitter<TextDocumentChangeEvent>();
6161

62+
private mapOfConcatDocumentsWithCellUris = new Map<string, string[]>();
63+
6264
constructor(
6365
private api: IVSCodeNotebook,
6466
private fs: IFileSystem,
@@ -127,9 +129,18 @@ export class NotebookConverter implements Disposable {
127129
if (wrapper) {
128130
// Diagnostics are supposed to be per file and are updated each time
129131
// Make sure to clear out old ones first
132+
const cellUris: string[] = [];
133+
const oldCellUris = this.mapOfConcatDocumentsWithCellUris.get(uri.toString()) || [];
130134
wrapper.notebook.getCells().forEach((c: NotebookCell) => {
131135
result.set(c.document.uri, []);
136+
cellUris.push(c.document.uri.toString());
132137
});
138+
// Possible some cells were deleted, we need to clear the diagnostics of those cells as well.
139+
const currentCellUris = new Set(cellUris);
140+
oldCellUris
141+
.filter((cellUri) => !currentCellUris.has(cellUri))
142+
.forEach((cellUri) => result.set(Uri.parse(cellUri), []));
143+
this.mapOfConcatDocumentsWithCellUris.set(uri.toString(), cellUris);
133144

134145
// Then for all the new ones, set their values.
135146
diagnostics.forEach((d) => {
@@ -141,6 +152,11 @@ export class NotebookConverter implements Disposable {
141152
}
142153
list.push(this.toIncomingDiagnostic(location.uri, d));
143154
});
155+
} else if (this.mapOfConcatDocumentsWithCellUris.has(uri.toString())) {
156+
(this.mapOfConcatDocumentsWithCellUris.get(uri.toString()) || [])
157+
.map((cellUri) => Uri.parse(cellUri))
158+
.forEach((cellUri) => result.set(cellUri, []));
159+
this.mapOfConcatDocumentsWithCellUris.delete(uri.toString());
144160
} else {
145161
result.set(uri, diagnostics);
146162
}

0 commit comments

Comments
 (0)