diff --git a/src/features/agents/Composer.module.css b/src/features/agents/Composer.module.css index b84e9b0af2..39758360b3 100644 --- a/src/features/agents/Composer.module.css +++ b/src/features/agents/Composer.module.css @@ -22,7 +22,7 @@ inset 0 0 0 1px var(--focus-ring), 0 0 0 1px var(--focus-ring); } -/* A file or folder dragged over it from the Project view or the OS. */ +/* A file or folder dragged over it from the Project view, the Commit tool window or the OS. */ .fileOver { box-shadow: inset 0 0 0 1px var(--focus-ring), diff --git a/src/features/agents/Composer.tsx b/src/features/agents/Composer.tsx index 9f08f7eb6c..b30e177156 100644 --- a/src/features/agents/Composer.tsx +++ b/src/features/agents/Composer.tsx @@ -55,6 +55,7 @@ import { type AgentContextSource, } from "@/workbench/contributions"; import { + CHANGES_DRAG, FILES_DRAG, Icon, IconButton, @@ -1465,13 +1466,14 @@ export function Composer(props: ComposerProps) { }; }, [addPaths, rememberImageCaret]); - // Files and folders dragged from the Project view, and file tabs dragged from the editor: - // the same `@` references. + // Files and folders dragged from the Project view, changed files dragged from the Commit tool + // window, and file tabs dragged from the editor: the same `@` references. useEffect(() => { const el = boxRef.current; if (!el) return; return registerDropTarget(el, { - accepts: (type, data) => type === FILES_DRAG || (type === EDITOR_TAB_MIME && !!tabFilePath(data.payload)), + accepts: (type, data) => + type === FILES_DRAG || type === CHANGES_DRAG || (type === EDITOR_TAB_MIME && !!tabFilePath(data.payload)), over: () => setFileOver(true), leave: () => setFileOver(false), drop: (data) => { diff --git a/src/features/agents/composerFolders.test.tsx b/src/features/agents/composerFolders.test.tsx index 23f354f07f..20883c4886 100644 --- a/src/features/agents/composerFolders.test.tsx +++ b/src/features/agents/composerFolders.test.tsx @@ -6,7 +6,7 @@ import { trackEditorCopies } from "./references"; import userEvent from "@testing-library/user-event"; import { caretOf, setCaret, textOf, typeChars, typeIn } from "./composerTesting"; import chipStyles from "./ReferenceChip.module.css"; -import { FILES_DRAG, dragSession, useContextMenuStore } from "@/ui"; +import { CHANGES_DRAG, FILES_DRAG, dragSession, useContextMenuStore } from "@/ui"; import { EDITOR_TAB_MIME, provideEditorService, type EditorService } from "@/workbench/contributions"; import { useProject } from "@/platform/project"; @@ -154,6 +154,26 @@ describe("Composer dropped paths", () => { expect(textOf(box)).toBe("@src/a.ts @src/dir/ "); }); + it("references the files dragged from the Commit tool window's Changes tree", () => { + const box = renderComposer(); + const restore = document.elementFromPoint; + document.elementFromPoint = () => box; + try { + act(() => { + const drag = dragSession({ type: CHANGES_DRAG, payload: "/p/docs/a.md\n/p/docs/b.md\n" }); + expect(drag.move(0, 0)).toBe(true); + expect(drag.drop(0, 0)).toBe(true); + }); + } finally { + document.elementFromPoint = restore; + } + expect(composerDraft(ID).files).toEqual([ + { path: "/p/docs/a.md", rel: "docs/a.md" }, + { path: "/p/docs/b.md", rel: "docs/b.md" }, + ]); + expect(textOf(box)).toBe("@docs/a.md @docs/b.md "); + }); + it("references the file of an editor tab dragged onto it and ignores other tabs", () => { provideEditorService({ tabInput: (_pane: string, tab: string) => diff --git a/src/features/vcs/CommitView.tsx b/src/features/vcs/CommitView.tsx index 65aa365775..914358ea02 100644 --- a/src/features/vcs/CommitView.tsx +++ b/src/features/vcs/CommitView.tsx @@ -32,6 +32,7 @@ import { } from "@/platform/projectRoots"; import { Button, + CHANGES_DRAG, Checkbox, EmptyState, Icon, @@ -162,8 +163,6 @@ import { saveVcsSetting } from "./settingsUi"; import { TOGGLE_IGNORED_FILES, useIgnoredFiles } from "./ignoredFiles"; import styles from "./CommitView.module.css"; -/** MIME type of files dragged onto a change list (absolute paths, newline-separated). */ -const CHANGES_MIME = "application/x-openidea-changes"; const EMPTY_ATTACHED: AttachedProject[] = []; /** Shown text of a node: group names are translated, except the names of user change lists. */ @@ -1338,14 +1337,14 @@ export function ChangesTree({ mode, amended }: { mode: "commit" | "local" | "dia // changelist takes dropped files into it. Dragging a change-list // header moves all its files, and dragging the Unversioned Files // header adds all of them to Git and into the list, as in PyCharm's - // Commit view. + // Commit view. Dropped on an agent composer, the files are `@` references. const list = n.changelist; const dnd = { ...(list !== undefined && { ref: (el: HTMLSpanElement | null) => el ? registerDropTarget(el, { - accepts: (type) => type === CHANGES_MIME, + accepts: (type) => type === CHANGES_DRAG, over: () => setDropTarget(list), leave: () => setDropTarget(null), drop: (data) => { @@ -1364,7 +1363,7 @@ export function ChangesTree({ mode, amended }: { mode: "commit" | "local" | "dia ? filesOf(selectedNodes().filter((node) => node.group !== "ignored")).map((f) => f.path) : n.files.map((f) => f.path); startPointerDrag(e, e.currentTarget, { - type: CHANGES_MIME, + type: CHANGES_DRAG, payload: paths.map(stripSlash).join("\n"), }); }, diff --git a/src/features/vcs/__tests__/changesDrag.test.tsx b/src/features/vcs/__tests__/changesDrag.test.tsx new file mode 100644 index 0000000000..bc71eda6c1 --- /dev/null +++ b/src/features/vcs/__tests__/changesDrag.test.tsx @@ -0,0 +1,136 @@ +import { act, fireEvent, render, screen } from "@testing-library/react"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +vi.mock("@/platform/ipc", async (importOriginal) => { + const real = await importOriginal(); + return { ...real, call: vi.fn(async () => null) }; +}); + +vi.mock("../DiffEditor", () => ({ DiffEditor: () =>
})); + +import { CHANGES_DRAG, registerDropTarget } from "@/ui"; +import { ChangesTree } from "../CommitView"; +import { initialChangelists, useChangelists } from "../changelists"; +import { useCommit } from "../commitStore"; +import { useVcs } from "../store"; + +const change = (path: string) => ({ + path, + relPath: path.replace(/^\/p\//, ""), + staged: null, + unstaged: "modified" as const, + renamedFrom: null, +}); + +/** The `.nodeRow` carrying a label (the drag source). */ +function row(name: string): HTMLElement { + const label = screen.getByText(name, { exact: true }); + const r = label.closest("span[class*='nodeRow']"); + if (!r) throw new Error(`no node row for ${name}`); + return r as HTMLElement; +} + +function treeItem(name: string): HTMLElement { + return screen.getByText(name, { exact: true }).closest('[role="treeitem"]') as HTMLElement; +} + +describe("dragging changed files out of the Changes tree", () => { + const originalElementFromPoint = document.elementFromPoint; + + beforeEach(() => { + localStorage.clear(); + useCommit.setState({ groupByDir: false, checks: new Map(), selection: [] }); + useChangelists.setState({ + state: { + ...initialChangelists(), + lists: [{ id: "default", name: "Changes", comment: "" }], + files: { "docs/a.md": "default", "docs/b.md": "default", "docs/c.md": "default" }, + }, + }); + useVcs.setState({ + root: "/p", + repo: "/p", + roots: ["/p"], + heads: {}, + status: [change("/p/docs/a.md"), change("/p/docs/b.md"), change("/p/docs/c.md")], + }); + }); + + afterEach(() => { + document.elementFromPoint = originalElementFromPoint; + }); + + /** A target elsewhere in the window (an agent composer) that takes what the tree drags. */ + function outside() { + const host = document.createElement("div"); + document.body.appendChild(host); + const dropped: Array<{ type: string; payload: string }> = []; + const unregister = registerDropTarget(host, { + accepts: (type) => type === CHANGES_DRAG, + over: () => {}, + leave: () => {}, + drop: (data) => dropped.push(data), + }); + document.elementFromPoint = () => host; + return { dropped, dispose: () => (unregister(), host.remove()) }; + } + + const drag = (source: HTMLElement) => { + fireEvent(source, new MouseEvent("pointerdown", { bubbles: true, button: 0, clientX: 10, clientY: 10 })); + act(() => { + window.dispatchEvent(new MouseEvent("pointermove", { bubbles: true, buttons: 1, clientX: 60, clientY: 60 })); + }); + act(() => { + window.dispatchEvent(new MouseEvent("pointerup", { bubbles: true, clientX: 60, clientY: 60 })); + }); + }; + + it("carries every selected file of a multi-selection", () => { + render(); + const target = outside(); + try { + act(() => fireEvent.mouseDown(treeItem("a.md"))); + act(() => fireEvent.mouseDown(treeItem("b.md"), { metaKey: true })); + drag(row("b.md")); + } finally { + target.dispose(); + } + expect(target.dropped).toEqual([{ type: CHANGES_DRAG, payload: "/p/docs/a.md\n/p/docs/b.md" }]); + }); + + it("carries the pressed row alone when it is not part of the selection", () => { + render(); + const target = outside(); + try { + act(() => fireEvent.mouseDown(treeItem("a.md"))); + act(() => fireEvent.mouseDown(treeItem("b.md"), { metaKey: true })); + drag(row("c.md")); + } finally { + target.dispose(); + } + expect(target.dropped).toEqual([{ type: CHANGES_DRAG, payload: "/p/docs/c.md" }]); + }); + + it("stays out of targets that take only Project view files", () => { + render(); + const host = document.createElement("div"); + document.body.appendChild(host); + const dropped = vi.fn(); + const unregister = registerDropTarget(host, { + accepts: (type) => type !== CHANGES_DRAG, + over: () => {}, + leave: () => {}, + drop: dropped, + }); + document.elementFromPoint = () => host; + try { + drag(row("a.md")); + } finally { + unregister(); + host.remove(); + } + expect(dropped).not.toHaveBeenCalled(); + // Nothing moved between change lists either. + expect(useChangelists.getState().state.files).toMatchObject({ "docs/a.md": "default" }); + }); +}); diff --git a/src/ui/index.ts b/src/ui/index.ts index 056ac9f4fb..f1fbd7cbe8 100644 --- a/src/ui/index.ts +++ b/src/ui/index.ts @@ -12,6 +12,7 @@ export * from "./Select"; export * from "./Tabs"; export * from "./Toolbar"; export { + CHANGES_DRAG, FILES_DRAG, REMOTE_FILES_DRAG, dragRequiresAlt, diff --git a/src/ui/pointerDrag.ts b/src/ui/pointerDrag.ts index 3d3ba7b791..ce6cb61f04 100644 --- a/src/ui/pointerDrag.ts +++ b/src/ui/pointerDrag.ts @@ -17,6 +17,11 @@ export interface DragData { export const FILES_DRAG = "application/x-openidea-files"; /** Files and folders dragged from Remote Host: `payload` is JSON `{ server, paths }` (remote paths). */ export const REMOTE_FILES_DRAG = "application/x-openidea-remote-files"; +/** + * Changed files dragged from the Commit tool window's Changes tree: `payload` is the absolute + * paths joined by `\n`. A change list takes them into it; the agent composer references them. + */ +export const CHANGES_DRAG = "application/x-openidea-changes"; export interface DropTarget { accepts(type: string, data: DragData): boolean;