11using System ;
22using System . Collections . Generic ;
3- using System . Linq ;
43
54namespace ChenPipi . CodeExecutor . Editor
65{
@@ -17,6 +16,7 @@ public static class CodeExecutorData
1716 private class UserData
1817 {
1918 public int version = 0 ;
19+ public List < string > categories = new List < string > ( ) ;
2020 public SnippetInfo newSnippet = new SnippetInfo ( ) ;
2121 public List < SnippetInfo > snippets = new List < SnippetInfo > ( )
2222 {
@@ -27,8 +27,11 @@ private class UserData
2727 editTime = 0 ,
2828 top = false ,
2929 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 ,
3235 } ,
3336 new SnippetInfo ( )
3437 {
@@ -38,7 +41,8 @@ private class UserData
3841 top = false ,
3942 name = "CrazyThursday" ,
4043 code = "UnityEngine.Debug.LogError(\" [CodeExecutor] Crazy Thursday\" );" ,
41- mode = "C#"
44+ mode = "C#" ,
45+ category = "Test" ,
4246 } ,
4347 new SnippetInfo ( )
4448 {
@@ -48,7 +52,8 @@ private class UserData
4852 top = false ,
4953 name = "TestImport" ,
5054 code = "@import(\" CrazyThursday\" )\r \n \n UnityEngine.Debug.LogError(\" [CodeExecutor] V Me 50\" );" ,
51- mode = "C#"
55+ mode = "C#" ,
56+ category = "Test" ,
5257 } ,
5358 } ;
5459 }
@@ -70,6 +75,85 @@ private static UserData userData
7075
7176 #endregion
7277
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+
73157 /// <summary>
74158 /// 新代码段
75159 /// </summary>
@@ -78,76 +162,76 @@ public static SnippetInfo newSnippet
78162 get => userData . newSnippet ;
79163 }
80164
165+ #endregion
166+
81167 #region Snippets
82168
83- public static List < SnippetInfo > snippets => userData . snippets ;
169+ #region Snippets Mapping
84170
85171 private static readonly Dictionary < string , SnippetInfo > s_GuidMap = new Dictionary < string , SnippetInfo > ( ) ;
86172
87173 private static void GenerateMapping ( )
88174 {
89175 s_GuidMap . Clear ( ) ;
90- foreach ( SnippetInfo snippetInfo in snippets )
176+ foreach ( SnippetInfo snippet in snippets )
91177 {
92- s_GuidMap . Add ( snippetInfo . guid , snippetInfo ) ;
178+ s_GuidMap . Add ( snippet . guid , snippet ) ;
93179 }
94180 }
95181
182+ #endregion
183+
184+ public static List < SnippetInfo > GetSnippets ( )
185+ {
186+ return snippets ;
187+ }
188+
189+ private static List < SnippetInfo > snippets => userData . snippets ;
190+
96191 public static SnippetInfo GetSnippet ( string guid )
97192 {
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 ) )
103194 {
104- return snippetInfo ;
195+ return snippet ;
105196 }
106197 return null ;
107198 }
108199
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 )
110201 {
111202 if ( string . IsNullOrEmpty ( name ) )
112203 {
113204 name = "Unnamed" ;
114205 }
115206 long time = PipiUtility . GetTimestamp ( ) ;
116- SnippetInfo snippetInfo = new SnippetInfo ( )
207+ SnippetInfo snippet = new SnippetInfo ( )
117208 {
118209 guid = PipiUtility . NewGuid ( ) ,
119210 createTime = time ,
120211 editTime = time ,
121212 code = code ,
122213 name = name ,
123214 mode = mode ,
215+ category = category ,
124216 } ;
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 ;
128220 }
129221
130222 public static void RemoveSnippet ( string guid )
131223 {
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 ) )
137225 {
138226 return ;
139227 }
140228 s_GuidMap . Remove ( guid ) ;
141- snippets . Remove ( snippetInfo ) ;
229+ snippets . Remove ( snippet ) ;
142230 }
143231
144232 public static bool HasSnippet ( string guid )
145233 {
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 ) ) ;
151235 }
152236
153237 public static SnippetInfo GetSnippetWithName ( string name , string mode = null )
@@ -171,6 +255,19 @@ public static bool HasSnippetWithName(string name)
171255 return ( GetSnippetWithName ( name ) != null ) ;
172256 }
173257
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+
174271 #endregion
175272
176273 #region Basic Interface
@@ -230,38 +327,34 @@ private static void SetLocal(UserData value)
230327
231328 }
232329
233- #region ItemInfo
330+ #region Type Definition
234331
235332 /// <summary>
236333 /// 条目信息
237334 /// </summary>
238335 [ Serializable ]
239336 public class SnippetInfo
240337 {
241-
242- public string guid = string . Empty ;
338+ public string guid = null ;
243339 public long createTime = 0 ;
244340 public long editTime = 0 ;
245341 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 ;
249346
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 ) ) ;
254348
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 ) ) ;
259350
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 ) ) ;
264352
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+ ) ;
265358 }
266359
267360 [ Serializable ]
@@ -273,9 +366,10 @@ public class SnippetWrapper
273366 [ Serializable ]
274367 public class SnippetInfoSimplified
275368 {
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 ;
279373 }
280374
281375 #endregion
0 commit comments