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.

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

import { PlayIcon } from "lucide-react";
import React from "react";
import { MinimalHotkeys } from "@/components/shortcuts/renderShortcut";
import { Button } from "@/components/ui/button";
import { Tooltip } from "@/components/ui/tooltip";
import { isPlatformMac } from "@/core/hotkeys/shortcuts";

export const AI_EDIT_ACTION_LABELS = {
  accept: "Keep change",
  decline: "Revert change",
} as const;

/**
 * Common keyboard shortcut handlers for AI completions
 */
export const createAiCompletionOnKeydown = (opts: {
  handleAcceptCompletion: () => void;
  handleDeclineCompletion: () => void;
  isLoading: boolean;
  hasCompletion: boolean;
}) => {
  const {
    handleAcceptCompletion,
    handleDeclineCompletion,
    isLoading,
    hasCompletion,
  } = opts;

  return (e: React.KeyboardEvent<HTMLDivElement>) => {
    const metaKey = isPlatformMac() ? e.metaKey : e.ctrlKey;

    // Mod+Enter should accept the completion, if there is one
    if (metaKey && e.key === "Enter" && !isLoading && hasCompletion) {
      handleAcceptCompletion();
    }

    // Mod+Shift+Delete should decline the completion
    const deleteKey = e.key === "Delete" || e.key === "Backspace";
    if (deleteKey && metaKey && e.shiftKey) {
      handleDeclineCompletion();
      e.preventDefault();
      e.stopPropagation();
    }
  };
};

export const CompletionActionsCellFooter: React.FC<{
  isLoading: boolean;
  onAccept: () => void;
  onDecline: () => void;
  size?: "xs" | "sm";
  multipleCompletions?: boolean;
  runCell?: () => void;
  acceptLabel?: string;
  declineLabel?: string;
}> = ({
  isLoading,
  onAccept,
  onDecline,
  runCell,
  acceptLabel,
  declineLabel,
  size,
}) => {
  return (
    <>
      <AcceptCompletionButton
        isLoading={isLoading}
        onAccept={onAccept}
        size={size}
        runCell={runCell}
        label={acceptLabel}
      />
      <RejectCompletionButton
        onDecline={onDecline}
        size={size}
        label={declineLabel}
        disabled={isLoading}
      />
    </>
  );
};

export const AcceptCompletionButton: React.FC<{
  isLoading: boolean;
  onAccept: () => void;
  multipleCompletions?: boolean;
  size?: "xs" | "sm";
  buttonStyles?: string;
  playButtonStyles?: string;
  borderless?: boolean;
  acceptShortcut?: string;
  runCell?: () => void;
  label?: string;
}> = ({
  isLoading,
  onAccept,
  multipleCompletions = false,
  size = "sm",
  buttonStyles,
  acceptShortcut,
  runCell,
  playButtonStyles,
  borderless = false,
  label,
}) => {
  const handleAcceptAndRun = () => {
    onAccept();
    if (runCell) {
      runCell();
    }
  };

  const text = label ?? (multipleCompletions ? "Accept all" : "Accept");

  const baseClasses = `h-6 text-(--grass-11) bg-(--grass-3)/60
    hover:bg-(--grass-3) dark:bg-(--grass-4)/80 dark:hover:bg-(--grass-3) font-semibold
    active:bg-(--grass-5) dark:active:bg-(--grass-4)
    border-(--green-6) border hover:shadow-xs`;

  if (runCell) {
    return (
      <div className="flex">
        <Button
          variant="text"
          size={size}
          disabled={isLoading}
          onClick={onAccept}
          className={`${baseClasses} rounded-r-none ${borderless && "border-none rounded-md rounded-r-none"} ${buttonStyles}`}
        >
          {text}
          {acceptShortcut && (
            <MinimalHotkeys className="ml-1 inline" shortcut={acceptShortcut} />
          )}
        </Button>
        <Tooltip
          content={`${text} and run ${multipleCompletions ? "all cells" : "cell"}`}
        >
          <Button
            variant="text"
            size={size}
            disabled={isLoading}
            onClick={handleAcceptAndRun}
            className={`${baseClasses} rounded-l-none px-1.5 ${borderless && "border-0 border-l"} ${playButtonStyles}`}
          >
            <PlayIcon className="h-2.5 w-2.5" />
          </Button>
        </Tooltip>
      </div>
    );
  }

  return (
    <Button
      variant="text"
      size={size}
      disabled={isLoading}
      onClick={onAccept}
      className={`${baseClasses} rounded px-3 ${buttonStyles}`}
    >
      {text}
      {acceptShortcut && (
        <MinimalHotkeys className="ml-1 inline" shortcut={acceptShortcut} />
      )}
    </Button>
  );
};

export const RejectCompletionButton: React.FC<{
  onDecline: () => void;
  disabled?: boolean;
  multipleCompletions?: boolean;
  size?: "xs" | "sm";
  className?: string;
  borderless?: boolean;
  declineShortcut?: string;
  label?: string;
}> = ({
  onDecline,
  disabled = false,
  multipleCompletions = false,
  size = "sm",
  className,
  declineShortcut,
  borderless = false,
  label,
}) => {
  return (
    <Button
      variant="text"
      size={size}
      disabled={disabled}
      onClick={onDecline}
      className={`h-6 text-(--red-10) bg-(--red-3)/60 hover:bg-(--red-3)
    dark:bg-(--red-4)/80 dark:hover:bg-(--red-3) rounded px-3 font-semibold
    active:bg-(--red-5) dark:active:bg-(--red-4)
    border-(--red-6) border hover:shadow-xs ${borderless && "border-none rounded-md"} ${className}`}
    >
      {label ?? `Reject${multipleCompletions ? " all" : ""}`}
      {declineShortcut && (
        <MinimalHotkeys className="ml-1 inline" shortcut={declineShortcut} />
      )}
    </Button>
  );
};