1. Suppress ArrayIndexOutOfBoundsException occurring in a certain case of formatting...
[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 = {
34                         IPreferenceConstants.PHP_STRING_DQ,
35                         IPreferenceConstants.PHP_VARIABLE,
36                         IPreferenceConstants.PHP_VARIABLE_DOLLAR };
37
38         private class PHPWordRule extends WordRule {
39                 private StringBuffer fBuffer = new StringBuffer();
40
41                 public PHPWordRule(IWordDetector detector) {
42                         super(detector, Token.UNDEFINED);
43                 }
44
45                 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
46                         super(detector, defaultToken);
47                 }
48
49                 public IToken evaluate(ICharacterScanner scanner) {
50                         int c = scanner.read();
51                         boolean isUnderscore = false;
52                         if (fDetector.isWordStart((char) c)) {
53                                 if (fColumn == UNDEFINED
54                                                 || (fColumn == scanner.getColumn() - 1)) {
55
56                                         fBuffer.setLength(0);
57                                         fBuffer.append((char) c);
58                                         c = scanner.read();
59                                         if (c == '_') {
60                                                 isUnderscore = true;
61                                         }
62                                         while (c != ICharacterScanner.EOF
63                                                         && fDetector.isWordPart((char) c)) {
64                                                 fBuffer.append((char) c);
65                                                 c = scanner.read();
66                                                 // hack for coloring object elements with variable color
67                                                 // instead of string color
68                                                 if (c == '-') {
69                                                         int c2 = scanner.read();
70                                                         if (c2 == '>') {
71                                                                 fBuffer.append(c);
72                                                                 fBuffer.append(c2);
73                                                                 c = scanner.read();
74                                                         } else {
75                                                                 scanner.unread();
76                                                         }
77                                                 }
78                                                 // hack end
79                                         }
80                                         scanner.unread();
81                                         if (isUnderscore) {
82                                                 return getToken(IPreferenceConstants.PHP_VARIABLE_DOLLAR);
83                                         }
84                                         return getToken(IPreferenceConstants.PHP_VARIABLE);
85                                 }
86                         }
87
88                         scanner.unread();
89                         return Token.UNDEFINED;
90                 }
91         }
92
93         public PHPStringDQCodeScanner(IColorManager manager, IPreferenceStore store) {
94                 super(manager, store);
95                 initialize();
96         }
97
98         public IDocument getDocument() {
99                 return fDocument;
100         }
101
102         /*
103          * @see AbstractJavaScanner#getTokenProperties()
104          */
105         protected String[] getTokenProperties() {
106                 return fgTokenProperties;
107         }
108
109         /*
110          * @see AbstractJavaScanner#createRules()
111          */
112         protected List createRules() {
113
114                 List list = new ArrayList();
115
116                 // Add rule for tags.
117                 Token token = getToken(IPreferenceConstants.PHP_STRING_DQ);
118                 PHPWordRule wordRule = new PHPWordRule(new PHPVariableDetector(), token);
119
120                 list.add(wordRule);
121
122                 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_STRING_DQ));
123                 return list;
124         }
125 }