From ad0a755f07f897785f38d2af55dac4db96a3b950 Mon Sep 17 00:00:00 2001
From: LU XIANGRUI <1078666705@qq.com>
Date: Wed, 2 Dec 2026 22:51:56 +0900
Subject: [PATCH] fix(vcs): let the branch tree collapse and expand while
searching (#7471)
In Git > Log > Branches, folder arrows, Expand All and Collapse All did nothing while a search was active. A new search still shows every match expanded, but the user can now collapse and expand within it; clearing the search restores the expansion from before.
Fixes #7438
Co-authored-by: wildcatDownstairs <26499420+wildcatDownstairs@users.noreply.github.com>
---
.../vcs/__tests__/logBranches.test.tsx | 50 +++++++++++++++++++
src/features/vcs/log/BranchesPanel.tsx | 20 +++++---
2 files changed, 64 insertions(+), 6 deletions(-)
diff --git a/src/features/vcs/__tests__/logBranches.test.tsx b/src/features/vcs/__tests__/logBranches.test.tsx
index f4a4ed928b..1261a8bcb1 100644
--- a/src/features/vcs/__tests__/logBranches.test.tsx
+++ b/src/features/vcs/__tests__/logBranches.test.tsx
@@ -129,6 +129,56 @@ describe("Git Log branches pane", () => {
expect(screen.getByText("feature")).toBeInTheDocument();
});
+ describe("while searching", () => {
+ const remotes = ["bugfix", "develop", "smoke"].map((n) => b(`origin/stable/sp4/${n}`, { remote: true }));
+ const search = (value: string) =>
+ fireEvent.change(screen.getByLabelText("Filter branches and tags"), { target: { value } });
+ const toggle = (label: string, key: "ArrowLeft" | "ArrowRight") => {
+ fireEvent.mouseDown(screen.getByText(label));
+ fireEvent.keyDown(screen.getByRole("tree", { name: "Branches" }), { key });
+ };
+ beforeEach(() => useVcs.setState({ branches: [...branches, ...remotes] }));
+
+ it("collapses and expands a matched folder", () => {
+ render();
+ search("sp4/");
+ expect(screen.getByText("bugfix")).toBeInTheDocument();
+ toggle("sp4", "ArrowLeft");
+ expect(screen.queryByText("bugfix")).toBeNull();
+ toggle("sp4", "ArrowRight");
+ expect(screen.getByText("bugfix")).toBeInTheDocument();
+ });
+
+ it("expands every match again when the search changes", () => {
+ render();
+ search("sp4/");
+ toggle("sp4", "ArrowLeft");
+ search("sp4/b");
+ expect(screen.getByText("bugfix")).toBeInTheDocument();
+ search("sp4/");
+ expect(screen.getByText("bugfix")).toBeInTheDocument();
+ });
+
+ it("puts back the expansion from before the search when it is cleared", () => {
+ render();
+ toggle("Local", "ArrowLeft");
+ expect(screen.queryByText("feature")).toBeNull();
+ search("sp4/");
+ toggle("sp4", "ArrowLeft");
+ search("");
+ expect(screen.queryByText("feature")).toBeNull();
+ });
+
+ it("collapses and expands every group from the toolbar", () => {
+ render();
+ search("sp4/");
+ fireEvent.click(screen.getByRole("button", { name: "Collapse All" }));
+ expect(screen.queryByText("bugfix")).toBeNull();
+ fireEvent.click(screen.getByRole("button", { name: "Expand All" }));
+ expect(screen.getByText("bugfix")).toBeInTheDocument();
+ });
+ });
+
it("binds Expand All / Collapse All to the primary modifier with plus and minus", () => {
render();
const tree = screen.getByRole("tree", { name: "Branches" });
diff --git a/src/features/vcs/log/BranchesPanel.tsx b/src/features/vcs/log/BranchesPanel.tsx
index 7fa84c3423..ffda7adf15 100644
--- a/src/features/vcs/log/BranchesPanel.tsx
+++ b/src/features/vcs/log/BranchesPanel.tsx
@@ -229,8 +229,16 @@ export function BranchesPanel() {
lines,
]);
const roots = useMemo(() => filterBranchTree(allRoots, query), [allRoots, query]);
- // While searching every match is shown expanded.
- const searchExpanded = useMemo(() => (query.trim() ? new Set(branchGroupKeys(roots)) : undefined), [roots, query]);
+ // A new search shows every match expanded; within that search the user can still collapse and
+ // expand (IDEA), and clearing it brings back the expansion from before the search.
+ const allMatches = useMemo(() => (query.trim() ? new Set(branchGroupKeys(roots)) : undefined), [roots, query]);
+ const [searchKeys, setSearchKeys] = useState>();
+ const searchExpanded = allMatches && (searchKeys ?? allMatches);
+ const setExpanded = (keys: ReadonlySet) => (allMatches ? setSearchKeys(keys) : setExpandedKeys(keys));
+ const search = (next: string) => {
+ setQuery(next);
+ setSearchKeys(undefined);
+ };
const defaultExpanded = useMemo(
() => [
...branchGroupKeys(allRoots),
@@ -320,7 +328,7 @@ export function BranchesPanel() {
// The selected branch in each repository it was picked in (a merged branch: all of them).
const favoriteTargets = targets.filter((t) => t.branch && t.ref === target?.ref);
const favorite = favoriteTargets.some((t) => isFavoriteBranch(t.branch!, favoritesOf(t.repo)));
- const expandAll = () => setExpandedKeys(new Set(expandable.keys));
+ const expandAll = () => setExpanded(new Set(expandable.keys));
// Collapse All keeps the selected rows reachable: their ancestors stay open, so the selection
// does not end up inside a collapsed group.
const collapseAll = () => {
@@ -328,7 +336,7 @@ export function BranchesPanel() {
for (const key of selected) {
for (let parent = expandable.parents.get(key); parent; parent = expandable.parents.get(parent)) keep.add(parent);
}
- setExpandedKeys(keep);
+ setExpanded(keep);
};
const onKeyDownCapture = (e: KeyboardEvent) => {
// Expand All / Collapse All while the branch tree has focus: ⌘+ / ⌘-, and on Windows and
@@ -427,7 +435,7 @@ export function BranchesPanel() {
@@ -441,7 +449,7 @@ export function BranchesPanel() {
isExpandable={(n) => !!n.children}
defaultExpanded={defaultExpanded}
expanded={searchExpanded ?? expandedKeys}
- onExpandedChange={(next) => setExpandedKeys(next)}
+ onExpandedChange={setExpanded}
getIcon={(n) =>
n.kind === "head"
? "commit"