@@ -56,6 +56,8 @@ public class ScriptFormattingVisitor extends ScriptVisitorSupport {
5656
5757 private int maxIncludeWidth = 0 ;
5858
59+ private int maxParamWidth = 0 ;
60+
5961 public ScriptFormattingVisitor (SourceUnit sourceUnit , FormattingOptions options ) {
6062 this .sourceUnit = sourceUnit ;
6163 this .options = options ;
@@ -72,8 +74,6 @@ public void visit() {
7274 if ( !(moduleNode instanceof ScriptNode ) )
7375 return ;
7476 var scriptNode = (ScriptNode ) moduleNode ;
75- if ( options .harshilAlignment () )
76- maxIncludeWidth = getMaxIncludeWidth (scriptNode .getIncludes ());
7777 if ( scriptNode .getShebang () != null )
7878 fmt .append (scriptNode .getShebang ());
7979
@@ -93,6 +93,18 @@ public void visit() {
9393 declarations .sort (Comparator .comparing (node -> node .getLineNumber ()));
9494 }
9595
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+
96108 for ( var decl : declarations ) {
97109 if ( decl instanceof ClassNode cn && cn .isEnum () )
98110 visitEnum (cn );
@@ -113,24 +125,6 @@ else if( decl instanceof WorkflowNode wn )
113125 }
114126 }
115127
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-
134128 public String toString () {
135129 return fmt .toString ();
136130 }
@@ -186,23 +180,24 @@ public void visitInclude(IncludeNode node) {
186180 fmt .appendNewLine ();
187181 }
188182
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+ }
193188
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 ));
205197 }
198+ fmt .append (" = " );
199+ fmt .visit (node .value );
200+ fmt .appendNewLine ();
206201 }
207202
208203 protected int getParamWidth (ParamNode node ) {
0 commit comments