Skip to content

Commit 6d84dad

Browse files
Fix
1 parent a0007ab commit 6d84dad

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

generators.new/TemplateRenderer.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
1+
using System.Text.Json;
2+
13
using HandlebarsDotNet;
4+
using HandlebarsDotNet.IO;
25

36
using Humanizer;
47

8+
using Microsoft.CodeAnalysis.CSharp;
9+
510
namespace Generators;
611

712
internal static class TemplateRenderer
813
{
9-
static TemplateRenderer() =>
14+
static TemplateRenderer()
15+
{
1016
Handlebars.RegisterHelper("method_name", (writer, context, parameters) =>
1117
{
1218
var path = parameters.SelectMany(parameter => parameter as IEnumerable<string> ?? [parameter.ToString()!]);
1319
writer.WriteSafeString(string.Join(" ", path).Dehumanize());
1420
});
21+
Handlebars.Configuration.FormatterProviders.Add(new JsonElementFormatter());
22+
}
1523

1624
public static string RenderTests(Exercise exercise, TestCase[] testCases) =>
1725
CompileTemplate(exercise)(ToTemplateData(exercise, testCases));
@@ -31,4 +39,27 @@ private static Dictionary<string, TestCase[]> GroupTestCasesByProperty(TestCase[
3139
testCases
3240
.GroupBy(testCase => testCase.Property)
3341
.ToDictionary(kv => kv.Key, kv => kv.ToArray());
42+
43+
private sealed class JsonElementFormatter : IFormatter, IFormatterProvider
44+
{
45+
public void Format<T>(T value, in EncodedTextWriter writer)
46+
{
47+
if (value is not JsonElement element)
48+
throw new ArgumentException("Invalid type");
49+
50+
writer.Write(SymbolDisplay.FormatLiteral(element.ToString(), false));
51+
}
52+
53+
public bool TryCreateFormatter(Type type, out IFormatter formatter)
54+
{
55+
if (type != typeof(JsonElement))
56+
{
57+
formatter = null;
58+
return false;
59+
}
60+
61+
formatter = this;
62+
return true;
63+
}
64+
}
3465
}

0 commit comments

Comments
 (0)