Файли
openidea/src/features/plugins/markerIcons.ts
T
Pedro Pessoa e0982cc51d feat(plugins): brand document-indexer gutter markers with marker icons (#2196)
Indexed markers (Edges.markers) were always drawn with the navigation glyph,
so a plugin couldn't tell its marker kinds apart. A [[contributes.markerIcons]]
entry can now claim them by key prefix (`markers`), with an optional
`tooltip` shown above the targets. The host matches the longest prefix and
sends the declaration id with each marker; the editor resolves the icon and
the translated tooltip from the projected declaration. Manifest only, under
the existing API 1.3 gate of markerIcons.

Closes #2194
2026-11-09 04:53:03 +08:00

68 рядки
2.4 KiB
TypeScript

import { pluginsBackend } from "./backend";
import { pluginIconName } from "./pluginIcon";
export interface MarkerIconDecl {
id: string;
icon: string;
tooltip?: string | null;
}
export interface IndexedMarkerIcon {
icon: string | null;
tooltip: string | null;
}
const icons = new Map<string, Promise<string | null>>();
const declared = new Map<string, { icon: Promise<string | null>; tooltip: string | null }>();
const listeners = new Set<() => void>();
export function onMarkerIconsChanged(listener: () => void): () => void {
listeners.add(listener);
return () => {
listeners.delete(listener);
};
}
function changed() {
for (const listener of listeners) listener();
}
const key = (pluginId: string, path: string) => `${pluginId}\0${path}`;
/** Package paths are resolved only through declarations validated by the host. */
export function projectMarkerIcon(pluginId: string, decl: MarkerIconDecl): () => void {
const pending = pluginsBackend()
.assets(pluginId, `markerIcon/${decl.id}/`)
.then((assets) => {
const svg = (variant: string) => assets.find(([id]) => id === `markerIcon/${decl.id}/${variant}`)?.[1];
const light = svg("light");
return light ? pluginIconName(pluginId, { light, dark: svg("dark") ?? null }) : null;
})
.catch((e: unknown) => {
console.warn(`plugin ${pluginId}: marker icon ${decl.id}`, e);
return null;
});
const id = key(pluginId, decl.icon);
const entry = { icon: pending, tooltip: decl.tooltip ?? null };
const declId = key(pluginId, decl.id);
icons.set(id, pending);
declared.set(declId, entry);
changed();
return () => {
const current = icons.get(id) === pending || declared.get(declId) === entry;
if (icons.get(id) === pending) icons.delete(id);
if (declared.get(declId) === entry) declared.delete(declId);
if (current) changed();
};
}
export async function resolveMarkerIcon(pluginId: string, icon: string | null): Promise<string | null> {
if (!icon) return null;
return (await icons.get(key(pluginId, icon))) ?? icon;
}
/** The icon and tooltip of the `markerIcons` declaration `id` that the host matched to an indexed marker. */
export async function resolveIndexedMarkerIcon(pluginId: string, id: string): Promise<IndexedMarkerIcon | undefined> {
const entry = declared.get(key(pluginId, id));
return entry && { icon: await entry.icon, tooltip: entry.tooltip };
}