Skip to content

Commit

Permalink
Review feedback and fix new testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurens-W committed Dec 19, 2024
1 parent 2ce482b commit 5314c93
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1691,15 +1691,18 @@ public void visitGStringExpression(GStringExpression gstring) {

@Override
public void visitListExpression(ListExpression list) {
if (list.getExpressions().isEmpty()) {
queue.add(new G.ListLiteral(randomId(), sourceBefore("["), Markers.EMPTY,
JContainer.build(singletonList(new JRightPadded<>(new J.Empty(randomId(), EMPTY, Markers.EMPTY), sourceBefore("]"), Markers.EMPTY))),
typeMapping.type(list.getType())));
} else {
queue.add(new G.ListLiteral(randomId(), sourceBefore("["), Markers.EMPTY,
JContainer.build(visitRightPadded(list.getExpressions().toArray(new ASTNode[0]), "]")),
typeMapping.type(list.getType())));
}
queue.add(insideParentheses(list, fmt -> {
skip("[");
if (list.getExpressions().isEmpty()) {
return new G.ListLiteral(randomId(), fmt, Markers.EMPTY,
JContainer.build(singletonList(new JRightPadded<>(new J.Empty(randomId(), EMPTY, Markers.EMPTY), sourceBefore("]"), Markers.EMPTY))),
typeMapping.type(list.getType()));
} else {
return new G.ListLiteral(randomId(), fmt, Markers.EMPTY,
JContainer.build(visitRightPadded(list.getExpressions().toArray(new ASTNode[0]), "]")),
typeMapping.type(list.getType()));
}
}));
}

@Override
Expand All @@ -1714,17 +1717,19 @@ public void visitMapEntryExpression(MapEntryExpression expression) {

@Override
public void visitMapExpression(MapExpression map) {
Space prefix = sourceBefore("[");
JContainer<G.MapEntry> entries;
if (map.getMapEntryExpressions().isEmpty()) {
entries = JContainer.build(Collections.singletonList(JRightPadded.build(
new G.MapEntry(randomId(), whitespace(), Markers.EMPTY,
JRightPadded.build(new J.Empty(randomId(), sourceBefore(":"), Markers.EMPTY)),
new J.Empty(randomId(), sourceBefore("]"), Markers.EMPTY), null))));
} else {
entries = JContainer.build(visitRightPadded(map.getMapEntryExpressions().toArray(new ASTNode[0]), "]"));
}
queue.add(new G.MapLiteral(randomId(), prefix, Markers.EMPTY, entries, typeMapping.type(map.getType())));
queue.add(insideParentheses(map, fmt -> {
skip("[");
JContainer<G.MapEntry> entries;
if (map.getMapEntryExpressions().isEmpty()) {
entries = JContainer.build(Collections.singletonList(JRightPadded.build(
new G.MapEntry(randomId(), whitespace(), Markers.EMPTY,
JRightPadded.build(new J.Empty(randomId(), sourceBefore(":"), Markers.EMPTY)),
new J.Empty(randomId(), sourceBefore("]"), Markers.EMPTY), null))));
} else {
entries = JContainer.build(visitRightPadded(map.getMapEntryExpressions().toArray(new ASTNode[0]), "]"));
}
return new G.MapLiteral(randomId(), fmt, Markers.EMPTY, entries, typeMapping.type(map.getType()));
}));
}

@Override
Expand Down Expand Up @@ -2535,26 +2540,30 @@ private int sourceLengthOfString(ConstantExpression expr) {
MethodCallExpression expr = (MethodCallExpression) node;
int saveCursor = cursor;
whitespace();
int childBegin = cursor;
if (expr.getObjectExpression().getLineNumber() != expr.getLineNumber()) {
for (int i = 0; i < (expr.getObjectExpression().getLineNumber() - expr.getLineNumber()) - 1; i++) {
childBegin = source.indexOf('\n', childBegin);
}
childBegin += expr.getObjectExpression().getColumnNumber();
} else {
childBegin += expr.getObjectExpression().getColumnNumber() - expr.getColumnNumber();
}
int count = 0;
for (int i = cursor; i < childBegin; i++) {
if (source.charAt(i) == '(') {
count++;
}
}
int count = determineParenthesisLevel(expr);
cursor = saveCursor;
return count;
}
return null;
}

private int determineParenthesisLevel(MethodCallExpression expr) {
int objectExpressionBeginCursor = cursor;
if (expr.getObjectExpression().getLineNumber() > expr.getLineNumber()) {
for (int i = 0; i < (expr.getObjectExpression().getLineNumber() - expr.getLineNumber()); i++) {
objectExpressionBeginCursor = source.indexOf('\n', objectExpressionBeginCursor);
}
objectExpressionBeginCursor += expr.getObjectExpression().getColumnNumber();
} else {
return null;
objectExpressionBeginCursor += expr.getObjectExpression().getColumnNumber() - expr.getColumnNumber();
}
int count = 0;
for (int i = cursor; i < objectExpressionBeginCursor; i++) {
if (source.charAt(i) == '(') {
count++;
}
}
return count;
}

private int getDelimiterLength() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
class CastTest implements RewriteTest {

@Test
void javaStyleCastNew() {
void cast() {
rewriteRun(
groovy(
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ void insideFourParenthesesAndEnters() {
rewriteRun(
groovy(
"""
((((
def a = ((((
something(a)
))))
"""
Expand Down

0 comments on commit 5314c93

Please sign in to comment.