2 * Copyright 2000-2015 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.intellij.openapi.options.colors.pages;
18 import com.intellij.application.options.colors.InspectionColorSettingsPage;
19 import com.intellij.ide.highlighter.JavaFileHighlighter;
20 import com.intellij.ide.highlighter.JavaHighlightingColors;
21 import com.intellij.lang.Language;
22 import com.intellij.lang.java.JavaLanguage;
23 import com.intellij.openapi.editor.colors.CodeInsightColors;
24 import com.intellij.openapi.editor.colors.TextAttributesKey;
25 import com.intellij.openapi.fileTypes.StdFileTypes;
26 import com.intellij.openapi.fileTypes.SyntaxHighlighter;
27 import com.intellij.openapi.options.OptionsBundle;
28 import com.intellij.openapi.options.colors.AttributesDescriptor;
29 import com.intellij.openapi.options.colors.ColorDescriptor;
30 import com.intellij.openapi.options.colors.RainbowColorSettingsPage;
31 import com.intellij.pom.java.LanguageLevel;
32 import com.intellij.psi.codeStyle.DisplayPriority;
33 import com.intellij.psi.codeStyle.DisplayPrioritySortable;
34 import org.jetbrains.annotations.NonNls;
35 import org.jetbrains.annotations.NotNull;
36 import org.jetbrains.annotations.Nullable;
39 import java.util.HashMap;
42 public class JavaColorSettingsPage implements RainbowColorSettingsPage, InspectionColorSettingsPage, DisplayPrioritySortable {
43 private static final AttributesDescriptor[] ourDescriptors = {
44 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.keyword"), JavaHighlightingColors.KEYWORD),
45 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.number"), JavaHighlightingColors.NUMBER),
47 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.string"), JavaHighlightingColors.STRING),
48 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.valid.escape.in.string"), JavaHighlightingColors.VALID_STRING_ESCAPE),
49 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.invalid.escape.in.string"), JavaHighlightingColors.INVALID_STRING_ESCAPE),
51 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.operator.sign"), JavaHighlightingColors.OPERATION_SIGN),
52 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.parentheses"), JavaHighlightingColors.PARENTHESES),
53 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.braces"), JavaHighlightingColors.BRACES),
54 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.brackets"), JavaHighlightingColors.BRACKETS),
55 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.comma"), JavaHighlightingColors.COMMA),
56 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.semicolon"), JavaHighlightingColors.JAVA_SEMICOLON),
57 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.dot"), JavaHighlightingColors.DOT),
59 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.line.comment"), JavaHighlightingColors.LINE_COMMENT),
60 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.block.comment"), JavaHighlightingColors.JAVA_BLOCK_COMMENT),
61 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.javadoc.comment"), JavaHighlightingColors.DOC_COMMENT),
62 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.javadoc.tag"), JavaHighlightingColors.DOC_COMMENT_TAG),
63 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.javadoc.tag.value"), JavaHighlightingColors.DOC_COMMENT_TAG_VALUE),
64 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.javadoc.markup"), JavaHighlightingColors.DOC_COMMENT_MARKUP),
66 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.class"), JavaHighlightingColors.CLASS_NAME_ATTRIBUTES),
67 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.anonymous.class"), JavaHighlightingColors.ANONYMOUS_CLASS_NAME_ATTRIBUTES),
68 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.type.parameter"), JavaHighlightingColors.TYPE_PARAMETER_NAME_ATTRIBUTES),
69 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.abstract.class"), JavaHighlightingColors.ABSTRACT_CLASS_NAME_ATTRIBUTES),
70 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.interface"), JavaHighlightingColors.INTERFACE_NAME_ATTRIBUTES),
71 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.enum"), JavaHighlightingColors.ENUM_NAME_ATTRIBUTES),
72 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.local.variable"), JavaHighlightingColors.LOCAL_VARIABLE_ATTRIBUTES),
73 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.reassigned.local.variable"), JavaHighlightingColors.REASSIGNED_LOCAL_VARIABLE_ATTRIBUTES),
74 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.reassigned.parameter"), JavaHighlightingColors.REASSIGNED_PARAMETER_ATTRIBUTES),
75 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.implicit.anonymous.parameter"), JavaHighlightingColors.IMPLICIT_ANONYMOUS_CLASS_PARAMETER_ATTRIBUTES),
76 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.inline.parameter.hint"), JavaHighlightingColors.INLINE_PARAMETER_HINT),
77 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.instance.field"), JavaHighlightingColors.INSTANCE_FIELD_ATTRIBUTES),
78 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.instance.final.field"), JavaHighlightingColors.INSTANCE_FINAL_FIELD_ATTRIBUTES),
79 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.static.field"), JavaHighlightingColors.STATIC_FIELD_ATTRIBUTES),
80 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.static.final.field"), JavaHighlightingColors.STATIC_FINAL_FIELD_ATTRIBUTES),
81 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.parameter"), JavaHighlightingColors.PARAMETER_ATTRIBUTES),
82 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.lambda.parameter"), JavaHighlightingColors.LAMBDA_PARAMETER_ATTRIBUTES),
83 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.method.call"), JavaHighlightingColors.METHOD_CALL_ATTRIBUTES),
84 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.method.declaration"), JavaHighlightingColors.METHOD_DECLARATION_ATTRIBUTES),
85 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.constructor.call"), JavaHighlightingColors.CONSTRUCTOR_CALL_ATTRIBUTES),
86 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.constructor.declaration"), JavaHighlightingColors.CONSTRUCTOR_DECLARATION_ATTRIBUTES),
87 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.static.method"), JavaHighlightingColors.STATIC_METHOD_ATTRIBUTES),
88 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.abstract.method"), JavaHighlightingColors.ABSTRACT_METHOD_ATTRIBUTES),
89 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.inherited.method"), JavaHighlightingColors.INHERITED_METHOD_ATTRIBUTES),
91 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.annotation.name"), JavaHighlightingColors.ANNOTATION_NAME_ATTRIBUTES),
92 new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.annotation.attribute.name"), JavaHighlightingColors.ANNOTATION_ATTRIBUTE_NAME_ATTRIBUTES)
95 @NonNls private static final Map<String, TextAttributesKey> ourTags = new HashMap<>();
97 ourTags.put("field", JavaHighlightingColors.INSTANCE_FIELD_ATTRIBUTES);
98 ourTags.put("unusedField", CodeInsightColors.NOT_USED_ELEMENT_ATTRIBUTES);
99 ourTags.put("error", CodeInsightColors.ERRORS_ATTRIBUTES);
100 ourTags.put("warning", CodeInsightColors.WARNINGS_ATTRIBUTES);
101 ourTags.put("weak_warning", CodeInsightColors.WEAK_WARNING_ATTRIBUTES);
102 ourTags.put("server_problems", CodeInsightColors.GENERIC_SERVER_ERROR_OR_WARNING);
103 ourTags.put("server_duplicate", CodeInsightColors.DUPLICATE_FROM_SERVER);
104 ourTags.put("unknownType", CodeInsightColors.WRONG_REFERENCES_ATTRIBUTES);
105 ourTags.put("localVar", JavaHighlightingColors.LOCAL_VARIABLE_ATTRIBUTES);
106 ourTags.put("reassignedLocalVar", JavaHighlightingColors.REASSIGNED_LOCAL_VARIABLE_ATTRIBUTES);
107 ourTags.put("reassignedParameter", JavaHighlightingColors.REASSIGNED_PARAMETER_ATTRIBUTES);
108 ourTags.put("implicitAnonymousParameter", JavaHighlightingColors.IMPLICIT_ANONYMOUS_CLASS_PARAMETER_ATTRIBUTES);
109 ourTags.put("static", JavaHighlightingColors.STATIC_FIELD_ATTRIBUTES);
110 ourTags.put("static_final", JavaHighlightingColors.STATIC_FINAL_FIELD_ATTRIBUTES);
111 ourTags.put("deprecated", CodeInsightColors.DEPRECATED_ATTRIBUTES);
112 ourTags.put("constructorCall", JavaHighlightingColors.CONSTRUCTOR_CALL_ATTRIBUTES);
113 ourTags.put("constructorDeclaration", JavaHighlightingColors.CONSTRUCTOR_DECLARATION_ATTRIBUTES);
114 ourTags.put("methodCall", JavaHighlightingColors.METHOD_CALL_ATTRIBUTES);
115 ourTags.put("methodDeclaration", JavaHighlightingColors.METHOD_DECLARATION_ATTRIBUTES);
116 ourTags.put("static_method", JavaHighlightingColors.STATIC_METHOD_ATTRIBUTES);
117 ourTags.put("abstract_method", JavaHighlightingColors.ABSTRACT_METHOD_ATTRIBUTES);
118 ourTags.put("inherited_method", JavaHighlightingColors.INHERITED_METHOD_ATTRIBUTES);
119 ourTags.put("param", JavaHighlightingColors.PARAMETER_ATTRIBUTES);
120 ourTags.put("lambda_param", JavaHighlightingColors.LAMBDA_PARAMETER_ATTRIBUTES);
121 ourTags.put("class", JavaHighlightingColors.CLASS_NAME_ATTRIBUTES);
122 ourTags.put("anonymousClass", JavaHighlightingColors.ANONYMOUS_CLASS_NAME_ATTRIBUTES);
123 ourTags.put("typeParameter", JavaHighlightingColors.TYPE_PARAMETER_NAME_ATTRIBUTES);
124 ourTags.put("abstractClass", JavaHighlightingColors.ABSTRACT_CLASS_NAME_ATTRIBUTES);
125 ourTags.put("interface", JavaHighlightingColors.INTERFACE_NAME_ATTRIBUTES);
126 ourTags.put("enum", JavaHighlightingColors.ENUM_NAME_ATTRIBUTES);
127 ourTags.put("annotationName", JavaHighlightingColors.ANNOTATION_NAME_ATTRIBUTES);
128 ourTags.put("annotationAttributeName", JavaHighlightingColors.ANNOTATION_ATTRIBUTE_NAME_ATTRIBUTES);
129 ourTags.put("javadocTagValue", JavaHighlightingColors.DOC_COMMENT_TAG_VALUE);
130 ourTags.put("instanceFinalField", JavaHighlightingColors.INSTANCE_FINAL_FIELD_ATTRIBUTES);
135 public String getDisplayName() {
136 return OptionsBundle.message("options.java.display.name");
140 public Icon getIcon() {
141 return StdFileTypes.JAVA.getIcon();
146 public AttributesDescriptor[] getAttributeDescriptors() {
147 return ourDescriptors;
152 public ColorDescriptor[] getColorDescriptors() {
153 return ColorDescriptor.EMPTY_ARRAY;
158 public SyntaxHighlighter getHighlighter() {
159 return new JavaFileHighlighter(LanguageLevel.HIGHEST);
164 public String getDemoText() {
166 "/* Block comment */\n" +
167 "import <class>java.util.Date</class>;\n" +
169 " * Doc comment here for <code>SomeClass</code>\n" +
170 " * @param <javadocTagValue>T</javadocTagValue> type parameter\n" +
171 " * @see <class>Math</class>#<methodCall>sin</methodCall>(double)\n" +
173 "<annotationName>@Annotation</annotationName> (<annotationAttributeName>name</annotationAttributeName>=value)\n" +
174 "public class <class>SomeClass</class><<typeParameter>T</typeParameter> extends <interface>Runnable</interface>> { // some comment\n" +
175 " private <typeParameter>T</typeParameter> <field>field</field> = null;\n" +
176 " private double <unusedField>unusedField</unusedField> = 12345.67890;\n" +
177 " private <unknownType>UnknownType</unknownType> <field>anotherString</field> = \"Another\\nStrin\\g\";\n" +
178 " public static int <static>staticField</static> = 0;\n" +
179 " public final int <instanceFinalField>instanceFinalField</instanceFinalField> = 0;\n" +
181 " public <constructorDeclaration>SomeClass</constructorDeclaration>(<interface>AnInterface</interface> <param>param</param>, int[] <reassignedParameter>reassignedParam</reassignedParameter>) {\n" +
182 " <error>int <localVar>localVar</localVar> = \"IntelliJ\"</error>; // Error, incompatible types\n" +
183 " <class>System</class>.<static>out</static>.<methodCall>println</methodCall>(<field>anotherString</field> + <inherited_method>toString</inherited_method>() + <localVar>localVar</localVar>);\n" +
184 " long <localVar>time</localVar> = <class>Date</class>.<static_method><deprecated>parse</deprecated></static_method>(\"1.2.3\"); // Method is deprecated\n" +
185 " int <reassignedLocalVar>reassignedValue</reassignedLocalVar> = this.<warning>staticField</warning>; \n" +
186 " <reassignedLocalVar>reassignedValue</reassignedLocalVar> ++; \n" +
187 " <field>field</field>.<abstract_method>run</abstract_method>(); \n" +
188 " new <anonymousClass>SomeClass</anonymousClass>() {\n" +
190 " int <localVar>a</localVar> = <implicitAnonymousParameter>localVar</implicitAnonymousParameter>;\n" +
193 " <reassignedParameter>reassignedParam</reassignedParameter> = new <constructorCall>ArrayList</constructorCall><<class>String</class>>().toArray(new int[0]);\n" +
196 "enum <enum>AnEnum</enum> { <static_final>CONST1</static_final>, <static_final>CONST2</static_final> }\n"+
197 "interface <interface>AnInterface</interface> {\n" +
198 " int <static_final>CONSTANT</static_final> = 2;\n" +
199 " void <methodDeclaration>method</methodDeclaration>();\n" +
201 "abstract class <abstractClass>SomeAbstractClass</abstractClass> {\n" +
206 public Map<String,TextAttributesKey> getAdditionalHighlightingTagToDescriptorMap() {
211 public DisplayPriority getPriority() {
212 return DisplayPriority.KEY_LANGUAGE_SETTINGS;
216 public boolean isRainbowType(TextAttributesKey type) {
217 return JavaHighlightingColors.LOCAL_VARIABLE_ATTRIBUTES.equals(type)
218 || JavaHighlightingColors.REASSIGNED_LOCAL_VARIABLE_ATTRIBUTES.equals(type)
219 || JavaHighlightingColors.PARAMETER_ATTRIBUTES.equals(type)
220 || JavaHighlightingColors.REASSIGNED_PARAMETER_ATTRIBUTES.equals(type)
222 || JavaHighlightingColors.DOC_COMMENT_TAG_VALUE.equals(type);
227 public String getRainbowDemoText() {
229 "import <class>java.util.Date</class>;\n" +
230 "class <class>SomeClass</class> {\n" +
231 " public int <field>field</field>;\n" +
232 " <constructorDeclaration>SomeClass</constructorDeclaration>(<interface>AnInterface</interface> <param>param</param>) {\n" +
237 " * @param <javadocTagValue>param1</javadocTagValue> function param\n" +
238 " * @param <javadocTagValue>param2</javadocTagValue>\n" +
239 " * @param <javadocTagValue>param3</javadocTagValue>\n" +
240 " * @param <javadocTagValue>param4</javadocTagValue>\n" +
241 " * @param <javadocTagValue>param5</javadocTagValue>\n" +
243 " void <methodDeclaration>method</methodDeclaration>(int <param>param1</param>,\n" +
244 " int <param>param2</param>,\n" +
245 " int <param>param3</param>,\n" +
246 " int <param>param4</param>,\n" +
247 " int <param>param5</param>) {\n" +
248 " int <localVar>localVar1</localVar>, <localVar>localVar2</localVar>, <localVar>localVar3</localVar>, <localVar>localVar4</localVar>, <localVar>localVar5</localVar>;\n" +
250 " <localVar>localVar3</localVar> = <param>param2</param>;\n" +
251 " this.<field>field</field> = <localVar>localVar3</localVar> + <param>param1</param> + <param>param5</param>;\n" +
258 public Language getLanguage() {
259 return JavaLanguage.INSTANCE;