From 736e78ade622b9952cd32ed33f7832c2a048fd46 Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Thu, 19 Nov 2026 19:44:36 +0800 Subject: [PATCH] fix(vcs): select the best-matching branch while filtering the branches popup Typing a filter in the branches popup selected the first selectable row, which is the Local group header, so Enter didn't check out the branch it found. While filtering, the popup now selects the best match of the first kind that has one: a matching action, then local branches, remote ones and tags (for several repositories: repositories, then common branches, then one repository's). A match at the start of the name's last segment or at a word start ranks higher. Fixes #4471 --- src/features/vcs/BranchWidget.tsx | 27 +++++++-- src/features/vcs/__tests__/branches.test.ts | 39 +++++++++++++ .../vcs/__tests__/repoBranches.test.ts | 11 ++++ src/features/vcs/branches.ts | 57 +++++++++++++++++++ src/features/vcs/repoBranches.ts | 21 +++++++ 5 files changed, 150 insertions(+), 5 deletions(-) diff --git a/src/features/vcs/BranchWidget.tsx b/src/features/vcs/BranchWidget.tsx index 04a52319c2..4afa1af025 100644 --- a/src/features/vcs/BranchWidget.tsx +++ b/src/features/vcs/BranchWidget.tsx @@ -25,7 +25,7 @@ import { T, } from "@/ui"; import type { PopupAnchor } from "@/ui/Popup"; -import { buildBranchRows, withExpanded, type BranchRow, type PopupAction } from "./branches"; +import { buildBranchRows, preferredBranchRow, withExpanded, type BranchRow, type PopupAction } from "./branches"; import { branchPresentation, extensionPopupActions, useBranchWidgetExtensions } from "./branchWidgetExtensions"; import { branchMenu, commonBranchMenu, inRepo, tagMenu } from "./branchMenu"; import { loadFavoriteBranches, recentBranches, toggleFavoriteBranch, useFavoriteBranches } from "./branchOps"; @@ -33,6 +33,7 @@ import { inProgress, operationTitle, type InProgress } from "./operations"; import { buildMultiRepoRows, currentRepoFor, + preferredRepoRow, repoHeadLabel, widgetShowsRepo, type RepoEntry, @@ -45,6 +46,22 @@ import type { SyncDirection } from "./syncCommits"; import styles from "./BranchWidget.module.css"; import { tx } from "@/platform/i18n"; +/** `active` until the user moves: a new filter selects the row IDEA prefers for it. */ +const PREFERRED = -1; + +function pickActive( + rows: readonly R[], + active: number, + isSection: (r: R) => boolean, + preferred: () => number, +): number { + if (active === PREFERRED) { + const i = preferred(); + if (i >= 0) return i; + } else if (rows[active] && !isSection(rows[active]!)) return active; + return firstSelectable(rows, isSection); +} + export const useBranchesPopup = create<{ open: boolean }>(() => ({ open: false })); export const showBranchesPopup = () => useBranchesPopup.setState({ open: true }); @@ -446,7 +463,7 @@ function BranchesPanel({ onClose, actions: baseActions, root, fallback, onBack } [branches, recent, query, actions, favorites, tags, options, expanded], ); const isSection = (r: BranchRow) => r.type === "section"; - const activeIndex = rows[active] && !isSection(rows[active]!) ? active : firstSelectable(rows, isSection); + const activeIndex = pickActive(rows, active, isSection, () => preferredBranchRow(rows, query)); const scoped = (items: MenuItem[]) => (root ? inRepo(root, items) : items); const closeAll = () => { @@ -528,7 +545,7 @@ function BranchesPanel({ onClose, actions: baseActions, root, fallback, onBack } value={query} onChange={(v) => { setQuery(v); - setActive(0); + setActive(PREFERRED); }} placeholder={label} aria-label={label} @@ -683,7 +700,7 @@ function MultiRepoPanel({ onClose, actions: popupActions }: { onClose: () => voi [repos, all, query, actions, expanded, favorites, current, recent, tags, options], ); const isSection = (r: RepoRow) => r.type === "section"; - const activeIndex = rows[active] && !isSection(rows[active]!) ? active : firstSelectable(rows, isSection); + const activeIndex = pickActive(rows, active, isSection, () => preferredRepoRow(rows, query)); const closeAll = () => { setMenuFor(null); @@ -778,7 +795,7 @@ function MultiRepoPanel({ onClose, actions: popupActions }: { onClose: () => voi value={query} onChange={(v) => { setQuery(v); - setActive(0); + setActive(PREFERRED); }} placeholder="Search for branches, actions, repositories" aria-label="Search for branches, actions, repositories" diff --git a/src/features/vcs/__tests__/branches.test.ts b/src/features/vcs/__tests__/branches.test.ts index e0ec909826..9940ea87d0 100644 --- a/src/features/vcs/__tests__/branches.test.ts +++ b/src/features/vcs/__tests__/branches.test.ts @@ -7,6 +7,7 @@ import { buildBranchTree, filterBranchTree, isFavoriteBranch, + preferredBranchRow, pushRecent, type BranchRow, type BranchTreeNode, @@ -184,6 +185,44 @@ describe("buildBranchRows", () => { }); }); +describe("preferredBranchRow", () => { + const ticket = [ + b("feature/lwp/KXSC-3599"), + b("feature/lwp/KXSC-5994"), + b("feature/lwp/KXSC-5997"), + b("KXSC-5990"), + b("origin/feature/lwp/KXSC-5994", { remote: true }), + ]; + const picked = (list: Branch[], query: string, acts = actions) => { + const rows = buildBranchRows(list, [], query, acts); + const i = preferredBranchRow(rows, query); + return i < 0 ? null : summary(rows)[i]!.trim(); + }; + + it("selects the best-matching local branch, not the group above it", () => { + expect(picked(ticket, "599")).toBe("local:KXSC-5994"); + }); + + it("prefers a match at the start of the name's last segment", () => { + expect(picked([b("fix-main-build"), b("release/main")], "main")).toBe("local:main"); + expect(picked([b("feature/ordering"), b("hotfix/order")], "order")).toBe("local:order"); + }); + + it("falls back to remote branches, then tags", () => { + expect(picked([b("main"), b("origin/feature/x", { remote: true })], "feature")).toBe("remote:x"); + expect( + summary(buildBranchRows([b("main")], [], "v1", [], null, ["v1.0"]))[ + preferredBranchRow(buildBranchRows([b("main")], [], "v1", [], null, ["v1.0"]), "v1") + ], + ).toBe(" tag:v1.0"); + }); + + it("selects a matching action first, and nothing without a query", () => { + expect(picked(branches, "push")).toBe("> Push…"); + expect(preferredBranchRow(buildBranchRows(branches, [], "", actions), "")).toBe(-1); + }); +}); + describe("branchNamePrefixes", () => { it("lists local branch folders shallowest first, once each, without remote ones", () => { const prefixes = branchNamePrefixes([ diff --git a/src/features/vcs/__tests__/repoBranches.test.ts b/src/features/vcs/__tests__/repoBranches.test.ts index f38f2d756e..7b011cf407 100644 --- a/src/features/vcs/__tests__/repoBranches.test.ts +++ b/src/features/vcs/__tests__/repoBranches.test.ts @@ -5,6 +5,7 @@ import { buildMultiRepoRows, commonBranches, currentRepoFor, + preferredRepoRow, repoColorIndex, repoHeadLabel, widgetShowsRepo, @@ -163,6 +164,16 @@ describe("buildMultiRepoRows", () => { ]); }); + it("selects the best match while filtering: repositories, then common branches, then one repository's", () => { + const picked = (query: string) => { + const rows = buildMultiRepoRows({ ...input, expanded: new Map(), query }); + return summary(rows)[preferredRepoRow(rows, query)]?.trim(); + }; + expect(picked("dev")).toBe("dev (current)"); + expect(picked("deps")).toBe("repo ology-dependencies [dev] c2"); + expect(picked("fix")).toBe("ology-gateway:gw-fix"); + }); + it("filters repositories and branches, expands every group and lists each repository's own matches", () => { expect(summary(buildMultiRepoRows({ ...input, expanded: new Map(), query: "dev" }))).toEqual([ "v Common Local Branches", diff --git a/src/features/vcs/branches.ts b/src/features/vcs/branches.ts index fadb3a075a..2e20299cdd 100644 --- a/src/features/vcs/branches.ts +++ b/src/features/vcs/branches.ts @@ -208,6 +208,63 @@ export function recentLocal(local: readonly Branch[], recent: readonly string[]) export const sortTags = (tags: readonly string[]) => [...tags].sort((a, b) => b.localeCompare(a, undefined, { numeric: true })); +/** + * How well `name` matches `query`: at the start of its last segment, at a word start, matching the + * segment to its end, then nearer the segment's start. + */ +function matchDegree(name: string, query: string): number { + const match = wordMatch(name, query); + const at = match?.[0]; + if (at === undefined) return -Infinity; + const segment = name.lastIndexOf("/") + 1; + const wordStart = at === 0 || /[\s_\-./\\:]/.test(name[at - 1]!); + const toEnd = match!.at(-1) === name.length - 1; + return (at === segment ? 4 : 0) + (wordStart ? 2 : 0) + (toEnd ? 1 : 0) - Math.abs(at - segment) / (name.length + 1); +} + +/** + * The row to select while filtering, as IDEA's branches popup does: the best match of the + * lowest-ranked kind that has one (matching actions, then local branches, remote ones, tags). + * -1 without a query or a candidate. + */ +export function preferredMatch( + rows: readonly R[], + query: string, + candidate: (row: R) => { rank: number; name: string } | null, +): number { + const q = query.trim(); + if (!q) return -1; + let best = -1; + let bestRank = Infinity; + let bestDegree = -Infinity; + rows.forEach((row, i) => { + const c = candidate(row); + if (!c || c.rank > bestRank) return; + const degree = matchDegree(c.name, q); + if (c.rank < bestRank || degree > bestDegree) { + best = i; + bestRank = c.rank; + bestDegree = degree; + } + }); + return best; +} + +const GROUP_RANK: Record = { recent: 1, local: 2, remote: 3 }; + +/** `preferredMatch` for the branches popup of one repository. */ +export function preferredBranchRow(rows: readonly BranchRow[], query: string): number { + return preferredMatch(rows, query, (r) => + r.type === "action" + ? { rank: 0, name: tx(r.action.label) } + : r.type === "branch" + ? { rank: GROUP_RANK[r.group], name: r.branch.name } + : r.type === "tag" + ? { rank: 4, name: r.tag } + : null, + ); +} + /** * Rows for the Branches popup: actions (separated by group), then the collapsible * Recent (recently checked-out local branches other than the current one), Local, diff --git a/src/features/vcs/repoBranches.ts b/src/features/vcs/repoBranches.ts index ad908f3028..303d075799 100644 --- a/src/features/vcs/repoBranches.ts +++ b/src/features/vcs/repoBranches.ts @@ -7,6 +7,7 @@ import { actionRows, defaultFolderOpen, isFavoriteBranch, + preferredMatch, prefixTreeRows, recentLocal, sortLocal, @@ -346,3 +347,23 @@ export function buildMultiRepoRows({ } return rows; } + +/** `preferredMatch` for the branches popup of several repositories: actions, repositories, common branches, then one repository's. */ +export function preferredRepoRow(rows: readonly RepoRow[], query: string): number { + return preferredMatch(rows, query, (r) => { + switch (r.type) { + case "action": + return { rank: 0, name: tx(r.action.label) }; + case "repo": + return { rank: 1, name: r.repo.name }; + case "common": + return { rank: r.branch.remote ? 3 : 2, name: r.branch.name }; + case "branch": + return { rank: r.group === "remote" ? 5 : 4, name: r.branch.name }; + case "tag": + return { rank: 6, name: r.tag }; + default: + return null; + } + }); +}