Added PHPUnitEditor and corresponding PHPPreferencePage
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / php / PHPCodeScanner.java
1 /**********************************************************************
2 Copyright (c) 2000, 2002 IBM Corp. and others.
3 All rights reserved. This program and the accompanying materials
4 are made available under the terms of the Common Public License v1.0
5 which accompanies this distribution, and is available at
6 http://www.eclipse.org/legal/cpl-v10.html
7
8 Contributors:
9     IBM Corporation - Initial implementation
10     Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor.php;
13
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Vector;
17
18 import net.sourceforge.phpdt.internal.ui.text.JavaColorManager;
19 import net.sourceforge.phpeclipse.IPreferenceConstants;
20 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
21 import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
22 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
23 import net.sourceforge.phpeclipse.phpeditor.util.PHPWhitespaceDetector;
24 import net.sourceforge.phpeclipse.phpeditor.util.PHPWordDetector;
25
26 import org.eclipse.jface.preference.IPreferenceStore;
27 import org.eclipse.jface.preference.PreferenceConverter;
28 import org.eclipse.jface.text.TextAttribute;
29 import org.eclipse.jface.text.rules.EndOfLineRule;
30 import org.eclipse.jface.text.rules.ICharacterScanner;
31 import org.eclipse.jface.text.rules.IRule;
32 import org.eclipse.jface.text.rules.IToken;
33 import org.eclipse.jface.text.rules.IWordDetector;
34 import org.eclipse.jface.text.rules.MultiLineRule;
35 import org.eclipse.jface.text.rules.RuleBasedScanner;
36 import org.eclipse.jface.text.rules.SingleLineRule;
37 import org.eclipse.jface.text.rules.Token;
38 import org.eclipse.jface.text.rules.WhitespaceRule;
39 import org.eclipse.jface.text.rules.WordRule;
40 import org.eclipse.swt.SWT;
41 import org.eclipse.swt.graphics.Color;
42
43 /**
44  * PHP Code Scanner
45  */
46 public class PHPCodeScanner extends RuleBasedScanner implements IPreferenceConstants {
47
48   private static Token variable;
49   private static Token keyword;
50   private static Token type;
51   private static Token constant;
52   private static Token functionName;
53   private static Token string;
54   private static Token comment;
55   private static Token multi_comment;
56   private static Token other;
57
58   private class PHPWordRule extends WordRule {
59     private StringBuffer fBuffer = new StringBuffer();
60
61     public PHPWordRule(IWordDetector detector) {
62       super(detector, Token.UNDEFINED);
63     }
64
65     public PHPWordRule(IWordDetector detector, IToken defaultToken) {
66       super(detector, defaultToken);
67     }
68
69     public IToken evaluate(ICharacterScanner scanner) {
70       int c = scanner.read();
71       boolean isVariable = false;
72       if (fDetector.isWordStart((char) c)) {
73         if (c == '$') {
74           isVariable = true;
75         }
76         if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
77
78           fBuffer.setLength(0);
79           do {
80             fBuffer.append((char) c);
81             c = scanner.read();
82           } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
83           scanner.unread();
84
85           if (isVariable) {
86             return variable;
87           }
88           IToken token = (IToken) fWords.get(fBuffer.toString());
89           if (token != null)
90             return token;
91
92           if (fDefaultToken.isUndefined())
93             unreadBuffer(scanner);
94
95           return fDefaultToken;
96         }
97       }
98
99       scanner.unread();
100       return Token.UNDEFINED;
101     }
102   }
103
104   private PHPColorProvider fColorProvider;
105
106   /**
107         * Creates a PHP code scanner
108         */
109   public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store) {
110   //  final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
111     Color BackgroundColor = provider.getColor(PreferenceConverter.getColor(store, PHP_EDITOR_BACKGROUND));
112     variable =
113       new Token(
114         new TextAttribute(
115           provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE)),
116           BackgroundColor,
117           (store.getBoolean(PHP_VARIABLE_BOLD) ? SWT.BOLD : SWT.NONE)
118             + (store.getBoolean(PHP_VARIABLE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
119       keyword =
120         new Token(new TextAttribute(
121           provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD)),
122           BackgroundColor,
123     //SWT.NONE));
124    (store.getBoolean(PHP_KEYWORD_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_KEYWORD_ITALIC) ? SWT.ITALIC : SWT.NONE)));
125       type =
126         new Token(new TextAttribute(
127           provider.getColor(PreferenceConverter.getColor(store, PHP_TYPE)),
128           BackgroundColor,
129     //SWT.NONE));
130    (store.getBoolean(PHP_TYPE_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_TYPE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
131       functionName =
132         new Token(new TextAttribute(
133           provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)),
134           BackgroundColor,
135     //SWT.NONE));
136   (store.getBoolean(PHP_FUNCTIONNAME_BOLD) ? SWT.BOLD : SWT.NONE)
137     + (store.getBoolean(PHP_FUNCTIONNAME_ITALIC) ? SWT.ITALIC : SWT.NONE)));
138       constant =
139         new Token(new TextAttribute(
140           provider.getColor(PreferenceConverter.getColor(store, PHP_CONSTANT)),
141           BackgroundColor,
142     //SWT.NONE));
143    (store.getBoolean(PHP_CONSTANT_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_CONSTANT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
144       string =
145         new Token(new TextAttribute(
146           provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)),
147           BackgroundColor,
148     //SWT.NONE));
149    (store.getBoolean(PHP_STRING_BOLD) ? SWT.BOLD : SWT.NONE ) + (store.getBoolean(PHP_STRING_ITALIC) ? SWT.ITALIC : SWT.NONE)));
150       comment =
151         new Token(new TextAttribute(
152           provider.getColor(PreferenceConverter.getColor(store, PHP_SINGLELINE_COMMENT)),
153           BackgroundColor,
154     //SWT.NONE));
155   (store.getBoolean(PHP_SINGLELINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE )
156     + (store.getBoolean(PHP_SINGLELINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
157       multi_comment =
158         new Token(new TextAttribute(
159           provider.getColor(PreferenceConverter.getColor(store, PHP_MULTILINE_COMMENT)),
160           BackgroundColor,
161     //SWT.NONE));
162   (store.getBoolean(PHP_MULTILINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
163     + (store.getBoolean(PHP_MULTILINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
164       other =
165         new Token(new TextAttribute(
166           provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)),
167           BackgroundColor,
168     //SWT.NONE));
169    (store.getBoolean(PHP_DEFAULT_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_DEFAULT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
170     updateWordRules();
171   }
172
173   public void updateToken(JavaColorManager provider) {
174     final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
175
176     Color BackgroundColor = provider.getColor(PreferenceConverter.getColor(store, PHP_EDITOR_BACKGROUND));
177
178     variable.setData(
179       new TextAttribute(
180         provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE)),
181         BackgroundColor,
182         (store.getBoolean(PHP_VARIABLE_BOLD) ? SWT.BOLD : SWT.NONE)
183           + (store.getBoolean(PHP_VARIABLE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
184     keyword.setData(
185       new TextAttribute(
186         provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD)),
187         BackgroundColor,
188         (store.getBoolean(PHP_KEYWORD_BOLD) ? SWT.BOLD : SWT.NONE)
189           + (store.getBoolean(PHP_KEYWORD_ITALIC) ? SWT.ITALIC : SWT.NONE)));
190     type.setData(
191       new TextAttribute(
192         provider.getColor(PreferenceConverter.getColor(store, PHP_TYPE)),
193         BackgroundColor,
194         (store.getBoolean(PHP_TYPE_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_TYPE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
195     functionName.setData(
196       new TextAttribute(
197         provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)),
198         BackgroundColor,
199         (store.getBoolean(PHP_FUNCTIONNAME_BOLD) ? SWT.BOLD : SWT.NONE)
200           + (store.getBoolean(PHP_FUNCTIONNAME_ITALIC) ? SWT.ITALIC : SWT.NONE)));
201     constant.setData(
202       new TextAttribute(
203         provider.getColor(PreferenceConverter.getColor(store, PHP_CONSTANT)),
204         BackgroundColor,
205         (store.getBoolean(PHP_CONSTANT_BOLD) ? SWT.BOLD : SWT.NONE)
206           + (store.getBoolean(PHP_CONSTANT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
207     string.setData(
208       new TextAttribute(
209         provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)),
210         BackgroundColor,
211         (store.getBoolean(PHP_STRING_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_STRING_ITALIC) ? SWT.ITALIC : SWT.NONE)));
212     comment.setData(
213       new TextAttribute(
214         provider.getColor(PreferenceConverter.getColor(store, PHP_SINGLELINE_COMMENT)),
215         BackgroundColor,
216         (store.getBoolean(PHP_SINGLELINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
217           + (store.getBoolean(PHP_SINGLELINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
218     multi_comment.setData(
219       new TextAttribute(
220         provider.getColor(PreferenceConverter.getColor(store, PHP_MULTILINE_COMMENT)),
221         BackgroundColor,
222         (store.getBoolean(PHP_MULTILINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
223           + (store.getBoolean(PHP_MULTILINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
224     other.setData(
225       new TextAttribute(
226         provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)),
227         BackgroundColor,
228         (store.getBoolean(PHP_DEFAULT_BOLD) ? SWT.BOLD : SWT.NONE)
229           + (store.getBoolean(PHP_DEFAULT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
230   }
231
232   public void updateWordRules() {
233     List rules = new ArrayList();
234     // Add rule for single line comments.
235     rules.add(new EndOfLineRule("//", comment)); //$NON-NLS-1$
236     rules.add(new EndOfLineRule("#", comment));
237     // Add rule for strings and character constants.
238     rules.add(new MultiLineRule("\"", "\"", string, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
239     rules.add(new SingleLineRule("'", "'", string, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
240     // rules.add(new SingleLineRule("//", "//", php_comment));
241     rules.add(new MultiLineRule("/*", "*/", multi_comment));
242     // Add generic whitespace rule.
243     rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
244     // Add word rule for keywords, types, and constants.
245     PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), other);
246
247     PHPSyntaxRdr.readInSyntax();
248     Vector buffer = PHPSyntaxRdr.getsyntaxdata();
249     String strbuffer = null;
250     PHPElement elbuffer = null;
251     while ((buffer != null) && (!buffer.isEmpty() && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
252       if (elbuffer instanceof PHPKeyword)
253         wordRule.addWord(((PHPKeyword) elbuffer).getName(), keyword);
254       if (elbuffer instanceof PHPFunction)
255         wordRule.addWord(((PHPFunction) elbuffer).getName(), functionName);
256       if (elbuffer instanceof PHPType)
257         wordRule.addWord(elbuffer.getName(), type);
258       if (elbuffer instanceof PHPConstant)
259         wordRule.addWord(elbuffer.getName(), constant);
260     }
261     rules.add(wordRule);
262     IRule[] result = new IRule[rules.size()];
263     rules.toArray(result);
264     setRules(result);
265   }
266 }