Merge branch 'master' into uta/rainbow
[idea/community.git] / platform / lang-impl / src / com / intellij / openapi / options / colors / pages / DefaultLanguageColorsPage.java
1 /*
2  * Copyright 2000-2016 JetBrains s.r.o.
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 package com.intellij.openapi.options.colors.pages;
17
18 import com.intellij.lang.Language;
19 import com.intellij.openapi.editor.DefaultLanguageHighlighterColors;
20 import com.intellij.openapi.editor.HighlighterColors;
21 import com.intellij.openapi.editor.colors.TextAttributesKey;
22 import com.intellij.openapi.fileTypes.FileTypes;
23 import com.intellij.openapi.fileTypes.PlainSyntaxHighlighter;
24 import com.intellij.openapi.fileTypes.SyntaxHighlighter;
25 import com.intellij.openapi.options.OptionsBundle;
26 import com.intellij.openapi.options.colors.AttributesDescriptor;
27 import com.intellij.openapi.options.colors.ColorDescriptor;
28 import com.intellij.openapi.options.colors.ColorSettingsPage;
29 import com.intellij.openapi.options.colors.RainbowColorSettingsPage;
30 import com.intellij.psi.codeStyle.DisplayPriority;
31 import com.intellij.psi.codeStyle.DisplayPrioritySortable;
32 import org.jetbrains.annotations.NonNls;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
35
36 import javax.swing.*;
37 import java.util.HashMap;
38 import java.util.Map;
39
40 /**
41  * Allows to set default colors for multiple languages.
42  *
43  * @author Rustam Vishnyakov
44  */
45 public class DefaultLanguageColorsPage implements RainbowColorSettingsPage, DisplayPrioritySortable {
46
47   @NonNls private static final Map<String, TextAttributesKey> TAG_HIGHLIGHTING_MAP = new HashMap<>();
48
49   private final static TextAttributesKey FAKE_BAD_CHAR =
50     TextAttributesKey.createTextAttributesKey("FAKE_BAD_CHAR", HighlighterColors.BAD_CHARACTER);
51
52   static {
53     TAG_HIGHLIGHTING_MAP.put("bad_char", FAKE_BAD_CHAR);
54     TAG_HIGHLIGHTING_MAP.put("template_language", DefaultLanguageHighlighterColors.TEMPLATE_LANGUAGE_COLOR);
55     TAG_HIGHLIGHTING_MAP.put("identifier", DefaultLanguageHighlighterColors.IDENTIFIER);
56     TAG_HIGHLIGHTING_MAP.put("number", DefaultLanguageHighlighterColors.NUMBER);
57     TAG_HIGHLIGHTING_MAP.put("keyword", DefaultLanguageHighlighterColors.KEYWORD);
58     TAG_HIGHLIGHTING_MAP.put("string", DefaultLanguageHighlighterColors.STRING);
59     TAG_HIGHLIGHTING_MAP.put("line_comment", DefaultLanguageHighlighterColors.LINE_COMMENT);
60     TAG_HIGHLIGHTING_MAP.put("block_comment", DefaultLanguageHighlighterColors.BLOCK_COMMENT);
61     TAG_HIGHLIGHTING_MAP.put("operation_sign", DefaultLanguageHighlighterColors.OPERATION_SIGN);
62     TAG_HIGHLIGHTING_MAP.put("braces", DefaultLanguageHighlighterColors.BRACES);
63     TAG_HIGHLIGHTING_MAP.put("doc_comment", DefaultLanguageHighlighterColors.DOC_COMMENT);
64     TAG_HIGHLIGHTING_MAP.put("dot", DefaultLanguageHighlighterColors.DOT);
65     TAG_HIGHLIGHTING_MAP.put("semicolon", DefaultLanguageHighlighterColors.SEMICOLON);
66     TAG_HIGHLIGHTING_MAP.put("comma", DefaultLanguageHighlighterColors.COMMA);
67     TAG_HIGHLIGHTING_MAP.put("brackets", DefaultLanguageHighlighterColors.BRACKETS);
68     TAG_HIGHLIGHTING_MAP.put("parenths", DefaultLanguageHighlighterColors.PARENTHESES);
69     TAG_HIGHLIGHTING_MAP.put("func_decl", DefaultLanguageHighlighterColors.FUNCTION_DECLARATION);
70     TAG_HIGHLIGHTING_MAP.put("func_call", DefaultLanguageHighlighterColors.FUNCTION_CALL);
71     TAG_HIGHLIGHTING_MAP.put("param", DefaultLanguageHighlighterColors.PARAMETER);
72     TAG_HIGHLIGHTING_MAP.put("class_name", DefaultLanguageHighlighterColors.CLASS_NAME);
73     TAG_HIGHLIGHTING_MAP.put("class_ref", DefaultLanguageHighlighterColors.CLASS_REFERENCE);
74     TAG_HIGHLIGHTING_MAP.put("inst_method", DefaultLanguageHighlighterColors.INSTANCE_METHOD);
75     TAG_HIGHLIGHTING_MAP.put("inst_field", DefaultLanguageHighlighterColors.INSTANCE_FIELD);
76     TAG_HIGHLIGHTING_MAP.put("static_method", DefaultLanguageHighlighterColors.STATIC_METHOD);
77     TAG_HIGHLIGHTING_MAP.put("static_field", DefaultLanguageHighlighterColors.STATIC_FIELD);
78     TAG_HIGHLIGHTING_MAP.put("label", DefaultLanguageHighlighterColors.LABEL);
79     TAG_HIGHLIGHTING_MAP.put("local_var", DefaultLanguageHighlighterColors.LOCAL_VARIABLE);
80     TAG_HIGHLIGHTING_MAP.put("global_var", DefaultLanguageHighlighterColors.GLOBAL_VARIABLE);
81     TAG_HIGHLIGHTING_MAP.put("const", DefaultLanguageHighlighterColors.CONSTANT);
82     TAG_HIGHLIGHTING_MAP.put("interface", DefaultLanguageHighlighterColors.INTERFACE_NAME);
83     TAG_HIGHLIGHTING_MAP.put("doc_markup", DefaultLanguageHighlighterColors.DOC_COMMENT_MARKUP);
84     TAG_HIGHLIGHTING_MAP.put("doc_tag", DefaultLanguageHighlighterColors.DOC_COMMENT_TAG);
85     TAG_HIGHLIGHTING_MAP.put("doc_tag_value", DefaultLanguageHighlighterColors.DOC_COMMENT_TAG_VALUE);
86     TAG_HIGHLIGHTING_MAP.put("valid_esc_seq", DefaultLanguageHighlighterColors.VALID_STRING_ESCAPE);
87     TAG_HIGHLIGHTING_MAP.put("invalid_esc_seq", DefaultLanguageHighlighterColors.INVALID_STRING_ESCAPE);
88     TAG_HIGHLIGHTING_MAP.put("predefined", DefaultLanguageHighlighterColors.PREDEFINED_SYMBOL);
89     TAG_HIGHLIGHTING_MAP.put("metadata", DefaultLanguageHighlighterColors.METADATA);
90     TAG_HIGHLIGHTING_MAP.put("tag", DefaultLanguageHighlighterColors.MARKUP_TAG);
91     TAG_HIGHLIGHTING_MAP.put("attribute", DefaultLanguageHighlighterColors.MARKUP_ATTRIBUTE);
92     TAG_HIGHLIGHTING_MAP.put("entity", DefaultLanguageHighlighterColors.MARKUP_ENTITY);
93   }
94
95   private final static AttributesDescriptor[] ATTRIBUTES_DESCRIPTORS = {
96     new AttributesDescriptor(
97       OptionsBundle.message("options.java.attribute.descriptor.bad.character"), HighlighterColors.BAD_CHARACTER),
98     new AttributesDescriptor(
99       OptionsBundle.message("options.language.defaults.keyword"), DefaultLanguageHighlighterColors.KEYWORD),
100     new AttributesDescriptor(
101       OptionsBundle.message("options.language.defaults.identifier"), DefaultLanguageHighlighterColors.IDENTIFIER),
102     new AttributesDescriptor(
103       OptionsBundle.message("options.language.defaults.string"), DefaultLanguageHighlighterColors.STRING),
104     new AttributesDescriptor(
105       OptionsBundle.message("options.language.defaults.valid.esc.seq"), DefaultLanguageHighlighterColors.VALID_STRING_ESCAPE),
106     new AttributesDescriptor(
107       OptionsBundle.message("options.language.defaults.invalid.esc.seq"), DefaultLanguageHighlighterColors.INVALID_STRING_ESCAPE),
108     new AttributesDescriptor(
109       OptionsBundle.message("options.language.defaults.number"), DefaultLanguageHighlighterColors.NUMBER),
110     new AttributesDescriptor(
111       OptionsBundle.message("options.language.defaults.operation"), DefaultLanguageHighlighterColors.OPERATION_SIGN),
112     new AttributesDescriptor(
113       OptionsBundle.message("options.language.defaults.braces"), DefaultLanguageHighlighterColors.BRACES),
114     new AttributesDescriptor(
115       OptionsBundle.message("options.language.defaults.parentheses"), DefaultLanguageHighlighterColors.PARENTHESES),
116     new AttributesDescriptor(
117       OptionsBundle.message("options.language.defaults.brackets"), DefaultLanguageHighlighterColors.BRACKETS),
118     new AttributesDescriptor(
119       OptionsBundle.message("options.language.defaults.dot"), DefaultLanguageHighlighterColors.DOT),
120     new AttributesDescriptor(
121       OptionsBundle.message("options.language.defaults.comma"), DefaultLanguageHighlighterColors.COMMA),
122     new AttributesDescriptor(
123       OptionsBundle.message("options.language.defaults.semicolon"), DefaultLanguageHighlighterColors.SEMICOLON),
124     new AttributesDescriptor(
125       OptionsBundle.message("options.language.defaults.line.comment"), DefaultLanguageHighlighterColors.LINE_COMMENT),
126     new AttributesDescriptor(
127       OptionsBundle.message("options.language.defaults.block.comment"), DefaultLanguageHighlighterColors.BLOCK_COMMENT),
128     new AttributesDescriptor(
129       OptionsBundle.message("options.language.defaults.doc.comment"), DefaultLanguageHighlighterColors.DOC_COMMENT),
130     new AttributesDescriptor(
131       OptionsBundle.message("options.language.defaults.doc.markup"), DefaultLanguageHighlighterColors.DOC_COMMENT_MARKUP),
132     new AttributesDescriptor(
133       OptionsBundle.message("options.language.defaults.doc.tag"), DefaultLanguageHighlighterColors.DOC_COMMENT_TAG),
134     new AttributesDescriptor(
135       OptionsBundle.message("options.language.defaults.doc.tag.value"), DefaultLanguageHighlighterColors.DOC_COMMENT_TAG_VALUE),
136     new AttributesDescriptor(
137       OptionsBundle.message("options.language.defaults.label"), DefaultLanguageHighlighterColors.LABEL),
138     new AttributesDescriptor(
139       OptionsBundle.message("options.language.defaults.constant"), DefaultLanguageHighlighterColors.CONSTANT),
140     new AttributesDescriptor(
141       OptionsBundle.message("options.language.defaults.predefined"), DefaultLanguageHighlighterColors.PREDEFINED_SYMBOL),
142     new AttributesDescriptor(
143       OptionsBundle.message("options.language.defaults.local.variable"), DefaultLanguageHighlighterColors.LOCAL_VARIABLE),
144     new AttributesDescriptor(
145       OptionsBundle.message("options.language.defaults.global.variable"), DefaultLanguageHighlighterColors.GLOBAL_VARIABLE),
146     new AttributesDescriptor(
147       OptionsBundle.message("options.language.defaults.function.declaration"), DefaultLanguageHighlighterColors.FUNCTION_DECLARATION),
148     new AttributesDescriptor(
149       OptionsBundle.message("options.language.defaults.function.call"), DefaultLanguageHighlighterColors.FUNCTION_CALL),
150     new AttributesDescriptor(
151       OptionsBundle.message("options.language.defaults.parameter"), DefaultLanguageHighlighterColors.PARAMETER),
152     new AttributesDescriptor(
153       OptionsBundle.message("options.language.defaults.interface.name"), DefaultLanguageHighlighterColors.INTERFACE_NAME),
154     new AttributesDescriptor(
155       OptionsBundle.message("options.language.defaults.metadata"), DefaultLanguageHighlighterColors.METADATA),
156     new AttributesDescriptor(
157       OptionsBundle.message("options.language.defaults.class.name"), DefaultLanguageHighlighterColors.CLASS_NAME),
158     new AttributesDescriptor(
159       OptionsBundle.message("options.language.defaults.class.reference"), DefaultLanguageHighlighterColors.CLASS_REFERENCE),
160     new AttributesDescriptor(
161       OptionsBundle.message("options.language.defaults.instance.method"), DefaultLanguageHighlighterColors.INSTANCE_METHOD),
162     new AttributesDescriptor(
163       OptionsBundle.message("options.language.defaults.instance.field"), DefaultLanguageHighlighterColors.INSTANCE_FIELD),
164     new AttributesDescriptor(
165       OptionsBundle.message("options.language.defaults.static.method"), DefaultLanguageHighlighterColors.STATIC_METHOD),
166     new AttributesDescriptor(
167       OptionsBundle.message("options.language.defaults.static.field"), DefaultLanguageHighlighterColors.STATIC_FIELD),
168
169     new AttributesDescriptor(
170       OptionsBundle.message("options.language.defaults.markup.tag"), DefaultLanguageHighlighterColors.MARKUP_TAG),
171     new AttributesDescriptor(
172       OptionsBundle.message("options.language.defaults.markup.attribute"), DefaultLanguageHighlighterColors.MARKUP_ATTRIBUTE),
173     new AttributesDescriptor(
174       OptionsBundle.message("options.language.defaults.markup.entity"), DefaultLanguageHighlighterColors.MARKUP_ENTITY),
175     new AttributesDescriptor(
176       OptionsBundle.message("options.language.defaults.template.language"), DefaultLanguageHighlighterColors.TEMPLATE_LANGUAGE_COLOR),
177     
178   };
179
180   @Nullable
181   @Override
182   public Icon getIcon() {
183     return FileTypes.PLAIN_TEXT.getIcon();
184   }
185
186   @NotNull
187   @Override
188   public SyntaxHighlighter getHighlighter() {
189     return new PlainSyntaxHighlighter();
190   }
191
192   @NotNull
193   @Override
194   public String getDemoText() {
195     return
196       "Bad characters: <bad_char>????</bad_char>\n" +
197       "<keyword>Keyword</keyword>\n" +
198       "<identifier>Identifier</identifier>\n" +
199       "<string>'String <valid_esc_seq>\\n</valid_esc_seq><invalid_esc_seq>\\?</invalid_esc_seq>'</string>\n" +
200       "<number>12345</number>\n" +
201       "<operation_sign>Operator</operation_sign>\n" +
202       "Dot: <dot>.</dot> comma: <comma>,</comma> semicolon: <semicolon>;</semicolon>\n" +
203       "<braces>{</braces> Braces <braces>}</braces>\n" +
204       "<parenths>(</parenths> Parentheses <parenths>)</parenths>\n" +
205       "<brackets>[</brackets> Brackets <brackets>]</brackets>\n" +
206       "<line_comment>// Line comment</line_comment>\n" +
207       "<block_comment>/* Block comment */</block_comment>\n" +
208       "<doc_comment>/** \n" +
209       " * Doc comment\n" +
210       " * <doc_tag>@tag</doc_tag> <doc_markup><code>Markup</code></doc_markup>\n" +
211       " * <doc_tag>@param</doc_tag> <doc_tag_value>param_name</doc_tag_value> documentation\n" +
212       " */</doc_comment>\n" +
213       "<label>:Label</label>\n" +
214       "<predefined>predefined_symbol()</predefined>\n" +
215       "<const>CONSTANT</const>\n" +
216       "Global <global_var>variable</global_var>\n" +
217       "Function <func_decl>declaration</func_decl> (<param>parameter</param>)\n" +
218       "    Local <local_var>variable</local_var>\n" +
219       "Function <func_call>call</func_call>()\n" +
220       "Interface <interface>Name</interface>\n" +
221       "<metadata>@Metadata</metadata>\n" +
222       "Class <class_name>Name</class_name>\n" +
223       "    instance <inst_method>method</inst_method>\n" +
224       "    instance <inst_field>field</inst_field>\n" +
225       "    static <static_method>method</static_method>\n" +
226       "    static <static_field>field</static_field>\n" +
227       "\n" +
228       "<tag><keyword>@TAG</keyword> <attribute>attribute</attribute>=<string>Value</string></tag>\n" +
229       "    Entity: <entity>&amp;</entity>\n" +
230       "    <template_language>{% Template language %}</template_language>";
231   }
232
233   @Nullable
234   @Override
235   public Map<String, TextAttributesKey> getAdditionalHighlightingTagToDescriptorMap() {
236     return TAG_HIGHLIGHTING_MAP;
237   }
238
239   @NotNull
240   @Override
241   public AttributesDescriptor[] getAttributeDescriptors() {
242     return ATTRIBUTES_DESCRIPTORS;
243   }
244
245   @NotNull
246   @Override
247   public ColorDescriptor[] getColorDescriptors() {
248     return ColorDescriptor.EMPTY_ARRAY;
249   }
250
251   @NotNull
252   @Override
253   public String getDisplayName() {
254     return OptionsBundle.message("options.language.defaults.display.name");
255   }
256
257   @Override
258   public DisplayPriority getPriority() {
259     return DisplayPriority.GENERAL_SETTINGS;
260   }
261
262   @Override
263   public boolean isRainbowType(TextAttributesKey type) {
264     return DefaultLanguageHighlighterColors.LOCAL_VARIABLE.equals(type)
265            || DefaultLanguageHighlighterColors.PARAMETER.equals(type)
266            || DefaultLanguageHighlighterColors.DOC_COMMENT_TAG_VALUE.equals(type);
267   }
268
269   @NotNull
270   @Override
271   public String getRainbowDemoText() {
272     return
273       "Global <global_var>variable1</global_var>\n" +
274       "Global <global_var>variable2</global_var>\n" +
275       "<doc_comment>/** \n" +
276       " * Doc comment\n" +
277       " * <doc_tag>@tag</doc_tag> <doc_markup><code>Markup</code></doc_markup>\n" +
278       " * <doc_tag>@param</doc_tag> <doc_tag_value>parameter1</doc_tag_value> documentation\n" +
279       " * <doc_tag>@param</doc_tag> <doc_tag_value>parameter2</doc_tag_value> documentation\n" +
280       " * <doc_tag>@param</doc_tag> <doc_tag_value>parameter3</doc_tag_value> documentation\n" +
281       " * <doc_tag>@param</doc_tag> <doc_tag_value>parameter4</doc_tag_value> documentation\n" +
282       " * <doc_tag>@param</doc_tag> <doc_tag_value>parameter5</doc_tag_value> documentation\n" +
283       " */</doc_comment>\n" +
284       "Function <func_decl>declaration</func_decl> (<param>parameter1</param>\n" +
285       "                      <param>parameter2</param>\n" +
286       "                      <param>parameter3</param>\n" +
287       "                      <param>parameter4</param>\n" +
288       "                      <param>parameter5</param>)\n" +
289       "    Local <local_var>variable1</local_var>\n" +
290       "    Local <local_var>variable2</local_var>\n" +
291       "    Local <local_var>variable3</local_var>\n" +
292       "    Local <local_var>variable4</local_var>\n" +
293       "    Local <local_var>variable5</local_var>\n" +
294       "Function <func_call>call</func_call>()";
295   }
296
297   @Nullable
298   @Override
299   public Language getLanguage() {
300     return null;
301   }
302 }