-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #185 from kindermannhubert/object-formatter-format…
…ting Formatting of output
- Loading branch information
Showing
44 changed files
with
5,435 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...epl.Services/Roslyn/Microsoft.CodeAnalysis.CSharp.Scripting.Hosting/CSharpMemberFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
#nullable disable | ||
|
||
using Microsoft.CodeAnalysis.CSharp.Symbols; | ||
using Microsoft.CodeAnalysis.Scripting.Hosting; | ||
|
||
namespace Microsoft.CodeAnalysis.CSharp.Scripting.Hosting; | ||
|
||
internal class CSharpMemberFilter : CommonMemberFilter | ||
{ | ||
protected override bool IsGeneratedMemberName(string name) | ||
{ | ||
// Generated fields, e.g. "<property_name>k__BackingField" | ||
return GeneratedNames.IsGeneratedMemberName(name); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...vices/Roslyn/Microsoft.CodeAnalysis.CSharp.Scripting.Hosting/CSharpObjectFormatterImpl.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
#nullable disable | ||
|
||
using System.Reflection; | ||
using CSharpRepl.Services; | ||
using CSharpRepl.Services.SyntaxHighlighting; | ||
using Microsoft.CodeAnalysis.Scripting.Hosting; | ||
using MemberFilter = Microsoft.CodeAnalysis.Scripting.Hosting.MemberFilter; | ||
|
||
namespace Microsoft.CodeAnalysis.CSharp.Scripting.Hosting; | ||
|
||
internal class CSharpObjectFormatterImpl : CommonObjectFormatter | ||
{ | ||
protected override CommonTypeNameFormatter TypeNameFormatter { get; } | ||
protected override CommonPrimitiveFormatter PrimitiveFormatter { get; } | ||
protected override MemberFilter Filter { get; } | ||
|
||
internal CSharpObjectFormatterImpl(SyntaxHighlighter syntaxHighlighter, Configuration config) | ||
: base(syntaxHighlighter, config) | ||
{ | ||
PrimitiveFormatter = new CSharpPrimitiveFormatter(syntaxHighlighter); | ||
TypeNameFormatter = new CSharpTypeNameFormatter(PrimitiveFormatter); | ||
Filter = new CSharpMemberFilter(); | ||
} | ||
|
||
protected override string FormatRefKind(ParameterInfo parameter) => parameter.IsOut ? "out" : "ref"; | ||
} |
102 changes: 102 additions & 0 deletions
102
...rvices/Roslyn/Microsoft.CodeAnalysis.CSharp.Scripting.Hosting/CSharpPrimitiveFormatter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Globalization; | ||
using CSharpRepl.Services.SyntaxHighlighting; | ||
using Microsoft.CodeAnalysis.Classification; | ||
using Microsoft.CodeAnalysis.Scripting.Hosting; | ||
using PrettyPrompt.Highlighting; | ||
|
||
namespace Microsoft.CodeAnalysis.CSharp.Scripting.Hosting; | ||
|
||
using static ObjectFormatterHelpers; | ||
|
||
internal class CSharpPrimitiveFormatter : CommonPrimitiveFormatter | ||
{ | ||
private readonly ConsoleFormat numericLiteralFormat; | ||
private readonly ConsoleFormat stringLiteralFormat; | ||
private readonly ConsoleFormat keywordFormat; | ||
|
||
public CSharpPrimitiveFormatter(SyntaxHighlighter syntaxHighlighter) | ||
{ | ||
numericLiteralFormat = new ConsoleFormat(Foreground: syntaxHighlighter.GetColor(ClassificationTypeNames.NumericLiteral)); | ||
stringLiteralFormat = new ConsoleFormat(Foreground: syntaxHighlighter.GetColor(ClassificationTypeNames.StringLiteral)); | ||
keywordFormat = new ConsoleFormat(Foreground: syntaxHighlighter.GetColor(ClassificationTypeNames.Keyword)); | ||
NullLiteral = new FormattedString("null", keywordFormat); | ||
} | ||
|
||
protected override FormattedString NullLiteral { get; } | ||
|
||
protected override FormattedString FormatLiteral(bool value) | ||
{ | ||
return new(ObjectDisplay.FormatLiteral(value), keywordFormat); | ||
} | ||
|
||
protected override FormattedString FormatLiteral(string value, bool useQuotes, bool escapeNonPrintable, int numberRadix = NumberRadixDecimal) | ||
{ | ||
var options = GetObjectDisplayOptions(useQuotes: useQuotes, escapeNonPrintable: escapeNonPrintable, numberRadix: numberRadix); | ||
return new(ObjectDisplay.FormatLiteral(value, options), stringLiteralFormat); | ||
} | ||
|
||
protected override FormattedString FormatLiteral(char c, bool useQuotes, bool escapeNonPrintable, bool includeCodePoints = false, int numberRadix = NumberRadixDecimal) | ||
{ | ||
var options = GetObjectDisplayOptions(useQuotes: useQuotes, escapeNonPrintable: escapeNonPrintable, includeCodePoints: includeCodePoints, numberRadix: numberRadix); | ||
return new(ObjectDisplay.FormatLiteral(c, options), stringLiteralFormat); | ||
} | ||
|
||
protected override FormattedString FormatLiteral(sbyte value, int numberRadix = NumberRadixDecimal, CultureInfo? cultureInfo = null) | ||
{ | ||
return new(ObjectDisplay.FormatLiteral(value, GetObjectDisplayOptions(numberRadix: numberRadix), cultureInfo), numericLiteralFormat); | ||
} | ||
|
||
protected override FormattedString FormatLiteral(byte value, int numberRadix = NumberRadixDecimal, CultureInfo? cultureInfo = null) | ||
{ | ||
return new(ObjectDisplay.FormatLiteral(value, GetObjectDisplayOptions(numberRadix: numberRadix), cultureInfo), numericLiteralFormat); | ||
} | ||
|
||
protected override FormattedString FormatLiteral(short value, int numberRadix = NumberRadixDecimal, CultureInfo? cultureInfo = null) | ||
{ | ||
return new(ObjectDisplay.FormatLiteral(value, GetObjectDisplayOptions(numberRadix: numberRadix), cultureInfo), numericLiteralFormat); | ||
} | ||
|
||
protected override FormattedString FormatLiteral(ushort value, int numberRadix = NumberRadixDecimal, CultureInfo? cultureInfo = null) | ||
{ | ||
return new(ObjectDisplay.FormatLiteral(value, GetObjectDisplayOptions(numberRadix: numberRadix), cultureInfo), numericLiteralFormat); | ||
} | ||
|
||
protected override FormattedString FormatLiteral(int value, int numberRadix = NumberRadixDecimal, CultureInfo? cultureInfo = null) | ||
{ | ||
return new(ObjectDisplay.FormatLiteral(value, GetObjectDisplayOptions(numberRadix: numberRadix), cultureInfo), numericLiteralFormat); | ||
} | ||
|
||
protected override FormattedString FormatLiteral(uint value, int numberRadix = NumberRadixDecimal, CultureInfo? cultureInfo = null) | ||
{ | ||
return new(ObjectDisplay.FormatLiteral(value, GetObjectDisplayOptions(numberRadix: numberRadix), cultureInfo), numericLiteralFormat); | ||
} | ||
|
||
protected override FormattedString FormatLiteral(long value, int numberRadix = NumberRadixDecimal, CultureInfo? cultureInfo = null) | ||
{ | ||
return new(ObjectDisplay.FormatLiteral(value, GetObjectDisplayOptions(numberRadix: numberRadix), cultureInfo), numericLiteralFormat); | ||
} | ||
|
||
protected override FormattedString FormatLiteral(ulong value, int numberRadix = NumberRadixDecimal, CultureInfo? cultureInfo = null) | ||
{ | ||
return new(ObjectDisplay.FormatLiteral(value, GetObjectDisplayOptions(numberRadix: numberRadix), cultureInfo), numericLiteralFormat); | ||
} | ||
|
||
protected override FormattedString FormatLiteral(double value, CultureInfo? cultureInfo = null) | ||
{ | ||
return new(ObjectDisplay.FormatLiteral(value, ObjectDisplayOptions.None, cultureInfo), numericLiteralFormat); | ||
} | ||
|
||
protected override FormattedString FormatLiteral(float value, CultureInfo? cultureInfo = null) | ||
{ | ||
return new(ObjectDisplay.FormatLiteral(value, ObjectDisplayOptions.None, cultureInfo), numericLiteralFormat); | ||
} | ||
|
||
protected override FormattedString FormatLiteral(decimal value, CultureInfo? cultureInfo = null) | ||
{ | ||
return new(ObjectDisplay.FormatLiteral(value, ObjectDisplayOptions.None, cultureInfo), numericLiteralFormat); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...ervices/Roslyn/Microsoft.CodeAnalysis.CSharp.Scripting.Hosting/CSharpTypeNameFormatter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
#nullable disable | ||
|
||
using System; | ||
using Microsoft.CodeAnalysis.CSharp.Symbols; | ||
using Microsoft.CodeAnalysis.Scripting.Hosting; | ||
|
||
namespace Microsoft.CodeAnalysis.CSharp.Scripting.Hosting; | ||
|
||
internal class CSharpTypeNameFormatter : CommonTypeNameFormatter | ||
{ | ||
protected override CommonPrimitiveFormatter PrimitiveFormatter { get; } | ||
|
||
public CSharpTypeNameFormatter(CommonPrimitiveFormatter primitiveFormatter) | ||
{ | ||
PrimitiveFormatter = primitiveFormatter; | ||
} | ||
|
||
protected override string GenericParameterOpening => "<"; | ||
protected override string GenericParameterClosing => ">"; | ||
protected override string ArrayOpening => "["; | ||
protected override string ArrayClosing => "]"; | ||
|
||
protected override string GetPrimitiveTypeName(SpecialType type) | ||
{ | ||
switch (type) | ||
{ | ||
case SpecialType.System_Boolean: return "bool"; | ||
case SpecialType.System_Byte: return "byte"; | ||
case SpecialType.System_Char: return "char"; | ||
case SpecialType.System_Decimal: return "decimal"; | ||
case SpecialType.System_Double: return "double"; | ||
case SpecialType.System_Int16: return "short"; | ||
case SpecialType.System_Int32: return "int"; | ||
case SpecialType.System_Int64: return "long"; | ||
case SpecialType.System_SByte: return "sbyte"; | ||
case SpecialType.System_Single: return "float"; | ||
case SpecialType.System_String: return "string"; | ||
case SpecialType.System_UInt16: return "ushort"; | ||
case SpecialType.System_UInt32: return "uint"; | ||
case SpecialType.System_UInt64: return "ulong"; | ||
case SpecialType.System_Object: return "object"; | ||
|
||
default: | ||
return null; | ||
} | ||
} | ||
|
||
public override string FormatTypeName(Type type, CommonTypeNameFormatterOptions options) | ||
{ | ||
string stateMachineName; | ||
if (GeneratedNameParser.TryParseSourceMethodNameFromGeneratedName(type.Name, GeneratedNameKind.StateMachineType, out stateMachineName)) | ||
{ | ||
return stateMachineName; | ||
} | ||
|
||
return base.FormatTypeName(type, options); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
CSharpRepl.Services/Roslyn/Microsoft.CodeAnalysis.CSharp.Symbols/GeneratedNameConstants.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
namespace Microsoft.CodeAnalysis.CSharp.Symbols; | ||
|
||
internal static class GeneratedNameConstants | ||
{ | ||
internal const char DotReplacementInTypeNames = '-'; | ||
internal const string SynthesizedLocalNamePrefix = "CS$"; | ||
internal const string SuffixSeparator = "__"; | ||
internal const char LocalFunctionNameTerminator = '|'; | ||
} |
Oops, something went wrong.