Skip to content

Commit

Permalink
Add ClosingParenthese marker to add extra parentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
jevanlingen committed Nov 29, 2024
1 parent 3fd9cf0 commit fe98f02
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1630,27 +1630,20 @@ public void visitMapExpression(MapExpression map) {
queue.add(new G.MapLiteral(randomId(), prefix, Markers.EMPTY, entries, typeMapping.type(map.getType())));
}

// zero or more ")" AND "?." or "*." or "." AND <rest>
private final Pattern MATCH_INVOKE = Pattern.compile("^(\\)*)(\\*?\\??\\.).*", Pattern.DOTALL);

@Override
public void visitMethodCallExpression(MethodCallExpression call) {
queue.add(insideParentheses(call, fmt -> {
Markers markers = Markers.EMPTY;

ImplicitDot implicitDot = null;
JRightPadded<Expression> select = null;
if (!call.isImplicitThis()) {
System.out.println("aaaa");
Expression selectExpr = visit(call.getObjectExpression());
int saveCursor = cursor;
Space afterSelect = whitespace();
Matcher matcher = MATCH_INVOKE.matcher(source.substring(cursor));
if (matcher.matches()) {
if (source.charAt(cursor) == '.' || source.charAt(cursor) == '?' || source.charAt(cursor) == '*') {
cursor = saveCursor;
afterSelect = sourceBefore(matcher.group(1) + matcher.group(2));
/*if (!matcher.group(1).isEmpty()) {
afterSelect.withWhitespace(matcher.group(1) + afterSelect.getWhitespace());
}*/
afterSelect = sourceBefore(call.isSpreadSafe() ? "*." : call.isSafe() ? "?." : ".");
} else {
implicitDot = new ImplicitDot(randomId());
}
Expand Down Expand Up @@ -1691,7 +1684,6 @@ public void visitMethodCallExpression(MethodCallExpression call) {
name = name.withMarkers(name.getMarkers().add(implicitDot));
}
// Method invocations may have type information that can enrich the type information of its parameters
Markers markers = Markers.EMPTY;
if (call.getArguments() instanceof ArgumentListExpression) {
ArgumentListExpression args = (ArgumentListExpression) call.getArguments();
if (call.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE) != null) {
Expand Down Expand Up @@ -1764,6 +1756,16 @@ public void visitMethodCallExpression(MethodCallExpression call) {
} else {
methodType = typeMapping.methodType(methodNode);
}

if (source.charAt(cursor) == ')') {
int closingParenthese = 0;
while (source.charAt(cursor) == ')') {
closingParenthese++;
cursor++;
}
markers = markers.add(new ClosingParenthese(randomId(), closingParenthese));
}

return new J.MethodInvocation(randomId(), fmt, markers,
select, null, name, args, methodType);
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.openrewrite.groovy.tree.GContainer;
import org.openrewrite.groovy.tree.GRightPadded;
import org.openrewrite.groovy.tree.GSpace;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.java.JavaPrinter;
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.Marker;
Expand Down Expand Up @@ -380,6 +381,12 @@ public J visitMethodInvocation(J.MethodInvocation method, PrintOutputCapture<P>
}
}

if (method.getMarkers().findFirst(ClosingParenthese.class).isPresent()) {
System.out.println(method);
System.out.println("PRESENT");
p.append(StringUtils.repeat(")", method.getMarkers().findFirst(ClosingParenthese.class).get().getAmount()));
}

afterSyntax(method, p);
return method;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2021 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.groovy.marker;

import lombok.Value;
import lombok.With;
import org.openrewrite.marker.Marker;

import java.util.UUID;

@Value
@With
public class ClosingParenthese implements Marker {
UUID id;
int amount;
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ void extraParenthesesAroundMethodInvocationSelects() {
rewriteRun(
groovy(
"""
def foo(Map map) {
map.containsKey("foo") && (map.get("foo")).equals("bar")
def foo(Map someMap) {
(someMap.get("bar")).equals("baz")
}
"""
)
Expand Down

0 comments on commit fe98f02

Please sign in to comment.