• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.google.googlejavaformat.java;
16 
17 import com.google.auto.value.AutoValue;
18 import com.google.errorprone.annotations.Immutable;
19 
20 /**
21  * Options for a google-java-format invocation.
22  *
23  * <p>Like gofmt, the google-java-format CLI exposes <em>no</em> configuration options (aside from
24  * {@code --aosp}).
25  *
26  * <p>The goal of google-java-format is to provide consistent formatting, and to free developers
27  * from arguments over style choices. It is an explicit non-goal to support developers' individual
28  * preferences, and in fact it would work directly against our primary goals.
29  */
30 @Immutable
31 @AutoValue
32 public abstract class JavaFormatterOptions {
33 
34   public enum Style {
35     /** The default Google Java Style configuration. */
36     GOOGLE(1),
37 
38     /** The AOSP-compliant configuration. */
39     AOSP(2);
40 
41     private final int indentationMultiplier;
42 
Style(int indentationMultiplier)43     Style(int indentationMultiplier) {
44       this.indentationMultiplier = indentationMultiplier;
45     }
46 
indentationMultiplier()47     int indentationMultiplier() {
48       return indentationMultiplier;
49     }
50   }
51 
52   /** Returns the multiplier for the unit of indent. */
indentationMultiplier()53   public int indentationMultiplier() {
54     return style().indentationMultiplier();
55   }
56 
formatJavadoc()57   public abstract boolean formatJavadoc();
58 
reorderModifiers()59   public abstract boolean reorderModifiers();
60 
61   /** Returns the code style. */
style()62   public abstract Style style();
63 
64   /** Returns the default formatting options. */
defaultOptions()65   public static JavaFormatterOptions defaultOptions() {
66     return builder().build();
67   }
68 
69   /** Returns a builder for {@link JavaFormatterOptions}. */
builder()70   public static Builder builder() {
71     return new AutoValue_JavaFormatterOptions.Builder()
72         .style(Style.GOOGLE)
73         .formatJavadoc(true)
74         .reorderModifiers(true);
75   }
76 
77   /** A builder for {@link JavaFormatterOptions}. */
78   @AutoValue.Builder
79   public abstract static class Builder {
80 
style(Style style)81     public abstract Builder style(Style style);
82 
formatJavadoc(boolean formatJavadoc)83     public abstract Builder formatJavadoc(boolean formatJavadoc);
84 
reorderModifiers(boolean reorderModifiers)85     public abstract Builder reorderModifiers(boolean reorderModifiers);
86 
build()87     public abstract JavaFormatterOptions build();
88   }
89 }
90