Skip to content

Commit 2a0fa3c

Browse files
committed
Wrap lines based on trailing comma (close #74)
Signed-off-by: Ben Sherman <[email protected]>
1 parent 5bc416e commit 2a0fa3c

File tree

3 files changed

+65
-27
lines changed

3 files changed

+65
-27
lines changed

modules/compiler/src/main/java/config/parser/ConfigAstBuilder.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,8 @@ private Expression pathClosureElement(Expression expression, Expression closure)
726726

727727
private Expression pathArgumentsElement(Expression caller, ArgumentsContext ctx) {
728728
var arguments = argumentList(ctx.argumentList());
729+
if( ctx.COMMA() != null )
730+
arguments.putNodeMetaData(TRAILING_COMMA, Boolean.TRUE);
729731
return ast( methodCall(caller, arguments), caller, ctx );
730732
}
731733

@@ -952,6 +954,8 @@ private Expression gstringPath(ParserRuleContext ctx) {
952954
private Expression creator(CreatorContext ctx) {
953955
var type = createdName(ctx.createdName());
954956
var arguments = argumentList(ctx.arguments().argumentList());
957+
if( ctx.arguments().COMMA() != null )
958+
arguments.putNodeMetaData(TRAILING_COMMA, Boolean.TRUE);
955959
return ctorX(type, arguments);
956960
}
957961

@@ -971,7 +975,10 @@ private Expression list(ListContext ctx) {
971975
if( ctx.COMMA() != null && ctx.expressionList() == null )
972976
throw createParsingFailedException("Empty list literal should not contain any comma(,)", ctx.COMMA());
973977

974-
return listX(expressionList(ctx.expressionList()));
978+
var result = listX(expressionList(ctx.expressionList()));
979+
if( ctx.COMMA() != null )
980+
result.putNodeMetaData(TRAILING_COMMA, Boolean.TRUE);
981+
return result;
975982
}
976983

977984
private List<Expression> expressionList(ExpressionListContext ctx) {
@@ -990,7 +997,10 @@ private Expression map(MapContext ctx) {
990997
var entries = ctx.mapEntryList().mapEntry().stream()
991998
.map(this::mapEntry)
992999
.collect(Collectors.toList());
993-
return mapX(entries);
1000+
var result = mapX(entries);
1001+
if( ctx.COMMA() != null )
1002+
result.putNodeMetaData(TRAILING_COMMA, Boolean.TRUE);
1003+
return result;
9941004
}
9951005

9961006
private MapEntryExpression mapEntry(MapEntryContext ctx) {
@@ -1398,6 +1408,7 @@ public void reportContextSensitivity(Parser recognizer, DFA dfa, int startIndex,
13981408
private static final String INSIDE_PARENTHESES_LEVEL = "_INSIDE_PARENTHESES_LEVEL";
13991409
private static final String LEADING_COMMENTS = "_LEADING_COMMENTS";
14001410
private static final String QUOTE_CHAR = "_QUOTE_CHAR";
1411+
private static final String TRAILING_COMMA = "_TRAILING_COMMA";
14011412
private static final String VERBATIM_TEXT = "_VERBATIM_TEXT";
14021413

14031414
private static final List<String> GROOVY_KEYWORDS = List.of(

modules/compiler/src/main/java/script/formatter/Formatter.java

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,12 @@ else if( !(node.getElseBlock() instanceof EmptyStatement) ) {
179179
}
180180
}
181181

182-
private Expression currentStmtExpr;
182+
private Expression currentRootExpr;
183183

184184
@Override
185185
public void visitExpressionStatement(ExpressionStatement node) {
186-
var cse = currentStmtExpr;
187-
currentStmtExpr = node.getExpression();
186+
var cre = currentRootExpr;
187+
currentRootExpr = node.getExpression();
188188
appendLeadingComments(node);
189189
appendIndent();
190190
if( node.getStatementLabels() != null ) {
@@ -195,16 +195,19 @@ public void visitExpressionStatement(ExpressionStatement node) {
195195
}
196196
visit(node.getExpression());
197197
appendNewLine();
198-
currentStmtExpr = cse;
198+
currentRootExpr = cre;
199199
}
200200

201201
@Override
202202
public void visitReturnStatement(ReturnStatement node) {
203+
var cre = currentRootExpr;
204+
currentRootExpr = node.getExpression();
203205
appendLeadingComments(node);
204206
appendIndent();
205207
append("return ");
206208
visit(node.getExpression());
207209
appendNewLine();
210+
currentRootExpr = cre;
208211
}
209212

210213
@Override
@@ -359,9 +362,13 @@ public void visitBinaryExpression(BinaryExpression node) {
359362
inVariableDeclaration = true;
360363
visit(node.getLeftExpression());
361364
inVariableDeclaration = false;
362-
if( !(node.getRightExpression() instanceof EmptyExpression) ) {
365+
var source = node.getRightExpression();
366+
if( !(source instanceof EmptyExpression) ) {
363367
append(" = ");
364-
visit(node.getRightExpression());
368+
var cre = currentRootExpr;
369+
currentRootExpr = source;
370+
visit(source);
371+
currentRootExpr = cre;
365372
}
366373
return;
367374
}
@@ -378,12 +385,6 @@ public void visitBinaryExpression(BinaryExpression node) {
378385
if( beginWrappedPipeChain )
379386
inWrappedPipeChain = true;
380387

381-
Expression cse = null;
382-
if( currentStmtExpr == node && node.getOperation().isA(Types.ASSIGNMENT_OPERATOR) ) {
383-
cse = currentStmtExpr;
384-
currentStmtExpr = node.getRightExpression();
385-
}
386-
387388
visit(node.getLeftExpression());
388389

389390
if( inWrappedPipeChain ) {
@@ -399,15 +400,21 @@ public void visitBinaryExpression(BinaryExpression node) {
399400

400401
var iwpc = inWrappedPipeChain;
401402
inWrappedPipeChain = false;
402-
visit(node.getRightExpression());
403+
if( node.getOperation().isA(Types.ASSIGNMENT_OPERATOR) ) {
404+
var source = node.getRightExpression();
405+
var cre = currentRootExpr;
406+
currentRootExpr = source;
407+
visit(source);
408+
currentRootExpr = cre;
409+
}
410+
else {
411+
visit(node.getRightExpression());
412+
}
403413
inWrappedPipeChain = iwpc;
404414

405415
if( inWrappedPipeChain )
406416
decIndent();
407417

408-
if( cse != null )
409-
currentStmtExpr = cse;
410-
411418
if( beginWrappedPipeChain )
412419
inWrappedPipeChain = false;
413420
}
@@ -485,7 +492,7 @@ public void visitParameters(Parameter[] parameters) {
485492
}
486493
append(param.getName());
487494
if( param.hasInitialExpression() ) {
488-
append('=');
495+
append(" = ");
489496
visit(param.getInitialExpression());
490497
}
491498
if( i + 1 < parameters.length )
@@ -510,7 +517,7 @@ public void visitTupleExpression(TupleExpression node) {
510517

511518
@Override
512519
public void visitListExpression(ListExpression node) {
513-
var wrap = shouldWrapExpression(node);
520+
var wrap = hasTrailingComma(node) || shouldWrapExpression(node);
514521
append('[');
515522
if( wrap )
516523
incIndent();
@@ -525,13 +532,14 @@ public void visitListExpression(ListExpression node) {
525532

526533
protected void visitPositionalArgs(List<Expression> args, boolean wrap) {
527534
var comma = wrap ? "," : ", ";
535+
var trailingComma = wrap && args.size() > 1;
528536
for( int i = 0; i < args.size(); i++ ) {
529537
if( wrap ) {
530538
appendNewLine();
531539
appendIndent();
532540
}
533541
visit(args.get(i));
534-
if( i + 1 < args.size() )
542+
if( trailingComma || i + 1 < args.size() )
535543
append(comma);
536544
}
537545
}
@@ -542,7 +550,7 @@ public void visitMapExpression(MapExpression node) {
542550
append("[:]");
543551
return;
544552
}
545-
var wrap = shouldWrapExpression(node);
553+
var wrap = hasTrailingComma(node) || shouldWrapExpression(node);
546554
append('[');
547555
if( wrap )
548556
incIndent();
@@ -557,13 +565,14 @@ public void visitMapExpression(MapExpression node) {
557565

558566
protected void visitNamedArgs(List<MapEntryExpression> args, boolean wrap) {
559567
var comma = wrap ? "," : ", ";
568+
var trailingComma = wrap && args.size() > 1;
560569
for( int i = 0; i < args.size(); i++ ) {
561570
if( wrap ) {
562571
appendNewLine();
563572
appendIndent();
564573
}
565574
visit(args.get(i));
566-
if( i + 1 < args.size() )
575+
if( trailingComma || i + 1 < args.size() )
567576
append(comma);
568577
}
569578
}
@@ -713,6 +722,10 @@ public void visit(Expression node) {
713722

714723
// helpers
715724

725+
private static boolean hasTrailingComma(Expression node) {
726+
return node.getNodeMetaData(TRAILING_COMMA) != null;
727+
}
728+
716729
public static boolean isLegacyType(ClassNode cn) {
717730
return cn.getNodeMetaData(LEGACY_TYPE) != null;
718731
}
@@ -722,13 +735,15 @@ private boolean shouldWrapExpression(Expression node) {
722735
}
723736

724737
private boolean shouldWrapMethodCall(MethodCallExpression node) {
738+
if( hasTrailingComma(node.getArguments()) )
739+
return true;
725740
var start = node.getMethod();
726741
var end = node.getArguments();
727742
return start.getLineNumber() < end.getLastLineNumber();
728743
}
729744

730745
private boolean shouldWrapMethodChain(MethodCallExpression node) {
731-
if( currentStmtExpr != node )
746+
if( currentRootExpr != node )
732747
return false;
733748
if( !shouldWrapExpression(node) )
734749
return false;
@@ -746,7 +761,7 @@ private boolean shouldWrapMethodChain(MethodCallExpression node) {
746761
}
747762

748763
private boolean shouldWrapPipeExpression(BinaryExpression node) {
749-
return currentStmtExpr == node && node.getOperation().isA(Types.PIPE) && shouldWrapExpression(node);
764+
return currentRootExpr == node && node.getOperation().isA(Types.PIPE) && shouldWrapExpression(node);
750765
}
751766

752767
private static final String SLASH_STR = "/";
@@ -761,6 +776,7 @@ private boolean shouldWrapPipeExpression(BinaryExpression node) {
761776
private static final String LEADING_COMMENTS = "_LEADING_COMMENTS";
762777
private static final String LEGACY_TYPE = "_LEGACY_TYPE";
763778
private static final String QUOTE_CHAR = "_QUOTE_CHAR";
779+
private static final String TRAILING_COMMA = "_TRAILING_COMMA";
764780
private static final String TRAILING_COMMENT = "_TRAILING_COMMENT";
765781
private static final String VERBATIM_TEXT = "_VERBATIM_TEXT";
766782

modules/compiler/src/main/java/script/parser/ScriptAstBuilder.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,8 @@ private Expression pathClosureElement(Expression expression, Expression closure)
10771077

10781078
private Expression pathArgumentsElement(Expression caller, ArgumentsContext ctx) {
10791079
var arguments = argumentList(ctx.argumentList());
1080+
if( ctx.COMMA() != null )
1081+
arguments.putNodeMetaData(TRAILING_COMMA, Boolean.TRUE);
10801082
return ast( methodCall(caller, arguments), caller, ctx );
10811083
}
10821084

@@ -1303,6 +1305,8 @@ private Expression gstringPath(ParserRuleContext ctx) {
13031305
private Expression creator(CreatorContext ctx) {
13041306
var type = createdName(ctx.createdName());
13051307
var arguments = argumentList(ctx.arguments().argumentList());
1308+
if( ctx.arguments().COMMA() != null )
1309+
arguments.putNodeMetaData(TRAILING_COMMA, Boolean.TRUE);
13061310
return ctorX(type, arguments);
13071311
}
13081312

@@ -1347,7 +1351,10 @@ private Expression list(ListContext ctx) {
13471351
if( ctx.COMMA() != null && ctx.expressionList() == null )
13481352
throw createParsingFailedException("Empty list literal should not contain any comma(,)", ctx.COMMA());
13491353

1350-
return listX(expressionList(ctx.expressionList()));
1354+
var result = listX(expressionList(ctx.expressionList()));
1355+
if( ctx.COMMA() != null )
1356+
result.putNodeMetaData(TRAILING_COMMA, Boolean.TRUE);
1357+
return result;
13511358
}
13521359

13531360
private List<Expression> expressionList(ExpressionListContext ctx) {
@@ -1366,7 +1373,10 @@ private Expression map(MapContext ctx) {
13661373
var entries = ctx.mapEntryList().mapEntry().stream()
13671374
.map(this::mapEntry)
13681375
.collect(Collectors.toList());
1369-
return mapX(entries);
1376+
var result = mapX(entries);
1377+
if( ctx.COMMA() != null )
1378+
result.putNodeMetaData(TRAILING_COMMA, Boolean.TRUE);
1379+
return result;
13701380
}
13711381

13721382
private MapEntryExpression mapEntry(MapEntryContext ctx) {
@@ -1824,6 +1834,7 @@ public void reportContextSensitivity(Parser recognizer, DFA dfa, int startIndex,
18241834
private static final String LEADING_COMMENTS = "_LEADING_COMMENTS";
18251835
private static final String LEGACY_TYPE = "_LEGACY_TYPE";
18261836
private static final String QUOTE_CHAR = "_QUOTE_CHAR";
1837+
private static final String TRAILING_COMMA = "_TRAILING_COMMA";
18271838
private static final String TRAILING_COMMENT = "_TRAILING_COMMENT";
18281839
private static final String VERBATIM_TEXT = "_VERBATIM_TEXT";
18291840

0 commit comments

Comments
 (0)