JSHighlighter.java

1    /* 
2     * Copyright 2000-2005 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.lang.javascript; 
17    
18   import com.intellij.lexer.Lexer; 
19   import com.intellij.openapi.editor.HighlighterColors; 
20   import com.intellij.openapi.editor.colors.TextAttributesKey; 
21   import com.intellij.openapi.editor.markup.TextAttributes; 
22   import com.intellij.openapi.fileTypes.SyntaxHighlighterBase; 
23   import com.intellij.psi.StringEscapesTokenTypes; 
24   import com.intellij.psi.tree.IElementType; 
25    
26   import java.awt.*; 
27   import java.util.HashMap; 
28   import java.util.Map; 
29    
30   /** 
31    * Created by IntelliJ IDEA. 
32    * User: max 
33    * Date: Jan 27, 2005 
34    * Time: 11:22:04 PM 
35    * To change this template use File | Settings | File Templates. 
36    */ 
37   public class JSHighlighter extends SyntaxHighlighterBase { 
38     private static Map<IElementType, TextAttributesKey> keys1; 
39     private static Map<IElementType, TextAttributesKey> keys2; 
40    
41     public Lexer getHighlightingLexer() { 
42       //TODO: return new JavaScriptHighlightingLexer(); 
43       return new JavaScriptLexer(); 
44     } 
45    
46     static final TextAttributesKey JS_KEYWORD = TextAttributesKey.createTextAttributesKey( 
47                                                   "JS.KEYWORD", 
48                                                   HighlighterColors.JAVA_KEYWORD.getDefaultAttributes() 
49                                                 ); 
50    
51     static final TextAttributesKey JS_STRING = TextAttributesKey.createTextAttributesKey( 
52                                                  "JS.STRING", 
53                                                  HighlighterColors.JAVA_STRING.getDefaultAttributes() 
54                                                ); 
55    
56     static final TextAttributesKey JS_NUMBER = TextAttributesKey.createTextAttributesKey( 
57                                                  "JS.NUMBER", 
58                                                  HighlighterColors.JAVA_NUMBER.getDefaultAttributes() 
59                                                ); 
60    
61     static final TextAttributesKey JS_REGEXP = TextAttributesKey.createTextAttributesKey( 
62                                                  "JS.REGEXP", 
63                                                  new TextAttributes(Color.blue.brighter(), null, null, null, Font.PLAIN) 
64                                                ); 
65    
66     static final TextAttributesKey JS_LINE_COMMENT = TextAttributesKey.createTextAttributesKey( 
67                                                        "JS.LINE_COMMENT", 
68                                                        HighlighterColors.JAVA_LINE_COMMENT.getDefaultAttributes() 
69                                                      ); 
70    
71     static final TextAttributesKey JS_BLOCK_COMMENT = TextAttributesKey.createTextAttributesKey( 
72                                                         "JS.BLOCK_COMMENT", 
73                                                         HighlighterColors.JAVA_BLOCK_COMMENT.getDefaultAttributes() 
74                                                       ); 
75    
76     static final TextAttributesKey JS_DOC_COMMENT = TextAttributesKey.createTextAttributesKey( 
77                                                       "JS.DOC_COMMENT", 
78                                                       HighlighterColors.JAVA_DOC_COMMENT.getDefaultAttributes() 
79                                                     ); 
80    
81     static final TextAttributesKey JS_OPERATION_SIGN = TextAttributesKey.createTextAttributesKey( 
82                                                          "JS.OPERATION_SIGN", 
83                                                          HighlighterColors.JAVA_OPERATION_SIGN.getDefaultAttributes() 
84                                                        ); 
85    
86     static final TextAttributesKey JS_PARENTHS = TextAttributesKey.createTextAttributesKey( 
87                                                    "JS.PARENTHS", 
88                                                    HighlighterColors.JAVA_PARENTHS.getDefaultAttributes() 
89                                                  ); 
90    
91     static final TextAttributesKey JS_BRACKETS = TextAttributesKey.createTextAttributesKey( 
92                                                    "JS.BRACKETS", 
93                                                    HighlighterColors.JAVA_BRACKETS.getDefaultAttributes() 
94                                                  ); 
95    
96     static final TextAttributesKey JS_BRACES = TextAttributesKey.createTextAttributesKey( 
97                                                  "JS.BRACES", 
98                                                  HighlighterColors.JAVA_BRACES.getDefaultAttributes() 
99                                                ); 
100   
101    static final TextAttributesKey JS_COMMA = TextAttributesKey.createTextAttributesKey( 
102                                                "JS.COMMA", 
103                                                HighlighterColors.JAVA_COMMA.getDefaultAttributes() 
104                                              ); 
105   
106    static final TextAttributesKey JS_DOT = TextAttributesKey.createTextAttributesKey( 
107                                              "JS.DOT", 
108                                              HighlighterColors.JAVA_DOT.getDefaultAttributes() 
109                                            ); 
110   
111    static final TextAttributesKey JS_SEMICOLON = TextAttributesKey.createTextAttributesKey( 
112                                                    "JS.SEMICOLON", 
113                                                    HighlighterColors.JAVA_SEMICOLON.getDefaultAttributes() 
114                                                  ); 
115   
116   
117    static { 
118      keys1 = new HashMap<IElementType, TextAttributesKey>(); 
119      keys2 = new HashMap<IElementType, TextAttributesKey>(); 
120   
121      fillMap(keys1, JSTokenTypes.KEYWORDS, JS_KEYWORD); 
122      fillMap(keys1, JSTokenTypes.OPERATIONS, JS_OPERATION_SIGN); 
123   
124      keys1.put(StringEscapesTokenTypes.VALID_STRING_ESCAPE_TOKEN, HighlighterColors.JAVA_VALID_STRING_ESCAPE); 
125      keys1.put(StringEscapesTokenTypes.INVALID_CHARACTER_ESCAPE_TOKEN, HighlighterColors.JAVA_INVALID_STRING_ESCAPE); 
126      keys1.put(StringEscapesTokenTypes.INVALID_UNICODE_ESCAPE_TOKEN, HighlighterColors.JAVA_INVALID_STRING_ESCAPE); 
127   
128      keys1.put(JSTokenTypes.NUMERIC_LITERAL, JS_NUMBER); 
129      keys1.put(JSTokenTypes.STRING_LITERAL, JS_STRING); 
130      keys1.put(JSTokenTypes.REGEXP_LITERAL, JS_REGEXP); 
131   
132      keys1.put(JSTokenTypes.LPAR, JS_PARENTHS); 
133      keys1.put(JSTokenTypes.RPAR, JS_PARENTHS); 
134   
135      keys1.put(JSTokenTypes.LBRACE, JS_BRACES); 
136      keys1.put(JSTokenTypes.RBRACE, JS_BRACES); 
137   
138      keys1.put(JSTokenTypes.LBRACKET, JS_BRACKETS); 
139      keys1.put(JSTokenTypes.RBRACKET, JS_BRACKETS); 
140   
141      keys1.put(JSTokenTypes.COMMA, JS_COMMA); 
142      keys1.put(JSTokenTypes.DOT, JS_DOT); 
143      keys1.put(JSTokenTypes.SEMICOLON, JS_SEMICOLON); 
144   
145      keys1.put(JSTokenTypes.C_STYLE_COMMENT, JS_BLOCK_COMMENT); 
146      keys1.put(JSTokenTypes.XML_STYLE_COMMENT, JS_BLOCK_COMMENT); 
147      keys1.put(JSTokenTypes.DOC_COMMENT, JS_DOC_COMMENT); 
148      keys1.put(JSTokenTypes.END_OF_LINE_COMMENT, JS_LINE_COMMENT); 
149      keys1.put(JSTokenTypes.BAD_CHARACTER, HighlighterColors.BAD_CHARACTER); 
150    } 
151   
152    public TextAttributesKey[] getTokenHighlights(IElementType tokenType) { 
153      return pack(keys1.get(tokenType), keys2.get(tokenType)); 
154    } 
155   
156    public Map<IElementType, TextAttributesKey> getKeys1() { 
157      return (Map<IElementType, TextAttributesKey>)((HashMap)keys1).clone(); 
158    } 
159   
160    public Map<IElementType, TextAttributesKey> getKeys2() { 
161      return (Map<IElementType, TextAttributesKey>)((HashMap)keys2).clone(); 
162    } 
163  } 
164