-
Notifications
You must be signed in to change notification settings - Fork 367
/
PlainTextFormatter{T}.cs
138 lines (113 loc) · 4.22 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
132
133
134
135
136
137
138
// 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.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
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);
}
public static PlainTextFormatter<T> CreateForAnyObject(bool includeInternals = false)
{
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(includeInternals).ToArray()));
}
public static PlainTextFormatter<T> CreateForMembers(params Expression<Func<T, object>>[] members)
{
var format = PlainTextFormatter.CreateFormatDelegate<T>(
typeof(T).GetMembers(members).ToArray());
return new PlainTextFormatter<T>(format);
}
[SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "Part of Pattern")]
public static PlainTextFormatter<T> CreateForAnyEnumerable(bool includeInternals = false)
{
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(Formatter).GetMethod(nameof(Formatter.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), typeof(int?) });
var m = genericMethod.CreateDelegate(delegateType);
return new((value, context) =>
{
m.DynamicInvoke(value, context.Writer, context, Formatter<T>.ListExpansionLimit);
return true;
});
}
return new((value, context) =>
{
switch (value)
{
case IEnumerable enumerable:
Formatter.Join(enumerable,
context.Writer,
context, Formatter<T>.ListExpansionLimit);
break;
default:
context.Writer.Write(value.ToString());
break;
}
return true;
});
}
public static PlainTextFormatter<T> Default = CreateForAnyEnumerable(false);
}