|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
|
2 // Use of this source code is governed by a BSD-style license that can be |
|
3 // found in the LICENSE file. |
|
4 |
|
5 package org.chromium.debug.ui.editors; |
|
6 |
|
7 import org.eclipse.jface.text.TextAttribute; |
|
8 import org.eclipse.jface.text.rules.BufferedRuleBasedScanner; |
|
9 import org.eclipse.jface.text.rules.EndOfLineRule; |
|
10 import org.eclipse.jface.text.rules.IRule; |
|
11 import org.eclipse.jface.text.rules.IWhitespaceDetector; |
|
12 import org.eclipse.jface.text.rules.IWordDetector; |
|
13 import org.eclipse.jface.text.rules.MultiLineRule; |
|
14 import org.eclipse.jface.text.rules.NumberRule; |
|
15 import org.eclipse.jface.text.rules.SingleLineRule; |
|
16 import org.eclipse.jface.text.rules.Token; |
|
17 import org.eclipse.jface.text.rules.WhitespaceRule; |
|
18 import org.eclipse.jface.text.rules.WordRule; |
|
19 import org.eclipse.swt.SWT; |
|
20 import org.eclipse.swt.graphics.RGB; |
|
21 |
|
22 /** |
|
23 * JavaScript code scanner for source code highlighting. |
|
24 */ |
|
25 public class JsCodeScanner extends BufferedRuleBasedScanner { |
|
26 |
|
27 private Token commentToken; |
|
28 |
|
29 private final TextAttribute commentAttribute = |
|
30 new TextAttribute(EditorColors.getColor(new RGB(63, 127, 95)), null, SWT.NORMAL); |
|
31 |
|
32 private final TextAttribute jsDocAttribute = |
|
33 new TextAttribute(EditorColors.getColor(new RGB(127,127,159)), null, SWT.NORMAL); |
|
34 |
|
35 public JsCodeScanner() { |
|
36 createRules(); |
|
37 } |
|
38 |
|
39 public TextAttribute getCommentAttribute() { |
|
40 return commentAttribute; |
|
41 } |
|
42 |
|
43 public TextAttribute getJsDocAttribute() { |
|
44 return jsDocAttribute; |
|
45 } |
|
46 |
|
47 /** |
|
48 * Use the default Eclipse higlighting scheme. |
|
49 */ |
|
50 private void createRules() { |
|
51 Token keywordToken = new Token( |
|
52 new TextAttribute(EditorColors.getColor(new RGB(127, 0, 85)), null, SWT.BOLD)); |
|
53 |
|
54 commentToken = new Token(commentAttribute); |
|
55 |
|
56 Token jsDocToken = new Token(jsDocAttribute); |
|
57 |
|
58 Token stringToken = new Token( |
|
59 new TextAttribute(EditorColors.getColor(new RGB(42, 0, 255)), null, SWT.NORMAL)); |
|
60 |
|
61 RGB blackRgb = new RGB(0, 0, 0); |
|
62 |
|
63 Token numberToken = new Token( |
|
64 new TextAttribute(EditorColors.getColor(blackRgb), null, SWT.NORMAL)); |
|
65 |
|
66 Token normalToken = new Token( |
|
67 new TextAttribute(EditorColors.getColor(blackRgb), null, SWT.NORMAL)); |
|
68 setDefaultReturnToken(normalToken); |
|
69 |
|
70 setRules(new IRule[] { |
|
71 new EndOfLineRule("//", commentToken), //$NON-NLS-1$ |
|
72 new KeywordRule(keywordToken), |
|
73 new MultiLineRule("/**", "*/", jsDocToken, (char) 0, false), //$NON-NLS-1$ //$NON-NLS-2$ |
|
74 new MultiLineRule("/*", "*/", commentToken, (char) 0, false), //$NON-NLS-1$ //$NON-NLS-2$ |
|
75 new SingleLineRule("\"", "\"", stringToken, '\\'), //$NON-NLS-1$ //$NON-NLS-2$ |
|
76 // Regexp |
|
77 new SingleLineRule("/", "/", stringToken, '\\'), //$NON-NLS-1$ //$NON-NLS-2$ |
|
78 new SingleLineRule("'", "'", stringToken, '\\'), //$NON-NLS-1$ //$NON-NLS-2$ |
|
79 new WhitespaceRule(new IWhitespaceDetector() { |
|
80 public boolean isWhitespace(char c) { |
|
81 return Character.isWhitespace(c); |
|
82 } |
|
83 }), |
|
84 |
|
85 new WordRule(new WordDetector(), normalToken), |
|
86 new NumberRule(numberToken), |
|
87 }); |
|
88 } |
|
89 |
|
90 private static class KeywordRule extends WordRule { |
|
91 |
|
92 private static final String[] KEYWORDS = { |
|
93 "break", //$NON-NLS-1$ |
|
94 "case", //$NON-NLS-1$ |
|
95 "catch", //$NON-NLS-1$ |
|
96 "const", //$NON-NLS-1$ |
|
97 "continue", //$NON-NLS-1$ |
|
98 "debugger", //$NON-NLS-1$ |
|
99 "default", //$NON-NLS-1$ |
|
100 "delete", //$NON-NLS-1$ |
|
101 "do", //$NON-NLS-1$ |
|
102 "else", //$NON-NLS-1$ |
|
103 "false", //$NON-NLS-1$ |
|
104 "finally", //$NON-NLS-1$ |
|
105 "for", //$NON-NLS-1$ |
|
106 "function", //$NON-NLS-1$ |
|
107 "if", //$NON-NLS-1$ |
|
108 "in", //$NON-NLS-1$ |
|
109 "instanceof", //$NON-NLS-1$ |
|
110 "new", //$NON-NLS-1$ |
|
111 "null", //$NON-NLS-1$ |
|
112 "return", //$NON-NLS-1$ |
|
113 "switch", //$NON-NLS-1$ |
|
114 "this", //$NON-NLS-1$ |
|
115 "throw", //$NON-NLS-1$ |
|
116 "true", //$NON-NLS-1$ |
|
117 "try", //$NON-NLS-1$ |
|
118 "typeof", //$NON-NLS-1$ |
|
119 "var", //$NON-NLS-1$ |
|
120 "void", //$NON-NLS-1$ |
|
121 "while", //$NON-NLS-1$ |
|
122 "with", //$NON-NLS-1$ |
|
123 |
|
124 // Highlight important qualifiers |
|
125 "__proto__", //$NON-NLS-1$ |
|
126 "prototype", //$NON-NLS-1$ |
|
127 }; |
|
128 |
|
129 public KeywordRule(Token token) { |
|
130 super(new WordDetector()); |
|
131 |
|
132 for (String word : KEYWORDS) { |
|
133 addWord(word, token); |
|
134 } |
|
135 } |
|
136 } |
|
137 |
|
138 private static class WordDetector implements IWordDetector { |
|
139 |
|
140 public boolean isWordPart(char c) { |
|
141 return Character.isJavaIdentifierPart(c); |
|
142 } |
|
143 |
|
144 public boolean isWordStart(char c) { |
|
145 return Character.isJavaIdentifierStart(c); |
|
146 } |
|
147 |
|
148 } |
|
149 } |