2929import com .google .googlejavaformat .FormatterDiagnostic ;
3030import com .google .googlejavaformat .Op ;
3131import com .google .googlejavaformat .OpsBuilder ;
32+ import com .google .googlejavaformat .java .JavaFormatterOptions .JavadocFormatter ;
33+ import com .google .googlejavaformat .java .JavaFormatterOptions .SortImports ;
34+ import com .google .googlejavaformat .java .JavaFormatterOptions .Style ;
3235
3336import org .eclipse .jdt .core .JavaCore ;
3437import org .eclipse .jdt .core .dom .AST ;
7477 */
7578@ Immutable
7679public final class Formatter {
77- static final int MAX_WIDTH = 100 ;
80+
7881 static final Range <Integer > EMPTY_RANGE = Range .closedOpen (-1 , -1 );
7982
83+ private final JavaFormatterOptions options ;
84+
8085 /**
8186 * A new Formatter instance with default options.
8287 */
8388 public Formatter () {
89+ this (new JavaFormatterOptions (JavadocFormatter .NONE , Style .GOOGLE , SortImports .NO ));
90+ }
91+
92+ Formatter (JavaFormatterOptions options ) {
93+ this .options = options ;
8494 }
8595
8696 /**
8797 * Construct a {@code Formatter} given a Java compilation unit. Parses the code; builds a
8898 * {@link JavaInput} and the corresponding {@link JavaOutput}.
8999 * @param javaInput the input, a Java compilation unit
90100 * @param javaOutput the {@link JavaOutput}
91- * @param maxWidth the maximum formatted width
101+ * @param options the {@link JavaFormatterOptions}
92102 * @param errors mutable list to receive errors
93- * @param indentationMultiplier the multiplier for the unit of indent; the default is 1
94103 */
95104 static void format (
96105 JavaInput javaInput ,
97106 JavaOutput javaOutput ,
98- int maxWidth ,
99- List <FormatterDiagnostic > errors ,
100- int indentationMultiplier ) {
107+ JavaFormatterOptions options ,
108+ List <FormatterDiagnostic > errors ) {
101109 ASTParser parser = ASTParser .newParser (AST .JLS8 );
102110 parser .setSource (javaInput .getText ().toCharArray ());
103111 @ SuppressWarnings ("unchecked" ) // safe by specification
104- Map <String , String > options = JavaCore .getOptions ();
105- JavaCore .setComplianceOptions (JavaCore .VERSION_1_8 , options );
106- parser .setCompilerOptions (options );
112+ Map <String , String > parserOptions = JavaCore .getOptions ();
113+ JavaCore .setComplianceOptions (JavaCore .VERSION_1_8 , parserOptions );
114+ parser .setCompilerOptions (parserOptions );
107115 CompilationUnit unit = (CompilationUnit ) parser .createAST (null );
108116 javaInput .setCompilationUnit (unit );
109117 if (unit .getMessages ().length > 0 ) {
@@ -114,11 +122,12 @@ static void format(
114122 }
115123 OpsBuilder builder = new OpsBuilder (javaInput , javaOutput , errors );
116124 // Output the compilation unit.
117- new JavaInputAstVisitor (builder , indentationMultiplier ).visit (unit );
125+ new JavaInputAstVisitor (builder , options . indentationMultiplier () ).visit (unit );
118126 builder .sync (javaInput .getText ().length ());
119127 builder .drain ();
120128 Doc doc = new DocBuilder ().withOps (builder .build ()).build ();
121- doc .computeBreaks (javaOutput .getCommentsHelper (), maxWidth , new Doc .State (+0 , 0 ));
129+ doc .computeBreaks (
130+ javaOutput .getCommentsHelper (), options .maxLineLength (), new Doc .State (+0 , 0 ));
122131 doc .write (javaOutput );
123132 javaOutput .flush ();
124133 }
@@ -143,9 +152,9 @@ public void formatSource(CharSource input, CharSink output)
143152 */
144153 public String formatSource (String input ) throws FormatterException {
145154 JavaInput javaInput = new JavaInput (STDIN_FILENAME , input );
146- JavaOutput javaOutput = new JavaOutput (javaInput , new JavaCommentsHelper ());
155+ JavaOutput javaOutput = new JavaOutput (javaInput , new JavaCommentsHelper (options ));
147156 List <FormatterDiagnostic > errors = new ArrayList <>();
148- format (javaInput , javaOutput , MAX_WIDTH , errors , 1 );
157+ format (javaInput , javaOutput , options , errors );
149158 if (!errors .isEmpty ()) {
150159 throw new FormatterException (errors );
151160 }
@@ -171,9 +180,9 @@ public String formatSource(String input) throws FormatterException {
171180 public String formatSource (String input , List <Range <Integer >> characterRanges )
172181 throws FormatterException {
173182 JavaInput javaInput = new JavaInput (STDIN_FILENAME , input );
174- JavaOutput javaOutput = new JavaOutput (javaInput , new JavaCommentsHelper ());
183+ JavaOutput javaOutput = new JavaOutput (javaInput , new JavaCommentsHelper (options ));
175184 List <FormatterDiagnostic > errors = new ArrayList <>();
176- format (javaInput , javaOutput , MAX_WIDTH , errors , 1 );
185+ format (javaInput , javaOutput , options , errors );
177186 if (!errors .isEmpty ()) {
178187 throw new FormatterException (errors );
179188 }
@@ -198,9 +207,9 @@ public String formatSource(String input, List<Range<Integer>> characterRanges)
198207 public ImmutableList <Replacement > getFormatReplacements (
199208 String input , List <Range <Integer >> characterRanges ) throws FormatterException {
200209 JavaInput javaInput = new JavaInput (STDIN_FILENAME , input );
201- JavaOutput javaOutput = new JavaOutput (javaInput , new JavaCommentsHelper ());
210+ JavaOutput javaOutput = new JavaOutput (javaInput , new JavaCommentsHelper (options ));
202211 List <FormatterDiagnostic > errors = new ArrayList <>();
203- format (javaInput , javaOutput , MAX_WIDTH , errors , 1 );
212+ format (javaInput , javaOutput , options , errors );
204213 if (!errors .isEmpty ()) {
205214 throw new FormatterException (errors );
206215 }
0 commit comments