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.

include code for 'Generate with AI' by default & fix flakey test (#6014)

Shahmir Varqhae853b7543719
diff --git a/frontend/src/components/editor/ai/ai-completion-editor.tsx b/frontend/src/components/editor/ai/ai-completion-editor.tsx
index 2fe971ec6a..382245d9e2 100644
--- a/frontend/src/components/editor/ai/ai-completion-editor.tsx
+++ b/frontend/src/components/editor/ai/ai-completion-editor.tsx
@@ -3,7 +3,7 @@
import { useCompletion } from "@ai-sdk/react";
import { EditorView } from "@codemirror/view";
import { Loader2Icon, SparklesIcon, XIcon } from "lucide-react";
-import React, { useEffect, useState } from "react";
+import React, { useEffect, useId, useState } from "react";
import CodeMirrorMerge from "react-codemirror-merge";
import { Button } from "@/components/ui/button";
import { customPythonLanguageSupport } from "@/core/codemirror/language/languages/python";
@@ -68,6 +68,7 @@ export const AiCompletionEditor: React.FC<Props> = ({
const [includeOtherCells, setIncludeOtherCells] = useAtom(
includeOtherCellsAtom,
);
+ const includeOtherCellsCheckboxId = useId();
const runtimeManager = useRuntimeManager();
@@ -197,14 +198,14 @@ export const AiCompletionEditor: React.FC<Props> = ({
<div className="flex flex-row items-start gap-1 overflow-hidden">
<Checkbox
data-testid="include-other-cells-checkbox"
- id="include-other-cells"
+ id={includeOtherCellsCheckboxId}
checked={includeOtherCells}
onCheckedChange={(checked) =>
setIncludeOtherCells(Boolean(checked))
}
/>
<Label
- htmlFor="include-other-cells"
+ htmlFor={includeOtherCellsCheckboxId}
className="text-muted-foreground text-xs whitespace-nowrap ellipsis"
>
Include all code
diff --git a/frontend/src/core/ai/state.ts b/frontend/src/core/ai/state.ts
index 8784b09994..5e81b0bf0b 100644
--- a/frontend/src/core/ai/state.ts
+++ b/frontend/src/core/ai/state.ts
@@ -11,7 +11,12 @@ export const aiCompletionCellAtom = atom<{
cellId: CellId;
initialPrompt?: string;
} | null>(null);
-export const includeOtherCellsAtom = atom<boolean>(false);
+
+const INCLUDE_OTHER_CELLS_KEY = "marimo:ai:includeOtherCells";
+export const includeOtherCellsAtom = atomWithStorage<boolean>(
+ INCLUDE_OTHER_CELLS_KEY,
+ true,
+);
export interface Message {
id: string;
diff --git a/frontend/src/core/codemirror/language/__tests__/extension.test.ts b/frontend/src/core/codemirror/language/__tests__/extension.test.ts
index 5589547cb3..4f0188916f 100644
--- a/frontend/src/core/codemirror/language/__tests__/extension.test.ts
+++ b/frontend/src/core/codemirror/language/__tests__/extension.test.ts
@@ -3,7 +3,7 @@
import { EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
-import { describe, expect, it } from "vitest";
+import { afterEach, describe, expect, it } from "vitest";
import type { CellId } from "@/core/cells/ids";
import { DUCKDB_ENGINE } from "@/core/datasets/engines";
import { OverridingHotkeyProvider } from "@/core/hotkeys/hotkeys";
@@ -16,6 +16,21 @@ import {
} from "../extension";
import { languageMetadataField } from "../metadata";
+let view: EditorView | null = null;
+
+// Clean up created EditorView instances
+afterEach(() => {
+ if (view) {
+ view.destroy();
+ view = null;
+ }
+});
+
+function createEditorView(state: EditorState): EditorView {
+ view = new EditorView({ state });
+ return view;
+}
+
function createState(content: string, selection?: { anchor: number }) {
const state = EditorState.create({
doc: content,
@@ -74,7 +89,7 @@ describe("switchLanguage", () => {
const state = createState("print('Hello')\nprint('Goodbye')", {
anchor: 2,
});
- const mockEditor = new EditorView({ state });
+ const mockEditor = createEditorView(state);
switchLanguage(mockEditor, { language: "python", keepCodeAsIs: true });
expect(mockEditor.state.field(languageAdapterState).type).toBe("python");
expect(mockEditor.state.doc.toString()).toEqual(
@@ -129,7 +144,7 @@ describe("switchLanguage", () => {
const state = createState("print('Hello')\nprint('Goodbye')", {
anchor: 2,
});
- const mockEditor = new EditorView({ state });
+ const mockEditor = createEditorView(state);
switchLanguage(mockEditor, { language: "python", keepCodeAsIs: false });
expect(mockEditor.state.doc.toString()).toEqual(
"print('Hello')\nprint('Goodbye')",
@@ -176,7 +191,7 @@ describe("switchLanguage", () => {
it("sets default metadata when switching from Python to SQL with keepCodeAsIs false", () => {
const state = createState("SELECT * FROM df");
- const mockEditor = new EditorView({ state });
+ const mockEditor = createEditorView(state);
expect(mockEditor.state.field(languageMetadataField)).toEqual({});
@@ -204,7 +219,7 @@ describe("switchLanguage", () => {
it("handle when switching from Python to Markdown with keepCodeAsIs true", () => {
const state = createState("# hello");
- const mockEditor = new EditorView({ state });
+ const mockEditor = createEditorView(state);
expect(mockEditor.state.field(languageMetadataField)).toEqual({});
switchLanguage(mockEditor, { language: "markdown", keepCodeAsIs: true });
@@ -216,7 +231,7 @@ describe("switchLanguage", () => {
it("handles when switching from Python to Markdown to SQL with keepCodeAsIs true", () => {
const state = createState("SELECT * FROM df");
- const mockEditor = new EditorView({ state });
+ const mockEditor = createEditorView(state);
expect(mockEditor.state.field(languageMetadataField)).toEqual({});
switchLanguage(mockEditor, { language: "markdown", keepCodeAsIs: true });