Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / builder / IdentifierIndexManager.java
index 1047779..2e76596 100644 (file)
@@ -1,5 +1,6 @@
 package net.sourceforge.phpeclipse.builder;
 
+import java.io.BufferedInputStream;
 import java.io.BufferedReader;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
@@ -20,7 +21,8 @@ import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
 import net.sourceforge.phpdt.internal.compiler.parser.SyntaxError;
-import net.sourceforge.phpeclipse.mover.obfuscator.PHPIdentifier;
+import net.sourceforge.phpdt.internal.compiler.util.Util;
+import net.sourceforge.phpeclipse.obfuscator.PHPIdentifier;
 
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.runtime.CoreException;
@@ -89,9 +91,9 @@ public class IdentifierIndexManager {
       fToken = TokenNameERROR;
     }
 
-    private void parseDeclarations(StringBuffer buf, boolean goBack) {
+    private void parseDeclarations(char[] parent, StringBuffer buf, boolean goBack) {
       char[] ident;
-                       char[] classVariable;
+      char[] classVariable;
       int counter = 0;
       int phpdocOffset = -1;
       int phpdocLength = -1;
@@ -111,8 +113,8 @@ public class IdentifierIndexManager {
             getNextToken();
             if (fToken == TokenNameVariable) {
               ident = fScanner.getCurrentIdentifierSource();
-                                                       classVariable = new char[ident.length-1];
-                                                       System.arraycopy(ident, 1, classVariable, 0, ident.length-1);
+              classVariable = new char[ident.length - 1];
+              System.arraycopy(ident, 1, classVariable, 0, ident.length - 1);
               addIdentifierInformation('v', classVariable, buf, phpdocOffset, phpdocLength);
               getNextToken();
             }
@@ -123,9 +125,20 @@ public class IdentifierIndexManager {
             }
             if (fToken == TokenNameIdentifier) {
               ident = fScanner.getCurrentIdentifierSource();
-              addIdentifierInformation('m', ident, buf, phpdocOffset, phpdocLength);
+              if (parent != null && equalCharArrays(parent, ident)) {
+                // constructor function
+                addIdentifierInformation('k', ident, buf, phpdocOffset, phpdocLength);
+              } else {
+                if (parent != null) {
+                  // class method function
+                  addIdentifierInformation('m', ident, buf, phpdocOffset, phpdocLength);
+                } else {
+                  // nested function ?!
+                  addIdentifierInformation('f', ident, buf, phpdocOffset, phpdocLength);
+                }
+              }
               getNextToken();
-              parseDeclarations(buf, true);
+              parseDeclarations(null, buf, true);
             }
           } else if (fToken == TokenNameclass) {
             getNextToken();
@@ -138,7 +151,7 @@ public class IdentifierIndexManager {
               while (fToken != TokenNameLBRACE && fToken != TokenNameEOF && fToken != TokenNameERROR) {
                 getNextToken();
               }
-              parseDeclarations(buf, true);
+              parseDeclarations(ident, buf, true);
             }
           } else if (fToken == TokenNamedefine) {
             getNextToken();
@@ -201,7 +214,7 @@ public class IdentifierIndexManager {
               ident = fScanner.getCurrentIdentifierSource();
               addIdentifierInformation('f', ident, buf, phpdocOffset, phpdocLength);
               getNextToken();
-              parseDeclarations(buf, true);
+              parseDeclarations(null, buf, true);
             }
           } else if (fToken == TokenNameclass) {
             getNextToken();
@@ -215,7 +228,7 @@ public class IdentifierIndexManager {
                 getNextToken();
               }
 
-              parseDeclarations(buf, true);
+              parseDeclarations(ident, buf, true);
 
             }
           } else if (fToken == TokenNamedefine) {
@@ -239,18 +252,18 @@ public class IdentifierIndexManager {
     }
   }
 
-       class StringComparator implements Comparator { 
-               public int compare(Object o1, Object o2) { 
-                       String s1 = (String)o1; 
-                       String s2 = (String)o2; 
-                       return s1.compareTo(s2); 
-               //      return s1.toUpperCase().compareTo(s2.toUpperCase()); 
-               }                                     
-               public boolean equals(Object o) { 
-                       String s = (String)o; 
-                       return compare(this, o)==0; 
-               } 
-       } 
+  class StringComparator implements Comparator {
+    public int compare(Object o1, Object o2) {
+      String s1 = (String) o1;
+      String s2 = (String) o2;
+      return s1.compareTo(s2);
+      //       return s1.toUpperCase().compareTo(s2.toUpperCase()); 
+    }
+    public boolean equals(Object o) {
+      String s = (String) o;
+      return compare(this, o) == 0;
+    }
+  }
 
   private HashMap fFileMap;
   private String fFilename;
@@ -263,32 +276,64 @@ public class IdentifierIndexManager {
   }
 
   /**
+   * Check if 2 char arrays are equal
+   * 
+   * @param a
+   * @param b
+   * @return
+   */
+  private static boolean equalCharArrays(char[] a, char[] b) {
+    if (a.length != b.length) {
+      return false;
+    }
+    for (int i = 0; i < b.length; i++) {
+      if (a[i] != b[i]) {
+        return false;
+      }
+    }
+    return true;
+  }
+
+  /**
    * Add the information for a given IFile resource
    *
    */
   public void addFile(IFile fileToParse) {
-    InputStream iStream;
+    //    InputStream iStream;
     LineCreator lineCreator = new LineCreator();
     try {
-      iStream = fileToParse.getContents();
-
-      StringBuffer buf = new StringBuffer();
-      int c0;
+      //      iStream = fileToParse.getContents();
+      //
+      //      StringBuffer buf = new StringBuffer();
+      //      int c0;
+      //      try {
+      //        while ((c0 = iStream.read()) != (-1)) {
+      //          buf.append((char) c0);
+      //        }
+      //      } catch (IOException e) {
+      //        return;
+      //      }
+      InputStream stream = null;
       try {
-        while ((c0 = iStream.read()) != (-1)) {
-          buf.append((char) c0);
+        stream = new BufferedInputStream(fileToParse.getContents());
+
+        StringBuffer lineBuffer = new StringBuffer();
+        lineBuffer.append(fileToParse.getFullPath().toString());
+        int lineLength = lineBuffer.length();
+        // lineCreator.parseIdentifiers(buf.toString().toCharArray(), lineBuffer);
+        lineCreator.parseIdentifiers(Util.getInputStreamAsCharArray(stream, -1, null), lineBuffer);
+        if (lineLength != lineBuffer.length()) {
+          addLine(lineBuffer.toString());
         }
       } catch (IOException e) {
         return;
-      }
-
-      StringBuffer lineBuffer = new StringBuffer();
-      //      lineBuffer.append(fileToParse.getLocation().toString());
-      lineBuffer.append(fileToParse.getFullPath().toString());
-      int lineLength = lineBuffer.length();
-      lineCreator.parseIdentifiers(buf.toString().toCharArray(), lineBuffer);
-      if (lineLength != lineBuffer.length()) {
-        addLine(lineBuffer.toString());
+      } finally {
+        try {
+          if (stream != null) {
+            stream.close();
+          }
+        } catch (IOException e) {
+        }
       }
     } catch (CoreException e1) {
       // TODO Auto-generated catch block
@@ -337,6 +382,10 @@ public class IdentifierIndexManager {
           identifier = token.substring(1);
           phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.FUNCTION, phpFileName);
           break;
+        case 'k' : // constructor function name
+          identifier = token.substring(1);
+          phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.CONSTRUCTOR, phpFileName);
+          break;
         case 'm' : //method inside a class
           identifier = token.substring(1);
           phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.METHOD, phpFileName, classname);
@@ -504,6 +553,10 @@ public class IdentifierIndexManager {
           identifier = token.substring(1);
           phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.FUNCTION, phpFileName);
           break;
+        case 'k' : // constructor function name
+          identifier = token.substring(1);
+          phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.CONSTRUCTOR, phpFileName);
+          break;
         case 'm' : //method inside a class
           identifier = token.substring(1);
           phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.METHOD, phpFileName, classname);
@@ -553,13 +606,13 @@ public class IdentifierIndexManager {
       }
       fileWriter.close();
     } catch (FileNotFoundException e) {
-       // ignore exception; project is deleted by user
+      // ignore exception; project is deleted by user
     } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
   }
-  
+
   /**
    * @param fromKey
    * @param toKey