• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 Google Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.googlejavaformat.intellij;
18 
19 import com.google.googlejavaformat.java.JavaFormatterOptions;
20 import com.intellij.openapi.components.PersistentStateComponent;
21 import com.intellij.openapi.components.State;
22 import com.intellij.openapi.components.Storage;
23 import com.intellij.openapi.project.Project;
24 import org.checkerframework.checker.nullness.qual.Nullable;
25 import org.jetbrains.annotations.NotNull;
26 
27 @State(
28     name = "GoogleJavaFormatSettings",
29     storages = {@Storage("google-java-format.xml")})
30 class GoogleJavaFormatSettings implements PersistentStateComponent<GoogleJavaFormatSettings.State> {
31 
32   private final Project project;
33 
34   private State state = new State();
35 
GoogleJavaFormatSettings(Project project)36   GoogleJavaFormatSettings(Project project) {
37     this.project = project;
38   }
39 
getInstance(Project project)40   static GoogleJavaFormatSettings getInstance(Project project) {
41     return project.getService(GoogleJavaFormatSettings.class);
42   }
43 
44   @Nullable
45   @Override
getState()46   public State getState() {
47     return state;
48   }
49 
50   @Override
loadState(@otNull State state)51   public void loadState(@NotNull State state) {
52     this.state = state;
53   }
54 
isEnabled()55   boolean isEnabled() {
56     return state.enabled.equals(EnabledState.ENABLED);
57   }
58 
setEnabled(boolean enabled)59   void setEnabled(boolean enabled) {
60     setEnabled(enabled ? EnabledState.ENABLED : EnabledState.DISABLED);
61   }
62 
setEnabled(EnabledState enabled)63   void setEnabled(EnabledState enabled) {
64     if (enabled.equals(EnabledState.ENABLED)) {
65       JreConfigurationChecker.checkJreConfiguration(project);
66     }
67     state.enabled = enabled;
68   }
69 
isUninitialized()70   boolean isUninitialized() {
71     return state.enabled.equals(EnabledState.UNKNOWN);
72   }
73 
getStyle()74   JavaFormatterOptions.Style getStyle() {
75     return state.style;
76   }
77 
setStyle(JavaFormatterOptions.Style style)78   void setStyle(JavaFormatterOptions.Style style) {
79     state.style = style;
80   }
81 
82   enum EnabledState {
83     UNKNOWN,
84     ENABLED,
85     DISABLED
86   }
87 
88   static class State {
89 
90     private EnabledState enabled = EnabledState.UNKNOWN;
91     public JavaFormatterOptions.Style style = JavaFormatterOptions.Style.GOOGLE;
92 
93     // enabled used to be a boolean so we use bean property methods for backwards compatibility
setEnabled(@ullable String enabledStr)94     public void setEnabled(@Nullable String enabledStr) {
95       if (enabledStr == null) {
96         enabled = EnabledState.UNKNOWN;
97       } else if (Boolean.parseBoolean(enabledStr)) {
98         enabled = EnabledState.ENABLED;
99       } else {
100         enabled = EnabledState.DISABLED;
101       }
102     }
103 
getEnabled()104     public String getEnabled() {
105       switch (enabled) {
106         case ENABLED:
107           return "true";
108         case DISABLED:
109           return "false";
110         default:
111           return null;
112       }
113     }
114   }
115 }
116