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.

829 B
Raw Blame
/* 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);
}