2 * Copyright 2000-2009 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
.ide
.highlighter
;
18 import com
.intellij
.lexer
.JavaHighlightingLexer
;
19 import com
.intellij
.lexer
.Lexer
;
20 import com
.intellij
.openapi
.editor
.HighlighterColors
;
21 import com
.intellij
.openapi
.editor
.SyntaxHighlighterColors
;
22 import com
.intellij
.openapi
.editor
.colors
.TextAttributesKey
;
23 import com
.intellij
.openapi
.fileTypes
.SyntaxHighlighterBase
;
24 import com
.intellij
.pom
.java
.LanguageLevel
;
25 import com
.intellij
.psi
.JavaDocTokenType
;
26 import com
.intellij
.psi
.JavaTokenType
;
27 import com
.intellij
.psi
.StringEscapesTokenTypes
;
28 import com
.intellij
.psi
.impl
.source
.tree
.JavaDocElementType
;
29 import com
.intellij
.psi
.tree
.IElementType
;
30 import com
.intellij
.psi
.xml
.XmlTokenType
;
31 import org
.jetbrains
.annotations
.NotNull
;
33 import java
.util
.HashMap
;
36 public class JavaFileHighlighter
extends SyntaxHighlighterBase
{
37 private static final Map
<IElementType
, TextAttributesKey
> ourMap1
;
38 private static final Map
<IElementType
, TextAttributesKey
> ourMap2
;
40 private final LanguageLevel myLanguageLevel
;
42 public JavaFileHighlighter() {
43 this(LanguageLevel
.HIGHEST
);
46 public JavaFileHighlighter(LanguageLevel languageLevel
) {
47 myLanguageLevel
= languageLevel
;
51 ourMap1
= new HashMap
<IElementType
, TextAttributesKey
>();
52 ourMap2
= new HashMap
<IElementType
, TextAttributesKey
>();
54 fillMap(ourMap1
, JavaTokenType
.KEYWORD_BIT_SET
, SyntaxHighlighterColors
.KEYWORD
);
55 fillMap(ourMap1
, JavaTokenType
.OPERATION_BIT_SET
, SyntaxHighlighterColors
.OPERATION_SIGN
);
56 fillMap(ourMap1
, JavaTokenType
.OPERATION_BIT_SET
, SyntaxHighlighterColors
.OPERATION_SIGN
);
58 for (IElementType type
: JavaDocTokenType
.ALL_JAVADOC_TOKENS
.getTypes()) {
59 ourMap1
.put(type
, SyntaxHighlighterColors
.DOC_COMMENT
);
62 ourMap1
.put(XmlTokenType
.XML_DATA_CHARACTERS
, SyntaxHighlighterColors
.DOC_COMMENT
);
63 ourMap1
.put(XmlTokenType
.XML_REAL_WHITE_SPACE
, SyntaxHighlighterColors
.DOC_COMMENT
);
64 ourMap1
.put(XmlTokenType
.TAG_WHITE_SPACE
, SyntaxHighlighterColors
.DOC_COMMENT
);
66 ourMap1
.put(JavaTokenType
.INTEGER_LITERAL
, SyntaxHighlighterColors
.NUMBER
);
67 ourMap1
.put(JavaTokenType
.LONG_LITERAL
, SyntaxHighlighterColors
.NUMBER
);
68 ourMap1
.put(JavaTokenType
.FLOAT_LITERAL
, SyntaxHighlighterColors
.NUMBER
);
69 ourMap1
.put(JavaTokenType
.DOUBLE_LITERAL
, SyntaxHighlighterColors
.NUMBER
);
70 ourMap1
.put(JavaTokenType
.STRING_LITERAL
, SyntaxHighlighterColors
.STRING
);
71 ourMap1
.put(StringEscapesTokenTypes
.VALID_STRING_ESCAPE_TOKEN
, SyntaxHighlighterColors
.VALID_STRING_ESCAPE
);
72 ourMap1
.put(StringEscapesTokenTypes
.INVALID_CHARACTER_ESCAPE_TOKEN
, SyntaxHighlighterColors
.INVALID_STRING_ESCAPE
);
73 ourMap1
.put(StringEscapesTokenTypes
.INVALID_UNICODE_ESCAPE_TOKEN
, SyntaxHighlighterColors
.INVALID_STRING_ESCAPE
);
74 ourMap1
.put(JavaTokenType
.CHARACTER_LITERAL
, SyntaxHighlighterColors
.STRING
);
76 ourMap1
.put(JavaTokenType
.LPARENTH
, SyntaxHighlighterColors
.PARENTHS
);
77 ourMap1
.put(JavaTokenType
.RPARENTH
, SyntaxHighlighterColors
.PARENTHS
);
79 ourMap1
.put(JavaTokenType
.LBRACE
, SyntaxHighlighterColors
.BRACES
);
80 ourMap1
.put(JavaTokenType
.RBRACE
, SyntaxHighlighterColors
.BRACES
);
82 ourMap1
.put(JavaTokenType
.LBRACKET
, SyntaxHighlighterColors
.BRACKETS
);
83 ourMap1
.put(JavaTokenType
.RBRACKET
, SyntaxHighlighterColors
.BRACKETS
);
85 ourMap1
.put(JavaTokenType
.COMMA
, SyntaxHighlighterColors
.COMMA
);
86 ourMap1
.put(JavaTokenType
.DOT
, SyntaxHighlighterColors
.DOT
);
87 ourMap1
.put(JavaTokenType
.SEMICOLON
, SyntaxHighlighterColors
.JAVA_SEMICOLON
);
89 //ourMap1[JavaTokenType.BOOLEAN_LITERAL] = HighlighterColors.JAVA_KEYWORD;
90 //ourMap1[JavaTokenType.NULL_LITERAL] = HighlighterColors.JAVA_KEYWORD;
91 ourMap1
.put(JavaTokenType
.C_STYLE_COMMENT
, SyntaxHighlighterColors
.JAVA_BLOCK_COMMENT
);
92 ourMap1
.put(JavaDocElementType
.DOC_COMMENT
, SyntaxHighlighterColors
.DOC_COMMENT
);
93 ourMap1
.put(JavaTokenType
.END_OF_LINE_COMMENT
, SyntaxHighlighterColors
.LINE_COMMENT
);
94 ourMap1
.put(JavaTokenType
.BAD_CHARACTER
, HighlighterColors
.BAD_CHARACTER
);
96 ourMap1
.put(JavaDocTokenType
.DOC_TAG_NAME
, SyntaxHighlighterColors
.DOC_COMMENT
);
97 ourMap2
.put(JavaDocTokenType
.DOC_TAG_NAME
, SyntaxHighlighterColors
.DOC_COMMENT_TAG
);
99 IElementType
[] javaDocMarkup
= new IElementType
[]{XmlTokenType
.XML_START_TAG_START
,
100 XmlTokenType
.XML_END_TAG_START
,
101 XmlTokenType
.XML_TAG_END
,
102 XmlTokenType
.XML_EMPTY_ELEMENT_END
,
103 XmlTokenType
.TAG_WHITE_SPACE
,
104 XmlTokenType
.XML_TAG_NAME
,
105 XmlTokenType
.XML_NAME
,
106 XmlTokenType
.XML_ATTRIBUTE_VALUE_TOKEN
,
107 XmlTokenType
.XML_ATTRIBUTE_VALUE_START_DELIMITER
,
108 XmlTokenType
.XML_ATTRIBUTE_VALUE_END_DELIMITER
,
109 XmlTokenType
.XML_CHAR_ENTITY_REF
,
110 XmlTokenType
.XML_EQ
};
112 for (IElementType idx
: javaDocMarkup
) {
113 ourMap1
.put(idx
, SyntaxHighlighterColors
.DOC_COMMENT
);
114 ourMap2
.put(idx
, SyntaxHighlighterColors
.DOC_COMMENT_MARKUP
);
119 public Lexer
getHighlightingLexer() {
120 return new JavaHighlightingLexer(myLanguageLevel
);
124 public TextAttributesKey
[] getTokenHighlights(IElementType tokenType
) {
125 return pack(ourMap1
.get(tokenType
), ourMap2
.get(tokenType
));