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.

2.4 KB
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */

import { python } from "@codemirror/lang-python";
import { EditorState, type Extension } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import type { Meta, StoryObj } from "@storybook/react-vite";
import CodeMirror from "@uiw/react-codemirror";
import { useEffect, useMemo, useRef } from "react";
import { CopilotConfig } from "@/core/codemirror/copilot/copilot-config";
import { copilotBundle } from "@/core/codemirror/copilot/extension";
import { OverridingHotkeyProvider } from "@/core/hotkeys/hotkeys";
import { basicBundle, type CodeMirrorSetupOpts } from "../core/codemirror/cm";

const meta: Meta = {
  title: "Editor",
  args: {},
};

export default meta;
type Story = StoryObj;

const CONTENT = `
class Foo:
    def __init__(self):
        pass

# Some comment

# some other comment
def bar():
    pass


def foo():
    # another comment
    pass
`.trim();

const Editor = (opts: { extensions?: Extension[] }): React.ReactNode => {
  const ref = useRef<HTMLDivElement>(null);
  useEffect(() => {
    if (!ref.current) {
      return;
    }

    const view = new EditorView({
      state: EditorState.create({
        extensions: opts.extensions,
        doc: CONTENT,
      }),
      parent: ref.current,
    });

    return () => view.destroy();
  }, [opts.extensions]);

  return <div className="cm" ref={ref} />;
};

const PrimaryEditor = ({ theme }: Pick<CodeMirrorSetupOpts, "theme">) => {
  const extensions = useMemo(
    () =>
      basicBundle({
        completionConfig: {
          activate_on_typing: false,
          copilot: false,
          signature_hint_on_typing: false,
        },
        theme,
        hotkeys: new OverridingHotkeyProvider({}),
      } as unknown as CodeMirrorSetupOpts),
    [theme],
  );

  return (
    <div className="Cell m-20 w-[60%] overflow-hidden">
      <Editor extensions={extensions} />
    </div>
  );
};

export const Primary: Story = {
  render: (_args, ctx) => <PrimaryEditor theme={ctx.globals.theme} />,
};

export const DefaultPython: Story = {
  render: () => (
    <div className="m-20 w-[60%] overflow-hidden">
      <CodeMirror
        extensions={[
          python(),
          copilotBundle({
            activate_on_typing: true,
            signature_hint_on_typing: false,
            copilot: false,
            codeium_api_key: null,
          }),
        ]}
      />
      <CopilotConfig />
    </div>
  ),
};