@@ -19,6 +19,7 @@ public class ExpressionPrinter : ExpressionVisitor
19
19
private readonly IndentedStringBuilder _stringBuilder ;
20
20
private readonly Dictionary < ParameterExpression , string > _parametersInScope ;
21
21
private readonly List < ParameterExpression > _namelessParameters ;
22
+ private readonly List < ParameterExpression > _encounteredParameters ;
22
23
23
24
private readonly Dictionary < ExpressionType , string > _binaryOperandMap = new Dictionary < ExpressionType , string >
24
25
{
@@ -47,6 +48,7 @@ public ExpressionPrinter()
47
48
_stringBuilder = new IndentedStringBuilder ( ) ;
48
49
_parametersInScope = new Dictionary < ParameterExpression , string > ( ) ;
49
50
_namelessParameters = new List < ParameterExpression > ( ) ;
51
+ _encounteredParameters = new List < ParameterExpression > ( ) ;
50
52
}
51
53
52
54
public virtual IndentedStringBuilder StringBuilder => _stringBuilder ;
@@ -55,7 +57,7 @@ public ExpressionPrinter()
55
57
56
58
private int ? CharacterLimit { get ; set ; }
57
59
58
- private bool PrintConnections { get ; set ; }
60
+ private bool GenerateUniqueParameterIds { get ; set ; }
59
61
60
62
public virtual void VisitList < T > (
61
63
IReadOnlyList < T > items ,
@@ -105,12 +107,6 @@ public virtual ExpressionPrinter IncrementIndent()
105
107
return this ;
106
108
}
107
109
108
- public virtual ExpressionPrinter IncrementIndent ( bool connectNode )
109
- {
110
- _stringBuilder . IncrementIndent ( connectNode ) ;
111
- return this ;
112
- }
113
-
114
110
public virtual ExpressionPrinter DecrementIndent ( )
115
111
{
116
112
_stringBuilder . DecrementIndent ( ) ;
@@ -132,18 +128,32 @@ private void AppendLine([NotNull] string message)
132
128
}
133
129
134
130
public virtual string Print (
131
+ Expression expression ,
132
+ bool removeFormatting = false ,
133
+ int ? characterLimit = null )
134
+ => PrintCore ( expression , removeFormatting , characterLimit , generateUniqueParameterIds : false ) ;
135
+
136
+ public virtual string PrintDebug (
135
137
Expression expression ,
136
138
bool removeFormatting = false ,
137
139
int ? characterLimit = null ,
138
- bool printConnections = true )
140
+ bool generateUniqueParameterIds = true )
141
+ => PrintCore ( expression , removeFormatting , characterLimit , generateUniqueParameterIds ) ;
142
+
143
+ protected virtual string PrintCore (
144
+ Expression expression ,
145
+ bool removeFormatting ,
146
+ int ? characterLimit ,
147
+ bool generateUniqueParameterIds )
139
148
{
140
149
_stringBuilder . Clear ( ) ;
141
150
_parametersInScope . Clear ( ) ;
142
151
_namelessParameters . Clear ( ) ;
152
+ _encounteredParameters . Clear ( ) ;
143
153
144
154
RemoveFormatting = removeFormatting ;
145
155
CharacterLimit = characterLimit ;
146
- PrintConnections = printConnections ;
156
+ GenerateUniqueParameterIds = generateUniqueParameterIds ;
147
157
148
158
Visit ( expression ) ;
149
159
@@ -318,12 +328,6 @@ protected override Expression VisitBinary(BinaryExpression binaryExpression)
318
328
protected override Expression VisitBlock ( BlockExpression blockExpression )
319
329
{
320
330
AppendLine ( ) ;
321
-
322
- if ( PrintConnections )
323
- {
324
- _stringBuilder . SuspendCurrentNode ( ) ;
325
- }
326
-
327
331
AppendLine ( "{" ) ;
328
332
_stringBuilder . IncrementIndent ( ) ;
329
333
@@ -359,11 +363,6 @@ protected override Expression VisitBlock(BlockExpression blockExpression)
359
363
_stringBuilder . DecrementIndent ( ) ;
360
364
Append ( "}" ) ;
361
365
362
- if ( PrintConnections )
363
- {
364
- _stringBuilder . ReconnectCurrentNode ( ) ;
365
- }
366
-
367
366
return blockExpression ;
368
367
}
369
368
@@ -384,11 +383,6 @@ protected override Expression VisitConditional(ConditionalExpression conditional
384
383
385
384
protected override Expression VisitConstant ( ConstantExpression constantExpression )
386
385
{
387
- if ( PrintConnections )
388
- {
389
- _stringBuilder . SuspendCurrentNode ( ) ;
390
- }
391
-
392
386
if ( constantExpression . Value is IPrintable printable )
393
387
{
394
388
printable . Print ( this ) ;
@@ -402,11 +396,6 @@ protected override Expression VisitConstant(ConstantExpression constantExpressio
402
396
Print ( constantExpression . Value ) ;
403
397
}
404
398
405
- if ( PrintConnections )
406
- {
407
- _stringBuilder . ReconnectCurrentNode ( ) ;
408
- }
409
-
410
399
return constantExpression ;
411
400
}
412
401
@@ -474,7 +463,7 @@ protected override Expression VisitLambda<T>(Expression<T> lambdaExpression)
474
463
_parametersInScope . Add ( parameter , parameterName ) ;
475
464
}
476
465
477
- _stringBuilder . Append ( parameter . Type . ShortDisplayName ( ) + " " + parameterName ) ;
466
+ Visit ( parameter ) ;
478
467
479
468
if ( parameter != lambdaExpression . Parameters . Last ( ) )
480
469
{
@@ -620,8 +609,7 @@ var argumentNames
620
609
621
610
if ( ! isSimpleMethodOrProperty )
622
611
{
623
- var shouldPrintConnections = PrintConnections && ! _nonConnectableMethods . Contains ( methodCallExpression . Method . Name ) ;
624
- _stringBuilder . IncrementIndent ( shouldPrintConnections ) ;
612
+ _stringBuilder. IncrementIndent ( ) ;
625
613
}
626
614
627
615
for ( var i = 0 ; i < methodCallExpression . Arguments . Count ; i++ )
@@ -633,12 +621,6 @@ var argumentNames
633
621
_stringBuilder. Append ( argumentNames [ i ] + ": " ) ;
634
622
}
635
623
636
- if ( i == methodCallExpression . Arguments . Count - 1
637
- && ! isSimpleMethodOrProperty )
638
- {
639
- _stringBuilder . DisconnectCurrentNode ( ) ;
640
- }
641
-
642
624
Visit( argument ) ;
643
625
644
626
if ( i < methodCallExpression . Arguments . Count - 1 )
@@ -715,12 +697,6 @@ protected override Expression VisitNewArray(NewArrayExpression newArrayExpressio
715
697
var appendAction = isComplex ? ( Action < string > ) AppendLine : Append ;
716
698
717
699
appendAction( "new " + newArrayExpression . Type . GetElementType ( ) . ShortDisplayName ( ) + "[]" ) ;
718
-
719
- if ( PrintConnections )
720
- {
721
- _stringBuilder. SuspendCurrentNode ( ) ;
722
- }
723
-
724
700
appendAction( "{ " ) ;
725
701
726
702
if ( isComplex )
@@ -737,11 +713,6 @@ protected override Expression VisitNewArray(NewArrayExpression newArrayExpressio
737
713
738
714
Append( "}" ) ;
739
715
740
- if ( PrintConnections )
741
- {
742
- _stringBuilder. ReconnectCurrentNode ( ) ;
743
- }
744
-
745
716
return newArrayExpression;
746
717
}
747
718
@@ -779,6 +750,21 @@ protected override Expression VisitParameter(ParameterExpression parameterExpres
779
750
Append( ")" ) ;
780
751
}
781
752
753
+ if ( GenerateUniqueParameterIds )
754
+ {
755
+ var parameterIndex = _encounteredParameters. Count ;
756
+ if ( _encounteredParameters . Contains ( parameterExpression ) )
757
+ {
758
+ parameterIndex = _encounteredParameters. IndexOf ( parameterExpression ) ;
759
+ }
760
+ else
761
+ {
762
+ _encounteredParameters. Add ( parameterExpression ) ;
763
+ }
764
+
765
+ _stringBuilder. Append ( "{" + parameterIndex + "}" ) ;
766
+ }
767
+
782
768
return parameterExpression;
783
769
}
784
770
@@ -893,7 +879,6 @@ private void VisitArguments(IReadOnlyList<Expression> arguments, Action<string>
893
879
if ( areConnected && i == arguments . Count - 1 )
894
880
{
895
881
Append ( "" ) ;
896
- _stringBuilder. DisconnectCurrentNode ( ) ;
897
882
}
898
883
899
884
Visit ( arguments [ i ] ) ;
0 commit comments