Skip to content

Commit

Permalink
Fix up generic type handling (fixes #21), fix exceptions when attempt…
Browse files Browse the repository at this point in the history
…ing to create generic types that don't exist or when trying to search for non-datamodel types (fixes #22)
  • Loading branch information
BlueCyro committed Jul 20, 2024
1 parent ce4c3a4 commit fdf56b0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CherryPick.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<LangVersion>latest</LangVersion>


<Version>1.1.1</Version>
<Version>1.1.2</Version>
<AssemblyVersion>$(Version)</AssemblyVersion>
<Authors>Cyro</Authors>
<Product>CherryPick Component Searcher</Product>
Expand Down
58 changes: 43 additions & 15 deletions CherryPicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ static IEnumerable<CategoryNode<Type>> flatten(IEnumerable<CategoryNode<Type>> c
{
foreach (var element in category.Elements)
{
WorkerDetails detail = new(element.GetNiceName(), category.GetPath(), element);
details.Add(detail);
if (element.IsDataModelType())
{
WorkerDetails detail = new(element.GetNiceName(), category.GetPath(), element);
details.Add(detail);
}
}
}

Expand Down Expand Up @@ -227,28 +230,53 @@ public void EditChanged(TextEditor editor)
PerformMatch(matchTxt, resultCount);
foreach (var result in _results.Values)
{
string arg = result.Type.IsGenericTypeDefinition ? Path.Combine(result.Path, result.Type.FullName) : searchRoot.World.Types().EncodeType(result.Type);
var pressed = result.Type.IsGenericTypeDefinition ? onGenericPressed : onAddPressed;
bool isGenType = result.Type.IsGenericTypeDefinition;
string arg = "";

try
{
arg = isGenType ? Path.Combine(result.Path, result.Type.AssemblyQualifiedName) : searchRoot.World.Types().EncodeType(result.Type);
}
catch (ArgumentException)
{
CherryPick.Warn($"Tried to encode a non-data model type: {result.Type}");
continue;
}

var pressed = isGenType ? onGenericPressed : onAddPressed;
CreateButton(result, pressed, arg, searchBuilder, editor, RadiantUI_Constants.Sub.CYAN);
}


WorkerDetails firstGeneric = _results.Values.FirstOrDefault(w => w.Type.IsGenericTypeDefinition);
if (genericType != null)
try
{
string typeName = firstGeneric.Type.FullName;
typeName = typeName.Substring(0, typeName.IndexOf("`")) + genericType;
Type? constructed = NiceTypeParser.TryParse(typeName);


if (constructed != null)
WorkerDetails firstGeneric = _results.Values.First(w => w.Type.IsGenericTypeDefinition);
if (genericType != null)
{
WorkerDetails detail = new(constructed.GetNiceName(), firstGeneric.Path, constructed);
Button typeButton = CreateButton(detail, onAddPressed, searchRoot.World.Types().EncodeType(constructed), searchBuilder, editor, RadiantUI_Constants.Sub.ORANGE);
typeButton.Slot.OrderOffset = -1024;
string typeName = firstGeneric.Type.FullName;
typeName = typeName.Substring(0, typeName.IndexOf("`")) + genericType;
Type? constructed = NiceTypeParser.TryParse(typeName);


if (constructed != null)
{
try
{
string arg = searchRoot.World.Types().EncodeType(constructed);
}
catch (ArgumentException)
{
CherryPick.Warn($"Tried to encode a non-data model type: {constructed}");
return;
}

WorkerDetails detail = new(constructed.GetNiceName(), firstGeneric.Path, constructed);
Button typeButton = CreateButton(detail, onAddPressed, searchRoot.World.Types().EncodeType(constructed), searchBuilder, editor, RadiantUI_Constants.Sub.ORANGE);
typeButton.Slot.OrderOffset = -1024;
}
}
}
catch (InvalidOperationException) { } // Swallow this exception in particular because First() will throw if nothing satisfies the lambda condition
}


Expand Down

0 comments on commit fdf56b0

Please sign in to comment.