Skip to content

Commit 657239b

Browse files
authored
Fix material icon & diff highlight (#33844)
1 parent c102492 commit 657239b

File tree

10 files changed

+247
-3279
lines changed

10 files changed

+247
-3279
lines changed

modules/fileicon/material.go

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,9 @@ import (
1818
)
1919

2020
type materialIconRulesData struct {
21-
IconDefinitions map[string]*struct {
22-
IconPath string `json:"iconPath"`
23-
} `json:"iconDefinitions"`
2421
FileNames map[string]string `json:"fileNames"`
2522
FolderNames map[string]string `json:"folderNames"`
2623
FileExtensions map[string]string `json:"fileExtensions"`
27-
LanguageIDs map[string]string `json:"languageIds"`
2824
}
2925

3026
type MaterialIconProvider struct {
@@ -36,6 +32,7 @@ type MaterialIconProvider struct {
3632
var materialIconProvider MaterialIconProvider
3733

3834
func DefaultMaterialIconProvider() *MaterialIconProvider {
35+
materialIconProvider.once.Do(materialIconProvider.loadData)
3936
return &materialIconProvider
4037
}
4138

@@ -88,8 +85,6 @@ func (m *MaterialIconProvider) renderFileIconSVG(ctx reqctx.RequestContext, name
8885
}
8986

9087
func (m *MaterialIconProvider) FileIcon(ctx reqctx.RequestContext, entry *git.TreeEntry) template.HTML {
91-
m.once.Do(m.loadData)
92-
9388
if m.rules == nil {
9489
return BasicThemeIcon(entry)
9590
}
@@ -101,7 +96,7 @@ func (m *MaterialIconProvider) FileIcon(ctx reqctx.RequestContext, entry *git.Tr
10196
return svg.RenderHTML("octicon-file-symlink-file") // TODO: find some better icons for them
10297
}
10398

104-
name := m.findIconName(entry)
99+
name := m.findIconNameByGit(entry)
105100
if name == "folder" {
106101
// the material icon pack's "folder" icon doesn't look good, so use our built-in one
107102
return svg.RenderHTML("material-folder-generic")
@@ -112,34 +107,23 @@ func (m *MaterialIconProvider) FileIcon(ctx reqctx.RequestContext, entry *git.Tr
112107
return svg.RenderHTML("octicon-file")
113108
}
114109

115-
func (m *MaterialIconProvider) findIconName(entry *git.TreeEntry) string {
116-
if entry.IsSubModule() {
117-
return "folder-git"
118-
}
119-
110+
func (m *MaterialIconProvider) FindIconName(name string, isDir bool) string {
120111
iconsData := m.rules
121-
fileName := path.Base(entry.Name())
122-
123-
if entry.IsDir() {
124-
if s, ok := iconsData.FolderNames[fileName]; ok {
125-
return s
126-
}
127-
if s, ok := iconsData.FolderNames[strings.ToLower(fileName)]; ok {
112+
fileNameLower := strings.ToLower(path.Base(name))
113+
if isDir {
114+
if s, ok := iconsData.FolderNames[fileNameLower]; ok {
128115
return s
129116
}
130117
return "folder"
131118
}
132119

133-
if s, ok := iconsData.FileNames[fileName]; ok {
134-
return s
135-
}
136-
if s, ok := iconsData.FileNames[strings.ToLower(fileName)]; ok {
120+
if s, ok := iconsData.FileNames[fileNameLower]; ok {
137121
return s
138122
}
139123

140-
for i := len(fileName) - 1; i >= 0; i-- {
141-
if fileName[i] == '.' {
142-
ext := fileName[i+1:]
124+
for i := len(fileNameLower) - 1; i >= 0; i-- {
125+
if fileNameLower[i] == '.' {
126+
ext := fileNameLower[i+1:]
143127
if s, ok := iconsData.FileExtensions[ext]; ok {
144128
return s
145129
}
@@ -148,3 +132,10 @@ func (m *MaterialIconProvider) findIconName(entry *git.TreeEntry) string {
148132

149133
return "file"
150134
}
135+
136+
func (m *MaterialIconProvider) findIconNameByGit(entry *git.TreeEntry) string {
137+
if entry.IsSubModule() {
138+
return "folder-git"
139+
}
140+
return m.FindIconName(entry.Name(), entry.IsDir())
141+
}

modules/fileicon/material_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package fileicon_test
5+
6+
import (
7+
"testing"
8+
9+
"code.gitea.io/gitea/models/unittest"
10+
"code.gitea.io/gitea/modules/fileicon"
11+
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func TestMain(m *testing.M) {
16+
unittest.MainTest(m, &unittest.TestOptions{FixtureFiles: []string{}})
17+
}
18+
19+
func TestFindIconName(t *testing.T) {
20+
unittest.PrepareTestEnv(t)
21+
p := fileicon.DefaultMaterialIconProvider()
22+
assert.Equal(t, "php", p.FindIconName("foo.php", false))
23+
assert.Equal(t, "php", p.FindIconName("foo.PHP", false))
24+
}

modules/git/commit_info_nogogit.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath
6565
log.Debug("missing commit for %s", entry.Name())
6666
}
6767

68-
// If the entry if a submodule add a submodule file for this
68+
// If the entry is a submodule add a submodule file for this
6969
if entry.IsSubModule() {
7070
subModuleURL := ""
7171
var fullPath string
@@ -85,8 +85,8 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath
8585
}
8686

8787
// Retrieve the commit for the treePath itself (see above). We basically
88-
// get it for free during the tree traversal and it's used for listing
89-
// pages to display information about newest commit for a given path.
88+
// get it for free during the tree traversal, and it's used for listing
89+
// pages to display information about the newest commit for a given path.
9090
var treeCommit *Commit
9191
var ok bool
9292
if treePath == "" {

0 commit comments

Comments
 (0)