Skip to content

Commit

Permalink
Return String instead of passing StringBuilder
Browse files Browse the repository at this point in the history
Include subpart of remainder to give an indication where the issue occurred
  • Loading branch information
Laurens-W committed Dec 10, 2024
1 parent bbdd436 commit 3786a05
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2085,7 +2085,7 @@ public TypeTree visitVariableExpressionType(VariableExpression expression) {
StringBuilder keyword = new StringBuilder();

if (expression.isDynamicTyped() || source.charAt(cursor) == ',') {
getKeyword(expression.getName(), keyword);
keyword.append(getKeyword(expression.getName()));
} else {
keyword.append(expression.getOriginType().getUnresolvedName());
cursor += keyword.length();
Expand Down Expand Up @@ -2171,31 +2171,33 @@ private <T> T pollQueue() {
}
}

private void getKeyword(String variableName, StringBuilder keyword) {
private String getKeyword(String variableName) {
String retVal;
String leftover = source.substring(cursor);
if (leftover.startsWith("final")) {
keyword.append("final");
retVal = "final";
cursor += 5;
} else if (leftover.startsWith("var")) {
keyword.append("var");
retVal = "var";
cursor += 3;
} else if (leftover.startsWith("def")) {
keyword.append("def");
retVal = "def";
cursor += 3;
} else if (leftover.startsWith(",")) {
keyword.append(",");
retVal = ",";
cursor++;
} else {
throw new IllegalStateException("Ran into unknown or unimplemented identifier");
throw new IllegalStateException("Ran into unknown or unimplemented identifier at " + leftover.substring(0, 10));
}
int saveCursor = cursor;
Space space = whitespace();
if (source.substring(cursor).startsWith(variableName)) {
cursor = saveCursor;
} else {
keyword.append(space.getWhitespace());
getKeyword(variableName, keyword);
retVal += space.getWhitespace();
retVal += getKeyword(variableName);
}
return retVal;
}

// handle the obscure case where there are empty parens ahead of a closure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@
package org.openrewrite.groovy.tree;

import org.junit.jupiter.api.Test;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.Parser;
import org.openrewrite.groovy.GroovyParsingException;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.SourceSpec;

import java.nio.file.Paths;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.openrewrite.groovy.Assertions.groovy;
import static org.openrewrite.internal.StringUtils.trimIndentPreserveCRLF;

@SuppressWarnings({"GroovyUnusedAssignment", "GrUnnecessarySemicolon"})
class AssignmentTest implements RewriteTest {
Expand Down Expand Up @@ -132,10 +141,10 @@ void multipleAssignmentsAtOneLine() {
rewriteRun(
groovy(
"""
def startItem = '| ', endItem = ' |'
def repeatLength = startItem.length() + output.length() + endItem.length()
println("\\n" + ("-" * repeatLength) + "\\n" + startItem + output + endItem + "\\n" + ("-" * repeatLength))
"""
def startItem = '| ', endItem = ' |'
def repeatLength = startItem.length() + output.length() + endItem.length()
println("\\n" + ("-" * repeatLength) + "\\n" + startItem + output + endItem + "\\n" + ("-" * repeatLength))
"""
)
);
}
Expand All @@ -145,8 +154,8 @@ void multipleAssignmentsAtOneLineSimple() {
rewriteRun(
groovy(
"""
def a = '1', b = '2'
"""
def a = '1', b = '2'
"""
)
);
}
Expand All @@ -156,9 +165,9 @@ void multipleAssignmentsAtMultipleLineDynamicType() {
rewriteRun(
groovy(
"""
def a = '1' ,
b = '2'
"""
def a = '1' ,
b = '2'
"""
)
);
}
Expand All @@ -168,9 +177,9 @@ void multipleAssignmentsAtMultipleLineStaticType() {
rewriteRun(
groovy(
"""
String a = '1' ,
b = '2'
"""
String a = '1' ,
b = '2'
"""
)
);
}
Expand Down

0 comments on commit 3786a05

Please sign in to comment.