Open History to see the last commit
1import vscodeIconsManifest from "./vscode-icons-manifest.json";
2import languageAssociationsData from "./vscode-icons-language-associations.json";
3
4const VSCODE_ICONS_VERSION = "v12.17.0";
5const VSCODE_ICONS_BASE_URL = `https://cdn.jsdelivr.net/gh/vscode-icons/vscode-icons@${VSCODE_ICONS_VERSION}/icons`;
6
7interface IconDefinition {
8 iconPath: string;
9}
10
11interface IconLookupSection {
12 file?: string;
13 folder?: string;
14 fileNames: Record<string, string>;
15 fileExtensions: Record<string, string>;
16 folderNames: Record<string, string>;
17 languageIds?: Record<string, string>;
18}
19
20interface VscodeIconsManifest extends IconLookupSection {
21 iconDefinitions: Record<string, IconDefinition>;
22 light: IconLookupSection;
23}
24
25interface LanguageAssociations {
26 version: string;
27 extensionToLanguageId: Record<string, string>;
28 fileNameToLanguageId: Record<string, string>;
29}
30
31const manifest = vscodeIconsManifest as VscodeIconsManifest;
32const languageAssociations = languageAssociationsData as LanguageAssociations;
33const iconDefinitions = manifest.iconDefinitions;
34
35const darkFileNames = toLowercaseLookup(manifest.fileNames);
36const lightFileNames = toLowercaseLookup(manifest.light.fileNames);
37const darkFileExtensions = toLowercaseLookup(manifest.fileExtensions);
38const lightFileExtensions = toLowercaseLookup(manifest.light.fileExtensions);
39const darkFolderNames = toLowercaseLookup(manifest.folderNames);
40const lightFolderNames = toLowercaseLookup(manifest.light.folderNames);
41const darkLanguageIds = toLowercaseLookup(manifest.languageIds ?? {});
42const lightLanguageIds = toLowercaseLookup(manifest.light.languageIds ?? {});
43const languageIdByExtension = toLowercaseLookup(languageAssociations.extensionToLanguageId);
44const languageIdByFileName = toLowercaseLookup(languageAssociations.fileNameToLanguageId);
45const localLanguageIdByExtensionOverrides = {
46 // Cursor rules files (*.mdc) are commonly treated as markdown in VSCode/Cursor.
47 mdc: "markdown",
48 // Upstream languages.ts currently maps .html to django-html before html.
49 // Prefer the base HTML icon for standalone HTML files.
50 html: "html",
51 // Upstream languages.ts maps yml/yaml to specialized language ids that can produce
52 // non-generic YAML icons (for example cloudfoundry/esphome). Prefer the base YAML icon
53 // unless a more specific basename/extension match (e.g. azure-pipelines.yml) is found.
54 yml: "yaml",
55 yaml: "yaml",
56} as const;
57
58const defaultDarkFileIconDefinition = manifest.file ?? "_file";
59const defaultLightFileIconDefinition = manifest.light.file ?? defaultDarkFileIconDefinition;
60const defaultDarkFolderIconDefinition = manifest.folder ?? "_folder";
61const defaultLightFolderIconDefinition = manifest.light.folder ?? defaultDarkFolderIconDefinition;
62
63function toLowercaseLookup(source: Record<string, string>): Record<string, string> {
64 const entries = Object.entries(source);
65 const lookup: Record<string, string> = {};
66 for (const [key, value] of entries) {
67 lookup[key.toLowerCase()] = value;
68 }
69 return lookup;
70}
71
72export function basenameOfPath(pathValue: string): string {
73 const slashIndex = pathValue.lastIndexOf("/");
74 if (slashIndex === -1) return pathValue;
75 return pathValue.slice(slashIndex + 1);
76}
77
78export function inferEntryKindFromPath(pathValue: string): "file" | "directory" {
79 const base = basenameOfPath(pathValue);
80 if (base.startsWith(".") && !base.slice(1).includes(".")) {
81 return "directory";
82 }
83 if (base.includes(".")) {
84 return "file";
85 }
86 return "directory";
87}
88
89function extensionCandidates(fileName: string): string[] {
90 const candidates = new Set<string>();
91 if (fileName.includes(".")) {
92 candidates.add(fileName);
93 }
94 let dotIndex = fileName.indexOf(".");
95 while (dotIndex !== -1 && dotIndex < fileName.length - 1) {
96 const candidate = fileName.slice(dotIndex + 1);
97 if (candidate.length > 0) {
98 candidates.add(candidate);
99 }
100 dotIndex = fileName.indexOf(".", dotIndex + 1);
101 }
102 return [...candidates];
103}
104
105function resolveLanguageFallbackDefinition(
106 pathValue: string,
107 theme: "light" | "dark",
108): string | null {
109 const basename = basenameOfPath(pathValue).toLowerCase();
110 const languageIds = theme === "light" ? lightLanguageIds : darkLanguageIds;
111
112 const fromBasenameLanguage = languageIdByFileName[basename];
113 if (fromBasenameLanguage) {
114 return languageIds[fromBasenameLanguage] ?? darkLanguageIds[fromBasenameLanguage] ?? null;
115 }
116
117 for (const candidate of extensionCandidates(basename)) {
118 const languageId =
119 localLanguageIdByExtensionOverrides[
120 candidate as keyof typeof localLanguageIdByExtensionOverrides
121 ] ?? languageIdByExtension[candidate];
122 if (!languageId) continue;
123 return languageIds[languageId] ?? darkLanguageIds[languageId] ?? null;
124 }
125
126 return null;
127}
128
129function iconFilenameForDefinitionKey(definitionKey: string | undefined): string | null {
130 if (!definitionKey) return null;
131 const iconPath = iconDefinitions[definitionKey]?.iconPath;
132 if (!iconPath) return null;
133 const slashIndex = iconPath.lastIndexOf("/");
134 if (slashIndex === -1) {
135 return iconPath;
136 }
137 return iconPath.slice(slashIndex + 1);
138}
139
140function resolveFileDefinition(pathValue: string, theme: "light" | "dark"): string {
141 const basename = basenameOfPath(pathValue).toLowerCase();
142 const fileNames = theme === "light" ? lightFileNames : darkFileNames;
143 const fileExtensions = theme === "light" ? lightFileExtensions : darkFileExtensions;
144
145 const fromFileName = fileNames[basename] ?? darkFileNames[basename];
146 if (fromFileName) return fromFileName;
147
148 for (const candidate of extensionCandidates(basename)) {
149 const fromExtension = fileExtensions[candidate] ?? darkFileExtensions[candidate];
150 if (fromExtension) return fromExtension;
151 }
152
153 const fromLanguage = resolveLanguageFallbackDefinition(pathValue, theme);
154 if (fromLanguage) return fromLanguage;
155
156 return theme === "light" ? defaultLightFileIconDefinition : defaultDarkFileIconDefinition;
157}
158
159function resolveFolderDefinition(pathValue: string, theme: "light" | "dark"): string {
160 const basename = basenameOfPath(pathValue).toLowerCase();
161 const folderNames = theme === "light" ? lightFolderNames : darkFolderNames;
162 return (
163 folderNames[basename] ??
164 darkFolderNames[basename] ??
165 (theme === "light" ? defaultLightFolderIconDefinition : defaultDarkFolderIconDefinition)
166 );
167}
168
169export function getVscodeIconUrlForEntry(
170 pathValue: string,
171 kind: "file" | "directory",
172 theme: "light" | "dark",
173): string {
174 const definitionKey =
175 kind === "directory"
176 ? resolveFolderDefinition(pathValue, theme)
177 : resolveFileDefinition(pathValue, theme);
178 const iconFilename =
179 iconFilenameForDefinitionKey(definitionKey) ??
180 (kind === "directory" ? "default_folder.svg" : "default_file.svg");
181 return `${VSCODE_ICONS_BASE_URL}/${iconFilename}`;
182}