Skip to content

Commit

Permalink
improve generation of invocations for static methods
Browse files Browse the repository at this point in the history
  • Loading branch information
NeVeSpl committed May 19, 2024
1 parent 8db0aff commit 3e00f64
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 126 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RevitDBExplorer.Domain.DataModel.Accessors
{
internal class ExecuteResultCollection
{
public List<(object Arg, object Result)> Results { get; } = new List<(object, object)> ();
public string Label { get; init; }
public string Param_0_Name { get; init; }


public static ExecuteResultCollection Create<T>(string param_0_Name)
{
return new ExecuteResultCollection()
{
Param_0_Name = param_0_Name,
Label = $"[{typeof(T).GetCSharpName()}]",
};
}


public void Add<TParam0Type, TReturnType>(TParam0Type arg, TReturnType result)
{
Results.Add ((arg, result));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using RevitDBExplorer.Domain.DataModel.Accessors;
using RevitDBExplorer.Domain.DataModel.ValueContainers.Base;

// (c) Revit Database Explorer https://github.com/NeVeSpl/RevitDBExplorer/blob/main/license.md

namespace RevitDBExplorer.Domain.DataModel.Members.Accessors
{
internal class MemberAccessorByFuncUltra<TSnoopedObjectType, TParam0Type, TReturnType> : MemberAccessorTypedWithDefaultPresenter<TSnoopedObjectType>
{
private readonly Func<Document, TSnoopedObjectType, TParam0Type, TReturnType> get;
private readonly IEnumerable<TParam0Type> param_0_arguments;
private readonly string param_0_Name;


public MemberAccessorByFuncUltra(Func<Document, TSnoopedObjectType, TParam0Type, TReturnType> get, IEnumerable<TParam0Type> param_0_arguments, string param_0_Name)
{
this.get = get;
this.param_0_arguments = param_0_arguments;
this.param_0_Name = param_0_Name;
}


protected override ReadResult Read(SnoopableContext context, TSnoopedObjectType snoopedObject)
{
var value = new ValueContainer<ExecuteResultCollection>();
var resultCollection = ExecuteResultCollection.Create<TReturnType>(param_0_Name);

foreach (var arg in param_0_arguments)
{
var result = get(context.Document, snoopedObject, arg);
resultCollection.Add(arg, result);
}

value.SetValueTyped(context, resultCollection);

return new ReadResult(value.ValueAsString, "[ByFuncUltra] " + value.TypeHandlerName, value.CanBeSnooped, value.CanBeVisualized, value);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Autodesk.Revit.DB;
using RevitDBExplorer.Domain.DataModel.Accessors;
Expand Down Expand Up @@ -38,6 +39,22 @@ public static ISnoopableMemberTemplate Create<TReturnType>(Expression<Func<Docum
var compiledGetter = getter.Compile();
var methodCallExpression = getter.Body as MethodCallExpression;
var memberAccessor = new MemberAccessorByFunc<TForType, TReturnType>(compiledGetter);

memberAccessor.UniqueId = $"{typeof(TForType).Name}_{getter.GetUniqueId()}";
memberAccessor.DefaultInvocation.Syntax = getter.ToCeSharp();

return WithCustomAC(methodCallExpression.Method.DeclaringType, methodCallExpression.Method.Name, memberAccessor, canBeUsed, kind, () => RevitDocumentationReader.GetMethodComments(methodCallExpression.Method));
}

public static ISnoopableMemberTemplate Create<TParam0Type, TReturnType>(Expression<Func<Document, TForType, TParam0Type, TReturnType>> getter,
IEnumerable<TParam0Type> param_0_arguments,
Func<TForType, bool> canBeUsed = null,
MemberKind kind = MemberKind.StaticMethod)
{
var compiledGetter = getter.Compile();
var methodCallExpression = getter.Body as MethodCallExpression;
var param_0_name = getter.Parameters[2].Name;
var memberAccessor = new MemberAccessorByFuncUltra<TForType, TParam0Type, TReturnType>(compiledGetter, param_0_arguments, param_0_name);

memberAccessor.UniqueId = $"{typeof(TForType).Name}_{getter.GetUniqueId()}";
memberAccessor.DefaultInvocation.Syntax = getter.ToCeSharp();
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Autodesk.Revit.DB.Structure;
using RevitDBExplorer.Domain.DataModel.Members;
using RevitDBExplorer.Domain.DataModel.Members.Base;
using RevitDBExplorer.Domain.DataModel.MembersTemplates.Accessors;

// (c) Revit Database Explorer https://github.com/NeVeSpl/RevitDBExplorer/blob/main/license.md

Expand All @@ -17,14 +16,14 @@ public IEnumerable<ISnoopableMemberTemplate> GetTemplates() =>
MemberTemplate<Element>.Create((doc, target) => doc.GetWorksetId(target.Id), kind: MemberKind.AsArgument),

#if R2023_MIN
MemberTemplate<Element>.WithCustomAC(typeof(AnalyticalToPhysicalAssociationManager), nameof(AnalyticalToPhysicalAssociationManager.HasAssociation), new AnalyticalToPhysicalAssociationManager_HasAssociation(), kind: MemberKind.AsArgument),
MemberTemplate<Element>.WithCustomAC(typeof(AnalyticalToPhysicalAssociationManager), nameof(AnalyticalToPhysicalAssociationManager.GetAssociatedElementId), new AnalyticalToPhysicalAssociationManager_GetAssociatedElementId(), kind: MemberKind.AsArgument),
MemberTemplate<Element>.Create((doc, target) => AnalyticalToPhysicalAssociationManager.GetAnalyticalToPhysicalAssociationManager(doc).HasAssociation(target.Id), kind: MemberKind.AsArgument),
MemberTemplate<Element>.Create((doc, target) => AnalyticalToPhysicalAssociationManager.GetAnalyticalToPhysicalAssociationManager(doc).GetAssociatedElementId(target.Id), kind: MemberKind.AsArgument),
#endif

#if R2024_MIN
MemberTemplate<Element>.Create((doc, target) => AnalyticalToPhysicalAssociationManager.IsAnalyticalElement(doc, target.Id), kind: MemberKind.StaticMethod),
MemberTemplate<Element>.Create((doc, target) => AnalyticalToPhysicalAssociationManager.IsPhysicalElement(doc, target.Id), kind: MemberKind.StaticMethod),
MemberTemplate<Element>.WithCustomAC(typeof(AnalyticalToPhysicalAssociationManager), nameof(AnalyticalToPhysicalAssociationManager.GetAssociatedElementIds), new AnalyticalToPhysicalAssociationManager_GetAssociatedElementIds(), kind: MemberKind.AsArgument),
MemberTemplate<Element>.Create((doc, target) => AnalyticalToPhysicalAssociationManager.GetAnalyticalToPhysicalAssociationManager(doc).GetAssociatedElementIds(target.Id), kind: MemberKind.AsArgument),
#endif

MemberTemplate<Element>.Create((document, target) => SolidSolidCutUtils.IsAllowedForSolidCut(target)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Autodesk.Revit.DB;
using RevitDBExplorer.Domain.DataModel.Members;
using RevitDBExplorer.Domain.DataModel.Members.Base;
using RevitDBExplorer.Domain.DataModel.MembersTemplates.Accessors;

// (c) Revit Database Explorer https://github.com/NeVeSpl/RevitDBExplorer/blob/main/license.md

Expand All @@ -12,9 +11,9 @@ internal class HostObject_Templates : IHaveMemberTemplates
{
public IEnumerable<ISnoopableMemberTemplate> GetTemplates() =>
[
MemberTemplate<HostObject>.Create((doc, target) => HostObjectUtils.GetTopFaces(target), kind: MemberKind.StaticMethod),
MemberTemplate<HostObject>.Create((doc, target) => HostObjectUtils.GetBottomFaces(target), kind: MemberKind.StaticMethod),
MemberTemplate<HostObject>.WithCustomAC(typeof(HostObjectUtils), "GetSideFaces", new HostObjectUtils_GetSideFaces(), kind: MemberKind.StaticMethod ),
MemberTemplate<HostObject>.Create((doc, target) => HostObjectUtils.GetTopFaces(target), kind: MemberKind.StaticMethod),
MemberTemplate<HostObject>.Create((doc, target) => HostObjectUtils.GetBottomFaces(target), kind: MemberKind.StaticMethod),
MemberTemplate<HostObject>.Create((doc, target, side) => HostObjectUtils.GetSideFaces(target, side), [ShellLayerType.Interior, ShellLayerType.Exterior]),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ internal static class ValueContainerFactory
new ElementHandler(),

// collections
new ExecuteResultCollectionHandler(),
new IListElementIdHandler(),
new IEnumerableHandler(),

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using RevitDBExplorer.Domain.DataModel.Accessors;
using RevitDBExplorer.Domain.DataModel.ValueContainers.Base;

// (c) Revit Database Explorer https://github.com/NeVeSpl/RevitDBExplorer/blob/main/license.md

namespace RevitDBExplorer.Domain.DataModel.ValueContainers
{
internal class ExecuteResultCollectionHandler : TypeHandler<ExecuteResultCollection>
{
protected override bool CanBeSnoooped(SnoopableContext context, ExecuteResultCollection value)
{
return value.Results.Count > 0;
}
protected override string ToLabel(SnoopableContext context, ExecuteResultCollection value)
{
return value.Label;
}

protected override IEnumerable<SnoopableObject> Snooop(SnoopableContext context, ExecuteResultCollection value)
{
foreach (var pair in value.Results)
{
var subObjects = new List<SnoopableObject>();

if (pair.Result is IEnumerable enumerable)
{
foreach (var item in enumerable)
{
subObjects.Add(new SnoopableObject(context.Document, item));
}
}
else
{
subObjects.Add(new SnoopableObject(context.Document, pair.Result));
}

yield return new SnoopableObject(context.Document, pair.Arg, subObjects) { NamePrefix = value.Param_0_Name + " = " } ;
}
}
}
}
8 changes: 5 additions & 3 deletions sources/RevitDBExplorer/Domain/RevitDocumentationReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static DocXml GetPropertyComments(PropertyInfo info)

if (parameters.Any())
{
titleCollored = ToInlinesMethod(returnType, name, parameters).ToArray();
titleCollored = ToInlinesMethod(returnType, info.DeclaringType.Name, name, parameters).ToArray();
}
else
{
Expand All @@ -83,7 +83,7 @@ public static DocXml GetMethodComments(MethodInfo info)
var methodComments = docXml?.GetMethodComments(info);
var returnType = info.ReturnType.GetCSharpName();
var invocation = "(" + String.Join(", ", info.GetParameters().Select(p => $"{p.ParameterType.GetCSharpName()} {p.Name}").ToArray()) + ")";
var titleCollored = ToInlinesMethod(returnType, info.Name, info.GetParameters()).ToArray();
var titleCollored = ToInlinesMethod(returnType, info.DeclaringType.Name, info.Name, info.GetParameters()).ToArray();

var doc = new DocXml(returnType, info.Name, invocation, titleCollored)
{
Expand Down Expand Up @@ -116,10 +116,12 @@ private static IEnumerable<Inline> ToInlinesProp(string returnType, string name,
}
yield return new Run("}");
}
private static IEnumerable<Inline> ToInlinesMethod(string returnType, string name, ParameterInfo[] parameterInfos)
private static IEnumerable<Inline> ToInlinesMethod(string returnType, string declaringType, string name, ParameterInfo[] parameterInfos)
{
yield return new Run(returnType) { Foreground = returnType.IsPrimitiveTypeName() ? PropTypeBrush : TypeBrush };
yield return new Run(" ");
yield return new Run(declaringType) { Foreground = TypeBrush };
yield return new Run(".");
yield return new Run(name) { Foreground = NameBrush };
yield return new Run("(");
for (int i = 0; i < parameterInfos.Length; i++)
Expand Down

0 comments on commit 3e00f64

Please sign in to comment.