Файли
openidea/src/editor/navigation/headerSource.ts
T
l0ng-ai 844611c7d5 feat(navigation): Navigate › Related Symbol for header/source and Rails files (#3699)
Related Symbol… (⌃⌘↑ / Ctrl+Alt+Home, in the Navigate menu and the editor's
Go To submenu) goes to the files tied to the current one by convention: one
target opens directly, several open a Choose Target list, none shows
"No related symbols". Providers register through editor/relatedProviders.

- C, C++, Objective-C and CUDA: a header and its source files of the same
  name, the ones in the same folder first.
- Rails: a controller action and its views, a controller and all its views,
  helper and model, a view and its controller action, a helper and its
  controller and views, a model and its controller and migrations.

Refs #900
2026-11-16 14:58:13 +08:00

54 рядки
2.3 KiB
TypeScript

/** Related Symbol for C, C++, Objective-C and CUDA: a header and its source files, by name. */
import type { RelatedItem } from "@/editor/relatedProviders";
import { tx } from "@/platform/i18n";
const HEADERS = ["h", "hh", "hpp", "hxx", "h++", "cuh"];
const SOURCES = ["c", "cc", "cpp", "cxx", "c++", "m", "mm", "cu"];
function split(path: string): { dir: string; stem: string; ext: string } | null {
const slash = path.lastIndexOf("/");
const name = path.slice(slash + 1);
const dot = name.lastIndexOf(".");
if (dot <= 0) return null;
return { dir: path.slice(0, slash), stem: name.slice(0, dot), ext: name.slice(dot + 1).toLowerCase() };
}
/** The file names whose stem matches `path`'s on the other side (header ↔ source), or none. */
export function counterpartNames(path: string): { names: string[]; group: string } | null {
const parts = split(path);
if (!parts) return null;
if (HEADERS.includes(parts.ext)) return { names: SOURCES.map((e) => `${parts.stem}.${e}`), group: tx("Source") };
if (SOURCES.includes(parts.ext)) return { names: HEADERS.map((e) => `${parts.stem}.${e}`), group: tx("Header") };
return null;
}
function sharedPrefix(a: string, b: string): number {
const x = a.split("/");
const y = b.split("/");
let n = 0;
while (n < x.length && n < y.length && x[n] === y[n]) n++;
return n;
}
/**
* The counterparts of `path` among `candidates`: those in its own folder when there are any
* (the pair to switch to), else all of them, nearest folders first.
*/
export function rankCounterparts(path: string, candidates: readonly string[]): string[] {
const dir = split(path)?.dir ?? "";
const unique = [...new Set(candidates)].filter((c) => c !== path);
const local = unique.filter((c) => split(c)?.dir === dir);
if (local.length) return local;
return unique.sort((a, b) => sharedPrefix(path, b) - sharedPrefix(path, a) || a.localeCompare(b));
}
export async function headerSourceItems(
path: string,
findNamed: (name: string) => Promise<string[]>,
): Promise<RelatedItem[]> {
const counterpart = counterpartNames(path);
if (!counterpart) return [];
const found = await Promise.all(counterpart.names.map((n) => findNamed(n).catch(() => [] as string[])));
return rankCounterparts(path, found.flat()).map((p) => ({ path: p, group: counterpart.group }));
}