JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/printer/PrettyPrinterConfiguration.java
1/*
2 * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser.
3 * Copyright (C) 2011, 2013-2020 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
22package com.github.javaparser.printer;
23
24import com.github.javaparser.ast.visitor.VoidVisitor;
25
26import java.util.function.Function;
27
28import static com.github.javaparser.printer.PrettyPrinterConfiguration.IndentType.SPACES;
29import static com.github.javaparser.utils.Utils.SYSTEM_EOL;
30import static com.github.javaparser.utils.Utils.assertNonNegative;
31import static com.github.javaparser.utils.Utils.assertNotNull;
32import static com.github.javaparser.utils.Utils.assertPositive;
33
34/**
35 * Configuration options for the {@link PrettyPrinter}.
36 */
37public 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(() -*&gt; {
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 spaceAroundOperators = true;
80    private boolean columnAlignParameters = false;
81    private boolean columnAlignFirstMethodChain = false;
82    /**
83     * Indent the case when it is true, don't if false
84     * switch(x) {            switch(x) {
85     *    case 1:             case 1:
86     *        return y;           return y;
87     *    case 2:             case 2:
88     *        return z;           return x;
89     *}                       }
90     */
91    private boolean indentCaseInSwitch = true;
92    private IndentType indentType = SPACES;
93    private int tabWidth = 4;
94    private int indentSize = 4;
95    private String endOfLineCharacter = SYSTEM_EOL;
96    private Function<PrettyPrinterConfigurationVoidVisitor<Void>> visitorFactory = PrettyPrintVisitor::new;
97    private int maxEnumConstantsToAlignHorizontally = DEFAULT_MAX_ENUM_CONSTANTS_TO_ALIGN_HORIZONTALLY;
98
99    /**
100     * @return the string that will be used to indent.
101     */
102    public String getIndent() {
103        StringBuilder indentString = new StringBuilder();
104        char indentChar;
105        switch (indentType) {
106            case SPACES:
107                indentChar = ' ';
108                break;
109
110            case TABS:
111            case TABS_WITH_SPACE_ALIGN:
112                indentChar = '\t';
113                break;
114
115            default:
116                throw new AssertionError("Unhandled indent type");
117        }
118        for (int i = 0i < indentSizei++) {
119            indentString.append(indentChar);
120        }
121        return indentString.toString();
122    }
123
124    public int getIndentSize() {
125        return indentSize;
126    }
127
128    /**
129     * Set the size of the indent in characters.
130     */
131    public PrettyPrinterConfiguration setIndentSize(int indentSize) {
132        this.indentSize = assertNonNegative(indentSize);
133        return this;
134    }
135
136    /**
137     * Get the type of indent to produce.
138     */
139    public IndentType getIndentType() {
140        return indentType;
141    }
142
143    /**
144     * Set the type of indent to produce.
145     */
146    public PrettyPrinterConfiguration setIndentType(IndentType indentType) {
147        this.indentType = assertNotNull(indentType);
148        return this;
149    }
150
151
152
153    /**
154     * Get the tab width for pretty aligning.
155     */
156    public int getTabWidth() {
157        return tabWidth;
158    }
159
160    /**
161     * Set the tab width for pretty aligning.
162     */
163    public PrettyPrinterConfiguration setTabWidth(int tabWidth) {
164        this.tabWidth = assertPositive(tabWidth);
165        return this;
166    }
167
168    public boolean isOrderImports() {
169        return orderImports;
170    }
171
172    public boolean isPrintComments() {
173        return printComments;
174    }
175
176    public boolean isIgnoreComments() {
177        return !printComments;
178    }
179    
180    public boolean isSpaceAroundOperators() { return spaceAroundOperators; }
181
182    public boolean isPrintJavadoc() {
183        return printJavadoc;
184    }
185
186    public boolean isColumnAlignParameters() {
187        return columnAlignParameters;
188    }
189
190    public boolean isColumnAlignFirstMethodChain() { return columnAlignFirstMethodChain; }
191
192    public boolean isIndentCaseInSwitch() {
193        return indentCaseInSwitch;
194    }
195
196
197    /**
198     * When true, all comments will be printed, unless printJavadoc is false, then only line and block comments will be
199     * printed.
200     */
201    public PrettyPrinterConfiguration setPrintComments(boolean printComments) {
202        this.printComments = printComments;
203        return this;
204    }
205
206    /**
207     * When true, Javadoc will be printed.
208     */
209    public PrettyPrinterConfiguration setPrintJavadoc(boolean printJavadoc) {
210        this.printJavadoc = printJavadoc;
211        return this;
212    }
213
214    /**
215     * Set if there should be spaces between operators
216     */
217    public PrettyPrinterConfiguration setSpaceAroundOperators(boolean spaceAroundOperators){
218        this.spaceAroundOperators = spaceAroundOperators;
219        return this;
220    }
221
222    public PrettyPrinterConfiguration setColumnAlignParameters(boolean columnAlignParameters) {
223        this.columnAlignParameters = columnAlignParameters;
224        return this;
225    }
226
227    public PrettyPrinterConfiguration setColumnAlignFirstMethodChain(boolean columnAlignFirstMethodChain) {
228        this.columnAlignFirstMethodChain = columnAlignFirstMethodChain;
229        return this;
230    }
231
232    public PrettyPrinterConfiguration setIndentCaseInSwitch(boolean indentInSwitch) {
233        this.indentCaseInSwitch = indentInSwitch;
234        return this;
235    }
236
237    public Function<PrettyPrinterConfigurationVoidVisitor<Void>> getVisitorFactory() {
238        return visitorFactory;
239    }
240
241    /**
242     * Set the factory that creates the PrettyPrintVisitor. By changing this you can make the PrettyPrinter use a custom
243     * PrettyPrinterVisitor.
244     */
245    public PrettyPrinterConfiguration setVisitorFactory(Function<PrettyPrinterConfigurationVoidVisitor<Void>> visitorFactory) {
246        this.visitorFactory = assertNotNull(visitorFactory);
247        return this;
248    }
249
250    public String getEndOfLineCharacter() {
251        return endOfLineCharacter;
252    }
253
254    /**
255     * Set the character to append when a line should end. Example values: "\n", "\r\n", "".
256     */
257    public PrettyPrinterConfiguration setEndOfLineCharacter(String endOfLineCharacter) {
258        this.endOfLineCharacter = assertNotNull(endOfLineCharacter);
259        return this;
260    }
261
262    /**
263     * When true, orders imports by alphabetically.
264     */
265    public PrettyPrinterConfiguration setOrderImports(boolean orderImports) {
266        this.orderImports = orderImports;
267        return this;
268    }
269
270
271
272    public int getMaxEnumConstantsToAlignHorizontally() {
273        return maxEnumConstantsToAlignHorizontally;
274    }
275
276    /**
277     * By default enum constants get aligned like this:
278     * <pre>
279     *     enum X {
280     *        A, B, C, D
281     *     }
282     * </pre>
283     * until the amount of constants passes this value (5 by default).
284     * Then they get aligned like this:
285     * <pre>
286     *     enum X {
287     *        A,
288     *        B,
289     *        C,
290     *        D,
291     *        E,
292     *        F,
293     *        G
294     *     }
295     * </pre>
296     * Set it to a large number to always align horizontally.
297     * Set it to 1 or less to always align vertically.
298     */
299    public PrettyPrinterConfiguration setMaxEnumConstantsToAlignHorizontally(int maxEnumConstantsToAlignHorizontally) {
300        this.maxEnumConstantsToAlignHorizontally = maxEnumConstantsToAlignHorizontally;
301        return this;
302    }
303}
304
MembersX
PrettyPrinterConfiguration:getVisitorFactory
PrettyPrinterConfiguration:setIndentType
PrettyPrinterConfiguration:isSpaceAroundOperators
PrettyPrinterConfiguration:isIndentCaseInSwitch
PrettyPrinterConfiguration:isColumnAlignParameters
PrettyPrinterConfiguration:isPrintComments
PrettyPrinterConfiguration:setColumnAlignFirstMethodChain
PrettyPrinterConfiguration:getIndent:Block:indentString
PrettyPrinterConfiguration:setPrintComments
PrettyPrinterConfiguration:printComments
PrettyPrinterConfiguration:getTabWidth
PrettyPrinterConfiguration:IndentType:SPACES
PrettyPrinterConfiguration:setPrintJavadoc
PrettyPrinterConfiguration:getIndentSize
PrettyPrinterConfiguration:indentSize
PrettyPrinterConfiguration:orderImports
PrettyPrinterConfiguration:setColumnAlignParameters
PrettyPrinterConfiguration:isColumnAlignFirstMethodChain
PrettyPrinterConfiguration:setIndentCaseInSwitch
PrettyPrinterConfiguration:maxEnumConstantsToAlignHorizontally
PrettyPrinterConfiguration:tabWidth
PrettyPrinterConfiguration:isIgnoreComments
PrettyPrinterConfiguration:columnAlignFirstMethodChain
PrettyPrinterConfiguration:setIndentSize
PrettyPrinterConfiguration:isOrderImports
PrettyPrinterConfiguration:getMaxEnumConstantsToAlignHorizontally
PrettyPrinterConfiguration:setEndOfLineCharacter
PrettyPrinterConfiguration:getEndOfLineCharacter
PrettyPrinterConfiguration:getIndent
PrettyPrinterConfiguration:IndentType:TABS_WITH_SPACE_ALIGN
PrettyPrinterConfiguration:endOfLineCharacter
PrettyPrinterConfiguration:DEFAULT_MAX_ENUM_CONSTANTS_TO_ALIGN_HORIZONTALLY
PrettyPrinterConfiguration:printJavadoc
PrettyPrinterConfiguration:setSpaceAroundOperators
PrettyPrinterConfiguration:columnAlignParameters
PrettyPrinterConfiguration:setMaxEnumConstantsToAlignHorizontally
PrettyPrinterConfiguration:IndentType:TABS
PrettyPrinterConfiguration:setTabWidth
PrettyPrinterConfiguration:getIndentType
PrettyPrinterConfiguration:setVisitorFactory
PrettyPrinterConfiguration:spaceAroundOperators
PrettyPrinterConfiguration:indentType
PrettyPrinterConfiguration:setOrderImports
PrettyPrinterConfiguration:visitorFactory
PrettyPrinterConfiguration:getIndent:Block:indentChar
PrettyPrinterConfiguration:indentCaseInSwitch
PrettyPrinterConfiguration:isPrintJavadoc
Members
X