дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 17:47:08 +02:00
* fix(code-insight): stabilize Show Usages popup sizing and placement The popup used a fixed 680px width and 14-row limit. Restoring a saved size also prevented it from adapting to incoming results. Follow the IntelliJ popup sizing and placement flow while limiting the filename column to 25% of the popup width to leave space for code. Sizing and columns: - Restore both saved dimensions while waiting for results. Once results arrive, cap the natural width at the saved width and calculate the height from content. Only preview mode also uses the saved height. - Measure filenames, line numbers and code with the actual fonts, with a 500px natural width limit per column. Fit short filenames to their content and ellipsize long names at 25% of the row width. Reserve 5ch for line numbers and at least 8em for code, which fills the remaining space. - Include the filename cap, code minimum width, header, controls, borders and gaps in the natural popup width. Do not widen the popup for text that the filename column will clip. - Cache maximum natural widths during streaming and filtering. After a manual resize, keep updating filename measurements but stop resizing the popup automatically until it is reopened or preview is toggled. - Show up to 30 rows normally or 20 with preview. Leave half a row when the list scrolls, and let the list fill the available height. - Preserve the 360x160 minimum size and existing storage keys. Fit the viewport when it is smaller, and save the actual size on close. Placement and preview: - Open below the caret. When it is outside the visible editor, use a point one third across and halfway down the visible editing area. - Add optional fitToScreen placement to Popup. Keep the fitted position when content shrinks, and move only when necessary to stay visible. Reset placement when the anchor changes or preview rebuilds the popup. - Reuse the shared split panels with a default 60/40 list/preview split, persisted proportions, mouse dragging and keyboard adjustment. Set the preview minimum to five actual editor lines and update it after font or editor geometry changes. - Preserve the preview's pixel height while manually resizing the outer popup. Wait for the splitter's final measurement before restoring it. - Add onResizeStart on the first pointer movement. Clicking the resize handle alone does not lock preview height. Toggling preview clears saved dimensions, measurements, manual size and drag position, and cancels pending resize callbacks. - Add a translated accessible label to the preview divider and preserve its keyboard controls alongside usage-list navigation. Verification: - 17 Playwright layout and demo tests passed in local Google Chrome, covering both themes, the 25% cap, narrow windows, filtering, saved dimensions, position stability, preview, keyboard controls and resize. Both filename-cap regression cases failed before the fix and passed afterward. - Wait for fonts and entry animations before dragging the one-pixel divider or measuring dimensions. Account for popup growth when the search caption changes while checking the filename cap. - 49 Popup and Show Usages unit tests passed on the final version. - pnpm typecheck and pnpm build passed, including Safari 15.4 checks. Changed-file oxlint, oxfmt and git diff --check passed. - An earlier full test run had 20,483 passed, 65 skipped and two existing translation-check failures in color-preview/varColors.ts and vcs/rebase.tsx. Earlier full lint and knip checks passed. Full format checking failed on an existing untracked .workbuddy memory file. - Before/after screenshots for both themes and narrow windows, plus interaction recordings, are retained in temporary verification folders. Tests used browser fixtures; the native Tauri webview was not tested because no Tauri MCP bridge was available. * fix(code-insight): restore focus across preview toggle, avoid width ratchet, and clean up cancelled resizes --------- Co-authored-by: l0ng-ai <[email protected]>
614 рядки
23 KiB
TypeScript
614 рядки
23 KiB
TypeScript
/** Find Usages (⌥F7 → "usages" tool window) and Show Usages (⌥⌘F7 → popup). */
|
|
import { create } from "zustand";
|
|
import { Channel } from "@tauri-apps/api/core";
|
|
import type { ClassifiedReference, ReferenceAccess, ReferenceKind, Location, References, RequestId } from "@/bindings";
|
|
import { contributedUsages, editorsFor } from "@/editor/api";
|
|
import { clearLineTokens } from "@/editor/lineTokens";
|
|
import { showPendingHint } from "@/editor/lsp/hint";
|
|
import { incompleteNote } from "@/editor/lsp/lsp";
|
|
import { dedupeLocations } from "@/editor/lsp/navigation";
|
|
import { overriddenMethods, overriddenReferences } from "@/editor/lsp/overriddenReferences";
|
|
import { commands, IpcError, isCancelled, nextRequestId } from "@/platform/ipc";
|
|
import { tx } from "@/platform/i18n";
|
|
import { shortcutLabel } from "@/platform/keymap";
|
|
import { synchronizeDocumentIndexes, toolWindowService } from "@/workbench/contributions";
|
|
import {
|
|
caretContext,
|
|
caretSynced,
|
|
caretPoint,
|
|
hint,
|
|
lspRequest,
|
|
openLocation,
|
|
readManyFileLines,
|
|
reportError,
|
|
withDocument,
|
|
type CaretContext,
|
|
} from "./ipc";
|
|
import { syntaxUsages } from "./syntaxUsages";
|
|
|
|
export interface Usage {
|
|
kind?: ReferenceKind;
|
|
access?: ReferenceAccess | null;
|
|
/** Exact semantic range, retained when the line preview is clamped. */
|
|
sourceLocation?: Location;
|
|
path: string;
|
|
line: number;
|
|
col: number;
|
|
/** End column on `line` (clamped to the line for multi-line ranges). */
|
|
endCol: number;
|
|
/** The usage's line text (empty when the file couldn't be read). */
|
|
text: string;
|
|
}
|
|
|
|
export interface UsageGroup {
|
|
path: string;
|
|
usages: Usage[];
|
|
}
|
|
|
|
/** Groups reference locations by file (sorted by path, then position), attaching line previews. */
|
|
export function groupUsages(
|
|
locs: readonly Location[],
|
|
files: ReadonlyMap<string, readonly string[] | null>,
|
|
classified: readonly ClassifiedReference[] = [],
|
|
): UsageGroup[] {
|
|
const kinds = new Map(classified.map((r) => [usageLocationKey(r.location), r]));
|
|
const byPath = new Map<string, Usage[]>();
|
|
const seen = new Set<string>();
|
|
for (const l of locs) {
|
|
const key = usageLocationKey(l);
|
|
if (seen.has(key)) continue;
|
|
seen.add(key);
|
|
const text = files.get(l.path)?.[l.range.start.line] ?? kinds.get(key)?.text ?? "";
|
|
const endCol = l.range.end.line === l.range.start.line ? l.range.end.col : Math.max(text.length, l.range.start.col);
|
|
let list = byPath.get(l.path);
|
|
if (!list) byPath.set(l.path, (list = []));
|
|
list.push({
|
|
...(kinds.has(usageLocationKey(l))
|
|
? {
|
|
kind: kinds.get(usageLocationKey(l))!.kind,
|
|
access: kinds.get(usageLocationKey(l))!.access,
|
|
sourceLocation: l,
|
|
}
|
|
: {}),
|
|
path: l.path,
|
|
line: l.range.start.line,
|
|
col: l.range.start.col,
|
|
endCol,
|
|
text,
|
|
});
|
|
}
|
|
return [...byPath.entries()]
|
|
.sort(([a], [b]) => a.localeCompare(b))
|
|
.map(([path, usages]) => ({ path, usages: usages.sort((a, b) => a.line - b.line || a.col - b.col) }));
|
|
}
|
|
|
|
export function usageCount(groups: readonly UsageGroup[]): number {
|
|
return groups.reduce((n, g) => n + g.usages.length, 0);
|
|
}
|
|
|
|
/** Trims leading whitespace of a preview line, shifting the match range accordingly. */
|
|
export function trimPreview(u: Usage): { text: string; textCol: number; from: number; to: number } {
|
|
const lead = u.text.length - u.text.trimStart().length;
|
|
const text = u.text.trim();
|
|
const from = Math.max(0, u.col - lead);
|
|
const to = Math.max(from, Math.min(text.length, u.endCol - lead));
|
|
return { text, textCol: lead, from, to };
|
|
}
|
|
|
|
/** Project Files, or Project and Libraries (Java's dependency jars and JDK too). */
|
|
export type UsageScope = "project" | "libraries";
|
|
|
|
/** The scope a search ran in, when there was a choice (`null` when the server has none). */
|
|
function searchedScope(libraries: boolean, librariesSkipped: boolean, widenable: boolean): UsageScope | null {
|
|
if (librariesSkipped) return "project";
|
|
return libraries && widenable ? "libraries" : null;
|
|
}
|
|
|
|
export interface UsageSearch {
|
|
/** Identifies one search run (UI state such as expansion resets per run). */
|
|
id: number;
|
|
symbol: string | null;
|
|
loading: boolean;
|
|
groups: UsageGroup[];
|
|
error: string | null;
|
|
/** Results came from the syntax fallback: says so (e.g. "Text occurrences (language server not ready)"). */
|
|
note: string | null;
|
|
/** Re-runs the same query (rerun button). */
|
|
rerun: (() => void) | null;
|
|
/** Where it searched, when the server can search beyond Project Files (Java's jars and JDK). */
|
|
scope?: UsageScope | null;
|
|
/** Runs it again in Project and Libraries. */
|
|
searchLibraries?: (() => void) | null;
|
|
/** Kept when a new search runs (Pin Tab). */
|
|
pinned?: boolean;
|
|
}
|
|
|
|
let searchSeq = 0;
|
|
|
|
/** One tab per search; as in IDEA a new search replaces the selected tab unless it is pinned. */
|
|
interface UsagesState {
|
|
tabs: UsageSearch[];
|
|
active: number | null;
|
|
}
|
|
|
|
export const useUsages = create<UsagesState>(() => ({ tabs: [], active: null }));
|
|
|
|
/** The selected tab's search. */
|
|
export const activeUsageSearch = (s: UsagesState): UsageSearch | null =>
|
|
s.tabs.find((t) => t.id === s.active) ?? s.tabs.at(-1) ?? null;
|
|
|
|
const MAX_TABS = 8;
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// View options (persisted toggle in the tool window's ⋮ menu).
|
|
|
|
const OPTIONS_KEY = "openidea.usages.view.v1";
|
|
|
|
export const useUsagesOptions = create<{ preview: boolean; openInNewTab: boolean }>(() => {
|
|
try {
|
|
return {
|
|
preview: true,
|
|
openInNewTab: false,
|
|
...JSON.parse(globalThis.localStorage?.getItem(OPTIONS_KEY) ?? "{}"),
|
|
};
|
|
} catch {
|
|
return { preview: true, openInNewTab: false };
|
|
}
|
|
});
|
|
|
|
useUsagesOptions.subscribe((s) => {
|
|
try {
|
|
globalThis.localStorage?.setItem(OPTIONS_KEY, JSON.stringify(s));
|
|
} catch {
|
|
/* storage unavailable */
|
|
}
|
|
});
|
|
|
|
/** Shows `search` in a new tab, or in place of the selected one unless that is pinned
|
|
* (or "Open Results in New Tab" is on). */
|
|
export function openUsageTab(search: UsageSearch) {
|
|
const alwaysNew = useUsagesOptions.getState().openInNewTab;
|
|
useUsages.setState((s) => {
|
|
const reused = alwaysNew ? undefined : s.tabs.find((t) => t.id === s.active && !t.pinned);
|
|
const tabs = reused ? s.tabs.map((t) => (t === reused ? search : t)) : [...s.tabs, search];
|
|
return { tabs: tabs.slice(-MAX_TABS), active: search.id };
|
|
});
|
|
}
|
|
|
|
/** Replaces tab `id` (a rerun or a finished search), keeping its pin and selection. */
|
|
function replaceUsageTab(id: number, search: UsageSearch): boolean {
|
|
const s = useUsages.getState();
|
|
const old = s.tabs.find((t) => t.id === id);
|
|
if (!old) return false;
|
|
useUsages.setState({
|
|
tabs: s.tabs.map((t) => (t === old ? { ...search, pinned: old.pinned } : t)),
|
|
active: s.active === id ? search.id : s.active,
|
|
});
|
|
return true;
|
|
}
|
|
|
|
export function pinUsageTab(id: number, pinned: boolean) {
|
|
useUsages.setState((s) => ({ tabs: s.tabs.map((t) => (t.id === id ? { ...t, pinned } : t)) }));
|
|
}
|
|
|
|
export function closeUsageTab(id: number) {
|
|
cancelSearch(id);
|
|
useUsages.setState((s) => {
|
|
const i = s.tabs.findIndex((t) => t.id === id);
|
|
const tabs = s.tabs.filter((t) => t.id !== id);
|
|
const active = s.active === id ? (tabs[Math.min(i, tabs.length - 1)]?.id ?? null) : s.active;
|
|
return { tabs, active };
|
|
});
|
|
// As in IDEA, the tool window and its stripe button go away with its last tab.
|
|
if (!useUsages.getState().tabs.length) {
|
|
try {
|
|
toolWindowService().hide("usages", { removeFromStripe: true });
|
|
} catch {
|
|
// Workbench not ready (tests).
|
|
}
|
|
}
|
|
}
|
|
|
|
/** Rerun of the selected tab (⌘R in the Find Usages tool window). */
|
|
export function rerunActiveUsages() {
|
|
activeUsageSearch(useUsages.getState())?.rerun?.();
|
|
}
|
|
|
|
interface UsagesPopupState {
|
|
open: boolean;
|
|
anchor: { x: number; y: number };
|
|
symbol: string | null;
|
|
groups: UsageGroup[];
|
|
note: string | null;
|
|
/** Shows those found so far while the search goes on. */
|
|
loading: boolean;
|
|
/** Where it searched, when the server can search beyond Project Files. */
|
|
scope: UsageScope | null;
|
|
}
|
|
|
|
export const useUsagesPopup = create<UsagesPopupState>(() => ({
|
|
open: false,
|
|
anchor: { x: 0, y: 0 },
|
|
symbol: null,
|
|
groups: [],
|
|
note: null,
|
|
loading: false,
|
|
scope: null,
|
|
}));
|
|
|
|
interface FetchedUsages {
|
|
groups: UsageGroup[];
|
|
note: string | null;
|
|
librariesSkipped: boolean;
|
|
}
|
|
|
|
/** Paths whose lines are read to show these usages: not those the server gave along. */
|
|
function pathsToRead(locs: readonly Location[], classified: readonly ClassifiedReference[]): string[] {
|
|
const given = new Set(classified.filter((r) => r.text != null).map((r) => usageLocationKey(r.location)));
|
|
return locs.filter((l) => !given.has(usageLocationKey(l))).map((l) => l.path);
|
|
}
|
|
|
|
/**
|
|
* The server's references across the project (files outside its analysis scope
|
|
* included); when it is not ready or fails, the syntax fallback (an empty server
|
|
* answer is respected). Without a fallback the server error stands. Without
|
|
* `libraries`, Java's dependencies and JDK are not searched (Project Files); with it,
|
|
* those found in the sources go to `onPartial` before the slower search for the rest.
|
|
* Cancel with `commands.cancelRequest(request)`.
|
|
*/
|
|
export async function fetchUsages(
|
|
c: CaretContext,
|
|
request: RequestId = nextRequestId(),
|
|
onPartial?: (found: FetchedUsages) => void,
|
|
libraries = false,
|
|
): Promise<FetchedUsages> {
|
|
// Usage lines are read afresh: so are the files their syntax colours come from.
|
|
clearLineTokens();
|
|
const before = c.view?.state.doc;
|
|
let locs: Location[];
|
|
let classified: ClassifiedReference[] = [];
|
|
let note: string | null = null;
|
|
let librariesSkipped = false;
|
|
await caretSynced(c);
|
|
const indexed = await synchronizeDocumentIndexes(c.path, "usages");
|
|
if (c.view && c.view.state.doc !== before) throw new IpcError({ kind: "cancelled", message: "", data: null });
|
|
// Names the server doesn't connect (pytest fixtures): their provider answers instead.
|
|
const contributed = await contributedUsages({ doc: c.doc, path: c.path, pos: { line: c.line, col: c.col } }, request);
|
|
if (contributed) {
|
|
const lines = await readManyFileLines(contributed.map((l) => l.path));
|
|
return { groups: groupUsages(contributed, lines, []), note: null, librariesSkipped: false };
|
|
}
|
|
let settled = false;
|
|
const channel = new Channel<References>();
|
|
channel.onmessage = (refs) => {
|
|
if (!onPartial) return;
|
|
void readManyFileLines(pathsToRead(refs.locations, refs.classified ?? [])).then((files) => {
|
|
if (settled) return;
|
|
onPartial({
|
|
groups: groupUsages(refs.locations, files, refs.classified ?? []),
|
|
note: refs.incomplete ? incompleteNote(refs.incomplete, c.word) : null,
|
|
librariesSkipped: false,
|
|
});
|
|
});
|
|
};
|
|
// Needs no request: looked up while the server searches.
|
|
const overridden = overriddenMethods(c.path, c.doc, { line: c.line, col: c.col });
|
|
try {
|
|
const refs = await lspRequest(() =>
|
|
commands.lspReferences(request, c.doc, { line: c.line, col: c.col }, false, true, libraries, channel),
|
|
).finally(() => {
|
|
settled = true;
|
|
});
|
|
locs = refs.locations;
|
|
classified = refs.classified ?? [];
|
|
librariesSkipped = refs.librariesSkipped ?? false;
|
|
if (refs.incomplete) note = incompleteNote(refs.incomplete, c.word);
|
|
const inherited = await overriddenReferences(await overridden, (doc, at) =>
|
|
lspRequest(() => commands.lspReferences(request, doc, at, false, true, libraries, new Channel())),
|
|
);
|
|
for (const r of inherited) {
|
|
locs = locs.concat(r.locations);
|
|
if (r.incomplete) note ??= incompleteNote(r.incomplete, c.word);
|
|
}
|
|
if (inherited.length) locs = dedupeLocations(locs);
|
|
} catch (e) {
|
|
if (isCancelled(e)) throw e;
|
|
let fb = null;
|
|
try {
|
|
fb = await syntaxUsages(c);
|
|
} catch (fe) {
|
|
if (isCancelled(fe)) throw fe;
|
|
console.debug("[syntax] usages fallback", fe);
|
|
}
|
|
if (!fb) throw e;
|
|
locs = fb.locs;
|
|
note = fb.note;
|
|
}
|
|
if (!indexed) note ??= tx("May be incomplete: plugin indexes are still updating");
|
|
const files = await readManyFileLines(pathsToRead(locs, classified));
|
|
return { groups: groupUsages(locs, files, classified), note, librariesSkipped };
|
|
}
|
|
|
|
/** In-flight server requests: of tool window searches by search id, and of the popup. */
|
|
const inflight = new Map<number, RequestId>();
|
|
let popupRequest: RequestId | null = null;
|
|
|
|
function cancelSearch(id: number) {
|
|
const request = inflight.get(id);
|
|
if (request === undefined) return;
|
|
inflight.delete(id);
|
|
void commands.cancelRequest(request).catch(() => {});
|
|
}
|
|
|
|
/** The window switched projects: results (and in-flight searches) of the previous one are dropped. */
|
|
export function resetUsages() {
|
|
for (const id of [...inflight.keys()]) cancelSearch(id);
|
|
if (popupRequest !== null) void commands.cancelRequest(popupRequest).catch(() => {});
|
|
popupRequest = null;
|
|
useUsages.setState({ tabs: [], active: null });
|
|
recentSearches = [];
|
|
useUsagesPopup.setState({ open: false, symbol: null, groups: [], note: null, loading: false, scope: null });
|
|
popupTarget = null;
|
|
}
|
|
|
|
export async function findUsages() {
|
|
const c = caretContext();
|
|
if (!c) {
|
|
hint("Open a file to find usages");
|
|
return;
|
|
}
|
|
await searchUsages(c, (fetch) => fetch(c), false, false);
|
|
}
|
|
|
|
/** A past Find Usages search, run again by Recent Find Usages. */
|
|
export interface RecentUsageSearch {
|
|
path: string;
|
|
line: number;
|
|
col: number;
|
|
symbol: string | null;
|
|
libraries: boolean;
|
|
widenable: boolean;
|
|
}
|
|
|
|
const MAX_RECENT = 10;
|
|
let recentSearches: RecentUsageSearch[] = [];
|
|
|
|
/** Recent Find Usages: the last searches, newest first, one per caret position. */
|
|
export const recentUsageSearches = (): readonly RecentUsageSearch[] => recentSearches;
|
|
|
|
/** Runs `search` again in a new tab, in its file's document whether or not an editor still has it open. */
|
|
export async function rerunRecentUsages(search: RecentUsageSearch) {
|
|
const { path, line, col, symbol, libraries, widenable } = search;
|
|
await searchUsages(
|
|
{ path, line, col, word: symbol },
|
|
(fetch) =>
|
|
withDocument(path, (doc) =>
|
|
fetch({ doc, path, line, col, word: symbol, view: editorsFor(path)[0]?.view ?? null }),
|
|
),
|
|
libraries,
|
|
widenable,
|
|
);
|
|
}
|
|
|
|
/** Runs `fetch` with the caret of a search: the editor's, or one in a document opened for each run. */
|
|
type InDocument = <T>(fetch: (c: CaretContext) => Promise<T>) => Promise<T>;
|
|
|
|
/** Searches the usages at `c` into the tool window. */
|
|
async function searchUsages(
|
|
c: Pick<CaretContext, "path" | "line" | "col" | "word">,
|
|
inDocument: InDocument,
|
|
libraries: boolean,
|
|
widenable: boolean,
|
|
) {
|
|
const here = (r: RecentUsageSearch) => r.path === c.path && r.line === c.line && r.col === c.col;
|
|
// `widenable`: an earlier run of this search skipped the libraries.
|
|
const run = async (prev: number | null, libraries: boolean, widenable: boolean) => {
|
|
recentSearches = [
|
|
{ path: c.path, line: c.line, col: c.col, symbol: c.word, libraries, widenable },
|
|
...recentSearches.filter((r) => !here(r)),
|
|
].slice(0, MAX_RECENT);
|
|
const id = ++searchSeq;
|
|
const base = {
|
|
id,
|
|
symbol: c.word,
|
|
rerun: () => void run(id, libraries, widenable),
|
|
searchLibraries: () => void run(id, true, true),
|
|
};
|
|
const loading = { ...base, loading: true, groups: [], error: null, note: null };
|
|
if (prev !== null) cancelSearch(prev);
|
|
if (prev === null || !replaceUsageTab(prev, loading)) openUsageTab(loading);
|
|
toolWindowService().show("usages");
|
|
const request = nextRequestId();
|
|
inflight.set(id, request);
|
|
try {
|
|
const { groups, note, librariesSkipped } = await inDocument((caret) =>
|
|
fetchUsages(
|
|
caret,
|
|
request,
|
|
(found) => {
|
|
if (inflight.get(id) === request)
|
|
replaceUsageTab(id, { ...loading, groups: found.groups, note: found.note });
|
|
},
|
|
libraries,
|
|
),
|
|
);
|
|
const scope = searchedScope(libraries, librariesSkipped, widenable);
|
|
replaceUsageTab(id, { ...base, loading: false, groups, error: null, note, scope });
|
|
} catch (e) {
|
|
// Cancelled here (closed tab, rerun): a newer state already replaced it.
|
|
if (isCancelled(e) && !inflight.has(id)) return;
|
|
const message = e instanceof Error ? e.message : String(e);
|
|
if (!replaceUsageTab(id, { ...base, loading: false, groups: [], error: message, note: null })) return;
|
|
reportError(e);
|
|
} finally {
|
|
if (inflight.get(id) === request) inflight.delete(id);
|
|
}
|
|
};
|
|
await run(null, libraries, widenable);
|
|
}
|
|
|
|
/**
|
|
* Shows already-found usages in the tool window (Safe Delete's "View Usages").
|
|
* Ignores malformed arguments.
|
|
*/
|
|
export async function showUsageResults(arg: unknown) {
|
|
const a = (arg ?? {}) as {
|
|
symbol?: unknown;
|
|
locations?: unknown;
|
|
classified?: ClassifiedReference[];
|
|
note?: unknown;
|
|
};
|
|
if (!Array.isArray(a.locations)) return;
|
|
const locs = a.locations as Location[];
|
|
const symbol = typeof a.symbol === "string" ? a.symbol : null;
|
|
const id = ++searchSeq;
|
|
const base = { id, symbol, rerun: null };
|
|
openUsageTab({ ...base, loading: true, groups: [], error: null, note: null });
|
|
toolWindowService().show("usages");
|
|
clearLineTokens();
|
|
const files = await readManyFileLines(locs.map((l) => l.path));
|
|
replaceUsageTab(id, {
|
|
...base,
|
|
loading: false,
|
|
groups: groupUsages(locs, files, a.classified),
|
|
error: null,
|
|
note: typeof a.note === "string" ? a.note : null,
|
|
});
|
|
}
|
|
|
|
export function dismissUsagesPopup() {
|
|
if (popupRequest !== null) void commands.cancelRequest(popupRequest).catch(() => {});
|
|
popupRequest = null;
|
|
useUsagesPopup.setState({ open: false });
|
|
}
|
|
|
|
/** How long Show Usages waits for all usages before it opens with those found so far. */
|
|
const EARLY_POPUP_DELAY_MS = 300;
|
|
/** How long the hint after Show Usages stays (and invoking it again widens its search). */
|
|
const HINT_MS = 4000;
|
|
|
|
/**
|
|
* What the popup, or the hint after it, shows usages of: invoked again meanwhile, Show
|
|
* Usages searches it in Project and Libraries, wherever the caret is now.
|
|
*/
|
|
let popupTarget: { c: CaretContext; widenable: boolean; hintUntil: number } | null = null;
|
|
|
|
export async function showUsagesPopup() {
|
|
const target = popupTarget;
|
|
const popup = useUsagesPopup.getState();
|
|
if (target?.widenable && ((popup.open && popup.scope === "project") || Date.now() < target.hintUntil)) {
|
|
await searchPopup(target.c, true, true);
|
|
return;
|
|
}
|
|
const c = caretContext();
|
|
if (!c) {
|
|
hint("Open a file to show usages");
|
|
return;
|
|
}
|
|
await searchPopup(c, false, false);
|
|
}
|
|
|
|
/** The popup's scope chooser: searches its symbol again in `scope`. */
|
|
export function setUsagesPopupScope(scope: UsageScope) {
|
|
const target = popupTarget;
|
|
if (!target || scope === useUsagesPopup.getState().scope) return;
|
|
void searchPopup(target.c, scope === "libraries", target.widenable);
|
|
}
|
|
|
|
async function searchPopup(c: CaretContext, libraries: boolean, widenable: boolean) {
|
|
// Set from the start: the scope chooser can switch while this search goes on.
|
|
popupTarget = { c, widenable, hintUntil: 0 };
|
|
const reopened = useUsagesPopup.getState().open;
|
|
if (reopened) useUsagesPopup.setState({ loading: true, scope: libraries ? "libraries" : "project" });
|
|
if (popupRequest !== null) void commands.cancelRequest(popupRequest).catch(() => {});
|
|
const request = nextRequestId();
|
|
popupRequest = request;
|
|
// Opened while the search goes on when it takes a while, with the usages found so far;
|
|
// once it showed some, it stays open, even when they were all. A search that ends with
|
|
// none or one closes a popup that showed none yet and answers with the hint or the jump.
|
|
let early = false;
|
|
let shown = false;
|
|
let found: FetchedUsages | null = null;
|
|
// Until the popup or the answer shows (a reopened popup has its own spinner).
|
|
const pending = !reopened && c.view ? showPendingHint(c.view, "Searching for usages…") : () => {};
|
|
const openEarly = () => {
|
|
if (popupRequest !== request) return;
|
|
pending();
|
|
if (!early && !reopened) useUsagesPopup.setState({ open: true, anchor: caretPoint(true), symbol: c.word });
|
|
early = true;
|
|
shown ||= !!found;
|
|
const so = found ?? { groups: [], note: null };
|
|
// The scope searched: the chooser can have switched a widenable search back to Project Files.
|
|
const scope = widenable ? (libraries ? "libraries" : "project") : null;
|
|
useUsagesPopup.setState({ groups: so.groups, note: so.note, loading: true, scope });
|
|
};
|
|
const timer = setTimeout(openEarly, EARLY_POPUP_DELAY_MS);
|
|
const again = shortcutLabel("find.showUsages");
|
|
try {
|
|
const { groups, note, librariesSkipped } = await fetchUsages(
|
|
c,
|
|
request,
|
|
(partial) => {
|
|
if (!usageCount(partial.groups)) return;
|
|
found = partial;
|
|
if (early) openEarly();
|
|
},
|
|
libraries,
|
|
).finally(() => {
|
|
clearTimeout(timer);
|
|
pending();
|
|
});
|
|
if (popupRequest !== request) return;
|
|
const scope = searchedScope(libraries, librariesSkipped, widenable);
|
|
popupTarget = { c, widenable: widenable || scope !== null, hintUntil: 0 };
|
|
const total = usageCount(groups);
|
|
if (shown || reopened || total > 1) {
|
|
if (!early && !reopened) useUsagesPopup.setState({ open: true, anchor: caretPoint(true), symbol: c.word });
|
|
useUsagesPopup.setState({ groups, note, loading: false, scope });
|
|
return;
|
|
}
|
|
if (early) useUsagesPopup.setState({ open: false, loading: false });
|
|
// As in IDEA, the hint offers to widen the search, which Show Usages does while it shows.
|
|
const widen = scope === "project" && again !== "";
|
|
if (widen) popupTarget.hintUntil = Date.now() + HINT_MS;
|
|
// The count and scope, for tests: the hint is the whole answer.
|
|
const counted: Record<string, string> = scope ? { usages: String(total), scope } : { usages: String(total) };
|
|
if (!total) {
|
|
if (widen)
|
|
hint(
|
|
`No usages found in Project Files. Press ${again} again to search in 'Project and Libraries'`,
|
|
"info",
|
|
counted,
|
|
);
|
|
else if (scope === "project") hint("No usages found in Project Files", "info", counted);
|
|
else {
|
|
const none = c.word ? `No usages of '${c.word}' found` : "No usages found";
|
|
hint(note ? `${none}. ${note}` : none, "info", counted);
|
|
}
|
|
return;
|
|
}
|
|
// IDEA jumps to the only usage and says so in the target editor.
|
|
const u = groups[0]!.usages[0]!;
|
|
await openLocation(u.path, u.line, u.col);
|
|
if (widen)
|
|
hint(
|
|
`Only one usage in Project Files. Press ${again} again to search in 'Project and Libraries'`,
|
|
"info",
|
|
counted,
|
|
);
|
|
else if (scope === "libraries") hint("Only one usage in Project and Libraries", "info", counted);
|
|
else hint("Only one usage in Project Files", "info", counted);
|
|
} catch (e) {
|
|
if (popupRequest !== request || isCancelled(e)) return;
|
|
if (early && !shown && !reopened) useUsagesPopup.setState({ open: false, loading: false });
|
|
else if (early || reopened) useUsagesPopup.setState({ loading: false });
|
|
reportError(e);
|
|
} finally {
|
|
if (popupRequest === request) popupRequest = null;
|
|
}
|
|
}
|
|
|
|
function usageLocationKey(l: Location): string {
|
|
return JSON.stringify([l.path, l.range.start.line, l.range.start.col, l.range.end.line, l.range.end.col]);
|
|
}
|