feat(agents): reference changed files dragged from the Commit tool window (#8193)

Files dragged from the Changes tree (Commit tool window, the Git window's Local Changes tab and the Commit dialog) could only be dropped on a change list. They now also land on an agent composer as @ references, like files dragged from the Project view; a multi-selection drags every selected file. The changes drag type moves to ui/pointerDrag.ts as CHANGES_DRAG so the composer can accept it without importing the VCS feature; dropping onto a change list is unchanged.

Co-authored-by: chiyanking <[email protected]>
Цей коміт міститься в:
tongkangwangтаchiyanking authored and GitHub committed 2026-12-05 17:48:26 +08:00
1 джерело 63830dcbaf
коміт 94a28cbd1d
7 files changed
+173 -10

No files matched your search

+1 -1
Переглянути файл
@@ -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),
+5 -3
Переглянути файл
@@ -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) => {
+21 -1
Переглянути файл
@@ -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) =>
+4 -5
Переглянути файл
@@ -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"),
});
},
+136
Переглянути файл
@@ -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<typeof import("@/platform/ipc")>();
return { ...real, call: vi.fn(async () => null) };
});
vi.mock("../DiffEditor", () => ({ DiffEditor: () => <div /> }));
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(<ChangesTree mode="commit" />);
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(<ChangesTree mode="commit" />);
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(<ChangesTree mode="commit" />);
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" });
});
});
+1
Переглянути файл
@@ -12,6 +12,7 @@ export * from "./Select";
export * from "./Tabs";
export * from "./Toolbar";
export {
CHANGES_DRAG,
FILES_DRAG,
REMOTE_FILES_DRAG,
dragRequiresAlt,
+5
Переглянути файл
@@ -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;