• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007-2010 Júlio 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 com.github.javaparser.ast.visitor.VoidVisitor;
25 
26 import java.util.function.Function;
27 
28 import static com.github.javaparser.printer.PrettyPrinterConfiguration.IndentType.SPACES;
29 import static com.github.javaparser.utils.Utils.EOL;
30 import static com.github.javaparser.utils.Utils.assertNonNegative;
31 import static com.github.javaparser.utils.Utils.assertNotNull;
32 import static com.github.javaparser.utils.Utils.assertPositive;
33 
34 /**
35  * Configuration options for the {@link PrettyPrinter}.
36  */
37 public class PrettyPrinterConfiguration {
38     public enum IndentType {
39         /**
40          * Indent with spaces.
41          */
42         SPACES,
43 
44         /**
45          * Indent with tabs as far as possible.
46          * For proper aligning, the tab width is necessary and by default 4.
47          */
48         TABS,
49 
50         /**
51          * Indent with tabs but align with spaces when wrapping and aligning
52          * method call chains and method call parameters.
53          *
54          * <p/><i>Example result:</i>
55          * <pre>
56          * class Foo {
57          *
58          * \tvoid bar() {
59          * \t\tfoo().bar()
60          * \t\t......baz(() -> {
61          * \t\t..........\tboo().baa()
62          * \t\t..........\t......bee(a,
63          * \t\t..........\t..........b,
64          * \t\t..........\t..........c);
65          * \t\t..........})
66          * \t\t......bam();
67          * \t}
68          * }
69          * </pre>
70          */
71         TABS_WITH_SPACE_ALIGN
72     }
73 
74     public static final int DEFAULT_MAX_ENUM_CONSTANTS_TO_ALIGN_HORIZONTALLY = 5;
75 
76     private boolean orderImports = false;
77     private boolean printComments = true;
78     private boolean printJavadoc = true;
79     private boolean columnAlignParameters = false;
80     private boolean columnAlignFirstMethodChain = false;
81     private IndentType indentType = SPACES;
82     private int tabWidth = 4;
83     private int indentSize = 4;
84     private String endOfLineCharacter = EOL;
85     private Function<PrettyPrinterConfiguration, VoidVisitor<Void>> visitorFactory = PrettyPrintVisitor::new;
86     private int maxEnumConstantsToAlignHorizontally = DEFAULT_MAX_ENUM_CONSTANTS_TO_ALIGN_HORIZONTALLY;
87 
88     /**
89      * @return the string that will be used to indent.
90      */
getIndent()91     public String getIndent() {
92         StringBuilder indentString = new StringBuilder();
93         char indentChar;
94         switch (indentType) {
95             case SPACES:
96                 indentChar = ' ';
97                 break;
98 
99             case TABS:
100             case TABS_WITH_SPACE_ALIGN:
101                 indentChar = '\t';
102                 break;
103 
104             default:
105                 throw new AssertionError("Unhandled indent type");
106         }
107         for (int i = 0; i < indentSize; i++) {
108             indentString.append(indentChar);
109         }
110         return indentString.toString();
111     }
112 
getIndentSize()113     public int getIndentSize() {
114         return indentSize;
115     }
116 
117     /**
118      * Set the size of the indent in characters.
119      */
setIndentSize(int indentSize)120     public PrettyPrinterConfiguration setIndentSize(int indentSize) {
121         this.indentSize = assertNonNegative(indentSize);
122         return this;
123     }
124 
125     /**
126      * Get the type of indent to produce.
127      */
getIndentType()128     public IndentType getIndentType() {
129         return indentType;
130     }
131 
132     /**
133      * Set the type of indent to produce.
134      */
setIndentType(IndentType indentType)135     public PrettyPrinterConfiguration setIndentType(IndentType indentType) {
136         this.indentType = assertNotNull(indentType);
137         return this;
138     }
139 
140     /**
141      * Get the tab width for pretty aligning.
142      */
getTabWidth()143     public int getTabWidth() {
144         return tabWidth;
145     }
146 
147     /**
148      * Set the tab width for pretty aligning.
149      */
setTabWidth(int tabWidth)150     public PrettyPrinterConfiguration setTabWidth(int tabWidth) {
151         this.tabWidth = assertPositive(tabWidth);
152         return this;
153     }
154 
isOrderImports()155     public boolean isOrderImports() {
156         return orderImports;
157     }
158 
isPrintComments()159     public boolean isPrintComments() {
160         return printComments;
161     }
162 
isIgnoreComments()163     public boolean isIgnoreComments() {
164         return !printComments;
165     }
166 
isPrintJavadoc()167     public boolean isPrintJavadoc() {
168         return printJavadoc;
169     }
170 
isColumnAlignParameters()171     public boolean isColumnAlignParameters() {
172         return columnAlignParameters;
173     }
174 
isColumnAlignFirstMethodChain()175     public boolean isColumnAlignFirstMethodChain() {
176         return columnAlignFirstMethodChain;
177     }
178 
179     /**
180      * When true, all comments will be printed, unless printJavadoc is false, then only line and block comments will be
181      * printed.
182      */
setPrintComments(boolean printComments)183     public PrettyPrinterConfiguration setPrintComments(boolean printComments) {
184         this.printComments = printComments;
185         return this;
186     }
187 
188     /**
189      * When true, Javadoc will be printed.
190      */
setPrintJavadoc(boolean printJavadoc)191     public PrettyPrinterConfiguration setPrintJavadoc(boolean printJavadoc) {
192         this.printJavadoc = printJavadoc;
193         return this;
194     }
195 
setColumnAlignParameters(boolean columnAlignParameters)196     public PrettyPrinterConfiguration setColumnAlignParameters(boolean columnAlignParameters) {
197         this.columnAlignParameters = columnAlignParameters;
198         return this;
199     }
200 
setColumnAlignFirstMethodChain(boolean columnAlignFirstMethodChain)201     public PrettyPrinterConfiguration setColumnAlignFirstMethodChain(boolean columnAlignFirstMethodChain) {
202         this.columnAlignFirstMethodChain = columnAlignFirstMethodChain;
203         return this;
204     }
205 
getVisitorFactory()206     public Function<PrettyPrinterConfiguration, VoidVisitor<Void>> getVisitorFactory() {
207         return visitorFactory;
208     }
209 
210     /**
211      * Set the factory that creates the PrettyPrintVisitor. By changing this you can make the PrettyPrinter use a custom
212      * PrettyPrinterVisitor.
213      */
setVisitorFactory(Function<PrettyPrinterConfiguration, VoidVisitor<Void>> visitorFactory)214     public PrettyPrinterConfiguration setVisitorFactory(Function<PrettyPrinterConfiguration, VoidVisitor<Void>> visitorFactory) {
215         this.visitorFactory = assertNotNull(visitorFactory);
216         return this;
217     }
218 
getEndOfLineCharacter()219     public String getEndOfLineCharacter() {
220         return endOfLineCharacter;
221     }
222 
223     /**
224      * Set the character to append when a line should end. Example values: "\n", "\r\n", "".
225      */
setEndOfLineCharacter(String endOfLineCharacter)226     public PrettyPrinterConfiguration setEndOfLineCharacter(String endOfLineCharacter) {
227         this.endOfLineCharacter = assertNotNull(endOfLineCharacter);
228         return this;
229     }
230 
231     /**
232      * When true, orders imports by alphabetically.
233      */
setOrderImports(boolean orderImports)234     public PrettyPrinterConfiguration setOrderImports(boolean orderImports) {
235         this.orderImports = orderImports;
236         return this;
237     }
238 
getMaxEnumConstantsToAlignHorizontally()239     public int getMaxEnumConstantsToAlignHorizontally() {
240         return maxEnumConstantsToAlignHorizontally;
241     }
242 
243     /**
244      * By default enum constants get aligned like this:
245      * <pre>
246      *     enum X {
247      *        A, B, C, D
248      *     }
249      * </pre>
250      * until the amount of constants passes this value (5 by default).
251      * Then they get aligned like this:
252      * <pre>
253      *     enum X {
254      *        A,
255      *        B,
256      *        C,
257      *        D,
258      *        E,
259      *        F,
260      *        G
261      *     }
262      * </pre>
263      * Set it to a large number to always align horizontally.
264      * Set it to 1 or less to always align vertically.
265      */
setMaxEnumConstantsToAlignHorizontally(int maxEnumConstantsToAlignHorizontally)266     public PrettyPrinterConfiguration setMaxEnumConstantsToAlignHorizontally(int maxEnumConstantsToAlignHorizontally) {
267         this.maxEnumConstantsToAlignHorizontally = maxEnumConstantsToAlignHorizontally;
268         return this;
269     }
270 }
271