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.

feat: duplicate file from sidebar, insert snippet to read file (#5357)

Myles Scolnick75d4cfc47ba0
diff --git a/frontend/src/components/editor/file-tree/file-explorer.tsx b/frontend/src/components/editor/file-tree/file-explorer.tsx
index 98ac793360..83b79c46c8 100644
--- a/frontend/src/components/editor/file-tree/file-explorer.tsx
+++ b/frontend/src/components/editor/file-tree/file-explorer.tsx
@@ -3,6 +3,7 @@
import { useAtom } from "jotai";
import {
ArrowLeftIcon,
+ BetweenHorizontalStartIcon,
BracesIcon,
ChevronDownIcon,
ChevronRightIcon,
@@ -13,6 +14,7 @@ import {
ExternalLinkIcon,
FilePlus2Icon,
FolderPlusIcon,
+ ListTreeIcon,
MoreVerticalIcon,
PlaySquareIcon,
RefreshCcwIcon,
@@ -41,7 +43,13 @@ import {
} from "@/components/ui/dropdown-menu";
import { Tooltip } from "@/components/ui/tooltip";
import { toast } from "@/components/ui/use-toast";
-import { openFile, sendFileDetails } from "@/core/network/requests";
+import { useCellActions } from "@/core/cells/cells";
+import { useLastFocusedCellId } from "@/core/cells/focus";
+import {
+ openFile,
+ sendCreateFileOrFolder,
+ sendFileDetails,
+} from "@/core/network/requests";
import type { FileInfo } from "@/core/network/types";
import { isWasm } from "@/core/wasm/utils";
import { useAsyncData } from "@/hooks/useAsyncData";
@@ -50,6 +58,7 @@ import { cn } from "@/utils/cn";
import { copyToClipboard } from "@/utils/copy";
import { downloadBlob } from "@/utils/download";
import type { FilePath } from "@/utils/paths";
+import { fileSplit } from "@/utils/pathUtils";
import { FileViewer } from "./file-viewer";
import type { RequestingTree } from "./requesting-tree";
import { openStateAtom, treeAtom } from "./state";
@@ -216,7 +225,6 @@ interface ToolbarProps {
}
const Toolbar = ({
- tree,
onRefresh,
onCreateFile,
onCreateFolder,
@@ -353,6 +361,17 @@ const Node = ({ node, style, dragHandle }: NodeRendererProps<FileInfo>) => {
const Icon = FILE_TYPE_ICONS[fileType];
const { openConfirm, openPrompt } = useImperativeModal();
+ const { createNewCell } = useCellActions();
+ const lastFocusedCellId = useLastFocusedCellId();
+
+ const handleInsertCode = (code: string) => {
+ createNewCell({
+ code,
+ before: false,
+ cellId: lastFocusedCellId ?? "__end__",
+ });
+ };
+
const tree = use(RequestingTreeContext);
const handleOpenMarimoFile = async (
@@ -404,6 +423,40 @@ const Node = ({ node, style, dragHandle }: NodeRendererProps<FileInfo>) => {
});
});
+ const handleDuplicate = useEvent(async () => {
+ if (!tree || node.data.isDirectory) {
+ return;
+ }
+
+ const [name, extension] = fileSplit(node.data.name);
+ const duplicateName = `${name}_copy${extension}`;
+
+ try {
+ // First get the file contents
+ const details = await sendFileDetails({ path: node.data.path });
+
+ // Get the parent directory path
+ const parentPath = node.parent?.data.path || "";
+
+ // Create the duplicate file by creating a new file with the same contents
+ await sendCreateFileOrFolder({
+ path: parentPath,
+ type: "file",
+ name: duplicateName,
+ contents: details.contents ? btoa(details.contents) : undefined,
+ });
+
+ // Refresh the parent folder to show the new file
+ await tree.refreshAll([parentPath]);
+ } catch {
+ toast({
+ title: "Failed to duplicate file",
+ description: "Unable to create a duplicate of the file",
+ variant: "danger",
+ });
+ }
+ });
+
const renderActions = () => {
const iconProps = {
size: 14,
@@ -450,13 +503,19 @@ const Node = ({ node, style, dragHandle }: NodeRendererProps<FileInfo>) => {
<Edit3Icon {...iconProps} />
Rename
</DropdownMenuItem>
+ {!node.data.isDirectory && (
+ <DropdownMenuItem onSelect={handleDuplicate}>
+ <CopyIcon {...iconProps} />
+ Duplicate
+ </DropdownMenuItem>
+ )}
<DropdownMenuItem
onSelect={async () => {
await copyToClipboard(node.data.path);
toast({ title: "Copied to clipboard" });
}}
>
- <CopyIcon {...iconProps} />
+ <ListTreeIcon {...iconProps} />
Copy path
</DropdownMenuItem>
{tree && (
@@ -468,10 +527,22 @@ const Node = ({ node, style, dragHandle }: NodeRendererProps<FileInfo>) => {
toast({ title: "Copied to clipboard" });
}}
>
- <CopyIcon {...iconProps} />
+ <ListTreeIcon {...iconProps} />
Copy relative path
</DropdownMenuItem>
)}
+ <DropdownMenuSeparator />
+
+ <DropdownMenuItem
+ onSelect={() => {
+ const { path } = node.data;
+ const pythonCode = PYTHON_CODE_FOR_FILE_TYPE[fileType](path);
+ handleInsertCode(pythonCode);
+ }}
+ >
+ <BetweenHorizontalStartIcon {...iconProps} />
+ Insert snippet for reading file
+ </DropdownMenuItem>
<DropdownMenuItem
onSelect={async () => {
toast({
diff --git a/frontend/src/utils/pathUtils.test.ts b/frontend/src/utils/pathUtils.test.ts
index 561764ffce..992553d2b6 100644
--- a/frontend/src/utils/pathUtils.test.ts
+++ b/frontend/src/utils/pathUtils.test.ts
@@ -1,6 +1,6 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { describe, expect, it } from "vitest";
-import { getProtocolAndParentDirectories } from "./pathUtils";
+import { fileSplit, getProtocolAndParentDirectories } from "./pathUtils";
describe("getProtocolAndParentDirectories", () => {
it("should extract protocol and list parent directories correctly", () => {
@@ -100,3 +100,14 @@ describe("getProtocolAndParentDirectories", () => {
]);
});
});
+
+describe("fileSplit", () => {
+ it("should split a path into a name and extension", () => {
+ expect(fileSplit("path/to/file.txt")).toEqual(["path/to/file", ".txt"]);
+ expect(fileSplit("path/to/file")).toEqual(["path/to/file", ""]);
+ expect(fileSplit("path/to/file.txt.md")).toEqual([
+ "path/to/file.txt",
+ ".md",
+ ]);
+ });
+});
diff --git a/frontend/src/utils/pathUtils.ts b/frontend/src/utils/pathUtils.ts
index 559eb8665a..d123d44fca 100644
--- a/frontend/src/utils/pathUtils.ts
+++ b/frontend/src/utils/pathUtils.ts
@@ -62,3 +62,10 @@ export function getProtocolAndParentDirectories(
return { protocol, parentDirectories };
}
+
+export function fileSplit(path: string): [name: string, extension: string] {
+ const lastDotIndex = path.lastIndexOf(".");
+ const name = lastDotIndex > 0 ? path.slice(0, lastDotIndex) : path;
+ const extension = lastDotIndex > 0 ? path.slice(lastDotIndex) : "";
+ return [name, extension];
+}