@@ -56,6 +56,8 @@ public class ScriptFormattingVisitor extends ScriptVisitorSupport {
56
56
57
57
private int maxIncludeWidth = 0 ;
58
58
59
+ private int maxParamWidth = 0 ;
60
+
59
61
public ScriptFormattingVisitor (SourceUnit sourceUnit , FormattingOptions options ) {
60
62
this .sourceUnit = sourceUnit ;
61
63
this .options = options ;
@@ -72,8 +74,6 @@ public void visit() {
72
74
if ( !(moduleNode instanceof ScriptNode ) )
73
75
return ;
74
76
var scriptNode = (ScriptNode ) moduleNode ;
75
- if ( options .harshilAlignment () )
76
- maxIncludeWidth = getMaxIncludeWidth (scriptNode .getIncludes ());
77
77
if ( scriptNode .getShebang () != null )
78
78
fmt .append (scriptNode .getShebang ());
79
79
@@ -93,6 +93,18 @@ public void visit() {
93
93
declarations .sort (Comparator .comparing (node -> node .getLineNumber ()));
94
94
}
95
95
96
+ // -- prepare alignment widths if needed
97
+ if ( options .harshilAlignment () ) {
98
+ maxIncludeWidth = scriptNode .getIncludes ().stream ()
99
+ .flatMap (in -> in .modules .stream ())
100
+ .map (this ::getIncludeWidth )
101
+ .max (Integer ::compare ).orElse (0 );
102
+
103
+ maxParamWidth = scriptNode .getParams ().stream ()
104
+ .map (this ::getParamWidth )
105
+ .max (Integer ::compare ).orElse (0 );
106
+ }
107
+
96
108
for ( var decl : declarations ) {
97
109
if ( decl instanceof ClassNode cn && cn .isEnum () )
98
110
visitEnum (cn );
@@ -113,24 +125,6 @@ else if( decl instanceof WorkflowNode wn )
113
125
}
114
126
}
115
127
116
- protected int getMaxIncludeWidth (List <IncludeNode > includes ) {
117
- int maxWidth = 0 ;
118
- for ( var includeNode : includes ) {
119
- for ( var module : includeNode .modules ) {
120
- var width = getIncludeWidth (module );
121
- if ( maxWidth < width )
122
- maxWidth = width ;
123
- }
124
- }
125
- return maxWidth ;
126
- }
127
-
128
- protected int getIncludeWidth (IncludeVariable module ) {
129
- return module .alias != null
130
- ? module .name .length () + 4 + module .alias .length ()
131
- : module .name .length ();
132
- }
133
-
134
128
public String toString () {
135
129
return fmt .toString ();
136
130
}
@@ -186,23 +180,24 @@ public void visitInclude(IncludeNode node) {
186
180
fmt .appendNewLine ();
187
181
}
188
182
189
- protected void visitParams (List <ParamNode > nodes ) {
190
- var alignmentWidth = options .harshilAlignment ()
191
- ? nodes .stream ().map (this ::getParamWidth ).max (Integer ::compare ).orElse (0 )
192
- : 0 ;
183
+ protected int getIncludeWidth (IncludeVariable module ) {
184
+ return module .alias != null
185
+ ? module .name .length () + 4 + module .alias .length ()
186
+ : module .name .length ();
187
+ }
193
188
194
- for ( var node : nodes ) {
195
- fmt .appendLeadingComments (node );
196
- fmt .appendIndent ();
197
- fmt .visit (node .target );
198
- if ( alignmentWidth > 0 ) {
199
- var padding = alignmentWidth - getParamWidth (node );
200
- fmt .append (" " .repeat (padding ));
201
- }
202
- fmt .append (" = " );
203
- fmt .visit (node .value );
204
- fmt .appendNewLine ();
189
+ @ Override
190
+ public void visitParam (ParamNode node ) {
191
+ fmt .appendLeadingComments (node );
192
+ fmt .appendIndent ();
193
+ fmt .visit (node .target );
194
+ if ( maxParamWidth > 0 ) {
195
+ var padding = maxParamWidth - getParamWidth (node );
196
+ fmt .append (" " .repeat (padding ));
205
197
}
198
+ fmt .append (" = " );
199
+ fmt .visit (node .value );
200
+ fmt .appendNewLine ();
206
201
}
207
202
208
203
protected int getParamWidth (ParamNode node ) {
0 commit comments