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.

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

import { EditorView } from "@codemirror/view";
import { Logger } from "@/utils/Logger";

export function insertDebuggerAtLine(view: EditorView, line: number): boolean {
  // Get the document
  const { state } = view;
  const doc = state.doc;

  // Check if the line number is valid
  if (line <= 0 || line > doc.lines) {
    Logger.warn(
      `Invalid line number: ${line}. Document has ${doc.lines} lines.`,
    );
    return false;
  }

  // Get the target line
  const targetLine = doc.line(line);

  // Skip if line already contains breakpoint()
  if (targetLine.text.includes("breakpoint()")) {
    return true;
  }

  // Extract the indentation from the target line
  const lineContent = targetLine.text;
  const indentMatch = lineContent.match(/^(\s*)/);
  const indentation = indentMatch ? indentMatch[1] : "";

  // Create the breakpoint statement with the same indentation
  const breakpointStatement = `${indentation}breakpoint()\n`;

  // Get the position where we need to insert the breakpoint statement
  const insertPos = targetLine.from;

  // Create and dispatch the transaction
  view.dispatch({
    changes: {
      from: insertPos,
      to: insertPos,
      insert: breakpointStatement,
    },
    // Scroll to the breakpoint
    selection: {
      anchor: insertPos + breakpointStatement.length - 1,
      head: insertPos + breakpointStatement.length - 1,
    },
    scrollIntoView: true,
    effects: [EditorView.scrollIntoView(insertPos, { y: "center" })],
  });

  // Focus the editor after the transaction
  view.focus();

  return true;
}