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

...
Автор SHA1 Повідомлення Дата
l0ng-ai e5d85e0d39 fix(usages): distinguish files with the same name in the popup 2026-11-22 15:03:46 +08:00
6 змінених файлів з 90 додано та 26 видалено

No files matched your search

+45
Переглянути файл
@@ -0,0 +1,45 @@
import { expect, test } from "./fixtures";
test("Show Usages distinguishes same-named files without losing the line or snippet", async ({ workbench: page }) => {
await page.setViewportSize({ width: 480, height: 620 });
await page.evaluate(async () => {
const usages = "/src/features/code-insight/usages.ts";
const { useUsagesPopup } = await import(/* @vite-ignore */ usages);
const symbol = "submitOrder";
useUsagesPopup.setState({
open: true,
anchor: { x: 30, y: 90 },
symbol,
groups: (
[
["/dev/acme-api/packages/billing/src/Routes.ts", "return submitOrder(invoice);"],
["/dev/acme-api/packages/checkout/src/Routes.ts", "return submitOrder(cart);"],
["/dev/acme-api/src/Worker.ts", "await submitOrder(job);"],
] as const
).map(([path, text]) => {
const col = text.indexOf(symbol);
return { path, usages: [{ path, line: 6, col, endCol: col + symbol.length, text }] };
}),
});
});
const popup = page.getByRole("dialog", { name: "Usages", exact: true });
const rows = popup.getByRole("option");
const billing = rows.filter({ hasText: "invoice" });
const checkout = rows.filter({ hasText: "cart" });
await expect(rows).toHaveCount(3);
for (const [row, directory] of [
[billing, "billing"],
[checkout, "checkout"],
] as const) {
await expect(row.getByText("Routes.ts", { exact: true })).toBeVisible();
const caption = row.getByText(directory, { exact: true });
await expect(caption).toBeVisible();
expect(await caption.evaluate((el) => el.scrollWidth - el.clientWidth)).toBeLessThanOrEqual(1);
await expect(row.getByText("7", { exact: true })).toBeVisible();
await expect(row.locator("mark")).toHaveText("submitOrder");
}
await expect(checkout.getByTitle("/dev/acme-api/packages/checkout/src/Routes.ts", { exact: true })).toBeVisible();
await expect(rows.filter({ hasText: "Worker.ts" }).getByText("src", { exact: true })).toHaveCount(0);
});
+1
Переглянути файл
@@ -40,6 +40,7 @@ import { rootOfPath } from "@/platform/projectRoots";
import { isVirtualUri } from "@/platform/virtualDocs";
import { cornerInset } from "@/editor/core/cm/cornerInset";
export { iconAreaSource, mapLineStart, type IconAreaSource } from "@/editor/core/cm/iconArea";
export { disambiguateTitles } from "@/editor/core/model/tabTitles";
export {
invalidateNavigationProviders,
navigationEpoch,
+6
Переглянути файл
@@ -26,6 +26,12 @@ describe("disambiguateTitles", () => {
expect(m.get("/r/a/d/f.ts")).toBe("d");
});
it("distinguishes directory names that occur at different depths", () => {
const paths = ["/r/prod/shared/f.ts", "/r/shared/prod/f.ts", "/r/test/shared/f.ts"];
const labels = disambiguateTitles(paths);
expect(paths.map((path) => labels.get(path))).toEqual(["prod/shared", "shared/prod", "test"]);
});
it("ignores duplicates of the same path", () => {
expect(disambiguateTitles(["/p/a/x.ts", "/p/a/x.ts"]).size).toBe(0);
});
+19 -20
Переглянути файл
@@ -1,6 +1,6 @@
/**
* IDEA-style editor tab titles for files that share a name: the nearest parent
* directory that tells them apart is shown after the name (`lib.rs — ide-lsp`
* Directory hints for files that share a name: a distinguishing parent
* directory is shown after the name (`lib.rs — ide-lsp`
* for `crates/ide-lsp/src/lib.rs` next to `crates/ide-vcs/src/lib.rs`).
*/
@@ -27,25 +27,24 @@ export function disambiguateTitles(paths: readonly string[], titleOf = baseName)
for (const group of groups.values()) {
if (group.length < 2) continue;
const dirs = group.map(parents);
const nameCounts = new Map<string, number>();
const suffixCounts = new Map<string, number>();
const suffixes = dirs.map((names) => {
for (const name of new Set(names)) {
nameCounts.set(name, (nameCounts.get(name) ?? 0) + 1);
}
let suffix = "";
return names.map((name) => {
suffix = suffix ? `${name}/${suffix}` : name;
suffixCounts.set(suffix, (suffixCounts.get(suffix) ?? 0) + 1);
return suffix;
});
});
group.forEach((path, i) => {
const own = dirs[i]!;
let hint: string | null = null;
for (let depth = 0; depth < own.length; depth++) {
const seg = own[depth]!;
if (dirs.every((other, j) => j === i || other[depth] !== seg)) {
hint = seg;
break;
}
}
if (hint === null) {
// No single directory is unique: the shortest distinguishing parent path.
const suffix = (d: string[], k: number) => d.slice(0, k).reverse().join("/");
for (let k = 2; k <= own.length && hint === null; k++) {
const mine = suffix(own, k);
if (dirs.every((other, j) => j === i || suffix(other, k) !== mine)) hint = mine;
}
}
out.set(path, hint ?? (own.length ? own.slice().reverse().join("/") : "/"));
const name = dirs[i]!.find((segment) => nameCounts.get(segment) === 1);
const paths = suffixes[i]!;
const hint = name ?? paths.find((suffix, depth) => depth > 0 && suffixCounts.get(suffix) === 1);
out.set(path, hint ?? paths.at(-1) ?? "/");
});
}
return out;
+7 -1
Переглянути файл
@@ -94,10 +94,16 @@
flex: none;
color: var(--fg-muted);
}
.fileName {
.fileName,
.directory {
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
}
.directory {
flex: 0 2 auto;
color: var(--fg-subtle);
}
.line {
overflow: hidden;
text-align: right;
+12 -5
Переглянути файл
@@ -8,7 +8,7 @@ import { create } from "zustand";
import clsx from "clsx";
import { EditorState, StateEffect, StateField } from "@codemirror/state";
import { Decoration, EditorView } from "@codemirror/view";
import { codePreviewExtensions } from "@/editor/api";
import { codePreviewExtensions, disambiguateTitles } from "@/editor/api";
import { basename } from "@/platform/project";
import { shortcutLabel } from "@/platform/keymap";
import { Icon, IconButton, List, Popup, Select, Spinner, fileIconName, listNavigate } from "@/ui";
@@ -86,6 +86,14 @@ function UsagesPopupInner() {
const preview = useUsagesPopupPreview((s) => s.preview);
const rowHeight = useRowHeight(ROW);
const all = useMemo<Usage[]>(() => groups.flatMap((g) => g.usages), [groups]);
const directories = useMemo(
() =>
disambiguateTitles(
groups.map((g) => g.path),
basename,
),
[groups],
);
const [filter, setFilter] = useState("");
const accessOptions = useUsageAccessOptions();
const items = useMemo(
@@ -233,11 +241,10 @@ function UsagesPopupInner() {
}
renderItem={(u) => (
<div className={styles.row} data-testid="usage-row" data-path={u.path} data-line={u.line}>
<span className={styles.file}>
<span className={styles.file} title={u.path}>
<Icon name={fileIconName(basename(u.path), u.path)} size={16} className={styles.icon} />
<span className={styles.fileName} title={u.path}>
{basename(u.path)}
</span>
<span className={styles.fileName}>{basename(u.path)}</span>
{directories.has(u.path) && <span className={styles.directory}>{directories.get(u.path)}</span>}
</span>
<span className={styles.line}>{u.line + 1}</span>
<UsagePreview usage={u} />