-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add static Print methods to ExpressionPrinter #29854
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -70,34 +70,6 @@ public ExpressionPrinter() | |
private int? CharacterLimit { get; set; } | ||
private bool Verbose { get; set; } | ||
|
||
/// <summary> | ||
/// Visit given readonly collection of expression for printing. | ||
/// </summary> | ||
/// <param name="items">A collection of items to print.</param> | ||
/// <param name="joinAction">A join action to use when joining printout of individual item in the collection.</param> | ||
public virtual void VisitCollection<T>( | ||
IReadOnlyCollection<T> items, | ||
Action<ExpressionPrinter>? joinAction = null) | ||
where T : Expression | ||
{ | ||
joinAction ??= (p => p.Append(", ")); | ||
|
||
var first = true; | ||
foreach (var item in items) | ||
{ | ||
if (!first) | ||
{ | ||
joinAction(this); | ||
} | ||
else | ||
{ | ||
first = false; | ||
} | ||
|
||
Visit(item); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Appends a new line to current output being built. | ||
/// </summary> | ||
|
@@ -149,30 +121,40 @@ public virtual ExpressionPrinter Append(string value) | |
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Creates a printable string representation of the given expression. | ||
/// </summary> | ||
/// <param name="expression">The expression to print.</param> | ||
/// <returns>The printable representation.</returns> | ||
public static string Print(Expression expression) | ||
=> new ExpressionPrinter().PrintCore(expression); | ||
|
||
/// <summary> | ||
/// Creates a printable verbose string representation of the given expression. | ||
/// </summary> | ||
/// <param name="expression">The expression to print.</param> | ||
/// <returns>The printable representation.</returns> | ||
public static string PrintDebug(Expression expression) | ||
=> new ExpressionPrinter().PrintCore(expression, verbose: true); | ||
|
||
/// <summary> | ||
/// Creates a printable string representation of the given expression. | ||
/// </summary> | ||
/// <param name="expression">The expression to print.</param> | ||
/// <param name="characterLimit">An optional limit to the number of characters included. Additional output will be truncated.</param> | ||
/// <returns>The printable representation.</returns> | ||
public virtual string Print( | ||
Expression expression, | ||
int? characterLimit = null) | ||
=> PrintCore(expression, characterLimit, verbose: false); | ||
public virtual string PrintExpression(Expression expression, int? characterLimit = null) | ||
=> PrintCore(expression, characterLimit); | ||
|
||
/// <summary> | ||
/// Creates a printable verbose string representation of the given expression. | ||
/// </summary> | ||
/// <param name="expression">The expression to print.</param> | ||
/// <returns>The printable representation.</returns> | ||
public virtual string PrintDebug( | ||
Expression expression) | ||
=> PrintCore(expression, characterLimit: null, verbose: true); | ||
|
||
private string PrintCore( | ||
Expression expression, | ||
int? characterLimit, | ||
bool verbose) | ||
public virtual string PrintExpressionDebug(Expression expression) | ||
=> PrintCore(expression, verbose: true); | ||
|
||
private string PrintCore(Expression expression, int? characterLimit = null, bool verbose = false) | ||
{ | ||
_stringBuilder.Clear(); | ||
_parametersInScope.Clear(); | ||
|
@@ -186,8 +168,7 @@ private string PrintCore( | |
|
||
var queryPlan = PostProcess(_stringBuilder.ToString()); | ||
|
||
if (characterLimit != null | ||
&& characterLimit.Value > 0) | ||
if (characterLimit is > 0) | ||
{ | ||
queryPlan = queryPlan.Length > characterLimit | ||
? queryPlan[..characterLimit.Value] + "..." | ||
|
@@ -205,6 +186,32 @@ private string PrintCore( | |
public virtual string GenerateBinaryOperator(ExpressionType expressionType) | ||
=> _binaryOperandMap[expressionType]; | ||
|
||
/// <summary> | ||
/// Visit given readonly collection of expression for printing. | ||
/// </summary> | ||
/// <param name="items">A collection of items to print.</param> | ||
/// <param name="joinAction">A join action to use when joining printout of individual item in the collection.</param> | ||
public virtual void VisitCollection<T>(IReadOnlyCollection<T> items, Action<ExpressionPrinter>? joinAction = null) | ||
where T : Expression | ||
{ | ||
joinAction ??= (p => p.Append(", ")); | ||
|
||
var first = true; | ||
foreach (var item in items) | ||
{ | ||
if (!first) | ||
{ | ||
joinAction(this); | ||
} | ||
else | ||
{ | ||
first = false; | ||
} | ||
|
||
Visit(item); | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
[return: NotNullIfNotNull("expression")] | ||
public override Expression? Visit(Expression? expression) | ||
|
@@ -386,7 +393,7 @@ protected override Expression VisitBlock(BlockExpression blockExpression) | |
} | ||
} | ||
|
||
var expressions = blockExpression.Result != null | ||
var expressions = blockExpression.Expressions.Count > 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BlockExpression.Result is non-nullable, but does through for empty blocks (so this was a bug). |
||
? blockExpression.Expressions.Except(new[] { blockExpression.Result }) | ||
: blockExpression.Expressions; | ||
|
||
|
@@ -396,7 +403,7 @@ protected override Expression VisitBlock(BlockExpression blockExpression) | |
AppendLine(";"); | ||
} | ||
|
||
if (blockExpression.Result != null) | ||
if (blockExpression.Expressions.Count > 0) | ||
{ | ||
if (blockExpression.Result.Type != typeof(void)) | ||
{ | ||
|
@@ -438,50 +445,50 @@ protected override Expression VisitConstant(ConstantExpression constantExpressio | |
} | ||
else | ||
{ | ||
Print(constantExpression.Value); | ||
PrintValue(constantExpression.Value); | ||
} | ||
|
||
return constantExpression; | ||
} | ||
|
||
private void Print(object? value) | ||
{ | ||
if (value is IEnumerable enumerable | ||
&& !(value is string)) | ||
void PrintValue(object? value) | ||
{ | ||
_stringBuilder.Append(value.GetType().ShortDisplayName() + " { "); | ||
|
||
var first = true; | ||
foreach (var item in enumerable) | ||
if (value is IEnumerable enumerable | ||
&& !(value is string)) | ||
{ | ||
if (first) | ||
{ | ||
first = false; | ||
} | ||
else | ||
_stringBuilder.Append(value.GetType().ShortDisplayName() + " { "); | ||
|
||
var first = true; | ||
foreach (var item in enumerable) | ||
{ | ||
_stringBuilder.Append(", "); | ||
if (first) | ||
{ | ||
first = false; | ||
} | ||
else | ||
{ | ||
_stringBuilder.Append(", "); | ||
} | ||
|
||
PrintValue(item); | ||
} | ||
|
||
Print(item); | ||
_stringBuilder.Append(" }"); | ||
return; | ||
} | ||
|
||
_stringBuilder.Append(" }"); | ||
return; | ||
} | ||
var stringValue = value == null | ||
? "null" | ||
: value.ToString() != value.GetType().ToString() | ||
? value.ToString() | ||
: value.GetType().ShortDisplayName(); | ||
|
||
var stringValue = value == null | ||
? "null" | ||
: value.ToString() != value.GetType().ToString() | ||
? value.ToString() | ||
: value.GetType().ShortDisplayName(); | ||
if (value is string) | ||
{ | ||
stringValue = $@"""{stringValue}"""; | ||
} | ||
|
||
if (value is string) | ||
{ | ||
stringValue = $@"""{stringValue}"""; | ||
_stringBuilder.Append(stringValue ?? "Unknown"); | ||
} | ||
|
||
_stringBuilder.Append(stringValue ?? "Unknown"); | ||
} | ||
|
||
/// <inheritdoc /> | ||
|
@@ -642,8 +649,7 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp | |
_stringBuilder.AppendLine(); | ||
_stringBuilder.Append($".{method.Name}"); | ||
methodArguments = methodArguments.Skip(1).ToList(); | ||
if (method.Name == nameof(Enumerable.Cast) | ||
|| method.Name == nameof(Enumerable.OfType)) | ||
if (method.Name is nameof(Enumerable.Cast) or nameof(Enumerable.OfType)) | ||
{ | ||
PrintGenericArguments(method, _stringBuilder); | ||
} | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C# doesn't allow static/non-static overloading where the signature is otherwise the same. Since the static API is what we're likely to use in debugging (where shorter names are the most important), I'm proposing to have a static Print, and a longer non-static PrintExpression (which we'd use only in code).