Порівняти коміти

...
Автор SHA1 Повідомлення Дата
l0ng-ai 736e78ade6 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
2026-11-19 19:44:36 +08:00
5 змінених файлів з 150 додано та 5 видалено

No files matched your search

+22 -5
Переглянути файл
@@ -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<R>(
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"
+39
Переглянути файл
@@ -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([
+11
Переглянути файл
@@ -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",
+57
Переглянути файл
@@ -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<R>(
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<BranchGroup, number> = { 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,
+21
Переглянути файл
@@ -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;
}
});
}