Skip to content

Commit

Permalink
Make toString's printer configuration accessible
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed Feb 23, 2019
1 parent cc524c2 commit 621f1f7
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 15 deletions.
21 changes: 16 additions & 5 deletions javaparser-core/src/main/java/com/github/javaparser/ast/Node.java
Expand Up @@ -143,7 +143,7 @@ public enum Parsedness {
return 0;
};

private static final PrettyPrinter toStringPrinter = new PrettyPrinter(new PrettyPrinterConfiguration());
private static PrettyPrinterConfiguration toStringPrettyPrinterConfiguration = new PrettyPrinterConfiguration();

protected static final PrettyPrinterConfiguration prettyPrinterNoCommentsConfiguration = new PrettyPrinterConfiguration().setPrintComments(false);

Expand Down Expand Up @@ -275,15 +275,18 @@ public final Node setBlockComment(String comment) {
}

/**
* Return the String representation of this node.
*
* @return the String representation of this node
* @return pretty printed source code for this node and its children.
* Formatting can be configured with Node.setToStringPrettyPrinterConfiguration.
*/
@Override
public final String toString() {
return toStringPrinter.print(this);
return new PrettyPrinter(toStringPrettyPrinterConfiguration).print(this);
}

/**
* @return pretty printed source code for this node and its children.
* Formatting can be configured with parameter prettyPrinterConfiguration.
*/
public final String toString(PrettyPrinterConfiguration prettyPrinterConfiguration) {
return new PrettyPrinter(prettyPrinterConfiguration).print(this);
}
Expand Down Expand Up @@ -652,6 +655,14 @@ public Node setParsed(Parsedness parsed) {
return this;
}

public static PrettyPrinterConfiguration getToStringPrettyPrinterConfiguration() {
return toStringPrettyPrinterConfiguration;
}

public static void setToStringPrettyPrinterConfiguration(PrettyPrinterConfiguration toStringPrettyPrinterConfiguration) {
Node.toStringPrettyPrinterConfiguration = toStringPrettyPrinterConfiguration;
}

@Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator")
public boolean replace(Node node, Node replacementNode) {
if (node == null)
Expand Down
Expand Up @@ -218,6 +218,7 @@ public SwitchEntry setType(final Type type) {
}

@Override
@Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator")
public boolean replace(Node node, Node replacementNode) {
if (node == null)
return false;
Expand Down
Expand Up @@ -949,7 +949,7 @@ public static void genericPrettyPrint(Node node, SourcePrinter printer) {
public static String genericPrettyPrint(Node node) {
SourcePrinter sourcePrinter = new SourcePrinter();
forClass(node.getClass()).prettyPrint(node, sourcePrinter);
return sourcePrinter.getSource();
return sourcePrinter.toString();
}

public static CsmElement forClass(Class<? extends Node> nodeClazz) {
Expand Down
Expand Up @@ -58,8 +58,17 @@ public PrettyPrintVisitor(PrettyPrinterConfiguration prettyPrinterConfiguration)
printer = new SourcePrinter(configuration);
}

/**
* @deprecated use toString()
*/
@Deprecated
public String getSource() {
return printer.getSource();
return printer.toString();
}

@Override
public String toString() {
return printer.toString();
}

private void printModifiers(final NodeList<Modifier> modifiers) {
Expand Down
Expand Up @@ -22,6 +22,7 @@
package com.github.javaparser.printer;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.visitor.VoidVisitor;

/**
* Pretty printer for AST nodes.
Expand All @@ -38,8 +39,8 @@ public PrettyPrinter(PrettyPrinterConfiguration configuration) {
}

public String print(Node node) {
final PrettyPrintVisitor visitor = configuration.getVisitorFactory().apply(configuration);
final VoidVisitor<Void> visitor = configuration.getVisitorFactory().apply(configuration);
node.accept(visitor, null);
return visitor.getSource();
return visitor.toString();
}
}
Expand Up @@ -21,10 +21,11 @@

package com.github.javaparser.printer;

import com.github.javaparser.ast.visitor.VoidVisitor;

import java.util.function.Function;

import static com.github.javaparser.printer.PrettyPrinterConfiguration.IndentType.SPACES;
import static com.github.javaparser.printer.PrettyPrinterConfiguration.IndentType.TABS;
import static com.github.javaparser.utils.Utils.EOL;
import static com.github.javaparser.utils.Utils.assertNonNegative;
import static com.github.javaparser.utils.Utils.assertNotNull;
Expand Down Expand Up @@ -81,7 +82,7 @@ public enum IndentType {
private int tabWidth = 4;
private int indentSize = 4;
private String endOfLineCharacter = EOL;
private Function<PrettyPrinterConfiguration, PrettyPrintVisitor> visitorFactory = PrettyPrintVisitor::new;
private Function<PrettyPrinterConfiguration, VoidVisitor<Void>> visitorFactory = PrettyPrintVisitor::new;
private int maxEnumConstantsToAlignHorizontally = DEFAULT_MAX_ENUM_CONSTANTS_TO_ALIGN_HORIZONTALLY;

/**
Expand Down Expand Up @@ -202,15 +203,15 @@ public PrettyPrinterConfiguration setColumnAlignFirstMethodChain(boolean columnA
return this;
}

public Function<PrettyPrinterConfiguration, PrettyPrintVisitor> getVisitorFactory() {
public Function<PrettyPrinterConfiguration, VoidVisitor<Void>> getVisitorFactory() {
return visitorFactory;
}

/**
* Set the factory that creates the PrettyPrintVisitor. By changing this you can make the PrettyPrinter use a custom
* PrettyPrinterVisitor.
*/
public PrettyPrinterConfiguration setVisitorFactory(Function<PrettyPrinterConfiguration, PrettyPrintVisitor> visitorFactory) {
public PrettyPrinterConfiguration setVisitorFactory(Function<PrettyPrinterConfiguration, VoidVisitor<Void>> visitorFactory) {
this.visitorFactory = assertNotNull(visitorFactory);
return this;
}
Expand Down
Expand Up @@ -30,6 +30,9 @@

import static com.github.javaparser.Position.*;

/**
* A support class for code that outputs formatted source code.
*/
public class SourcePrinter {
private final String endOfLineCharacter;
private final String indentation;
Expand Down Expand Up @@ -216,17 +219,19 @@ public Position getCursor() {

/**
* @return the currently printed source code.
* @deprecated use toString()
*/
@Deprecated
public String getSource() {
return buf.toString();
return toString();
}

/**
* @return the currently printed source code.
*/
@Override
public String toString() {
return getSource();
return buf.toString();
}

/**
Expand Down

0 comments on commit 621f1f7

Please sign in to comment.