Skip to content

Commit

Permalink
refactor: DefaultJavaPrettyPrinter better uses OperatorHelper (#1553)
Browse files Browse the repository at this point in the history
  • Loading branch information
pvojtechovsky authored and monperrus committed Sep 22, 2017
1 parent ec1cb94 commit 5dbb9b6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 48 deletions.
29 changes: 20 additions & 9 deletions src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import spoon.reflect.code.CtVariableRead;
import spoon.reflect.code.CtVariableWrite;
import spoon.reflect.code.CtWhile;
import spoon.reflect.code.UnaryOperatorKind;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.declaration.CtAnnotation;
Expand Down Expand Up @@ -478,7 +479,9 @@ public <T, A extends T> void visitCtAssignment(CtAssignment<T, A> assignement) {
public <T> void visitCtBinaryOperator(CtBinaryOperator<T> operator) {
enterCtExpression(operator);
scan(operator.getLeftHandOperand());
printer.write(" ").writeOperator(operator.getKind()).write(" ");
printer.write(" ");
printer.write(OperatorHelper.getOperatorText(operator.getKind()));
printer.write(" ");
scan(operator.getRightHandOperand());
exitCtExpression(operator);
}
Expand Down Expand Up @@ -557,11 +560,11 @@ public void visitCtCatch(CtCatch catchBlock) {
printer.write(" catch (");
CtCatchVariable<? extends Throwable> parameter = catchBlock.getParameter();
if (parameter.getMultiTypes().size() > 0) {
try (ListPrinter lp = printer.createListPrinter(null, " | ", null)) {
for (int i = 0; i < parameter.getMultiTypes().size(); i++) {
CtTypeReference<?> type = parameter.getMultiTypes().get(i);
scan(type);
if (i < parameter.getMultiTypes().size() - 1) {
printer.write(" | ");
lp.printSeparatorIfAppropriate();
CtTypeReference<?> type = parameter.getMultiTypes().get(i);
scan(type);
}
}
printer.write(" " + parameter.getSimpleName());
Expand Down Expand Up @@ -1348,14 +1351,17 @@ private <T> boolean hasDeclaringTypeWithGenerics(CtTypeReference<T> reference) {
public <T> void visitCtLambda(CtLambda<T> lambda) {
enterCtExpression(lambda);

try (ListPrinter lp = printer.createListPrinter("(", ",", ") -> ")) {
try (ListPrinter lp = printer.createListPrinter("(", ",", ")")) {
if (lambda.getParameters().size() > 0) {
for (CtParameter<?> parameter : lambda.getParameters()) {
lp.printSeparatorIfAppropriate();
scan(parameter);
}
}
}
printer.write(' ');
printer.write("->");
printer.write(' ');

if (lambda.getBody() != null) {
scan(lambda.getBody());
Expand Down Expand Up @@ -1384,7 +1390,7 @@ public <T, A extends T> void visitCtOperatorAssignment(CtOperatorAssignment<T, A
enterCtExpression(assignment);
scan(assignment.getAssigned());
printer.write(" ");
printer.writeOperator(assignment.getKind());
printer.write(OperatorHelper.getOperatorText(assignment.getKind()));
printer.write("= ");
scan(assignment.getAssignment());
exitCtExpression(assignment);
Expand Down Expand Up @@ -1659,9 +1665,14 @@ private void visitCtTypeReference(CtTypeReference<?> ref, boolean withGenerics)
public <T> void visitCtUnaryOperator(CtUnaryOperator<T> operator) {
enterCtStatement(operator);
enterCtExpression(operator);
printer.preWriteUnaryOperator(operator.getKind());
UnaryOperatorKind op = operator.getKind();
if (OperatorHelper.isPrefixOperator(op)) {
printer.write(OperatorHelper.getOperatorText(op));
}
scan(operator.getOperand());
printer.postWriteUnaryOperator(operator.getKind());
if (OperatorHelper.isSufixOperator(op)) {
printer.write(OperatorHelper.getOperatorText(op));
}
exitCtExpression(operator);
}

Expand Down
40 changes: 1 addition & 39 deletions src/main/java/spoon/reflect/visitor/PrinterHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package spoon.reflect.visitor;

import spoon.compiler.Environment;
import spoon.reflect.code.BinaryOperatorKind;
import spoon.reflect.code.UnaryOperatorKind;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.declaration.CtElement;

Expand Down Expand Up @@ -226,42 +224,6 @@ public void putLineNumberMapping(int valueLine) {
lineNumberMapping.put(this.line, valueLine);
}

/**
* Write a pre unary operator.
*/
public void preWriteUnaryOperator(UnaryOperatorKind o) {
if (OperatorHelper.isPrefixOperator(o)) {
write(OperatorHelper.getOperatorText(o));
}
}

/**
* Write a post unary operator.
*/
public void postWriteUnaryOperator(UnaryOperatorKind o) {
if (OperatorHelper.isSufixOperator(o)) {
write(OperatorHelper.getOperatorText(o));
}
}

/**
* Writes a binary operator.
*/
public PrinterHelper writeOperator(BinaryOperatorKind o) {
write(OperatorHelper.getOperatorText(o));
return this;
}

public void writeCharLiteral(Character c, boolean mayContainsSpecialCharacter) {
StringBuilder sb = new StringBuilder(10);
LiteralHelper.appendCharLiteral(sb, c, mayContainsSpecialCharacter);
write(sb.toString());
}

public void writeStringLiteral(String value, boolean mayContainsSpecialCharacter) {
write(LiteralHelper.getStringLiteral(value, mayContainsSpecialCharacter));
}

public Map<Integer, Integer> getLineNumberMapping() {
return Collections.unmodifiableMap(lineNumberMapping);
}
Expand Down Expand Up @@ -292,7 +254,7 @@ public boolean hasNewContent() {
* @return the {@link ListPrinter} whose {@link ListPrinter#printSeparatorIfAppropriate()} has to be called
* before printing of each item.
*/
public ListPrinter createListPrinter(String start, String next, String end) {
ListPrinter createListPrinter(String start, String next, String end) {
return new ListPrinter(this, start, next, end);
}

Expand Down

0 comments on commit 5dbb9b6

Please sign in to comment.