Fix results TypeCompleter issues

This commit is contained in:
Sinai 2022-04-22 21:03:33 +10:00
parent 1643d4b7dd
commit 3afee7254c

View File

@ -36,7 +36,6 @@ namespace UnityExplorer.UI.Widgets.AutoComplete
readonly bool allowGeneric; readonly bool allowGeneric;
private HashSet<Type> allowedTypes; private HashSet<Type> allowedTypes;
private HashSet<Type> allAllowedTypes;
readonly List<Suggestion> suggestions = new(); readonly List<Suggestion> suggestions = new();
readonly HashSet<string> suggestedNames = new(); readonly HashSet<string> suggestedNames = new();
@ -77,7 +76,7 @@ namespace UnityExplorer.UI.Widgets.AutoComplete
inputField.OnValueChanged += OnInputFieldChanged; inputField.OnValueChanged += OnInputFieldChanged;
if (BaseType != null) if (BaseType != null || AllTypes)
CacheTypes(); CacheTypes();
} }
@ -87,17 +86,44 @@ namespace UnityExplorer.UI.Widgets.AutoComplete
allowedTypes = ReflectionUtility.GetImplementationsOf(BaseType, allowAbstract, allowGeneric, allowEnum); allowedTypes = ReflectionUtility.GetImplementationsOf(BaseType, allowAbstract, allowGeneric, allowEnum);
else else
allowedTypes = GetAllAllowedTypes(); allowedTypes = GetAllAllowedTypes();
// Check generic parameter constraints
if (GenericConstraints != null && GenericConstraints.Any())
{
List<Type> 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<Type> GetAllAllowedTypes() HashSet<Type> GetAllAllowedTypes()
{ {
if (allAllowedTypes == null) HashSet<Type> allAllowedTypes = new();
{
allAllowedTypes = new();
foreach (KeyValuePair<string, Type> entry in ReflectionUtility.AllTypes) foreach (KeyValuePair<string, Type> entry in ReflectionUtility.AllTypes)
{ {
// skip <PrivateImplementationDetails> and <AnonymousClass> classes
Type type = entry.Value; Type type = entry.Value;
if ((!allowAbstract && type.IsAbstract)
|| (!allowGeneric && type.IsGenericType)
|| (!allowEnum && type.IsEnum))
continue;
// skip <PrivateImplementationDetails> and <AnonymousClass> classes
if (type.FullName.Contains("PrivateImplementationDetails") if (type.FullName.Contains("PrivateImplementationDetails")
|| type.FullName.Contains("DisplayClass") || type.FullName.Contains("DisplayClass")
|| type.FullName.Contains('<')) || type.FullName.Contains('<'))
@ -106,7 +132,6 @@ namespace UnityExplorer.UI.Widgets.AutoComplete
} }
allAllowedTypes.Add(type); allAllowedTypes.Add(type);
} }
}
return allAllowedTypes; return allAllowedTypes;
} }
@ -152,17 +177,14 @@ namespace UnityExplorer.UI.Widgets.AutoComplete
} }
// shorthand types all inherit from System.Object // shorthand types all inherit from System.Object
if (AllTypes || BaseType == typeof(object)) if (shorthandToType.TryGetValue(input, out Type shorthand) && allowedTypes.Contains(shorthand))
{
if (shorthandToType.TryGetValue(input, out Type shorthand))
AddSuggestion(shorthand); AddSuggestion(shorthand);
foreach (KeyValuePair<string, Type> entry in shorthandToType) foreach (KeyValuePair<string, Type> entry in shorthandToType)
{ {
if (entry.Key.StartsWith(input, StringComparison.InvariantCultureIgnoreCase)) if (allowedTypes.Contains(entry.Value) && entry.Key.StartsWith(input, StringComparison.InvariantCultureIgnoreCase))
AddSuggestion(entry.Value); AddSuggestion(entry.Value);
} }
}
// Check for exact match first // Check for exact match first
if (ReflectionUtility.GetTypeByName(input) is Type t && allowedTypes.Contains(t)) if (ReflectionUtility.GetTypeByName(input) is Type t && allowedTypes.Contains(t))