Skip to content

Commit f61f301

Browse files
authored
Fix file icon mapping (go-gitea#33855)
Use the file extension mapping from VSCode's extensions. Otherwise js/ts/vba/... files won't get correct icons.
1 parent 608ccc3 commit f61f301

File tree

5 files changed

+809
-108
lines changed

5 files changed

+809
-108
lines changed

modules/fileicon/material.go

+22-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type materialIconRulesData struct {
2121
FileNames map[string]string `json:"fileNames"`
2222
FolderNames map[string]string `json:"folderNames"`
2323
FileExtensions map[string]string `json:"fileExtensions"`
24+
LanguageIDs map[string]string `json:"languageIds"`
2425
}
2526

2627
type MaterialIconProvider struct {
@@ -107,25 +108,40 @@ func (m *MaterialIconProvider) FileIcon(ctx reqctx.RequestContext, entry *git.Tr
107108
return svg.RenderHTML("octicon-file")
108109
}
109110

111+
func (m *MaterialIconProvider) findIconNameWithLangID(s string) string {
112+
if _, ok := m.svgs[s]; ok {
113+
return s
114+
}
115+
if s, ok := m.rules.LanguageIDs[s]; ok {
116+
if _, ok = m.svgs[s]; ok {
117+
return s
118+
}
119+
}
120+
return ""
121+
}
122+
110123
func (m *MaterialIconProvider) FindIconName(name string, isDir bool) string {
111-
iconsData := m.rules
112124
fileNameLower := strings.ToLower(path.Base(name))
113125
if isDir {
114-
if s, ok := iconsData.FolderNames[fileNameLower]; ok {
126+
if s, ok := m.rules.FolderNames[fileNameLower]; ok {
115127
return s
116128
}
117129
return "folder"
118130
}
119131

120-
if s, ok := iconsData.FileNames[fileNameLower]; ok {
121-
return s
132+
if s, ok := m.rules.FileNames[fileNameLower]; ok {
133+
if s = m.findIconNameWithLangID(s); s != "" {
134+
return s
135+
}
122136
}
123137

124138
for i := len(fileNameLower) - 1; i >= 0; i-- {
125139
if fileNameLower[i] == '.' {
126140
ext := fileNameLower[i+1:]
127-
if s, ok := iconsData.FileExtensions[ext]; ok {
128-
return s
141+
if s, ok := m.rules.FileExtensions[ext]; ok {
142+
if s = m.findIconNameWithLangID(s); s != "" {
143+
return s
144+
}
129145
}
130146
}
131147
}

modules/fileicon/material_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ func TestFindIconName(t *testing.T) {
2121
p := fileicon.DefaultMaterialIconProvider()
2222
assert.Equal(t, "php", p.FindIconName("foo.php", false))
2323
assert.Equal(t, "php", p.FindIconName("foo.PHP", false))
24+
assert.Equal(t, "javascript", p.FindIconName("foo.js", false))
25+
assert.Equal(t, "visualstudio", p.FindIconName("foo.vba", false))
2426
}

options/fileicon/material-icon-rules.json

+197-99
Original file line numberDiff line numberDiff line change
@@ -7038,107 +7038,201 @@
70387038
"gnu": "gnuplot",
70397039
"yaml-tmlanguage": "yaml",
70407040
"tmlanguage": "xml",
7041-
"git": "git",
7042-
"git-commit": "git",
7043-
"git-rebase": "git",
7044-
"ignore": "git",
7045-
"github-actions-workflow": "github-actions-workflow",
7046-
"yaml": "yaml",
7047-
"spring-boot-properties-yaml": "yaml",
7048-
"ansible": "yaml",
7049-
"ansible-jinja": "yaml",
7050-
"matlab": "matlab",
7051-
"makefile": "settings",
7052-
"spring-boot-properties": "settings",
7041+
"cljx": "clojure",
7042+
"clojure": "clojure",
7043+
"edn": "clojure",
7044+
"cppm": "cpp",
7045+
"ccm": "cpp",
7046+
"cxxm": "cpp",
7047+
"c++m": "cpp",
7048+
"ipp": "cpp",
7049+
"ixx": "cpp",
7050+
"tpp": "cpp",
7051+
"txx": "cpp",
7052+
"hpp.in": "cpp",
7053+
"h.in": "cpp",
70537054
"diff": "diff",
7054-
"razor": "razor",
7055-
"aspnetcorerazor": "razor",
7056-
"python": "python",
7057-
"javascript": "javascript",
7058-
"typescript": "typescript",
7055+
"rej": "diff",
7056+
"fsscript": "fsharp",
7057+
"gitignore_global": "ignore",
7058+
"gitignore": "ignore",
7059+
"git-blame-ignore-revs": "ignore",
7060+
"gvy": "groovy",
7061+
"nf": "groovy",
70597062
"handlebars": "handlebars",
7060-
"perl": "perl",
7061-
"perl6": "perl",
7062-
"haxe": "haxe",
7063-
"hxml": "haxe",
7064-
"puppet": "puppet",
7065-
"elixir": "elixir",
7066-
"livescript": "livescript",
7067-
"erlang": "erlang",
7068-
"julia": "julia",
7069-
"purescript": "purescript",
7070-
"stylus": "stylus",
7071-
"robotframework": "robot",
7072-
"testoutput": "visualstudio",
7073-
"solidity": "solidity",
7074-
"autoit": "autoit",
7075-
"terraform": "terraform",
7076-
"cucumber": "cucumber",
7077-
"postcss": "postcss",
7078-
"lang-cfml": "coldfusion",
7079-
"haskell": "haskell",
7080-
"ruby": "ruby",
7081-
"php": "php",
7082-
"hack": "hack",
7083-
"javascriptreact": "react",
7084-
"processing": "processing",
7085-
"django-html": "django",
7086-
"django-txt": "django",
7063+
"hjs": "handlebars",
7064+
"hlsli": "hlsl",
7065+
"fx": "hlsl",
7066+
"fxh": "hlsl",
7067+
"vsh": "hlsl",
7068+
"psh": "hlsl",
7069+
"cginc": "hlsl",
7070+
"compute": "hlsl",
70877071
"html": "html",
7088-
"gdscript": "godot",
7089-
"gdresource": "godot-assets",
7090-
"viml": "vim",
7091-
"prolog": "prolog",
7092-
"pawn": "pawn",
7093-
"reason": "reason",
7094-
"reason_lisp": "reason",
7095-
"doctex": "tex",
7096-
"latex": "tex",
7097-
"latex-expl3": "tex",
7098-
"apex": "salesforce",
7099-
"dockercompose": "docker",
7100-
"shellscript": "console",
7101-
"objective-c": "objective-c",
7102-
"objective-cpp": "objective-cpp",
7103-
"coffeescript": "coffee",
7104-
"fsharp": "fsharp",
7105-
"editorconfig": "editorconfig",
7106-
"clojure": "clojure",
7107-
"pip-requirements": "python-misc",
7108-
"vue-postcss": "vue",
7109-
"vue-html": "vue",
7110-
"bibtex": "lib",
7111-
"bibtex-style": "lib",
7112-
"jupyter": "jupyter",
7113-
"plaintext": "document",
7114-
"powershell": "powershell",
7115-
"rsweave": "r",
7116-
"rust": "rust",
7117-
"ssh_config": "lock",
7118-
"typescriptreact": "react_ts",
7119-
"search-result": "search",
7120-
"rescript": "rescript",
7121-
"twee3": "twine",
7122-
"twee3-harlowe-3": "twine",
7123-
"twee3-chapbook-1": "twine",
7124-
"twee3-sugarcube-2": "twine",
7125-
"grain": "grain",
7126-
"lolcode": "lolcode",
7127-
"idris": "idris",
7128-
"text-gemini": "gemini",
7129-
"wolfram": "wolframlanguage",
7130-
"shaderlab": "shader",
7131-
"cadence": "cadence",
7132-
"stylable": "stylable",
7133-
"capnb": "cds",
7134-
"cds-markdown-injection": "cds",
7135-
"concourse-pipeline-yaml": "concourse",
7136-
"concourse-task-yaml": "concourse",
7137-
"systemd-conf": "systemd",
7138-
"systemd-unit-file": "systemd",
7139-
"hosts": "hosts",
7140-
"ahk2": "ahk2",
7141-
"gnuplot": "gnuplot"
7072+
"shtml": "html",
7073+
"xht": "html",
7074+
"aspx": "html",
7075+
"jshtm": "html",
7076+
"volt": "html",
7077+
"rhtml": "html",
7078+
"directory": "properties",
7079+
"gitattributes": "properties",
7080+
"gitconfig": "properties",
7081+
"gitmodules": "properties",
7082+
"editorconfig": "properties",
7083+
"repo": "properties",
7084+
"jav": "java",
7085+
"js": "javascript",
7086+
"es6": "javascript",
7087+
"cjs": "javascript",
7088+
"pac": "javascript",
7089+
"bowerrc": "json",
7090+
"jscsrc": "json",
7091+
"webmanifest": "json",
7092+
"ts.map": "json",
7093+
"har": "json",
7094+
"jslintrc": "json",
7095+
"jsonld": "json",
7096+
"geojson": "json",
7097+
"vuerc": "json",
7098+
"eslintrc": "jsonc",
7099+
"eslintrc.json": "jsonc",
7100+
"jsfmtrc": "jsonc",
7101+
"jshintrc": "jsonc",
7102+
"hintrc": "jsonc",
7103+
"babelrc": "jsonc",
7104+
"jmd": "juliamarkdown",
7105+
"cls": "tex",
7106+
"bbx": "tex",
7107+
"cbx": "tex",
7108+
"ctx": "latex",
7109+
"mak": "makefile",
7110+
"mkd": "markdown",
7111+
"mdwn": "markdown",
7112+
"mdown": "markdown",
7113+
"markdn": "markdown",
7114+
"mdtxt": "markdown",
7115+
"mdtext": "markdown",
7116+
"workbook": "markdown",
7117+
"npmignore": "ignore",
7118+
"npmrc": "properties",
7119+
"m": "objective-c",
7120+
"mm": "objective-cpp",
7121+
"pod": "perl",
7122+
"t": "perl",
7123+
"psgi": "perl",
7124+
"rakumod": "raku",
7125+
"rakutest": "raku",
7126+
"rakudoc": "raku",
7127+
"nqp": "raku",
7128+
"p6": "raku",
7129+
"pl6": "raku",
7130+
"pm6": "raku",
7131+
"php": "php",
7132+
"php4": "php",
7133+
"php5": "php",
7134+
"phtml": "php",
7135+
"ctp": "php",
7136+
"psrc": "powershell",
7137+
"rpy": "python",
7138+
"pyw": "python",
7139+
"cpy": "python",
7140+
"gyp": "python",
7141+
"gypi": "python",
7142+
"pyi": "python",
7143+
"ipy": "python",
7144+
"pyt": "python",
7145+
"rhistory": "r",
7146+
"rprofile": "r",
7147+
"rt": "r",
7148+
"razor": "razor",
7149+
"rbx": "ruby",
7150+
"rjs": "ruby",
7151+
"gemspec": "ruby",
7152+
"rake": "ruby",
7153+
"ru": "ruby",
7154+
"podspec": "ruby",
7155+
"rbi": "ruby",
7156+
"bashrc": "shellscript",
7157+
"bash_aliases": "shellscript",
7158+
"bash_profile": "shellscript",
7159+
"bash_login": "shellscript",
7160+
"ebuild": "shellscript",
7161+
"eclass": "shellscript",
7162+
"profile": "shellscript",
7163+
"bash_logout": "shellscript",
7164+
"xprofile": "shellscript",
7165+
"xsession": "shellscript",
7166+
"xsessionrc": "shellscript",
7167+
"zshrc": "shellscript",
7168+
"zprofile": "shellscript",
7169+
"zlogin": "shellscript",
7170+
"zlogout": "shellscript",
7171+
"zshenv": "shellscript",
7172+
"zsh-theme": "shellscript",
7173+
"cshrc": "shellscript",
7174+
"tcshrc": "shellscript",
7175+
"yashrc": "shellscript",
7176+
"yash_profile": "shellscript",
7177+
"dsql": "sql",
7178+
"ts": "typescript",
7179+
"cts": "typescript",
7180+
"mts": "typescript",
7181+
"brs": "vb",
7182+
"bas": "vb",
7183+
"vba": "vb",
7184+
"ascx": "xml",
7185+
"atom": "xml",
7186+
"axml": "xml",
7187+
"axaml": "xml",
7188+
"bpmn": "xml",
7189+
"csl": "xml",
7190+
"csproj.user": "xml",
7191+
"dita": "xml",
7192+
"ditamap": "xml",
7193+
"ent": "xml",
7194+
"dtml": "xml",
7195+
"fxml": "xml",
7196+
"isml": "xml",
7197+
"jmx": "xml",
7198+
"launch": "xml",
7199+
"menu": "xml",
7200+
"nuspec": "xml",
7201+
"opml": "xml",
7202+
"owl": "xml",
7203+
"proj": "xml",
7204+
"pt": "xml",
7205+
"publishsettings": "xml",
7206+
"pubxml": "xml",
7207+
"pubxml.user": "xml",
7208+
"rdf": "xml",
7209+
"rng": "xml",
7210+
"rss": "xml",
7211+
"shproj": "xml",
7212+
"storyboard": "xml",
7213+
"targets": "xml",
7214+
"tld": "xml",
7215+
"tmx": "xml",
7216+
"vbproj": "xml",
7217+
"vbproj.user": "xml",
7218+
"wsdl": "xml",
7219+
"wxi": "xml",
7220+
"wxl": "xml",
7221+
"wxs": "xml",
7222+
"xbl": "xml",
7223+
"xib": "xml",
7224+
"xliff": "xml",
7225+
"xpdl": "xml",
7226+
"xul": "xml",
7227+
"xoml": "xml",
7228+
"yaml": "yaml",
7229+
"yml": "yaml",
7230+
"eyaml": "yaml",
7231+
"eyml": "yaml",
7232+
"cff": "yaml",
7233+
"yaml-tmpreferences": "yaml",
7234+
"yaml-tmtheme": "yaml",
7235+
"winget": "yaml"
71427236
},
71437237
"fileNames": {
71447238
".pug-lintrc": "pug",
@@ -9015,7 +9109,11 @@
90159109
"caddyfile": "caddy",
90169110
"pklproject": "pkl",
90179111
"pklproject.deps.json": "pkl",
9018-
".github/funding.yml": "github-sponsors"
9112+
".github/funding.yml": "github-sponsors",
9113+
"language-configuration.json": "jsonc",
9114+
"icon-theme.json": "jsonc",
9115+
"color-theme.json": "jsonc",
9116+
"*.log.?": "log"
90199117
},
90209118
"languageIds": {
90219119
"git": "git",

0 commit comments

Comments
 (0)