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(lsp): synchronize notebook text from live editors (#11186)

Lucien Huberda01f923b7c2
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 ee0ddfe3d8..d142698ec3 100644
--- a/frontend/src/core/codemirror/lsp/__tests__/notebook-lsp.test.ts
+++ b/frontend/src/core/codemirror/lsp/__tests__/notebook-lsp.test.ts
@@ -359,11 +359,111 @@ describe("NotebookLanguageServerClient", () => {
return undefined;
});
- notebookClient = new NotebookLanguageServerClient(mockClient, {}, () => ({
- [Cells.cell1]: new EditorView({ doc: "# this is a comment" }),
- [Cells.cell2]: new EditorView({ doc: "import math\nimport numpy" }),
- [Cells.cell3]: new EditorView({ doc: "print(math.sqrt(4))" }),
- }));
+ notebookClient = new NotebookLanguageServerClient(
+ mockClient,
+ {},
+ () => ({}),
+ );
+ });
+
+ describe("live editor snapshots", () => {
+ function createEditor(code: string) {
+ return new EditorView({
+ doc: code,
+ extensions: [
+ languageAdapterState.init(() => new PythonLanguageAdapter()),
+ languageMetadataField.init(() => ({})),
+ ],
+ });
+ }
+
+ it("sends edits and their removal without invalidating the notebook atom", async () => {
+ const cachedCodes = store.get(topologicalCodesAtom);
+ const editor = createEditor("print(math.sqrt(4))");
+ replaceNotebookEditors(notebookClient, () => ({ [Cells.cell3]: editor }));
+ const uri = CellDocumentUri.of(Cells.cell3);
+ try {
+ await notebookClient.textDocumentDidOpen({
+ textDocument: {
+ uri,
+ languageId: "python",
+ version: 1,
+ text: editor.state.doc.toString(),
+ },
+ });
+ for (const [index, code] of [
+ "math.missing()",
+ "print(math.sqrt(4))",
+ ].entries()) {
+ editor.dispatch({
+ changes: { from: 0, to: editor.state.doc.length, insert: code },
+ });
+ await notebookClient.textDocumentDidChange({
+ textDocument: { uri, version: index + 2 },
+ contentChanges: [{ text: code }],
+ });
+ expect(store.get(topologicalCodesAtom)).toEqual(cachedCodes);
+ expect(mockClient.textDocumentDidChange).toHaveBeenNthCalledWith(
+ index + 1,
+ {
+ textDocument: {
+ uri: notebookClient.documentUri,
+ version: index + 2,
+ },
+ contentChanges: [
+ {
+ text: `# this is a comment\nimport math\nimport numpy\n${code}`,
+ },
+ ],
+ },
+ );
+ }
+ } finally {
+ editor.destroy();
+ }
+ });
+
+ it("syncs pending edits before completion and uses their updated line offsets", async () => {
+ const editor = createEditor("import math\nimport numpy");
+ replaceNotebookEditors(notebookClient, () => ({ [Cells.cell2]: editor }));
+ try {
+ await notebookClient.textDocumentDidOpen({
+ textDocument: {
+ uri: CellDocumentUri.of(Cells.cell2),
+ languageId: "python",
+ version: 1,
+ text: editor.state.doc.toString(),
+ },
+ });
+ editor.dispatch({
+ changes: { from: editor.state.doc.length, insert: "\nextra = 1" },
+ });
+ mockClient.textDocumentCompletion.mockImplementation(async () => {
+ expect(mockClient.textDocumentDidChange).toHaveBeenCalledWith({
+ textDocument: { uri: notebookClient.documentUri, version: 2 },
+ contentChanges: [
+ {
+ text: "# this is a comment\nimport math\nimport numpy\nextra = 1\nprint(math.sqrt(4))",
+ },
+ ],
+ });
+ return null;
+ });
+ const params: LSP.CompletionParams = {
+ textDocument: { uri: CellDocumentUri.of(Cells.cell3) },
+ position: { line: 0, character: 11 },
+ };
+ await notebookClient.textDocumentCompletion(params);
+ expect(mockClient.textDocumentCompletion).toHaveBeenCalledWith({
+ textDocument: { uri: notebookClient.documentUri },
+ position: { line: 4, character: 11 },
+ });
+ await notebookClient.textDocumentCompletion(params);
+ expect(mockClient.textDocumentDidChange).toHaveBeenCalledTimes(1);
+ } finally {
+ editor.destroy();
+ }
+ });
});
describe("document lifecycle", () => {
@@ -1893,6 +1993,7 @@ describe("NotebookLanguageServerClient", () => {
const isolatedNotebookClient = new NotebookLanguageServerClient(
isolatedClient,
{},
+ () => ({}),
);
await isolatedNotebookClient.sync();
isolatedNotebookClient.patchProcessNotification();
diff --git a/frontend/src/core/codemirror/lsp/notebook-lsp.ts b/frontend/src/core/codemirror/lsp/notebook-lsp.ts
index 92780ecc27..3cc2203bfd 100644
--- a/frontend/src/core/codemirror/lsp/notebook-lsp.ts
+++ b/frontend/src/core/codemirror/lsp/notebook-lsp.ts
@@ -343,7 +343,21 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
}
private getNotebookCode() {
- return store.get(topologicalCodesAtom);
+ const { cellIds, codes } = store.get(topologicalCodesAtom);
+ const editors = this.getNotebookEditors();
+ // EditorView edits do not invalidate the cached notebook atom.
+ return {
+ cellIds,
+ codes: Objects.fromEntries(
+ cellIds.map((id) => {
+ const editor = editors[id];
+ return [
+ id,
+ editor ? getEditorCodeAsPython(editor) : (codes[id] ?? ""),
+ ];
+ }),
+ ),
+ };
}
private assertCellDocumentUri(
@@ -870,7 +884,7 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
}
public async textDocumentCompletion(params: LSP.CompletionParams) {
- const { lens } = this.snapshotter.getLatestSnapshot();
+ const { lens } = await this.sync();
// Check if URI is valid
if (!CellDocumentUri.is(params.textDocument.uri)) {