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