Added patch for #1434245
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / ui / text / PHPStringDQCodeScanner.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  **********************************************************************/
11 package net.sourceforge.phpdt.ui.text;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import net.sourceforge.phpdt.internal.ui.text.AbstractJavaScanner;
17 import net.sourceforge.phpeclipse.IPreferenceConstants;
18 import net.sourceforge.phpeclipse.phpeditor.util.PHPVariableDetector;
19
20 import org.eclipse.jface.preference.IPreferenceStore;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.rules.ICharacterScanner;
23 import org.eclipse.jface.text.rules.IToken;
24 import org.eclipse.jface.text.rules.IWordDetector;
25 import org.eclipse.jface.text.rules.Token;
26 import org.eclipse.jface.text.rules.WordRule;
27
28 /**
29  * A rule based PHPDoc scanner.
30  */
31 public final class PHPStringDQCodeScanner extends AbstractJavaScanner {
32
33   private static String[] fgTokenProperties = { IPreferenceConstants.PHP_STRING_DQ, IPreferenceConstants.PHP_VARIABLE, IPreferenceConstants.PHP_VARIABLE_DOLLAR};
34
35   private class PHPWordRule extends WordRule {
36         private StringBuffer fBuffer = new StringBuffer();
37
38         public PHPWordRule(IWordDetector detector) {
39           super(detector, Token.UNDEFINED);
40         }
41
42         public PHPWordRule(IWordDetector detector, IToken defaultToken) {
43           super(detector, defaultToken);
44         }
45
46         public IToken evaluate(ICharacterScanner scanner) {
47           int c = scanner.read();
48           boolean isUnderscore = false;
49           if (fDetector.isWordStart((char) c)) {
50                 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
51
52                   fBuffer.setLength(0);
53                   fBuffer.append((char) c);
54                   c = scanner.read();
55                   if (c=='_') {
56                           isUnderscore = true;
57                   }
58                   while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c)) {
59                         fBuffer.append((char) c);
60                         c = scanner.read();
61                         // hack for coloring object elements with variable color instead of string color
62                         if (c=='-') {
63                                 int c2=scanner.read();
64                                 if (c2=='>') {
65                                         fBuffer.append(c);
66                                         fBuffer.append(c2);
67                                         c=scanner.read();
68                                 }
69                                 else {
70                                         scanner.unread();
71                                 }
72                         }
73                         // hack end
74                   }
75                   scanner.unread();
76                   if (isUnderscore) {
77                           return getToken(IPreferenceConstants.PHP_VARIABLE_DOLLAR);
78                   }
79                   return getToken(IPreferenceConstants.PHP_VARIABLE);
80                 }
81           }
82
83           scanner.unread();
84           return Token.UNDEFINED;
85         }
86   }
87
88   public PHPStringDQCodeScanner(IColorManager manager, IPreferenceStore store) {
89         super(manager, store);
90         initialize();
91   }
92
93   public IDocument getDocument() {
94         return fDocument;
95   }
96
97   /*
98    * @see AbstractJavaScanner#getTokenProperties()
99    */
100   protected String[] getTokenProperties() {
101         return fgTokenProperties;
102   }
103
104   /*
105    * @see AbstractJavaScanner#createRules()
106    */
107   protected List createRules() {
108
109         List list = new ArrayList();
110
111         // Add rule for tags.
112         Token token = getToken(IPreferenceConstants.PHP_STRING_DQ);
113         PHPWordRule wordRule = new PHPWordRule(new PHPVariableDetector(), token);
114
115         list.add(wordRule);
116
117         setDefaultReturnToken(getToken(IPreferenceConstants.PHP_STRING_DQ));
118         return list;
119   }
120 }
121