Skip to content

Commit

Permalink
RDS: add option if generated code should be for a single or multiple …
Browse files Browse the repository at this point in the history
…inputs
  • Loading branch information
NeVeSpl committed Nov 8, 2023
1 parent a9795b0 commit f10c7da
Show file tree
Hide file tree
Showing 24 changed files with 380 additions and 177 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// (c) Revit Database Explorer https://github.com/NeVeSpl/RevitDBExplorer/blob/main/license.md

using RevitDBExplorer.Domain.RevitDatabaseScripting;

namespace RevitDBExplorer.Domain.DataModel.Accessors
{
internal interface IAccessorWithCodeGeneration : IAccessor
{
string GenerateInvocationForScript();
string GenerateInvocationForScript(TemplateInputsKind inputsKind);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ private object[] ResolveArguments(ParameterInfo[] paramsDef, Document doc, objec
}


public string GenerateInvocationForScript()
public string GenerateInvocationForScript(TemplateInputsKind inputsKind)
{
return new MemberInvocation_Template().Evaluate(getMethod, null);
return new MemberInvocationTemplateSelector().Evaluate(getMethod, null, inputsKind);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public override IEnumerable<SnoopableObject> Snoop(SnoopableContext context, TSn
}


public string GenerateInvocationForScript()
public string GenerateInvocationForScript(TemplateInputsKind inputsKind)
{
return new MemberInvocation_Template().Evaluate(getMethod, null);
return new MemberInvocationTemplateSelector().Evaluate(getMethod, null, inputsKind);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public IValueViewModel CreatePresenter(SnoopableContext context, object @object)
{
return EmptyPresenter.Instance;
}
public string GenerateInvocationForScript()
public string GenerateInvocationForScript(TemplateInputsKind inputsKind)
{
return new MemberInvocation_Template().Evaluate(getMethod, null);
return new MemberInvocationTemplateSelector().Evaluate(getMethod, null, inputsKind);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public IValueViewModel CreatePresenter(SnoopableContext context, object @object)
{
return EmptyPresenter.Instance;
}
public string GenerateInvocationForScript()
public string GenerateInvocationForScript(TemplateInputsKind inputsKind)
{
return new MemberInvocation_Template().Evaluate(getMethod, "");
return new MemberInvocationTemplateSelector().Evaluate(getMethod, "", inputsKind);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,22 @@ public IEnumerable<SnoopableObject> Snoop(SnoopableContext context, object @obje
}


public string GenerateInvocationForScript()
public string GenerateInvocationForScript(TemplateInputsKind inputsKind)
{
string paramValue = parameter.StorageType switch { StorageType.String => @"""""", StorageType.Integer => "0", StorageType.Double => "0.0", _ => "new ElementId(0)" };

if (parameter.IsShared)
{
return new ParameterShared_UpdateTemplate().Evaluate(parameter.GUID, paramValue);
return new ParameterShared_UpdateTemplate().Evaluate(parameter.GUID, paramValue, inputsKind);
}
if (parameter.Definition is InternalDefinition internalDef)
{
if (internalDef.BuiltInParameter != BuiltInParameter.INVALID)
{
return new ParameterBuiltIn_UpdateTemplate().Evaluate(internalDef.BuiltInParameter, paramValue);
return new ParameterBuiltIn_UpdateTemplate().Evaluate(internalDef.BuiltInParameter, paramValue, inputsKind);
}
}
return new ParameterProject_UpdateTemplate().Evaluate(parameter.Definition.Name, paramValue);
return new ParameterProject_UpdateTemplate().Evaluate(parameter.Definition.Name, paramValue, inputsKind);
}


Expand Down
6 changes: 3 additions & 3 deletions sources/RevitDBExplorer/Domain/DataModel/SnoopableItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ public void Freeze(int candies)
public abstract bool Equals(SnoopableItem other);


public string GenerateScript()
public string GenerateScript(TemplateInputsKind inputsKind)
{
if (accessor is IAccessorWithCodeGeneration accessorWithCodeGeneration)
{
return accessorWithCodeGeneration.GenerateInvocationForScript();
return accessorWithCodeGeneration.GenerateInvocationForScript(inputsKind);
}
var invocation = accessor.DefaultInvocation ?? Name;

return new MemberInvocation_SelectTemplate().Evaluate(parent.Object.GetType(), invocation);
return new MemberInvocationTemplateSelector().Evaluate(parent.Object.GetType(), invocation, TemplateCmdKind.Select, inputsKind);
}
}
}
10 changes: 7 additions & 3 deletions sources/RevitDBExplorer/Domain/DataModel/SnoopableMember.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using RevitDBExplorer.Domain.DataModel.Streams.Base;
using System.Collections.Generic;
using System.Windows.Documents;
using RevitDBExplorer.Domain.DataModel.Streams.Base;

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

Expand All @@ -25,15 +27,17 @@ public SnoopableMember(SnoopableObject parent, MemberDescriptor memberDescriptor
public override SourceOfObjects Snoop()
{
var fullTitle = Name;
IEnumerable<Inline> fullTitleColored = new[] { new Run(Name) };
if (!string.IsNullOrEmpty(Documentation.Name))
{
fullTitle = $"{Documentation.ReturnType} {Documentation.Name}{Documentation.Invocation}";
fullTitle = Documentation.Title;
fullTitleColored = Documentation.TitleCollored;
}

var title = $"{parent.Name}.{this.Name}";


return new SourceOfObjects(this) { Info = new InfoAboutSource(title, fullTitle) };
return new SourceOfObjects(this) { Info = new InfoAboutSource(title) { FullTitleColored = fullTitleColored } };
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Reflection;
using NSourceGenerators;

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

namespace RevitDBExplorer.Domain.RevitDatabaseScripting
{
public enum TemplateCmdKind { Select, Update };
public enum TemplateInputsKind { Single, Multiple };

internal class MemberInvocationTemplateSelector
{
public string Evaluate(MethodInfo getMethod, string invocation, TemplateInputsKind inputsKind)
{
invocation ??= getMethod.GenerateInvocation();
var cmdkind = getMethod.ReturnType == typeof(void) ? TemplateCmdKind.Update : TemplateCmdKind.Select;

return Evaluate(getMethod.DeclaringType, invocation, cmdkind, inputsKind);
}
public string Evaluate(Type type, string invocation, TemplateCmdKind cmdkind, TemplateInputsKind inputsKind)
{
var template = GetTemplateType(cmdkind, inputsKind);

return Evaluate(template, type, invocation);
}
private Type GetTemplateType(TemplateCmdKind cmdKind, TemplateInputsKind inputsKind)
{
if ((inputsKind == TemplateInputsKind.Single) && (cmdKind == TemplateCmdKind.Select))
{
return typeof(MemberInvocation_SelectSingle_Template);
}
if ((inputsKind == TemplateInputsKind.Single) && (cmdKind == TemplateCmdKind.Update))
{
return typeof(MemberInvocation_UpdateSingle_Template);
}
if ((inputsKind == TemplateInputsKind.Multiple) && (cmdKind == TemplateCmdKind.Select))
{
return typeof(MemberInvocation_SelectMultiple_Template);
}
if ((inputsKind == TemplateInputsKind.Multiple) && (cmdKind == TemplateCmdKind.Update))
{
return typeof(MemberInvocation_UpdateMultiple_Template);
}

throw new NotImplementedException();
}



private string Evaluate(System.Type templateType, System.Type type, string invocation)
{
var template = CodeToStringRepo.GetText(templateType.Name, true);
var result = template.ReplaceMany(new[]
{
("TypePlaceholder", type.GetCSharpName()),
("MethodPlaceholder()", invocation)
});

return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using NSourceGenerators;
using RevitDBExplorer.Domain.RevitDatabaseScripting.Dummies;

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

namespace RevitDBExplorer.Domain.RevitDatabaseScripting
{
internal class MemberInvocation_SelectMultiple_Template
{
[CodeToString(nameof(MemberInvocation_SelectMultiple_Template))]
IEnumerable<object> Select(Document document, IEnumerable<TypePlaceholder> inputs)
{
foreach (var item in inputs)
{
yield return item.MethodPlaceholder();
}
}
}


internal class MemberInvocation_SelectSingle_Template
{
[CodeToString(nameof(MemberInvocation_SelectSingle_Template))]
object Select(Document document, IEnumerable<TypePlaceholder> inputs)
{
var input = inputs.FirstOrDefault();
return input.MethodPlaceholder();
}
}


internal class MemberInvocation_UpdateMultiple_Template
{
[CodeToString(nameof(MemberInvocation_UpdateMultiple_Template))]
void Update(Document document, IEnumerable<TypePlaceholder> inputs)
{
foreach (var item in inputs)
{
item.MethodPlaceholder();
}
}
}


internal class MemberInvocation_UpdateSingle_Template
{
[CodeToString(nameof(MemberInvocation_UpdateSingle_Template))]
void Update(Document document, IEnumerable<TypePlaceholder> inputs)
{
var input = inputs.FirstOrDefault();
input.MethodPlaceholder();
}
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit f10c7da

Please sign in to comment.