summaryrefslogtreecommitdiff
path: root/java/java-impl/src/com/intellij/ide/JavaLanguageCodeStyleSettingsProvider.java
blob: 2f684304ae5849af2951a5e43cbae46007d40193 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.ide;

import com.intellij.application.options.IndentOptionsEditor;
import com.intellij.application.options.JavaIndentOptionsEditor;
import com.intellij.lang.Language;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.project.Project;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiFileFactory;
import com.intellij.psi.codeStyle.*;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.LocalTimeCounter;
import com.intellij.util.PlatformUtils;
import org.jetbrains.annotations.NotNull;

/**
 * @author rvishnyakov
 */
public class JavaLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSettingsProvider {
  @NotNull
  @Override
  public Language getLanguage() {
    return JavaLanguage.INSTANCE;
  }

  @Override
  public String getCodeSample(@NotNull SettingsType settingsType) {
    if (settingsType == SettingsType.SPACING_SETTINGS) return SPACING_SAMPLE;
    if (settingsType == SettingsType.BLANK_LINES_SETTINGS) return BLANK_LINE_SAMPLE;
    if (settingsType == SettingsType.WRAPPING_AND_BRACES_SETTINGS) return WRAPPING_CODE_SAMPLE;

    return GENERAL_CODE_SAMPLE;
  }

  @Override
  public int getRightMargin(@NotNull SettingsType settingsType) {
    if (settingsType == SettingsType.WRAPPING_AND_BRACES_SETTINGS) return 37;
    return super.getRightMargin(settingsType);
  }

  @Override
  public void customizeSettings(@NotNull CodeStyleSettingsCustomizable consumer, @NotNull SettingsType settingsType) {
    consumer.showAllStandardOptions();
    if (settingsType == SettingsType.SPACING_SETTINGS) {
      consumer.showCustomOption(JavaCodeStyleSettings.class, "SPACES_WITHIN_ANGLE_BRACKETS", "Angle brackets",CodeStyleSettingsCustomizable.SPACES_WITHIN);

      String groupName = CodeStyleSettingsCustomizable.SPACES_IN_TYPE_ARGUMENTS;
      consumer.moveStandardOption("SPACE_AFTER_COMMA_IN_TYPE_ARGUMENTS", groupName);
      consumer.showCustomOption(JavaCodeStyleSettings.class, "SPACE_AFTER_CLOSING_ANGLE_BRACKET_IN_TYPE_ARGUMENT", "After closing angle bracket", groupName);

      groupName = CodeStyleSettingsCustomizable.SPACES_IN_TYPE_PARAMETERS;
      consumer.showCustomOption(JavaCodeStyleSettings.class, "SPACE_BEFORE_OPENING_ANGLE_BRACKET_IN_TYPE_PARAMETER", "Before opening angle bracket", groupName);
      consumer.showCustomOption(JavaCodeStyleSettings.class, "SPACE_AROUND_TYPE_BOUNDS_IN_TYPE_PARAMETERS", "Around type bounds", groupName);
    }
  }

  @Override
  public PsiFile createFileFromText(final Project project, final String text) {
    final PsiFile file = PsiFileFactory.getInstance(project).createFileFromText(
      "sample.java", StdFileTypes.JAVA, text, LocalTimeCounter.currentTime(), true, false
    );
    file.putUserData(PsiUtil.FILE_LANGUAGE_LEVEL_KEY, LanguageLevel.HIGHEST);
    return file;
  }

  @Override
  public DisplayPriority getDisplayPriority() {
    if (PlatformUtils.isIdeaUltimate()) return DisplayPriority.KEY_LANGUAGE_SETTINGS;
    return DisplayPriority.LANGUAGE_SETTINGS;
  }

  @Override
  public CommonCodeStyleSettings getDefaultCommonSettings() {
    CommonCodeStyleSettings settings = new CommonCodeStyleSettings(JavaLanguage.INSTANCE);
    settings.initIndentOptions();
    return settings;
  }

  @Override
  public IndentOptionsEditor getIndentOptionsEditor() {
    return new JavaIndentOptionsEditor();
  }

