Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: Clear diagnostics for other cells (#5097)

Myles Scolnick422da5dc8fa9
diff --git a/frontend/src/components/editor/documentation.css b/frontend/src/components/editor/documentation.css
index 69d74b5074..d45f8ceb02 100644
--- a/frontend/src/components/editor/documentation.css
+++ b/frontend/src/components/editor/documentation.css
@@ -109,7 +109,7 @@
/* Custom overrides when in a tooltip */
.cm-tooltip .documentation,
.cm-tooltip .docs-documentation {
- @apply text-sm;
+ @apply text-sm py-1 px-2;
h1 {
@apply text-base;
diff --git a/frontend/src/core/codemirror/language/languages/python.ts b/frontend/src/core/codemirror/language/languages/python.ts
index 65b82848f5..055d7338b4 100644
--- a/frontend/src/core/codemirror/language/languages/python.ts
+++ b/frontend/src/core/codemirror/language/languages/python.ts
@@ -70,6 +70,8 @@ const lspClient = once((lspConfig: LSPConfig) => {
const ignoredRuffRules = [
// Even ruff documentation of this rule explains it is not useful in notebooks
"B018", // Useless expression
+ // isort
+ "I001", // Import block is un-sorted or un-formatted
];
const settings = {
pylsp: {
diff --git a/frontend/src/core/codemirror/lsp/__tests__/notebook-lsp.test.ts b/frontend/src/core/codemirror/lsp/__tests__/notebook-lsp.test.ts
index d800f1e27d..1a1193d3ad 100644
--- a/frontend/src/core/codemirror/lsp/__tests__/notebook-lsp.test.ts
+++ b/frontend/src/core/codemirror/lsp/__tests__/notebook-lsp.test.ts
@@ -211,6 +211,7 @@ describe("NotebookLanguageServerClient", () => {
[Cells.cell3]: "print(math.sqrt(4))",
},
});
+ (NotebookLanguageServerClient as any).SEEN_CELL_DOCUMENT_URIS.clear();
});
describe("textDocumentHover", () => {
@@ -569,13 +570,13 @@ describe("NotebookLanguageServerClient", () => {
describe("diagnostics handling", () => {
it("should transform diagnostic ranges and filter out-of-bounds diagnostics", async () => {
// Mock processNotification to capture the transformed diagnostics
- let capturedDiagnostics: LSP.PublishDiagnosticsParams | undefined;
+ const capturedDiagnostics: LSP.PublishDiagnosticsParams[] = [];
// @ts-expect-error: processNotification is private
mockClient.processNotification = vi
.fn()
.mockImplementation((notification: any) => {
if (notification.method === "textDocument/publishDiagnostics") {
- capturedDiagnostics = notification.params;
+ capturedDiagnostics.push(notification.params);
}
});
@@ -625,12 +626,15 @@ describe("NotebookLanguageServerClient", () => {
// @ts-expect-error: processNotification is private
notebookClient.client.processNotification(diagnosticsNotification);
- expect(capturedDiagnostics).toBeDefined();
- expect(capturedDiagnostics?.diagnostics).toHaveLength(1);
- expect(capturedDiagnostics?.diagnostics[0].range).toEqual({
+ expect(capturedDiagnostics).toHaveLength(2);
+ expect(capturedDiagnostics[0].diagnostics).toHaveLength(1);
+ expect(capturedDiagnostics[0].diagnostics[0].range).toEqual({
start: { line: 0, character: 0 },
end: { line: 0, character: 4 },
});
+
+ // Rest are cleared
+ expect(capturedDiagnostics[1].diagnostics).toHaveLength(0);
});
it("should clear diagnostics for all cells when receiving empty diagnostics", async () => {
diff --git a/frontend/src/core/codemirror/lsp/notebook-lsp.ts b/frontend/src/core/codemirror/lsp/notebook-lsp.ts
index 116f19ba2b..978dfff1fe 100644
--- a/frontend/src/core/codemirror/lsp/notebook-lsp.ts
+++ b/frontend/src/core/codemirror/lsp/notebook-lsp.ts
@@ -27,7 +27,7 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
}
>(20);
- private static readonly SEEN_CELL_DOCUMENT_URIS = new Set<LSP.DocumentUri>();
+ private static readonly SEEN_CELL_DOCUMENT_URIS = new Set<CellDocumentUri>();
constructor(
client: ILanguageServerClient,
@@ -348,6 +348,10 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
version,
lens,
});
+ invariant(
+ CellDocumentUri.is(cellDocumentUri),
+ "Execpted URI to be CellDocumentUri",
+ );
NotebookLanguageServerClient.SEEN_CELL_DOCUMENT_URIS.add(cellDocumentUri);
// Pass merged doc to super
@@ -517,22 +521,6 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
const diagnostics = notification.params.diagnostics;
- // If diagnostics are empty, we can just clear them for all cells
- if (diagnostics.length === 0) {
- Logger.debug("[lsp] clearing diagnostics");
-
- for (const cellDocumentUri of NotebookLanguageServerClient.SEEN_CELL_DOCUMENT_URIS) {
- previousProcessNotification({
- method: "textDocument/publishDiagnostics",
- params: {
- uri: cellDocumentUri,
- diagnostics: [],
- },
- });
- }
- return;
- }
-
const { lens, version: cellVersion } = payload;
// Pre-partition diagnostics by cell
@@ -556,21 +544,43 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
}
}
+ const cellsToClear = new Set(
+ NotebookLanguageServerClient.SEEN_CELL_DOCUMENT_URIS,
+ );
+
// Process each cell's diagnostics
for (const [cellId, cellDiagnostics] of diagnosticsByCellId.entries()) {
Logger.debug("[lsp] diagnostics for cell", cellId, cellDiagnostics);
+ const cellDocumentUri = CellDocumentUri.of(cellId);
+
+ cellsToClear.delete(cellDocumentUri);
previousProcessNotification({
...notification,
params: {
...notification.params,
- uri: CellDocumentUri.of(cellId),
+ uri: cellDocumentUri,
version: cellVersion,
diagnostics: cellDiagnostics,
},
});
}
+ // Clear the rest of the diagnostics
+ if (cellsToClear.size > 0) {
+ Logger.debug("[lsp] clearing diagnostics", cellsToClear);
+
+ for (const cellDocumentUri of cellsToClear) {
+ previousProcessNotification({
+ method: "textDocument/publishDiagnostics",
+ params: {
+ uri: cellDocumentUri,
+ diagnostics: [],
+ },
+ });
+ }
+ }
+
return;
}
diff --git a/frontend/src/core/codemirror/lsp/types.ts b/frontend/src/core/codemirror/lsp/types.ts
index 8ad970075b..e495d556d5 100644
--- a/frontend/src/core/codemirror/lsp/types.ts
+++ b/frontend/src/core/codemirror/lsp/types.ts
@@ -1,6 +1,7 @@
/* Copyright 2024 Marimo. All rights reserved. */
import type { CellId } from "@/core/cells/ids";
import { invariant } from "@/utils/invariant";
+import type { TypedString } from "@/utils/typed";
import type { LanguageServerClient } from "@marimo-team/codemirror-languageserver";
import type { DocumentUri } from "vscode-languageserver-protocol";
@@ -8,12 +9,14 @@ export type ILanguageServerClient = {
[key in keyof LanguageServerClient]: LanguageServerClient[key];
};
+export type CellDocumentUri = DocumentUri & TypedString<"CellDocumentUri">;
+
export const CellDocumentUri = {
PREFIX: "file:///",
- of(cellId: CellId): DocumentUri {
- return `${this.PREFIX}${cellId}`;
+ of(cellId: CellId): CellDocumentUri {
+ return `${this.PREFIX}${cellId}` as CellDocumentUri;
},
- is(uri: string): uri is DocumentUri {
+ is(uri: string): uri is CellDocumentUri {
return uri.startsWith(this.PREFIX);
},
parse(uri: string): CellId {
diff --git a/frontend/src/css/app/codemirror.css b/frontend/src/css/app/codemirror.css
index a700d69128..bdbfa2bcd6 100644
--- a/frontend/src/css/app/codemirror.css
+++ b/frontend/src/css/app/codemirror.css
@@ -51,10 +51,10 @@
max-height: 45vh; /* 45% of viewport height */
max-width: 40vw; /* 40% of viewport width */
overflow: auto;
+ scrollbar-width: thin;
/* Respect newlines. */
border-radius: 4px;
- padding: 0.625rem 0.875rem;
}
/* Hover tooltips get highest priority in display. */
diff --git a/frontend/src/css/table.css b/frontend/src/css/table.css
index d2700aa541..1d4c5eae34 100644
--- a/frontend/src/css/table.css
+++ b/frontend/src/css/table.css
@@ -3,6 +3,8 @@ table.dataframe {
display: block;
max-width: 100%;
overflow: auto;
+
+ scrollbar-width: thin;
}
.markdown table thead,