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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
improvement: add hotkeys to AI completion (#6094)
Myles Scolnickbd0044724725
diff --git a/frontend/src/components/editor/ai/add-cell-with-ai.tsx b/frontend/src/components/editor/ai/add-cell-with-ai.tsxindex c7f247e65d..dbcb0f2ad7 100644--- a/frontend/src/components/editor/ai/add-cell-with-ai.tsx+++ b/frontend/src/components/editor/ai/add-cell-with-ai.tsx@@ -50,6 +50,10 @@ import { cn } from "@/utils/cn";import { prettyError } from "@/utils/errors";import { useCellActions } from "../../../core/cells/cells";import { PythonIcon } from "../cell/code/icons";+import {+ CompletionActions,+ createAiCompletionOnKeydown,+} from "./completion-handlers";import {getAICompletionBody,mentionsCompletionSource,@@ -160,6 +164,23 @@ export const AddCellWithAI: React.FC<{</DropdownMenu>);+ const handleAcceptCompletion = () => {+ createNewCell({+ cellId: "__end__",+ before: false,+ code:+ language === "python"+ ? completion+ : SQLLanguageAdapter.fromQuery(completion),+ });+ setCompletion("");+ onClose();+ };++ const handleDeclineCompletion = () => {+ setCompletion("");+ };+const inputComponent = (<div className="flex items-center px-3"><SparklesIcon className="size-4 text-(--blue-11) mr-2" />@@ -174,6 +195,12 @@ export const AddCellWithAI: React.FC<{setCompletionBody(getAICompletionBody({ input: newValue }));}}onSubmit={submit}+ onKeyDown={createAiCompletionOnKeydown({+ handleAcceptCompletion,+ handleDeclineCompletion,+ isLoading,+ completion,+ })}/>{isLoading && (<Button@@ -212,37 +239,12 @@ export const AddCellWithAI: React.FC<{</span>)}{completion && (- <>- <Button- data-testid="accept-completion-button"- variant="text"- size="sm"- className="mb-0"- onClick={() => {- createNewCell({- cellId: "__end__",- before: false,- code:- language === "python"- ? completion- : SQLLanguageAdapter.fromQuery(completion),- });- setCompletion("");- onClose();- }}- >- <span className="text-(--grass-11)">Accept</span>- </Button>- <Button- data-testid="decline-completion-button"- variant="text"- size="sm"- className="mb-0 pl-1"- onClick={() => setCompletion("")}- >- <span className="text-(--red-10)">Reject</span>- </Button>- </>+ <CompletionActions+ isLoading={isLoading}+ onAccept={handleAcceptCompletion}+ onDecline={handleDeclineCompletion}+ size="sm"+ />)}<div className="ml-auto flex items-center gap-1">{languageDropdown}@@ -281,6 +283,7 @@ interface PromptInputProps {placeholder?: string;value: string;className?: string;+ onKeyDown?: (e: React.KeyboardEvent<HTMLDivElement>) => void;onClose: () => void;onChange: (value: string) => void;onSubmit: (e: KeyboardEvent | undefined, value: string) => void;@@ -301,6 +304,7 @@ export const PromptInput = ({className,onChange,onSubmit,+ onKeyDown,onClose,additionalCompletions,maxHeight,@@ -416,6 +420,7 @@ export const PromptInput = ({basicSetup={false}extensions={extensions}onChange={onChange}+ onKeyDown={onKeyDown}theme={theme === "dark" ? "dark" : "light"}placeholder={placeholder || "Generate with AI"}/>diff --git a/frontend/src/components/editor/ai/ai-completion-editor.tsx b/frontend/src/components/editor/ai/ai-completion-editor.tsxindex 04d7fd1643..b7c92515fd 100644--- a/frontend/src/components/editor/ai/ai-completion-editor.tsx+++ b/frontend/src/components/editor/ai/ai-completion-editor.tsx@@ -25,6 +25,10 @@ import { cn } from "@/utils/cn";import { prettyError } from "@/utils/errors";import { retryWithTimeout } from "@/utils/timeout";import { PromptInput } from "./add-cell-with-ai";+import {+ CompletionActions,+ createAiCompletionOnKeydown,+} from "./completion-handlers";import { getAICompletionBody } from "./completion-utils";const Original = CodeMirrorMerge.Original;@@ -136,6 +140,15 @@ export const AiCompletionEditor: React.FC<Props> = ({const { theme } = useTheme();+ const handleAcceptCompletion = () => {+ acceptChange(completion);+ setCompletion("");+ };++ const handleDeclineCompletion = () => {+ setCompletion("");+ };+return (<div className={cn("flex flex-col w-full rounded-[inherit]", className)}><div@@ -165,6 +178,12 @@ export const AiCompletionEditor: React.FC<Props> = ({handleSubmit();}}}+ onKeyDown={createAiCompletionOnKeydown({+ handleAcceptCompletion,+ handleDeclineCompletion,+ isLoading,+ completion,+ })}/>{isLoading && (<Button@@ -179,32 +198,12 @@ export const AiCompletionEditor: React.FC<Props> = ({</Button>)}{completion && (- <>- <Button- data-testid="accept-completion-button"- variant="text"- size="xs"- className="mb-0"- disabled={isLoading}- onClick={() => {- acceptChange(completion);- setCompletion("");- }}- >- <span className="text-(--grass-11) opacity-100">Accept</span>- </Button>- <Button- data-testid="decline-completion-button"- variant="text"- size="xs"- className="mb-0 pl-1"- onClick={() => {- setCompletion("");- }}- >- <span className="text-(--red-10)">Reject</span>- </Button>- </>+ <CompletionActions+ isLoading={isLoading}+ onAccept={handleAcceptCompletion}+ onDecline={handleDeclineCompletion}+ size="xs"+ />)}<div className="h-full w-px bg-border mx-2" /><Tooltip content="Include code from other cells">diff --git a/frontend/src/components/editor/ai/completion-handlers.tsx b/frontend/src/components/editor/ai/completion-handlers.tsxnew file mode 100644index 0000000000..e993763f67--- /dev/null+++ b/frontend/src/components/editor/ai/completion-handlers.tsx@@ -0,0 +1,91 @@+/* Copyright 2024 Marimo. All rights reserved. */++import React from "react";+import { MinimalHotkeys } from "@/components/shortcuts/renderShortcut";+import { Button } from "@/components/ui/button";+import { isPlatformMac } from "@/core/hotkeys/shortcuts";++/**+ * Common keyboard shortcut handlers for AI completions+ */+export const createAiCompletionOnKeydown = (opts: {+ handleAcceptCompletion: () => void;+ handleDeclineCompletion: () => void;+ isLoading: boolean;+ completion: string;+}) => {+ const {+ handleAcceptCompletion,+ handleDeclineCompletion,+ isLoading,+ completion,+ } = 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") {+ if (!isLoading && completion) {+ 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();+ }+ };+};++/**+ * Common completion action buttons with keyboard shortcuts+ */+export const CompletionActions: React.FC<{+ isLoading: boolean;+ onAccept: () => void;+ onDecline: () => void;+ size?: "xs" | "sm";+ acceptShortcut?: string;+ declineShortcut?: string;+}> = ({+ isLoading,+ onAccept,+ onDecline,+ size = "sm",+ acceptShortcut = "Mod-↵",+ declineShortcut = "Shift-Mod-Delete",+}) => {+ return (+ <>+ <Button+ data-testid="accept-completion-button"+ variant="text"+ size={size}+ className="mb-0"+ disabled={isLoading}+ onClick={onAccept}+ >+ <span className="text-(--grass-11) opacity-100">+ Accept{" "}+ <MinimalHotkeys className="ml-1 inline" shortcut={acceptShortcut} />+ </span>+ </Button>+ <Button+ data-testid="decline-completion-button"+ variant="text"+ size={size}+ className="mb-0 pl-1"+ onClick={onDecline}+ >+ <span className="text-(--red-10)">+ Reject{" "}+ <MinimalHotkeys className="ml-1 inline" shortcut={declineShortcut} />+ </span>+ </Button>+ </>+ );+};