-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Swich to Text Writer dotnet/runtime#95882
- Loading branch information
1 parent
ae0dc6b
commit de8c87d
Showing
36 changed files
with
1,947 additions
and
1,049 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System.Globalization; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
|
||
namespace Pretender.SourceGenerator | ||
{ | ||
internal static class CSharpSyntaxUtilities | ||
{ | ||
// Standard format for double and single on non-inbox frameworks to ensure value is round-trippable. | ||
public const string DoubleFormatString = "G17"; | ||
public const string SingleFormatString = "G9"; | ||
|
||
// Format a literal in C# format -- works around https://github.com/dotnet/roslyn/issues/58705 | ||
|
||
public static string FormatLiteral(object? value, ITypeSymbol type) | ||
{ | ||
if (value == null) | ||
{ | ||
return $"default({type.ToFullDisplayString()})"; | ||
} | ||
|
||
switch (value) | ||
{ | ||
case string @string: | ||
return SymbolDisplay.FormatLiteral(@string, quote: true); | ||
case char @char: | ||
return SymbolDisplay.FormatLiteral(@char, quote: true); | ||
case double.NegativeInfinity: | ||
return "double.NegativeInfinity"; | ||
case double.PositiveInfinity: | ||
return "double.PositiveInfinity"; | ||
case double.NaN: | ||
return "double.NaN"; | ||
case double @double: | ||
return $"{@double.ToString(DoubleFormatString, CultureInfo.InvariantCulture)}D"; | ||
case float.NegativeInfinity: | ||
return "float.NegativeInfinity"; | ||
case float.PositiveInfinity: | ||
return "float.PositiveInfinity"; | ||
case float.NaN: | ||
return "float.NaN"; | ||
case float @float: | ||
return $"{@float.ToString(SingleFormatString, CultureInfo.InvariantCulture)}F"; | ||
case decimal @decimal: | ||
// we do not need to specify a format string for decimal as it's default is round-trippable on all frameworks. | ||
return $"{@decimal.ToString(CultureInfo.InvariantCulture)}M"; | ||
case bool @bool: | ||
return @bool ? "true" : "false"; | ||
default: | ||
// Assume this is a number. | ||
return FormatNumber(); | ||
} | ||
string FormatNumber() => $"({type.ToFullDisplayString()})({Convert.ToString(value, CultureInfo.InvariantCulture)})"; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.