Файли
openidea/src/features/plugins/workspaceContains.ts
T
l0ng-ai 908740b3f2 feat(plugins): a marketplace that helps pick plugins (#2718)
* feat(plugins): a marketplace that helps pick plugins

Rows show each plugin's description, publisher, downloads and an inline
Install / Update button; plugins without an icon get a tinted initial.
While browsing, the list opens with suggestions for the open project and
the maintainers' picks, then all community plugins by downloads; the
official plugins come last as examples. The first plugin is selected so
the details are never empty, and they show release notes, the homepage
and why a plugin is suggested.

The index gains optional homepage, notes, downloads, featured and suggest
fields; openidea-plugin index fills homepage and notes from the manifest.

* fix(plugins): plugin icons fill their tile like the initials

* feat(plugins): drop the Preview badge from the plugins page

* feat(plugins): open the plugins page on Available
2026-11-11 20:13:28 +08:00

116 рядки
4.0 KiB
TypeScript

import { setContext } from "@/platform/context";
import { call, commands, onIdeEvent } from "@/platform/ipc";
import { useProject } from "@/platform/project";
import { usePlugins } from "./store";
const PREFIX = "workspaceContains:";
// A whole identifier: at the start or after a character that can't be part of one.
const KEY = /(?:^|[^\w.\-:/])(workspaceContains:[\w.\-:/]+)/g;
export function workspaceContainsKeys(whens: readonly (string | null | undefined)[]): string[] {
return [...new Set(whens.flatMap((when) => [...(when ?? "").matchAll(KEY)].map((m) => m[1]!)))];
}
function relativePath(key: string): string | null {
const path = key.slice(PREFIX.length).replace(/\/+$/, "");
if (!path || path.startsWith("/") || path.split("/").includes("..")) {
return null;
}
return path;
}
export function affectsWorkspaceContains(
changed: readonly string[],
roots: readonly string[],
keys: readonly string[],
): boolean {
const targets = keys.flatMap((key) => {
const path = relativePath(key);
return path === null ? [] : roots.map((root) => `${root}/${path}`);
});
return changed.some((p) => targets.some((t) => t === p || t.startsWith(`${p}/`)));
}
/** Whether `path` (relative) exists under any of `roots`. */
async function existsUnder(roots: readonly string[], path: string, list: (dir: string) => Promise<string[]>) {
const slash = path.lastIndexOf("/");
const parent = slash < 0 ? "" : path.slice(0, slash);
const name = path.slice(slash + 1);
const found = await Promise.all(
roots.map(async (root) => {
const names: string[] = await list(parent ? `${root}/${parent}` : root).catch(() => []);
return names.includes(name);
}),
);
return found.some(Boolean);
}
export async function refreshWorkspaceContains(
roots: readonly string[],
keys: readonly string[],
list: (dir: string) => Promise<string[]>,
isCurrent: () => boolean = () => true,
): Promise<void> {
const results = await Promise.all(
keys.map(async (key) => {
const path = relativePath(key);
return [key, path !== null && (await existsUnder(roots, path, list))] as const;
}),
);
if (!isCurrent()) {
return;
}
for (const [key, value] of results) {
setContext(key, value);
}
}
/** Which of the relative `paths` the open project has. */
export async function projectHas(
paths: readonly string[],
list: (dir: string) => Promise<string[]> = listNames,
): Promise<Set<string>> {
const roots = projectRoots();
const valid = paths.filter((p) => relativePath(`${PREFIX}${p}`) !== null);
const found = await Promise.all(valid.map(async (p) => [p, await existsUnder(roots, p, list)] as const));
return new Set(found.filter(([, ok]) => ok).map(([p]) => p));
}
const listNames = (dir: string) => call(commands.fsReadDir(dir)).then((entries) => entries.map((e) => e.name));
const projectRoots = () => {
const project = useProject.getState().project;
return project?.workspace?.projects ?? (project ? [project.root] : []);
};
export function startWorkspaceContains(): () => void {
let latest = 0;
const currentKeys = () =>
workspaceContainsKeys(usePlugins.getState().ui.map((u) => (u.decl as { when?: string | null }).when));
const refresh = () => {
const token = ++latest;
void refreshWorkspaceContains(projectRoots(), currentKeys(), listNames, () => token === latest);
};
const disposers = [
usePlugins.subscribe((s, prev) => {
if (s.ui !== prev.ui) refresh();
}),
useProject.subscribe((s, prev) => {
if (
s.project?.root !== prev.project?.root ||
s.project?.workspace?.projects.join("\n") !== prev.project?.workspace?.projects.join("\n")
) {
refresh();
}
}),
onIdeEvent("fsChanged", (e) => {
if (affectsWorkspaceContains([...e.created, ...e.removed], projectRoots(), currentKeys())) refresh();
}),
onIdeEvent("excludedDirsChanged", (e) => {
if (affectsWorkspaceContains(e.dirs, projectRoots(), currentKeys())) refresh();
}),
];
refresh();
return () => disposers.forEach((d) => d());
}