From 3afee7254ca56c23710bbf4bd3417335e161ba96 Mon Sep 17 00:00:00 2001 From: Sinai <49360850+sinai-dev@users.noreply.github.com> Date: Fri, 22 Apr 2022 21:03:33 +1000 Subject: [PATCH] Fix results TypeCompleter issues --- src/UI/Widgets/AutoComplete/TypeCompleter.cs | 68 +++++++++++++------- 1 file changed, 45 insertions(+), 23 deletions(-) diff --git a/src/UI/Widgets/AutoComplete/TypeCompleter.cs b/src/UI/Widgets/AutoComplete/TypeCompleter.cs index 540b4bc..c4f9625 100644 --- a/src/UI/Widgets/AutoComplete/TypeCompleter.cs +++ b/src/UI/Widgets/AutoComplete/TypeCompleter.cs @@ -36,7 +36,6 @@ namespace UnityExplorer.UI.Widgets.AutoComplete readonly bool allowGeneric; private HashSet allowedTypes; - private HashSet allAllowedTypes; readonly List suggestions = new(); readonly HashSet suggestedNames = new(); @@ -77,7 +76,7 @@ namespace UnityExplorer.UI.Widgets.AutoComplete inputField.OnValueChanged += OnInputFieldChanged; - if (BaseType != null) + if (BaseType != null || AllTypes) CacheTypes(); } @@ -87,25 +86,51 @@ namespace UnityExplorer.UI.Widgets.AutoComplete allowedTypes = ReflectionUtility.GetImplementationsOf(BaseType, allowAbstract, allowGeneric, allowEnum); else allowedTypes = GetAllAllowedTypes(); + + // Check generic parameter constraints + if (GenericConstraints != null && GenericConstraints.Any()) + { + List typesToRemove = new(); + foreach (Type type in allowedTypes) + { + bool allowed = true; + foreach (Type constraint in GenericConstraints) + { + if (!constraint.IsAssignableFrom(type)) + { + allowed = false; + break; + } + } + if (!allowed) + typesToRemove.Add(type); + } + + foreach (Type type in typesToRemove) + allowedTypes.Remove(type); + } } HashSet GetAllAllowedTypes() { - if (allAllowedTypes == null) + HashSet allAllowedTypes = new(); + foreach (KeyValuePair entry in ReflectionUtility.AllTypes) { - allAllowedTypes = new(); - foreach (KeyValuePair entry in ReflectionUtility.AllTypes) + Type type = entry.Value; + + if ((!allowAbstract && type.IsAbstract) + || (!allowGeneric && type.IsGenericType) + || (!allowEnum && type.IsEnum)) + continue; + + // skip and classes + if (type.FullName.Contains("PrivateImplementationDetails") + || type.FullName.Contains("DisplayClass") + || type.FullName.Contains('<')) { - // skip and classes - Type type = entry.Value; - if (type.FullName.Contains("PrivateImplementationDetails") - || type.FullName.Contains("DisplayClass") - || type.FullName.Contains('<')) - { - continue; - } - allAllowedTypes.Add(type); + continue; } + allAllowedTypes.Add(type); } return allAllowedTypes; @@ -152,16 +177,13 @@ namespace UnityExplorer.UI.Widgets.AutoComplete } // shorthand types all inherit from System.Object - if (AllTypes || BaseType == typeof(object)) - { - if (shorthandToType.TryGetValue(input, out Type shorthand)) - AddSuggestion(shorthand); + if (shorthandToType.TryGetValue(input, out Type shorthand) && allowedTypes.Contains(shorthand)) + AddSuggestion(shorthand); - foreach (KeyValuePair entry in shorthandToType) - { - if (entry.Key.StartsWith(input, StringComparison.InvariantCultureIgnoreCase)) - AddSuggestion(entry.Value); - } + foreach (KeyValuePair entry in shorthandToType) + { + if (allowedTypes.Contains(entry.Value) && entry.Key.StartsWith(input, StringComparison.InvariantCultureIgnoreCase)) + AddSuggestion(entry.Value); } // Check for exact match first