1
1
using System ;
2
2
using System . Collections . Generic ;
3
- using System . Linq ;
4
3
5
4
namespace ChenPipi . CodeExecutor . Editor
6
5
{
@@ -17,6 +16,7 @@ public static class CodeExecutorData
17
16
private class UserData
18
17
{
19
18
public int version = 0 ;
19
+ public List < string > categories = new List < string > ( ) ;
20
20
public SnippetInfo newSnippet = new SnippetInfo ( ) ;
21
21
public List < SnippetInfo > snippets = new List < SnippetInfo > ( )
22
22
{
@@ -27,8 +27,11 @@ private class UserData
27
27
editTime = 0 ,
28
28
top = false ,
29
29
name = "HelloWorld" ,
30
- code = "UnityEngine.Debug.Log(\" [CodeExecutor] Hello World!\" );\n UnityEngine.Debug.LogWarning(\" [CodeExecutor] Hello World!\" );\n UnityEngine.Debug.LogError(\" [CodeExecutor] Hello World!\" );" ,
31
- mode = "C#"
30
+ code = @"UnityEngine.Debug.Log(""[CodeExecutor] Hello World!"");
31
+ UnityEngine.Debug.LogWarning(""[CodeExecutor] Hello World!"");
32
+ UnityEngine.Debug.LogError(""[CodeExecutor] Hello World!"");" ,
33
+ mode = "C#" ,
34
+ category = null ,
32
35
} ,
33
36
new SnippetInfo ( )
34
37
{
@@ -38,7 +41,8 @@ private class UserData
38
41
top = false ,
39
42
name = "CrazyThursday" ,
40
43
code = "UnityEngine.Debug.LogError(\" [CodeExecutor] Crazy Thursday\" );" ,
41
- mode = "C#"
44
+ mode = "C#" ,
45
+ category = "Test" ,
42
46
} ,
43
47
new SnippetInfo ( )
44
48
{
@@ -48,7 +52,8 @@ private class UserData
48
52
top = false ,
49
53
name = "TestImport" ,
50
54
code = "@import(\" CrazyThursday\" )\r \n \n UnityEngine.Debug.LogError(\" [CodeExecutor] V Me 50\" );" ,
51
- mode = "C#"
55
+ mode = "C#" ,
56
+ category = "Test" ,
52
57
} ,
53
58
} ;
54
59
}
@@ -70,6 +75,85 @@ private static UserData userData
70
75
71
76
#endregion
72
77
78
+ #region Categories
79
+
80
+ public static List < string > GetCategories ( )
81
+ {
82
+ List < string > list = new List < string > ( userData . categories ) ;
83
+ foreach ( SnippetInfo snippet in snippets )
84
+ {
85
+ if ( ! string . IsNullOrEmpty ( snippet . category ) && ! list . Contains ( snippet . category ) )
86
+ {
87
+ userData . categories . Add ( snippet . category ) ;
88
+ list . Add ( snippet . category ) ;
89
+ }
90
+ }
91
+ return list ;
92
+ }
93
+
94
+ private static List < string > categories => s_UserData . categories ;
95
+
96
+ public static bool HasCategory ( string name )
97
+ {
98
+ if ( string . IsNullOrEmpty ( name ) )
99
+ {
100
+ return false ;
101
+ }
102
+ return categories . Contains ( name ) ;
103
+ }
104
+
105
+ public static void AddCategory ( string name )
106
+ {
107
+ if ( string . IsNullOrEmpty ( name ) || HasCategory ( name ) )
108
+ {
109
+ return ;
110
+ }
111
+ categories . Add ( name ) ;
112
+ }
113
+
114
+ public static void RemoveCategory ( string name )
115
+ {
116
+ if ( string . IsNullOrEmpty ( name ) )
117
+ {
118
+ return ;
119
+ }
120
+ categories . Remove ( name ) ;
121
+ // 更新代码段
122
+ foreach ( SnippetInfo snippet in snippets )
123
+ {
124
+ if ( name . Equals ( snippet . category , StringComparison . OrdinalIgnoreCase ) )
125
+ {
126
+ snippet . category = null ;
127
+ }
128
+ }
129
+ }
130
+
131
+ public static void RenameCategory ( string originalName , string newName )
132
+ {
133
+ if ( string . IsNullOrEmpty ( originalName ) )
134
+ {
135
+ return ;
136
+ }
137
+ int index = categories . IndexOf ( originalName ) ;
138
+ if ( index < 0 )
139
+ {
140
+ return ;
141
+ }
142
+ categories [ index ] = newName ;
143
+ // 更新代码段
144
+ foreach ( SnippetInfo snippet in snippets )
145
+ {
146
+ if ( originalName . Equals ( snippet . category , StringComparison . OrdinalIgnoreCase ) )
147
+ {
148
+ snippet . category = newName ;
149
+ }
150
+ }
151
+ }
152
+
153
+ #endregion
154
+
155
+ #region NewSnippet
156
+
73
157
/// <summary>
74
158
/// 新代码段
75
159
/// </summary>
@@ -78,76 +162,76 @@ public static SnippetInfo newSnippet
78
162
get => userData . newSnippet ;
79
163
}
80
164
165
+ #endregion
166
+
81
167
#region Snippets
82
168
83
- public static List < SnippetInfo > snippets => userData . snippets ;
169
+ #region Snippets Mapping
84
170
85
171
private static readonly Dictionary < string , SnippetInfo > s_GuidMap = new Dictionary < string , SnippetInfo > ( ) ;
86
172
87
173
private static void GenerateMapping ( )
88
174
{
89
175
s_GuidMap . Clear ( ) ;
90
- foreach ( SnippetInfo snippetInfo in snippets )
176
+ foreach ( SnippetInfo snippet in snippets )
91
177
{
92
- s_GuidMap . Add ( snippetInfo . guid , snippetInfo ) ;
178
+ s_GuidMap . Add ( snippet . guid , snippet ) ;
93
179
}
94
180
}
95
181
182
+ #endregion
183
+
184
+ public static List < SnippetInfo > GetSnippets ( )
185
+ {
186
+ return snippets ;
187
+ }
188
+
189
+ private static List < SnippetInfo > snippets => userData . snippets ;
190
+
96
191
public static SnippetInfo GetSnippet ( string guid )
97
192
{
98
- if ( string . IsNullOrEmpty ( guid ) )
99
- {
100
- return null ;
101
- }
102
- if ( s_GuidMap . TryGetValue ( guid , out SnippetInfo snippetInfo ) )
193
+ if ( ! string . IsNullOrEmpty ( guid ) && s_GuidMap . TryGetValue ( guid , out SnippetInfo snippet ) )
103
194
{
104
- return snippetInfo ;
195
+ return snippet ;
105
196
}
106
197
return null ;
107
198
}
108
199
109
- public static SnippetInfo AddSnippet ( string code , string name = null , string mode = null )
200
+ public static SnippetInfo AddSnippet ( string code , string name = null , string mode = null , string category = null )
110
201
{
111
202
if ( string . IsNullOrEmpty ( name ) )
112
203
{
113
204
name = "Unnamed" ;
114
205
}
115
206
long time = PipiUtility . GetTimestamp ( ) ;
116
- SnippetInfo snippetInfo = new SnippetInfo ( )
207
+ SnippetInfo snippet = new SnippetInfo ( )
117
208
{
118
209
guid = PipiUtility . NewGuid ( ) ,
119
210
createTime = time ,
120
211
editTime = time ,
121
212
code = code ,
122
213
name = name ,
123
214
mode = mode ,
215
+ category = category ,
124
216
} ;
125
- snippets . Add ( snippetInfo ) ;
126
- s_GuidMap . Add ( snippetInfo . guid , snippetInfo ) ;
127
- return snippetInfo ;
217
+ snippets . Add ( snippet ) ;
218
+ s_GuidMap . Add ( snippet . guid , snippet ) ;
219
+ return snippet ;
128
220
}
129
221
130
222
public static void RemoveSnippet ( string guid )
131
223
{
132
- if ( string . IsNullOrEmpty ( guid ) )
133
- {
134
- return ;
135
- }
136
- if ( ! s_GuidMap . TryGetValue ( guid , out SnippetInfo snippetInfo ) )
224
+ if ( string . IsNullOrEmpty ( guid ) || ! s_GuidMap . TryGetValue ( guid , out SnippetInfo snippet ) )
137
225
{
138
226
return ;
139
227
}
140
228
s_GuidMap . Remove ( guid ) ;
141
- snippets . Remove ( snippetInfo ) ;
229
+ snippets . Remove ( snippet ) ;
142
230
}
143
231
144
232
public static bool HasSnippet ( string guid )
145
233
{
146
- if ( string . IsNullOrEmpty ( guid ) )
147
- {
148
- return false ;
149
- }
150
- return s_GuidMap . ContainsKey ( guid ) ;
234
+ return ( ! string . IsNullOrEmpty ( guid ) && s_GuidMap . ContainsKey ( guid ) ) ;
151
235
}
152
236
153
237
public static SnippetInfo GetSnippetWithName ( string name , string mode = null )
@@ -171,6 +255,19 @@ public static bool HasSnippetWithName(string name)
171
255
return ( GetSnippetWithName ( name ) != null ) ;
172
256
}
173
257
258
+ public static List < SnippetInfo > GetSnippetsWithCategory ( string category )
259
+ {
260
+ List < SnippetInfo > list = new List < SnippetInfo > ( ) ;
261
+ foreach ( SnippetInfo snippet in snippets )
262
+ {
263
+ if ( snippet . MatchCategory ( category ) )
264
+ {
265
+ list . Add ( snippet ) ;
266
+ }
267
+ }
268
+ return list ;
269
+ }
270
+
174
271
#endregion
175
272
176
273
#region Basic Interface
@@ -230,38 +327,34 @@ private static void SetLocal(UserData value)
230
327
231
328
}
232
329
233
- #region ItemInfo
330
+ #region Type Definition
234
331
235
332
/// <summary>
236
333
/// 条目信息
237
334
/// </summary>
238
335
[ Serializable ]
239
336
public class SnippetInfo
240
337
{
241
-
242
- public string guid = string . Empty ;
338
+ public string guid = null ;
243
339
public long createTime = 0 ;
244
340
public long editTime = 0 ;
245
341
public bool top = false ;
246
- public string name = string . Empty ;
247
- public string code = string . Empty ;
248
- public string mode = string . Empty ;
342
+ public string name = null ;
343
+ public string code = null ;
344
+ public string mode = null ;
345
+ public string category = null ;
249
346
250
- public bool MatchGuid ( string guid )
251
- {
252
- return this . guid . Equals ( guid , StringComparison . OrdinalIgnoreCase ) ;
253
- }
347
+ public bool MatchGuid ( string guid ) => ( this . guid != null && this . guid . Equals ( guid , StringComparison . OrdinalIgnoreCase ) ) ;
254
348
255
- public bool MatchName ( string name )
256
- {
257
- return this . name . Equals ( name , StringComparison . OrdinalIgnoreCase ) ;
258
- }
349
+ public bool MatchName ( string name ) => ( this . name != null && this . name . Equals ( name , StringComparison . OrdinalIgnoreCase ) ) ;
259
350
260
- public bool MatchMode ( string mode )
261
- {
262
- return this . mode . Equals ( mode , StringComparison . OrdinalIgnoreCase ) ;
263
- }
351
+ public bool MatchMode ( string mode ) => ( this . mode != null && this . mode . Equals ( mode , StringComparison . OrdinalIgnoreCase ) ) ;
264
352
353
+ public bool MatchCategory ( string category ) =>
354
+ (
355
+ ( string . IsNullOrEmpty ( this . category ) && string . IsNullOrEmpty ( category ) ) ||
356
+ ( this . category != null && this . category . Equals ( category , StringComparison . OrdinalIgnoreCase ) )
357
+ ) ;
265
358
}
266
359
267
360
[ Serializable ]
@@ -273,9 +366,10 @@ public class SnippetWrapper
273
366
[ Serializable ]
274
367
public class SnippetInfoSimplified
275
368
{
276
- public string name = string . Empty ;
277
- public string code = string . Empty ;
278
- public string mode = string . Empty ;
369
+ public string name = null ;
370
+ public string code = null ;
371
+ public string mode = null ;
372
+ public string category = null ;
279
373
}
280
374
281
375
#endregion
0 commit comments