  private static final String GENERAL_CODE_SAMPLE =
    "public class Foo {\n" +
    "  public int[] X = new int[]{1, 3, 5 7, 9, 11};\n" +
    "\n" +
    "  public void foo(boolean a, int x, int y, int z) {\n" +
    "    label1:\n" +
    "    do {\n" +
    "      try {\n" +
    "        if (x > 0) {\n" +
    "          int someVariable = a ? x : y;\n" +
    "          int anotherVariable = a ? x : y;\n" +
    "        }\n" +
    "        else if (x < 0) {\n" +
    "          int someVariable = (y + z);\n" +
    "          someVariable = x = x + y;\n" +
    "        }\n" +
    "        else {\n" +
    "          label2:\n" +
    "          for (int i = 0; i < 5; i++) doSomething(i);\n" +
    "        }\n" +
    "        switch (a) {\n" +
    "          case 0:\n" +
    "            doCase0();\n" +
    "            break;\n" +
    "          default:\n" +
    "            doDefault();\n" +
    "        }\n" +
    "      }\n" +
    "      catch (Exception e) {\n" +
    "        processException(e.getMessage(), x + y, z, a);\n" +
    "      }\n" +
    "      finally {\n" +
    "        processFinally();\n" +
    "      }\n" +
    "    }\n" +
    "    while (true);\n" +
    "\n" +
    "    if (2 < 3) return;\n" +
    "    if (3 < 4) return;\n" +
    "    do {\n" +
    "      x++\n" +
    "    }\n" +
    "    while (x < 10000);\n" +
    "    while (x < 50000) x++;\n" +
    "    for (int i = 0; i < 5; i++) System.out.println(i);\n" +
    "  }\n" +
    "\n" +
    "  private class InnerClass implements I1, I2 {\n" +
    "    public void bar() throws E1, E2 {\n" +
    "    }\n" +
    "  }\n" +
    "}";

  private static final String BLANK_LINE_SAMPLE =
    "/*\n" +
    " * This is a sample file.\n" +
    " */\n" +
    "package com.intellij.samples;\n" +
    "\n" +
    "import com.intellij.idea.Main;\n" +
    "\n" +
    "import javax.swing.*;\n" +
    "import java.util.Vector;\n" +
    "\n" +
    "public class Foo {\n" +
    "  private int field1;\n" +
    "  private int field2;\n" +
    "\n" +
    "  public void foo1() {\n" +
    "      new Runnable() {\n" +
    "          public void run() {\n" +
    "          }\n" +
    "      }\n" +
    "  }\n" +
    "\n" +
    "  public class InnerClass {\n" +
    "  }\n" +
    "}\n" +
    "class AnotherClass {\n" +
    "}\n" +
    "interface TestInterface {\n" +
    "    int MAX = 10;\n" +
    "    int MIN = 1;\n" +
    "    void method1();\n" +
    "    void method2();\n" +
    "}";

  private static final String SPACING_SAMPLE =
    "@Annotation(param1 = \"value1\", param2 = \"value2\")\n" +
    "@SuppressWarnings({\"ALL\"})\n" +
    "public class Foo<T extends Bar & Abba, U> {\n" +
    "  int[] X = new int[]{1, 3, 5, 6, 7, 87, 1213, 2};\n" +
    "\n" +
    "  public void foo(int x, int y) {" +
    "    Runnable r = () -> {};\n" +
    "    Runnable r1 = this :: bar;\n" +
    "    for (int i = 0; i < x; i++) {\n" +
    "      y += (y ^ 0x123) << 2;\n" +
    "    }\n" +
    "    do {\n" +
    "      try(MyResource r1 = getResource(); MyResource r2 = null) {\n" +
    "        if (0 < x && x < 10) {\n" +
    "          while (x != y) {\n" +
    "            x = f(x * 3 + 5);\n" +
    "          }\n" +
    "        }\n" +
    "        else {\n" +
    "          synchronized (this) {\n" +
    "            switch (e.getCode()) {\n" +
    "              //...\n" +
    "            }\n" +
    "          }\n" +
    "        }\n" +
    "      }\n" +
    "      catch (MyException e) {\n" +
    "      }\n" +
    "      finally {\n" +
    "        int[] arr = (int[])g(y);\n" +
    "        x = y >= 0 ? arr[y] : -1;\n" +
    "        Map<String, String> sMap = new HashMap<String, String>();\n" +
    "        Bar.<String, Integer>mess(null);\n" +
    "      }\n" +
    "    }\n" +
    "    while (true);\n" +
    "  }\n" +
    "  void bar(){{return;}}\n" +
    "}\n" +
    "class Bar {\n" +
    "    static <U, T> U mess(T t) {\n" +
    "        return null;\n" +
    "    }\n" +
    "}\n" +
    "interface Abba {}";

