Ignore case for functions in syntax highlighting
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / php / PHPCodeScanner.java
index be00721..fd92df4 100644 (file)
@@ -12,7 +12,9 @@
 package net.sourceforge.phpeclipse.phpeditor.php;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import net.sourceforge.phpdt.internal.ui.text.AbstractJavaScanner;
 import net.sourceforge.phpdt.ui.text.IColorManager;
@@ -22,8 +24,7 @@ import net.sourceforge.phpeclipse.phpeditor.util.PHPWhitespaceDetector;
 import net.sourceforge.phpeclipse.phpeditor.util.PHPWordDetector;
 
 import org.eclipse.jface.preference.IPreferenceStore;
-import org.eclipse.jface.text.BadLocationException;
-import org.eclipse.jface.text.rules.EndOfLineRule;
+import org.eclipse.jface.text.Assert;
 import org.eclipse.jface.text.rules.ICharacterScanner;
 import org.eclipse.jface.text.rules.IRule;
 import org.eclipse.jface.text.rules.IToken;
@@ -40,7 +41,7 @@ public class PHPCodeScanner extends AbstractJavaScanner {
 
   /**
    * Rule to detect java operators.
-   * 
+   *
    * @since 3.0
    */
   protected class OperatorRule implements IRule {
@@ -80,7 +81,7 @@ public class PHPCodeScanner extends AbstractJavaScanner {
 
     /**
      * Creates a new operator rule.
-     * 
+     *
      * @param token
      *          Token to use for this rule
      */
@@ -92,7 +93,7 @@ public class PHPCodeScanner extends AbstractJavaScanner {
 
     /**
      * Is this character an operator character?
-     * 
+     *
      * @param character
      *          Character to determine whether it is an operator character
      * @return <code>true</code> iff the character is an operator, <code>false</code> otherwise.
@@ -141,7 +142,7 @@ public class PHPCodeScanner extends AbstractJavaScanner {
 
     /**
      * Check if lastCharacter/character are a PHP start or end token ( &lt;? ... ?&gt; )
-     * 
+     *
      * @param scanner
      * @param lastCharacter
      * @param character
@@ -181,7 +182,7 @@ public class PHPCodeScanner extends AbstractJavaScanner {
 
       if (character == '`') {
 
-        while (true) {
+        while (character != ICharacterScanner.EOF) {
           character = scanner.read();
           if (character == '\\') {
             character = scanner.read();
@@ -189,7 +190,8 @@ public class PHPCodeScanner extends AbstractJavaScanner {
             return fToken;
           }
         }
-
+        scanner.unread();
+        return Token.UNDEFINED;
       } else {
         scanner.unread();
         return Token.UNDEFINED;
@@ -200,6 +202,7 @@ public class PHPCodeScanner extends AbstractJavaScanner {
 
   private class PHPWordRule extends WordRule {
     private StringBuffer fBuffer = new StringBuffer();
+    protected Map fWordsIgnoreCase = new HashMap();
 
     public PHPWordRule(IWordDetector detector) {
       super(detector, Token.UNDEFINED);
@@ -209,9 +212,24 @@ public class PHPCodeScanner extends AbstractJavaScanner {
       super(detector, defaultToken);
     }
 
+    /**
+        * Adds a word and the token to be returned if it is detected.
+        *
+        * @param word the word this rule will search for, may not be <code>null</code>
+        * @param token the token to be returned if the word has been found, may not be <code>null</code>
+        */
+       public void addWordIgnoreCase(String word, IToken token) {
+               Assert.isNotNull(word);
+               Assert.isNotNull(token);
+
+               fWordsIgnoreCase.put(word, token);
+       }
+
     public IToken evaluate(ICharacterScanner scanner) {
       int c = scanner.read();
       boolean isVariable = false;
+      boolean isUnderscore = false;
+      String word;
       if (c == '<') {
         c = scanner.read();
         if (c != '?') {
@@ -262,16 +280,29 @@ public class PHPCodeScanner extends AbstractJavaScanner {
         if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
 
           fBuffer.setLength(0);
-          do {
-            fBuffer.append((char) c);
+          fBuffer.append((char) c);
+          c = scanner.read();
+          if (c == '_') {
+            isUnderscore = true;
+          }
+          while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c)) {
+               fBuffer.append((char) c);
             c = scanner.read();
-          } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
+          }
           scanner.unread();
 
           if (isVariable) {
+               if (isUnderscore) {
+                       return getToken(IPreferenceConstants.PHP_VARIABLE_DOLLAR);
+               }
             return getToken(IPreferenceConstants.PHP_VARIABLE);
           }
-          IToken token = (IToken) fWords.get(fBuffer.toString());
+          word = fBuffer.toString();
+          IToken token = (IToken) fWords.get(word);
+          if (token != null)
+            return token;
+
+          token = (IToken) fWordsIgnoreCase.get(word.toLowerCase());
           if (token != null)
             return token;
 
@@ -296,6 +327,7 @@ public class PHPCodeScanner extends AbstractJavaScanner {
       IPreferenceConstants.PHP_KEYWORD,
       IPreferenceConstants.PHP_FUNCTIONNAME,
       IPreferenceConstants.PHP_VARIABLE,
+      IPreferenceConstants.PHP_VARIABLE_DOLLAR,
       IPreferenceConstants.PHP_STRING_DQ,
       IPreferenceConstants.PHP_STRING_SQ,
       IPreferenceConstants.PHP_TYPE,
@@ -365,7 +397,7 @@ public class PHPCodeScanner extends AbstractJavaScanner {
           wordRule.addWord(name, keyword);
         }
       } else if (elbuffer instanceof PHPFunction) {
-        wordRule.addWord(((PHPFunction) elbuffer).getName(), functionName);
+        wordRule.addWordIgnoreCase(((PHPFunction) elbuffer).getName(), functionName);
       } else if (elbuffer instanceof PHPType) {
         wordRule.addWord(elbuffer.getName(), type);
       } else if (elbuffer instanceof PHPConstant) {