Skip to content

Commit 9944fda

Browse files
refactor(FileTypeConverter): 重构文件类型转换器
- 将 switch 语句替换为 if语句,提高代码可读性 - 使用变量存储文件扩展名,减少重复代码 - 添加新支持的文件类型:c 、cpp、h、hpp、cs、kt、swift、m、mm、go、rs、ts、tsx- 更新 README.md 中的示例图片
1 parent e07e79a commit 9944fda

6 files changed

Lines changed: 140 additions & 59 deletions

File tree

CodeGenerator/Converters/FileTypeConverter.cs

Lines changed: 137 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,65 +16,144 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
1616
return new BitmapImage(new Uri("/CodeGenerator;component/Images/file.png", UriTypeKind));
1717
}
1818

19-
switch (value)
20-
{
21-
case ".class":
22-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/class.png", UriTypeKind));
23-
case ".css":
24-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/css.png", UriTypeKind));
25-
case ".doc":
26-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/doc.png", UriTypeKind));
27-
case ".dockerfile":
28-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/dockerfile.png", UriTypeKind));
29-
case ".exe":
30-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/exe.png", UriTypeKind));
31-
case ".gitignore":
32-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/gitignore.png", UriTypeKind));
33-
case ".html":
34-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/html.png", UriTypeKind));
35-
case ".iso":
36-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/iso.png", UriTypeKind));
37-
case ".jar":
38-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/jar.png", UriTypeKind));
39-
case ".java":
40-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/java.png", UriTypeKind));
41-
case ".jpg":
42-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/jpg.png", UriTypeKind));
43-
case ".js":
44-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/js.png", UriTypeKind));
45-
case ".json":
46-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/json.png", UriTypeKind));
47-
case ".md":
48-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/md.png", UriTypeKind));
49-
case ".mp3":
50-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/mp3.png", UriTypeKind));
51-
case ".mp4":
52-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/mp4.png", UriTypeKind));
53-
case ".pdf":
54-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/pdf.png", UriTypeKind));
55-
case ".png":
56-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/png.png", UriTypeKind));
57-
case ".ppt":
58-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/ppt.png", UriTypeKind));
59-
case ".py":
60-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/py.png", UriTypeKind));
61-
case ".sql":
62-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/sql.png", UriTypeKind));
63-
case ".svg":
64-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/svg.png", UriTypeKind));
65-
case ".txt":
66-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/txt.png", UriTypeKind));
67-
case ".xls":
68-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/xls.png", UriTypeKind));
69-
case ".xml":
70-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/xml.png", UriTypeKind));
71-
case ".yml":
72-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/yml.png", UriTypeKind));
73-
case ".zip":
74-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/zip.png", UriTypeKind));
75-
default:
76-
return new BitmapImage(new Uri("/CodeGenerator;component/Images/file.png", UriTypeKind));
19+
var fileExtension = value.ToString().Trim().ToLower();
20+
21+
if (fileExtension.EndsWith(".class", StringComparison.OrdinalIgnoreCase))
22+
{
23+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/class.png", UriTypeKind));
24+
}
25+
26+
if (fileExtension.EndsWith(".css", StringComparison.OrdinalIgnoreCase))
27+
{
28+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/css.png", UriTypeKind));
29+
}
30+
31+
if (fileExtension.EndsWith(".doc", StringComparison.OrdinalIgnoreCase))
32+
{
33+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/doc.png", UriTypeKind));
34+
}
35+
36+
if (fileExtension.EndsWith(".dockerfile", StringComparison.OrdinalIgnoreCase))
37+
{
38+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/dockerfile.png", UriTypeKind));
39+
}
40+
41+
if (fileExtension.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
42+
{
43+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/exe.png", UriTypeKind));
44+
}
45+
46+
if (fileExtension.EndsWith(".gitignore", StringComparison.OrdinalIgnoreCase))
47+
{
48+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/gitignore.png", UriTypeKind));
49+
}
50+
51+
if (fileExtension.EndsWith(".html", StringComparison.OrdinalIgnoreCase))
52+
{
53+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/html.png", UriTypeKind));
54+
}
55+
56+
if (fileExtension.EndsWith(".iso", StringComparison.OrdinalIgnoreCase))
57+
{
58+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/iso.png", UriTypeKind));
59+
}
60+
61+
if (fileExtension.EndsWith(".jar", StringComparison.OrdinalIgnoreCase))
62+
{
63+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/jar.png", UriTypeKind));
64+
}
65+
66+
if (fileExtension.EndsWith(".java", StringComparison.OrdinalIgnoreCase))
67+
{
68+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/java.png", UriTypeKind));
69+
}
70+
71+
if (fileExtension.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase))
72+
{
73+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/jpg.png", UriTypeKind));
74+
}
75+
76+
if (fileExtension.EndsWith(".js", StringComparison.OrdinalIgnoreCase))
77+
{
78+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/js.png", UriTypeKind));
79+
}
80+
81+
if (fileExtension.EndsWith(".json", StringComparison.OrdinalIgnoreCase))
82+
{
83+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/json.png", UriTypeKind));
84+
}
85+
86+
if (fileExtension.EndsWith(".md", StringComparison.OrdinalIgnoreCase))
87+
{
88+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/md.png", UriTypeKind));
7789
}
90+
91+
if (fileExtension.EndsWith(".mp3", StringComparison.OrdinalIgnoreCase))
92+
{
93+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/mp3.png", UriTypeKind));
94+
}
95+
96+
if (fileExtension.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase))
97+
{
98+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/mp4.png", UriTypeKind));
99+
}
100+
101+
if (fileExtension.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase))
102+
{
103+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/pdf.png", UriTypeKind));
104+
}
105+
106+
if (fileExtension.EndsWith(".png", StringComparison.OrdinalIgnoreCase))
107+
{
108+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/png.png", UriTypeKind));
109+
}
110+
111+
if (fileExtension.EndsWith(".ppt", StringComparison.OrdinalIgnoreCase))
112+
{
113+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/ppt.png", UriTypeKind));
114+
}
115+
116+
if (fileExtension.EndsWith(".py", StringComparison.OrdinalIgnoreCase))
117+
{
118+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/py.png", UriTypeKind));
119+
}
120+
121+
if (fileExtension.EndsWith(".sql", StringComparison.OrdinalIgnoreCase))
122+
{
123+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/sql.png", UriTypeKind));
124+
}
125+
126+
if (fileExtension.EndsWith(".svg", StringComparison.OrdinalIgnoreCase))
127+
{
128+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/svg.png", UriTypeKind));
129+
}
130+
131+
if (fileExtension.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
132+
{
133+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/txt.png", UriTypeKind));
134+
}
135+
136+
if (fileExtension.EndsWith(".xls", StringComparison.OrdinalIgnoreCase))
137+
{
138+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/xls.png", UriTypeKind));
139+
}
140+
141+
if (fileExtension.EndsWith(".xml", StringComparison.OrdinalIgnoreCase))
142+
{
143+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/xml.png", UriTypeKind));
144+
}
145+
146+
if (fileExtension.EndsWith(".yml", StringComparison.OrdinalIgnoreCase))
147+
{
148+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/yml.png", UriTypeKind));
149+
}
150+
151+
if (fileExtension.EndsWith(".zip", StringComparison.OrdinalIgnoreCase))
152+
{
153+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/zip.png", UriTypeKind));
154+
}
155+
156+
return new BitmapImage(new Uri("/CodeGenerator;component/Images/file.png", UriTypeKind));
78157
}
79158

80159
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

Example/CodeGenerator.zip

728 Bytes
Binary file not shown.
-258 KB
Binary file not shown.
110 KB
Loading
287 KB
Loading

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010

1111
![微信截图_20240405184400.png](Example/微信截图_20240405184400.png)
1212

13+
![微信截图_20240405184400.png](Example/微信截图_20250512105341.png)
14+
1315
2、点击【选择文件夹】选择需要格式化的代码源文件(尽量不要将build文件夹带上,因为编译项目会产生大量编译文件,可能加载起来较慢),然后添加需要格式化的代码后缀,如:java、kt、cs、cpp等,支持多文件类型同时格式化。如果源码超过3000行,会取源码前30页和后30页,每页50行。如下图:
1416

15-
![微信截图_20240825140452.png](Example/微信截图_20240825140452.png)
17+
![微信截图_20240825140452.png](Example/微信截图_20250512111046.png)
1618

1719
3、双击文件,会使用用户电脑默认软件打开刚刚选中的文件,如下图:
1820

0 commit comments

Comments
 (0)