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
78function extensionCandidates(fileName: string): string[] {
79 const candidates = new Set<string>();
80 if (fileName.includes(".")) {
81 candidates.add(fileName);
82 }
83 let dotIndex = fileName.indexOf(".");
84 while (dotIndex !== -1 && dotIndex < fileName.length - 1) {
85 const candidate = fileName.slice(dotIndex + 1);
86 if (candidate.length > 0) {
87 candidates.add(candidate);
88 }
89 dotIndex = fileName.indexOf(".", dotIndex + 1);
90 }
91 return [...candidates];
92}
93
94function resolveLanguageFallbackDefinition(
95 pathValue: string,
96 theme: "light" | "dark",
97): string | null {
98 const basename = basenameOfPath(pathValue).toLowerCase();
99 const languageIds = theme === "light" ? lightLanguageIds : darkLanguageIds;
100
101 const fromBasenameLanguage = languageIdByFileName[basename];
102 if (fromBasenameLanguage) {
103 return languageIds[fromBasenameLanguage] ?? darkLanguageIds[fromBasenameLanguage] ?? null;
104 }
105
106 for (const candidate of extensionCandidates(basename)) {
107 const languageId =
108 localLanguageIdByExtensionOverrides[
109 candidate as keyof typeof localLanguageIdByExtensionOverrides
110 ] ?? languageIdByExtension[candidate];
111 if (!languageId) continue;
112 return languageIds[languageId] ?? darkLanguageIds[languageId] ?? null;
113 }
114
115 return null;
116}
117
118function iconFilenameForDefinitionKey(definitionKey: string | undefined): string | null {
119 if (!definitionKey) return null;
120 const iconPath = iconDefinitions[definitionKey]?.iconPath;
121 if (!iconPath) return null;
122 const slashIndex = iconPath.lastIndexOf("/");
123 if (slashIndex === -1) {
124 return iconPath;
125 }
126 return iconPath.slice(slashIndex + 1);
127}
128
129function resolveFileDefinition(pathValue: string, theme: "light" | "dark"): string {
130 const basename = basenameOfPath(pathValue).toLowerCase();
131 const fileNames = theme === "light" ? lightFileNames : darkFileNames;
132 const fileExtensions = theme === "light" ? lightFileExtensions : darkFileExtensions;
133
134 const fromFileName = fileNames[basename] ?? darkFileNames[basename];
135 if (fromFileName) return fromFileName;
136
137 for (const candidate of extensionCandidates(basename)) {
138 const fromExtension = fileExtensions[candidate] ?? darkFileExtensions[candidate];
139 if (fromExtension) return fromExtension;
140 }
141
142 const fromLanguage = resolveLanguageFallbackDefinition(pathValue, theme);
143 if (fromLanguage) return fromLanguage;
144
145 return theme === "light" ? defaultLightFileIconDefinition : defaultDarkFileIconDefinition;
146}
147
148function resolveFolderDefinition(pathValue: string, theme: "light" | "dark"): string {
149 const basename = basenameOfPath(pathValue).toLowerCase();
150 const folderNames = theme === "light" ? lightFolderNames : darkFolderNames;
151 return (
152 folderNames[basename] ??
153 darkFolderNames[basename] ??
154 (theme === "light" ? defaultLightFolderIconDefinition : defaultDarkFolderIconDefinition)
155 );
156}
157
158export function getVscodeIconUrlForEntry(
159 pathValue: string,
160 kind: "file" | "directory",
161 theme: "light" | "dark",
162): string {
163 const definitionKey =
164 kind === "directory"
165 ? resolveFolderDefinition(pathValue, theme)
166 : resolveFileDefinition(pathValue, theme);
167 const iconFilename =
168 iconFilenameForDefinitionKey(definitionKey) ??
169 (kind === "directory" ? "default_folder.svg" : "default_file.svg");
170 return `${VSCODE_ICONS_BASE_URL}/${iconFilename}`;
171}