Files
openidea/src/workbench/fileColors.ts
T
l0ng-ai bedd9d2fc9 feat(appearance): scopes and file colors for the project view and editor tabs (#3624)
Settings › Appearance & Behavior gets Scopes (named file sets as file:pattern expressions with ||, && and !, optionally limited to a module) and File Colors (a background colour per scope, first match wins, in the project view and on editor tabs; on by default with Tests green and Non-Project Files yellow). The project view's context menu gets File Color for a file or folder, kept as a scope of that path first in the project's File Colors, with Clear File Color.

Closes #3183
2026-11-16 09:36:14 +08:00

32 lines
1.5 KiB
TypeScript

/** File Colors for the views that show them (project view, editor tabs). */
import { useMemo } from "react";
import { fileColorOf, fileColorSettings, type FileColorPlace } from "@/platform/fileColors";
import { useProject } from "@/platform/project";
import { useScopeContext } from "@/platform/scopes";
import { useProjectViewRoots } from "./contributions";
const rawFileColors = (s: { settings: unknown }) => (s.settings as { fileColors?: unknown } | null)?.fileColors;
/**
* The colour lookup of `place`, or null while file colours are off there. It changes
* with the settings and the project's roots; lookups are memoised per path.
*/
export function useFileColors(place: FileColorPlace): ((path: string, dir?: boolean) => string | undefined) | null {
const raw = useProject(rawFileColors);
const ctx = useScopeContext();
const extra = useProjectViewRoots((s) => s.roots);
return useMemo(() => {
const s = fileColorSettings(raw);
if (!s.enabled || !s[place] || !s.colors.length) return null;
// Scratches are neither project nor non-project files.
const ownFolders = [...extra.values()].flatMap((r) => (r.scope === "scratches" && r.path ? [r.path] : []));
const scope = { ...ctx, ownFolders };
const memo = new Map<string, string | undefined>();
return (path, dir = false) => {
const key = dir ? `${path}/` : path;
if (!memo.has(key)) memo.set(key, fileColorOf(path, s, scope, dir));
return memo.get(key);
};
}, [raw, ctx, extra, place]);
}