Home | History | Annotate | Download | only in printer
      1 /*
      2  * Copyright (C) 2007-2010 Jlio Vilmar Gesser.
      3  * Copyright (C) 2011, 2013-2016 The JavaParser Team.
      4  *
      5  * This file is part of JavaParser.
      6  *
      7  * JavaParser can be used either under the terms of
      8  * a) the GNU Lesser General Public License as published by
      9  *     the Free Software Foundation, either version 3 of the License, or
     10  *     (at your option) any later version.
     11  * b) the terms of the Apache License
     12  *
     13  * You should have received a copy of both licenses in LICENCE.LGPL and
     14  * LICENCE.APACHE. Please refer to those files for details.
     15  *
     16  * JavaParser is distributed in the hope that it will be useful,
     17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     19  * GNU Lesser General Public License for more details.
     20  */
     21 
     22 package com.github.javaparser.printer;
     23 
     24 import java.util.function.Function;
     25 
     26 import static com.github.javaparser.utils.Utils.EOL;
     27 import static com.github.javaparser.utils.Utils.assertNotNull;
     28 
     29 /**
     30  * Configuration options for the {@link PrettyPrinter}.
     31  */
     32 public class PrettyPrinterConfiguration {
     33     public static final int DEFAULT_MAX_ENUM_CONSTANTS_TO_ALIGN_HORIZONTALLY = 5;
     34 
     35     private boolean orderImports = false;
     36     private boolean printComments = true;
     37     private boolean printJavadoc = true;
     38     private boolean columnAlignParameters = false;
     39     private boolean columnAlignFirstMethodChain = false;
     40     private String indent = "    ";
     41     private String endOfLineCharacter = EOL;
     42     private Function<PrettyPrinterConfiguration, PrettyPrintVisitor> visitorFactory = PrettyPrintVisitor::new;
     43     private int maxEnumConstantsToAlignHorizontally = DEFAULT_MAX_ENUM_CONSTANTS_TO_ALIGN_HORIZONTALLY;
     44 
     45     public String getIndent() {
     46         return indent;
     47     }
     48 
     49     /**
     50      * Set the string to use for indenting. For example: "\t", "    ", "".
     51      */
     52     public PrettyPrinterConfiguration setIndent(String indent) {
     53         this.indent = assertNotNull(indent);
     54         return this;
     55     }
     56 
     57     public boolean isOrderImports() {
     58         return orderImports;
     59     }
     60 
     61     /**
     62      * @deprecated this is always on.
     63      */
     64     @Deprecated
     65     public boolean isNormalizeEolInComment() {
     66         return true;
     67     }
     68 
     69     /**
     70      * @deprecated this is always on.
     71      */
     72     @Deprecated
     73     public PrettyPrinterConfiguration setNormalizeEolInComment(boolean normalizeEolInComment) {
     74         return this;
     75     }
     76 
     77     public boolean isPrintComments() {
     78         return printComments;
     79     }
     80 
     81     public boolean isIgnoreComments() {
     82         return !printComments;
     83     }
     84 
     85     public boolean isPrintJavadoc() {
     86         return printJavadoc;
     87     }
     88 
     89     public boolean isColumnAlignParameters() {
     90         return columnAlignParameters;
     91     }
     92 
     93     public boolean isColumnAlignFirstMethodChain() {
     94         return columnAlignFirstMethodChain;
     95     }
     96 
     97     /**
     98      * When true, all comments will be printed, unless printJavadoc is false, then only line and block comments will be
     99      * printed.
    100      */
    101     public PrettyPrinterConfiguration setPrintComments(boolean printComments) {
    102         this.printComments = printComments;
    103         return this;
    104     }
    105 
    106     /**
    107      * When true, Javadoc will be printed.
    108      */
    109     public PrettyPrinterConfiguration setPrintJavadoc(boolean printJavadoc) {
    110         this.printJavadoc = printJavadoc;
    111         return this;
    112     }
    113 
    114     public PrettyPrinterConfiguration setColumnAlignParameters(boolean columnAlignParameters) {
    115         this.columnAlignParameters = columnAlignParameters;
    116         return this;
    117     }
    118 
    119     public PrettyPrinterConfiguration setColumnAlignFirstMethodChain(boolean columnAlignFirstMethodChain) {
    120         this.columnAlignFirstMethodChain = columnAlignFirstMethodChain;
    121         return this;
    122     }
    123 
    124     public Function<PrettyPrinterConfiguration, PrettyPrintVisitor> getVisitorFactory() {
    125         return visitorFactory;
    126     }
    127 
    128     /**
    129      * Set the factory that creates the PrettyPrintVisitor. By changing this you can make the PrettyPrinter use a custom
    130      * PrettyPrinterVisitor.
    131      */
    132     public PrettyPrinterConfiguration setVisitorFactory(Function<PrettyPrinterConfiguration, PrettyPrintVisitor> visitorFactory) {
    133         this.visitorFactory = assertNotNull(visitorFactory);
    134         return this;
    135     }
    136 
    137     public String getEndOfLineCharacter() {
    138         return endOfLineCharacter;
    139     }
    140 
    141     /**
    142      * Set the character to append when a line should end. Example values: "\n", "\r\n", "".
    143      */
    144     public PrettyPrinterConfiguration setEndOfLineCharacter(String endOfLineCharacter) {
    145         this.endOfLineCharacter = assertNotNull(endOfLineCharacter);
    146         return this;
    147     }
    148 
    149     /**
    150      * When true, orders imports by alphabetically.
    151      */
    152     public PrettyPrinterConfiguration setOrderImports(boolean orderImports) {
    153         this.orderImports = orderImports;
    154         return this;
    155     }
    156 
    157     public int getMaxEnumConstantsToAlignHorizontally() {
    158         return maxEnumConstantsToAlignHorizontally;
    159     }
    160 
    161     /**
    162      * By default enum constants get aligned like this:
    163      * <pre>
    164      *     enum X {
    165      *        A, B, C, D
    166      *     }
    167      * </pre>
    168      * until the amount of constants passes this value (5 by default).
    169      * Then they get aligned like this:
    170      * <pre>
    171      *     enum X {
    172      *        A,
    173      *        B,
    174      *        C,
    175      *        D,
    176      *        E,
    177      *        F,
    178      *        G
    179      *     }
    180      * </pre>
    181      * Set it to a large number to always align horizontally.
    182      * Set it to 1 or less to always align vertically.
    183      */
    184     public PrettyPrinterConfiguration setMaxEnumConstantsToAlignHorizontally(int maxEnumConstantsToAlignHorizontally) {
    185         this.maxEnumConstantsToAlignHorizontally = maxEnumConstantsToAlignHorizontally;
    186         return this;
    187     }
    188 }
    189