-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathIntelli.cs
497 lines (449 loc) · 20.5 KB
/
Intelli.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/////////////////////////////////////////////////////////////////////////////////
// CodeLab for Paint.NET
// Copyright 2017-2018 Jason Wendt. All Rights Reserved.
// Portions Copyright ©2016 BoltBait. All Rights Reserved.
// Portions Copyright ©Microsoft Corporation. All Rights Reserved.
//
// THE DEVELOPERS MAKE NO WARRANTY OF ANY KIND REGARDING THE CODE. THEY
// SPECIFICALLY DISCLAIM ANY WARRANTY OF FITNESS FOR ANY PARTICULAR PURPOSE OR
// ANY OTHER WARRANTY. THE CODELAB DEVELOPERS DISCLAIM ALL LIABILITY RELATING
// TO THE USE OF THIS CODE. NO LICENSE, EXPRESS OR IMPLIED, BY ESTOPPEL OR
// OTHERWISE, TO ANY INTELLECTUAL PROPERTY RIGHTS IS GRANTED HEREIN.
//
// Latest distribution: https://www.BoltBait.com/pdn/codelab
/////////////////////////////////////////////////////////////////////////////////
using PaintDotNet;
using PaintDotNet.Effects;
using PaintDotNet.Rendering;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Text.RegularExpressions;
namespace PdnCodeLab
{
internal static class Intelli
{
internal static Dictionary<string, Type> Variables { get; }
internal static Dictionary<string, int> VarPos { get; }
internal static Dictionary<string, Type> Parameters { get; }
internal static Dictionary<string, Type> UserDefinedTypes { get; }
internal static Dictionary<string, Type> AutoCompleteTypes { get; private set; }
internal static Dictionary<string, Type> XamlAutoCompleteTypes { get; }
internal static Dictionary<string, Type> AllTypes { get; private set; }
internal static Dictionary<string, string> Snippets { get; }
internal static Dictionary<string, string> TypeAliases { get; }
internal static IEnumerable<string> Keywords { get; }
internal static IEnumerable<string> PdnAssemblyNames { get; private set; }
internal static Type UserScript { get; set; }
internal static string ClassList { get; private set; }
internal static string EnumList { get; private set; }
internal static string StructList { get; private set; }
internal static string InterfaceList { get; private set; }
internal const string UserScriptFullName = "PdnCodeLab.UserScript";
private static IEnumerable<MethodInfo> extMethods;
private static readonly ImmutableArray<Assembly> sdkAssemblies;
private static readonly ImmutableArray<Assembly> allPdnAssemblies;
private static readonly ImmutableArray<KeyValuePair<string, Type>> aliasTypes;
internal static IEnumerable<MethodInfo> GetExtensionMethod(this Type extendedType, string methodName)
{
return extMethods
.Where(method => method.Name == methodName)
.Select(method => method.Extends(extendedType))
.Where(method => method != null);
}
internal static IEnumerable<MethodInfo> GetExtensionMethods(this Type extendedType)
{
return extMethods
.Select(method => method.Extends(extendedType))
.Where(method => method != null);
}
internal static bool IsEnum(string enumName)
{
return enumName != null && (UserDefinedTypes.TryGetValue(enumName, out Type type) || AllTypes.TryGetValue(enumName, out type)) &&
type.IsEnum && Enum.GetValues(type).Length > 0;
}
internal static bool TryGetEnumNames(string enumName, out string[] names)
{
if (enumName == null || !(UserDefinedTypes.TryGetValue(enumName, out Type type) || AllTypes.TryGetValue(enumName, out type)) || !type.IsEnum)
{
names = null;
return false;
}
names = Enum.GetNames(type).Select(name => type.Name + "." + name).ToArray();
return true;
}
internal static void SetReferences(ProjectType projectType)
{
List<string> pdnAssemblyNames = new List<string>
{
"PaintDotNet.Base",
"PaintDotNet.ComponentModel",
"PaintDotNet.Core",
"PaintDotNet.Data",
"PaintDotNet.Framework",
"PaintDotNet.Fundamentals",
"PaintDotNet.ObjectModel",
"PaintDotNet.Primitives",
"PaintDotNet.PropertySystem"
};
List<string> intelliIgnoreList = new List<string>
{
"PresentationCore",
"PresentationFramework",
"WindowsBase"
};
switch (projectType)
{
case ProjectType.ClassicEffect:
pdnAssemblyNames.Add("PaintDotNet.Effects");
pdnAssemblyNames.Add("PaintDotNet.Effects.Core");
pdnAssemblyNames.Add("PaintDotNet.Effects.Legacy");
break;
case ProjectType.GpuDrawEffect:
case ProjectType.GpuImageEffect:
case ProjectType.BitmapEffect:
pdnAssemblyNames.Add("PaintDotNet.Effects.Core");
pdnAssemblyNames.Add("PaintDotNet.Effects.Gpu");
pdnAssemblyNames.Add("PaintDotNet.Windows");
pdnAssemblyNames.Add("PaintDotNet.Windows.Core");
pdnAssemblyNames.Add("PaintDotNet.Windows.Framework");
intelliIgnoreList.Add("System.Drawing.Common");
intelliIgnoreList.Add("System.Drawing.Primitives");
break;
case ProjectType.Reference:
pdnAssemblyNames.Add("PaintDotNet.Effects");
pdnAssemblyNames.Add("PaintDotNet.Effects.Core");
pdnAssemblyNames.Add("PaintDotNet.Effects.Gpu");
pdnAssemblyNames.Add("PaintDotNet.Effects.Legacy");
pdnAssemblyNames.Add("PaintDotNet.Windows");
pdnAssemblyNames.Add("PaintDotNet.Windows.Core");
pdnAssemblyNames.Add("PaintDotNet.Windows.Framework");
break;
}
PdnAssemblyNames = pdnAssemblyNames
.Append("PaintDotNet.Windows") // PaintDotNet.Windows needed for the Classic Effect constructor that uses a System.Drawing.Bitmap
.Distinct()
.OrderBy(x => x, StringComparer.Ordinal)
.ToImmutableArray();
ImmutableArray<Assembly> allAssemblies = sdkAssemblies
.Concat(allPdnAssemblies.Where(a => pdnAssemblyNames.Contains(a.GetName().Name, StringComparer.Ordinal)))
.ToImmutableArray();
ImmutableArray<Assembly> intelliAssemblies = allAssemblies
.Where(a => !intelliIgnoreList.Contains(a.GetName().Name, StringComparer.Ordinal))
.ToImmutableArray();
IEnumerable<string> refFilePaths = allAssemblies
.Append(allPdnAssemblies.First(a => a.GetName().Name.Equals("PaintDotNet.Windows", StringComparison.Ordinal)))
.Select(a => a.Location)
.Distinct();
ScriptBuilder.SetReferences(refFilePaths);
AllTypes = new Dictionary<string, Type>(aliasTypes);
AutoCompleteTypes = new Dictionary<string, Type>(aliasTypes);
HashSet<string> enums = new HashSet<string>();
HashSet<string> interfaces = new HashSet<string>();
HashSet<string> structs = new HashSet<string>()
{
"IntSliderControl",
"CheckboxControl",
"ColorWheelControl",
"AngleControl",
"PanSliderControl",
"DoubleSliderControl",
"LayerControl",
"ListBoxControl",
"RadioButtonControl",
"ReseedButtonControl"
};
HashSet<string> classes = new HashSet<string>()
{
"TextboxControl",
"FilenameControl",
"FolderControl",
"RollControl",
"MultiLineTextboxControl",
"LabelComment",
"FontFamily"
};
List<MethodInfo> extMethodsList = new List<MethodInfo>();
string[] namespaceWhiteList =
{
"Microsoft.Win32", "PaintDotNet", "PaintDotNet.AppModel", "PaintDotNet.Effects", "PaintDotNet.Imaging",
"PaintDotNet.Rendering", "PaintDotNet.Direct2D1", "PaintDotNet.Direct2D1.Effects", "PaintDotNet.Effects.Gpu",
"System", "System.Collections.Generic", "System.Diagnostics", "System.Drawing", "System.Drawing.Drawing2D",
"System.Drawing.Text", "System.IO", "System.IO.Compression", "System.Text.RegularExpressions"
};
// Add the referenced assembly types
foreach (Assembly a in intelliAssemblies)
{
foreach (Type type in a.GetExportedTypes())
{
string name = (type.IsGenericType) ? Regex.Replace(type.Name, @"`\d", string.Empty) : type.Name;
if (!AllTypes.ContainsKey(name))
{
AllTypes.Add(name, type);
}
else
{
Type t = AllTypes[name];
if (namespaceWhiteList.Contains(type.Namespace) &&
((type.IsGenericType && !t.IsGenericType) || !namespaceWhiteList.Contains(t.Namespace)))
{
AllTypes[name] = type;
}
}
if (type.IsNested || type.IsObsolete())
{
continue;
}
// Gather extensions methods contained in each type
if (type.IsSealed && !type.IsGenericType && type.IsOrHasExtension())
{
extMethodsList.AddRange(
type.GetMethods(BindingFlags.Static | BindingFlags.Public)
.Where(method => method.IsOrHasExtension() && !method.IsObsolete()));
}
else if (namespaceWhiteList.Contains(type.Namespace) &&
!type.Name.StartsWith("Property", StringComparison.OrdinalIgnoreCase) &&
!AutoCompleteTypes.ContainsKey(type.Name)
)
{
AutoCompleteTypes.Add(type.Name, type);
}
}
}
extMethods = extMethodsList;
if (projectType.Is5Effect())
{
// removed to prevent name collision with properties
AllTypes.Remove(nameof(Environment));
AutoCompleteTypes.Remove(nameof(Environment));
AllTypes.Remove(nameof(Document));
AutoCompleteTypes.Remove(nameof(Document));
}
foreach (KeyValuePair<string, Type> kvp in AllTypes)
{
Type type = kvp.Value;
string name = kvp.Key;
if (type.IsEnum)
{
enums.Add(name);
}
else if (type.IsValueType)
{
structs.Add(name);
}
else if (type.IsClass)
{
classes.Add(name);
}
else if (type.IsInterface)
{
interfaces.Add(name);
}
}
ClassList = classes.Join(" ");
EnumList = enums.Join(" ");
StructList = structs.Join(" ");
InterfaceList = interfaces.Join(" ");
}
static Intelli()
{
TypeAliases = new Dictionary<string, string>
{
{ "Byte", "byte" },
{ "SByte", "sbyte" },
{ "Int16", "short" },
{ "UInt16", "ushort" },
{ "Int32", "int" },
{ "UInt32", "uint" },
{ "Int64", "long" },
{ "UInt64", "ulong" },
{ "Single", "float" },
{ "Double", "double" },
{ "Decimal", "decimal" },
{ "Boolean", "bool" },
{ "Char", "char" },
{ "String", "string" },
{ "Object", "object" },
{ "Byte[]", "byte[]" },
{ "SByte[]", "sbyte[]" },
{ "Int16[]", "short[]" },
{ "UInt16[]", "ushort[]" },
{ "Int32[]", "int[]" },
{ "UInt32[]", "uint[]" },
{ "Int64[]", "long[]" },
{ "UInt64[]", "ulong[]" },
{ "Single[]", "float[]" },
{ "Double[]", "double[]" },
{ "Decimal[]", "decimal[]" },
{ "Boolean[]", "bool[]" },
{ "Char[]", "char[]" },
{ "String[]", "string[]" },
{ "Object[]", "object[]" },
{ "Void", "void" },
{ "ValueType", "struct" }
};
Keywords = new string[]
{
"abstract", "as", "base", "break", "case", "catch", "checked", "class", "const", "continue",
"default", "delegate", "do", "enum", "event", "explicit", "extern", "false", "finally", "fixed",
"get", "goto", "implicit", "in", "interface", "internal", "is", "lock", "new", "not", "null",
"operator", "out", "override", "params", "partial", "private", "protected", "public",
"readonly", "ref", "return", "stackalloc", "static", "sealed", "set", "sizeof", "struct",
"this", "throw", "true", "try", "typeof", "unchecked", "unsafe", "var", "virtual", "void", "volatile",
"with", "#endif", "#endregion"
};
string[] allPdnAssemblyNames = new string[]
{
"PaintDotNet.Base",
"PaintDotNet.ComponentModel",
"PaintDotNet.Core",
"PaintDotNet.Data",
"PaintDotNet.Effects",
"PaintDotNet.Effects.Core",
"PaintDotNet.Effects.Gpu",
"PaintDotNet.Effects.Legacy",
"PaintDotNet.Framework",
"PaintDotNet.Fundamentals",
"PaintDotNet.ObjectModel",
"PaintDotNet.Primitives",
"PaintDotNet.PropertySystem",
"PaintDotNet.Windows",
"PaintDotNet.Windows.Core",
"PaintDotNet.Windows.Framework"
};
// exclude assemblies that were loaded into separate contexts; i.e. Plugins
IEnumerable<Assembly> assemblies = AppDomain.CurrentDomain
.GetAssemblies()
.Where(a => !a.IsCollectible);
// Cherry pick certain dotPDN assemblies
allPdnAssemblies = assemblies
.Where(a => a.GetCustomAttribute<AssemblyCompanyAttribute>()?.Company == "dotPDN LLC" &&
allPdnAssemblyNames.Contains(a.GetName().Name, StringComparer.OrdinalIgnoreCase))
.ToImmutableArray();
// Cherry pick Microsoft assemblies
sdkAssemblies = assemblies
.Where(a => a.GetCustomAttribute<AssemblyCompanyAttribute>()?.Company == "Microsoft Corporation")
.Append(typeof(System.Diagnostics.TextWriterTraceListener).Assembly)
.Append(typeof(System.IO.Compression.GZipStream).Assembly)
.Distinct()
.ToImmutableArray();
Dictionary<string, string> userSnippets = null;
string userSnippetsJson = Settings.Snippets;
if (userSnippetsJson.Length > 0)
{
try
{
userSnippets = JsonSerializer.Deserialize<Dictionary<string, string>>(userSnippetsJson);
}
catch
{
}
}
Snippets = userSnippets ??
new Dictionary<string, string>()
{
{ "if", "if (true$)\r\n{\r\n \r\n}" },
{ "else", "else\r\n{\r\n $\r\n}" },
{ "while", "while (true$)\r\n{\r\n break;\r\n}" },
{ "for", "for (int i = 0; i < length$; i++)\r\n{\r\n \r\n}" },
{ "foreach", "foreach (var item in collection$)\r\n{\r\n \r\n}" },
{ "using", "using (resource$)\r\n{\r\n \r\n}" },
{ "switch", "switch (variable$)\r\n{\r\n case 0:\r\n break;\r\n default:\r\n break;\r\n}" },
{ "#region", "#region MyRegion$\r\n\r\n#endregion" },
{ "#if", "#if true$\r\n\r\n#endif" },
{ "try", "try\r\n{\r\n $\r\n}\r\ncatch (Exception ex)\r\n{\r\n \r\n}" }
};
Variables = new Dictionary<string, Type>();
Parameters = new Dictionary<string, Type>();
VarPos = new Dictionary<string, int>();
UserDefinedTypes = new Dictionary<string, Type>();
AutoCompleteTypes = new Dictionary<string, Type>();
aliasTypes = new Dictionary<string, Type>
{
// Add the predefined aliases of types in the System namespace
{ "bool", typeof(bool) },
{ "byte", typeof(byte) },
{ "sbyte", typeof(sbyte) },
{ "char", typeof(char) },
{ "decimal", typeof(decimal) },
{ "double", typeof(double) },
{ "float", typeof(float) },
{ "int", typeof(int) },
{ "uint", typeof(uint) },
{ "long", typeof(long) },
{ "ulong", typeof(ulong) },
{ "object", typeof(object) },
{ "short", typeof(short) },
{ "ushort", typeof(ushort) },
{ "string", typeof(string) },
// Add the aliases for the UI controls
{ "IntSliderControl", typeof(int) },
{ "CheckboxControl", typeof(bool) },
{ "ColorWheelControl", typeof(ColorBgra) },
{ "AngleControl", typeof(double) },
{ "PanSliderControl", typeof(Vector2Double) },
{ "TextboxControl", typeof(string) },
{ "FilenameControl", typeof(string) },
{ "FolderControl", typeof(string) },
{ "DoubleSliderControl", typeof(double) },
{ "RollControl", typeof(Vector3Double) },
{ "ListBoxControl", typeof(byte) },
{ "RadioButtonControl", typeof(byte) },
{ "ReseedButtonControl", typeof(byte) },
{ "MultiLineTextboxControl", typeof(string) }
}
.ToImmutableArray();
SetReferences(ProjectType.Default);
XamlAutoCompleteTypes = new Dictionary<string, Type>();
foreach (Type type in Assembly.GetAssembly(typeof(System.Windows.Media.Geometry)).GetExportedTypes())
{
if (type.IsNested || type.IsObsolete())
{
continue;
}
if (type.IsClass &&
type.Namespace.Equals("System.Windows.Media", StringComparison.OrdinalIgnoreCase) &&
!XamlAutoCompleteTypes.ContainsKey(type.Name)
)
{
XamlAutoCompleteTypes.Add(type.Name, type);
}
}
UserScript = typeof(DummyEffect); // Placeholder effect Type until it's replaced when the UserScript is actually compiled
}
private abstract class DummyEffect : BitmapEffect
{
protected DummyEffect() : base(nameof(DummyEffect), null, BitmapEffectOptions.Create())
{
}
protected override void OnRender(IBitmapEffectOutput output)
{
throw new NotImplementedException();
}
}
}
internal enum IntelliType
{
Method,
Property,
Event,
Field,
Keyword,
Type,
Variable,
Class,
Struct,
Enum,
Constant,
EnumItem,
Snippet,
Constructor,
Parameter,
Interface,
Delegate,
None
}
}