-
Notifications
You must be signed in to change notification settings - Fork 368
/
PlainTextFormatter{T}.cs
131 lines (106 loc) · 3.75 KB
/
PlainTextFormatter{T}.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
namespace Microsoft.DotNet.Interactive.Formatting;
public delegate bool FormatDelegate<in T>(T value, FormatContext context);
public class PlainTextFormatter<T> : TypeFormatter<T>
{
private readonly FormatDelegate<T> _format;
public PlainTextFormatter(FormatDelegate<T> format)
{
_format = format ?? throw new ArgumentNullException(nameof(format));
}
public PlainTextFormatter(Action<T, FormatContext> format)
{
_format = FormatInstance;
bool FormatInstance(T instance, FormatContext context)
{
format(instance, context);
return true;
}
}
public PlainTextFormatter(Func<T, string> format)
{
_format = (instance, context) =>
{
context.Writer.Write(format(instance));
return true;
};
}
public override string MimeType => PlainTextFormatter.MimeType;
public override bool Format(T value, FormatContext context)
{
if (value is null)
{
context.Writer.Write(Formatter.NullString);
return true;
}
return _format(value, context);
}
internal static PlainTextFormatter<T> CreateForAnyObject()
{
if (typeof(T).IsScalar())
{
return new PlainTextFormatter<T>((value, context) =>
{
context.Writer.Write(value);
return true;
});
}
return new PlainTextFormatter<T>(
PlainTextFormatter.CreateFormatDelegate<T>(
typeof(T).GetMembersToFormat().ToArray()));
}
internal static PlainTextFormatter<T> CreateForAnyEnumerable()
{
if (typeof(T) == typeof(string))
{
return new((value, context) =>
{
context.Writer.Write(value);
return true;
});
}
if (typeof(T).GetInterfaces()
.Where(i => i.IsGenericType)
.Where(i => i.GetGenericTypeDefinition() == typeof(IEnumerable<>))
.Select(i => i.GenericTypeArguments[0])
.FirstOrDefault() is { } t)
{
var joinMethod = typeof(PlainTextFormatter).GetMethod(nameof(PlainTextFormatter.JoinGeneric), BindingFlags.NonPublic | BindingFlags.Static);
var genericMethod = joinMethod!.MakeGenericMethod(new[] { t });
var enumerableType = typeof(IEnumerable<>).MakeGenericType(t);
var delegateType = typeof(Action<,,>).MakeGenericType(
new[] { enumerableType, typeof(TextWriter), typeof(FormatContext) });
var m = genericMethod.CreateDelegate(delegateType);
return new((value, context) =>
{
m.DynamicInvoke(value, context.Writer, context);
return true;
});
}
return new((value, context) =>
{
using var _ = context.IncrementDepth();
using var __ = context.IncrementDepth();
switch (value)
{
case IEnumerable enumerable:
PlainTextFormatter.Join(enumerable,
context.Writer,
context);
break;
default:
context.Writer.Write(value.ToString());
break;
}
return true;
});
}
public static PlainTextFormatter<T> Default = CreateForAnyEnumerable();
}