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.

Auto-close curly braces within strings (#7759)

Andrew701d8f78b04e
diff --git a/frontend/src/core/codemirror/cm.ts b/frontend/src/core/codemirror/cm.ts
index a7e9d6f841..a858754f68 100644
--- a/frontend/src/core/codemirror/cm.ts
+++ b/frontend/src/core/codemirror/cm.ts
@@ -64,6 +64,7 @@ import { getCurrentLanguageAdapter } from "./language/commands";
import { adaptiveLanguageConfiguration } from "./language/extension";
import { dndBundle } from "./misc/dnd";
import { pasteBundle } from "./misc/paste";
+import { stringsAutoCloseBraces } from "./misc/string-braces";
import { reactiveReferencesBundle } from "./reactive-references/extension";
import { darkTheme } from "./theme/dark";
import { lightTheme } from "./theme/light";
@@ -203,6 +204,7 @@ export const basicBundle = (opts: CodeMirrorSetupOpts): Extension[] => {
hintTooltip(lspConfig),
copilotBundle(completionConfig),
foldGutter(),
+ stringsAutoCloseBraces(),
closeBrackets(),
completionKeymap(),
// to avoid clash with charDeleteBackward keymap
diff --git a/frontend/src/core/codemirror/misc/__tests__/string-braces.test.ts b/frontend/src/core/codemirror/misc/__tests__/string-braces.test.ts
new file mode 100644
index 0000000000..e886a1a6ce
--- /dev/null
+++ b/frontend/src/core/codemirror/misc/__tests__/string-braces.test.ts
@@ -0,0 +1,200 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import { python } from "@codemirror/lang-python";
+import { EditorState } from "@codemirror/state";
+import { EditorView } from "@codemirror/view";
+import { afterEach, describe, expect, it } from "vitest";
+import { stringBraceInputHandler } from "../string-braces";
+
+function createEditor(
+ initialContent: string,
+ cursorPosition: number,
+): EditorView {
+ const state = EditorState.create({
+ doc: initialContent,
+ selection: { anchor: cursorPosition },
+ extensions: [python()],
+ });
+
+ const view = new EditorView({
+ state,
+ parent: document.body,
+ });
+
+ return view;
+}
+
+describe("string brace auto-closing", () => {
+ let view: EditorView;
+
+ afterEach(() => {
+ if (view) {
+ view.destroy();
+ if (document.body.contains(view.dom)) {
+ view.dom.remove();
+ }
+ }
+ });
+
+ it("should auto-close braces in f-strings", () => {
+ view = createEditor('f"hello ', 8);
+ const result = stringBraceInputHandler(view, 8, 8, "{");
+
+ expect(result).toBe(true);
+ expect(view.state.doc.toString()).toBe('f"hello {}');
+ expect(view.state.selection.main.head).toBe(9);
+ });
+
+ it("should auto-close braces in regular double-quoted strings", () => {
+ view = createEditor('"hello ', 7);
+ const result = stringBraceInputHandler(view, 7, 7, "{");
+
+ expect(result).toBe(true);
+ expect(view.state.doc.toString()).toBe('"hello {}');
+ expect(view.state.selection.main.head).toBe(8);
+ });
+
+ it("should auto-close braces in rf-strings", () => {
+ view = createEditor('rf"hello ', 9);
+ const result = stringBraceInputHandler(view, 9, 9, "{");
+
+ expect(result).toBe(true);
+ expect(view.state.doc.toString()).toBe('rf"hello {}');
+ expect(view.state.selection.main.head).toBe(10);
+ });
+
+ it("should auto-close braces in fr-strings", () => {
+ view = createEditor('fr"hello ', 9);
+ const result = stringBraceInputHandler(view, 9, 9, "{");
+
+ expect(result).toBe(true);
+ expect(view.state.doc.toString()).toBe('fr"hello {}');
+ expect(view.state.selection.main.head).toBe(10);
+ });
+
+ it("should auto-close braces in single-quoted strings", () => {
+ view = createEditor("'hello ", 7);
+ const result = stringBraceInputHandler(view, 7, 7, "{");
+
+ expect(result).toBe(true);
+ expect(view.state.doc.toString()).toBe("'hello {}");
+ expect(view.state.selection.main.head).toBe(8);
+ });
+
+ it("should auto-close braces in uppercase F-strings", () => {
+ view = createEditor('F"hello ', 8);
+ const result = stringBraceInputHandler(view, 8, 8, "{");
+
+ expect(result).toBe(true);
+ expect(view.state.doc.toString()).toBe('F"hello {}');
+ expect(view.state.selection.main.head).toBe(9);
+ });
+
+ it("should auto-close braces in raw strings without f/t", () => {
+ view = createEditor('r"hello ', 8);
+ const result = stringBraceInputHandler(view, 8, 8, "{");
+
+ expect(result).toBe(true);
+ expect(view.state.doc.toString()).toBe('r"hello {}');
+ expect(view.state.selection.main.head).toBe(9);
+ });
+
+ // Handled by other CodeMirror handler(s)
+ it("should NOT auto-close braces outside strings", () => {
+ view = createEditor("x = ", 4);
+ const result = stringBraceInputHandler(view, 4, 4, "{");
+
+ expect(result).toBe(false);
+ expect(view.state.doc.toString()).toBe("x = ");
+ });
+
+ it("should NOT auto-close braces when typing other characters", () => {
+ view = createEditor('f"hello ', 8);
+ const result = stringBraceInputHandler(view, 8, 8, "a");
+
+ expect(result).toBe(false);
+ expect(view.state.doc.toString()).toBe('f"hello ');
+ });
+
+ it("should handle braces at the start of string", () => {
+ view = createEditor('f"', 2);
+ const result = stringBraceInputHandler(view, 2, 2, "{");
+
+ expect(result).toBe(true);
+ expect(view.state.doc.toString()).toBe('f"{}');
+ expect(view.state.selection.main.head).toBe(3);
+ });
+
+ it("should handle braces in the middle of string content", () => {
+ view = createEditor('f"hello world ', 14);
+ const result = stringBraceInputHandler(view, 14, 14, "{");
+
+ expect(result).toBe(true);
+ expect(view.state.doc.toString()).toBe('f"hello world {}');
+ expect(view.state.selection.main.head).toBe(15);
+ });
+
+ it("should handle multiple braces in string", () => {
+ view = createEditor('f"hello {} world', 16);
+ const result = stringBraceInputHandler(view, 16, 16, "{");
+
+ expect(result).toBe(true);
+ expect(view.state.doc.toString()).toBe('f"hello {} world{}');
+ expect(view.state.selection.main.head).toBe(17);
+ });
+
+ it("should handle empty string", () => {
+ view = createEditor('f""', 2);
+ const result = stringBraceInputHandler(view, 2, 2, "{");
+
+ expect(result).toBe(true);
+ expect(view.state.doc.toString()).toBe('f"{}"');
+ expect(view.state.selection.main.head).toBe(3);
+ });
+
+ it("should auto-close braces in triple-quoted strings", () => {
+ view = createEditor('"""hello ', 9);
+ const result = stringBraceInputHandler(view, 9, 9, "{");
+
+ expect(result).toBe(true);
+ expect(view.state.doc.toString()).toBe('"""hello {}');
+ expect(view.state.selection.main.head).toBe(10);
+ });
+
+ it("should auto-close braces in triple-quoted f-strings", () => {
+ view = createEditor('f"""hello ', 10);
+ const result = stringBraceInputHandler(view, 10, 10, "{");
+
+ expect(result).toBe(true);
+ expect(view.state.doc.toString()).toBe('f"""hello {}');
+ expect(view.state.selection.main.head).toBe(11);
+ });
+
+ it("should auto-close braces in triple single-quoted strings", () => {
+ view = createEditor("'''hello ", 9);
+ const result = stringBraceInputHandler(view, 9, 9, "{");
+
+ expect(result).toBe(true);
+ expect(view.state.doc.toString()).toBe("'''hello {}");
+ expect(view.state.selection.main.head).toBe(10);
+ });
+
+ it("should auto-close braces in triple single-quoted f-strings", () => {
+ view = createEditor("f'''hello ", 10);
+ const result = stringBraceInputHandler(view, 10, 10, "{");
+
+ expect(result).toBe(true);
+ expect(view.state.doc.toString()).toBe("f'''hello {}");
+ expect(view.state.selection.main.head).toBe(11);
+ });
+
+ it("should NOT auto-close braces when text is selected", () => {
+ view = createEditor('f"hello world"', 8);
+ // User has selected "world" (from position 8 to 13)
+ const result = stringBraceInputHandler(view, 8, 13, "{");
+
+ expect(result).toBe(false);
+ // Document should remain unchanged since we return false
+ expect(view.state.doc.toString()).toBe('f"hello world"');
+ });
+});
diff --git a/frontend/src/core/codemirror/misc/string-braces.ts b/frontend/src/core/codemirror/misc/string-braces.ts
new file mode 100644
index 0000000000..56c11c4ed9
--- /dev/null
+++ b/frontend/src/core/codemirror/misc/string-braces.ts
@@ -0,0 +1,37 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+import { syntaxTree } from "@codemirror/language";
+import type { Extension } from "@codemirror/state";
+import { EditorView } from "@codemirror/view";
+
+export function stringBraceInputHandler(
+ view: EditorView,
+ from: number,
+ to: number,
+ text: string,
+): boolean {
+ if (text !== "{") {
+ return false;
+ }
+
+ if (from !== to) {
+ return false;
+ }
+
+ const tree = syntaxTree(view.state);
+ const node = tree.resolveInner(from, -1);
+
+ if (!node?.type.name.includes("String")) {
+ return false;
+ }
+
+ view.dispatch({
+ changes: { from, to, insert: "{}" },
+ selection: { anchor: from + 1 },
+ userEvent: "input.type",
+ });
+ return true;
+}
+
+export function stringsAutoCloseBraces(): Extension {
+ return EditorView.inputHandler.of(stringBraceInputHandler);
+}