  private static final String WRAPPING_CODE_SAMPLE =
    "/*\n" +
    " * This is a sample file.\n" +
    " */\n" +
    "\n" +
    "public class ThisIsASampleClass extends C1 implements I1, I2, I3, I4, I5 {\n" +
    "  private int f1 = 1;\n" +
    "  private String field2 = \"\";\n" +
    "  public void foo1(int i1, int i2, int i3, int i4, int i5, int i6, int i7) {}\n" +
    "  public static void longerMethod() throws Exception1, Exception2, Exception3 {\n" +
    "// todo something\n" +
    "    int\n" +
    "i = 0;\n" +
    "    int[] a = new int[] {1, 2, 0x0052, 0x0053, 0x0054};\n" +
    "    int var1 = 1; int var2 = 2;\n" +
    "    foo1(0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057);\n" +
    "    int x = (3 + 4 + 5 + 6) * (7 + 8 + 9 + 10) * (11 + 12 + 13 + 14 + 0xFFFFFFFF);\n" +
    "    String s1, s2, s3;\n" +
    "    s1 = s2 = s3 = \"012345678901456\";\n" +
    "    assert i + j + k + l + n+ m <= 2 : \"assert description\";" +
    "    int y = 2 > 3 ? 7 + 8 + 9 : 11 + 12 + 13;\n" +
    "    super.getFoo().foo().getBar().bar();\n" +
    "\n" +
    "    label: " +
    "    if (2 < 3) return; else if (2 > 3) return; else return;\n" +
    "    for (int i = 0; i < 0xFFFFFF; i += 2) System.out.println(i);\n" +
    "    while (x < 50000) x++;\n" +
    "    do x++ while (x < 10000);\n" +
    "    switch (a) {\n" +
    "    case 0:\n" +
    "      doCase0();\n" +
    "      break;\n" +
    "    default:\n" +
    "      doDefault();\n" +
    "    }\n" +
    "    try (MyResource r1 = getResource(); MyResource r2 = null) {\n" +
    "      doSomething();\n" +
    "    } catch (Exception e) {\n" +
    "      processException(e);\n" +
    "    } finally {\n" +
    "      processFinally();\n" +
    "    }\n" +
    "    do {\n" +
    "        x--;\n" +
    "    } while (x > 10) \n" +
    "  }\n" +
    "    public static void test() \n" +
    "        throws Exception { \n" +
    "        foo.foo().bar(\"arg1\", \n" +
    "                      \"arg2\"); \n" +
    "        new Object() {};" +
    "    } \n" +
    "    class TestInnerClass {}\n" +
    "    interface TestInnerInterface {}\n" +
    "}\n" +
    "\n" +
    "enum Breed {\n" +
    "    Dalmatian(), Labrador(), Dachshund()\n" +
    "}\n" +
    "\n" +
    "@Annotation1 @Annotation2 @Annotation3(param1=\"value1\", param2=\"value2\") @Annotation4 class Foo {\n" +
    "    @Annotation1 @Annotation3(param1=\"value1\", param2=\"value2\") public static void foo(){\n" +
    "    }\n" +
    "    @Annotation1 @Annotation3(param1=\"value1\", param2=\"value2\") public static int myFoo;\n" +
    "    public void method(@Annotation1 @Annotation3(param1=\"value1\", param2=\"value2\") final int param){\n" +
    "        @Annotation1 @Annotation3(param1=\"value1\", param2=\"value2\") final int localVariable;" +
    "    }\n" +
    "}";
}