fix some navigation action labels
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / builder / IdentifierIndexManager.java
index 7b7954c..1a38bc6 100644 (file)
@@ -32,7 +32,7 @@ import org.eclipse.core.runtime.IStatus;
 
 /**
  * Manages the identifer index information for a specific project
- *  
+ *
  */
 public class IdentifierIndexManager {
   public class LineCreator implements ITerminalSymbols {
@@ -46,7 +46,25 @@ public class IdentifierIndexManager {
 
     /**
      * Add the information of the current identifier to the line
-     * 
+     *
+     * @param typeOfIdentifier
+     *          the type of the identifier ('c'lass, 'd'efine, 'f'unction, 'm'ethod(class), 'v'ariable(class) 'g'lobal variable)
+     * @param identifier
+     *          current identifier
+     * @param line
+     *          Buffer for the current index line
+     */
+    private void addIdentifierInformation(char typeOfIdentifier, char[] identifier, StringBuffer line) {
+      line.append('\t');
+      line.append(typeOfIdentifier);
+      line.append(identifier);
+      //      line.append("\to"); // Offset
+      //      line.append(fScanner.getCurrentTokenStartPosition());
+    }
+
+    /**
+     * Add the information of the current identifier to the line
+     *
      * @param typeOfIdentifier
      *          the type of the identifier ('c'lass, 'd'efine, 'f'unction, 'm'ethod(class), 'v'ariable(class) 'g'lobal variable)
      * @param identifier
@@ -92,21 +110,21 @@ public class IdentifierIndexManager {
     /**
      * Get the next token from input
      */
-    private void getNextToken() {
-      try {
-        fToken = fScanner.getNextToken();
-        if (Scanner.DEBUG) {
-          int currentEndPosition = fScanner.getCurrentTokenEndPosition();
-          int currentStartPosition = fScanner.getCurrentTokenStartPosition();
-          System.out.print(currentStartPosition + "," + currentEndPosition + ": ");
-          System.out.println(fScanner.toStringAction(fToken));
-        }
-        return;
-      } catch (InvalidInputException e) {
-        // ignore errors
-        //        e.printStackTrace();
+    private void getNextToken() throws InvalidInputException {
+      //      try {
+      fToken = fScanner.getNextToken();
+      if (Scanner.DEBUG) {
+        int currentEndPosition = fScanner.getCurrentTokenEndPosition();
+        int currentStartPosition = fScanner.getCurrentTokenStartPosition();
+        System.out.print(currentStartPosition + "," + currentEndPosition + ": ");
+        System.out.println(fScanner.toStringAction(fToken));
       }
-      fToken = TokenNameERROR;
+      return;
+      //      } catch (InvalidInputException e) {
+      //        // ignore errors
+      //        // e.printStackTrace();
+      //      }
+      //      fToken = TokenNameERROR;
     }
 
     private void parseDeclarations(char[] parent, StringBuffer buf, boolean goBack) {
@@ -161,6 +179,30 @@ public class IdentifierIndexManager {
               ident = fScanner.getCurrentIdentifierSource();
               addIdentifierInformation('c', ident, buf, phpdocOffset, phpdocLength);
               getNextToken();
+              if (fToken == TokenNameextends) {
+                getNextToken();
+                while (fToken == TokenNameIdentifier) {
+                  ident = fScanner.getCurrentIdentifierSource();
+                  // extends ident
+                  addIdentifierInformation('e', ident, buf);
+                  getNextToken();
+                  if (fToken == TokenNameCOMMA) {
+                    getNextToken();
+                  }
+                }
+              }
+              if (fToken == TokenNameimplements) {
+                getNextToken();
+                while (fToken == TokenNameIdentifier) {
+                  ident = fScanner.getCurrentIdentifierSource();
+                  // implements ident
+                  addIdentifierInformation('e', ident, buf);
+                  getNextToken();
+                  if (fToken == TokenNameCOMMA) {
+                    getNextToken();
+                  }
+                }
+              }
               //skip tokens for classname, extends and others until we have
               // the opening '{'
               while (fToken != TokenNameLBRACE && fToken != TokenNameEOF && fToken != TokenNameERROR) {
@@ -225,6 +267,8 @@ public class IdentifierIndexManager {
             getNextToken();
           }
         }
+      } catch (InvalidInputException e) {
+        // ignore errors
       } catch (SyntaxError e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
@@ -241,8 +285,8 @@ public class IdentifierIndexManager {
       fScanner.setSource(charArray);
       fScanner.setPHPMode(false);
       fToken = TokenNameEOF;
-      getNextToken();
       try {
+        getNextToken();
         while (fToken != TokenNameEOF) { // && fToken != TokenNameERROR) {
           phpdocOffset = -1;
           hasModifiers = false;
@@ -276,6 +320,30 @@ public class IdentifierIndexManager {
               ident = fScanner.getCurrentIdentifierSource();
               addIdentifierInformation('c', ident, buf, phpdocOffset, phpdocLength);
               getNextToken();
+              if (fToken == TokenNameextends) {
+                getNextToken();
+                while (fToken == TokenNameIdentifier) {
+                  ident = fScanner.getCurrentIdentifierSource();
+                  // extends ident
+                  addIdentifierInformation('e', ident, buf);
+                  getNextToken();
+                  if (fToken == TokenNameCOMMA) {
+                    getNextToken();
+                  }
+                }
+              }
+              if (fToken == TokenNameimplements) {
+                getNextToken();
+                while (fToken == TokenNameIdentifier) {
+                  ident = fScanner.getCurrentIdentifierSource();
+                  // implements ident
+                  addIdentifierInformation('e', ident, buf);
+                  getNextToken();
+                  if (fToken == TokenNameCOMMA) {
+                    getNextToken();
+                  }
+                }
+              }
               //skip fTokens for classname, extends and others until we have
               // the opening '{'
               while (fToken != TokenNameLBRACE && fToken != TokenNameEOF && fToken != TokenNameERROR) {
@@ -310,6 +378,8 @@ public class IdentifierIndexManager {
             getNextToken();
           }
         }
+      } catch (InvalidInputException e) {
+        // ignore errors
       } catch (SyntaxError e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
@@ -345,7 +415,7 @@ public class IdentifierIndexManager {
 
   /**
    * Check if 2 char arrays are equal
-   * 
+   *
    * @param a
    * @param b
    * @return
@@ -368,7 +438,7 @@ public class IdentifierIndexManager {
 
   /**
    * Add the information for a given IFile resource
-   *  
+   *
    */
   public void addFile(IFile fileToParse) {
     //    InputStream iStream;
@@ -411,10 +481,80 @@ public class IdentifierIndexManager {
 
   /**
    * Adds a line of the index file for function, class, class-method and class-variable names
-   * 
+   *
    * @param line
    */
   private void addLine(String line) {
+    addLine(fIndentifierMap, fFileMap, line, null);
+  }
+
+  public TreeMap getIdentifiers(IFile file) {
+    TreeMap treeMap = new TreeMap(new StringComparator());
+    addIdentifiers(treeMap, file);
+    return treeMap;
+  }
+
+  public TreeMap getIdentifiers(String startClazz) {
+    TreeMap treeMap = new TreeMap(new StringComparator());
+    addIdentifiers(treeMap, startClazz);
+    return treeMap;
+  }
+
+  public void addIdentifiers(TreeMap treeMap, IFile file) {
+    String line = (String) fFileMap.get(file.getProjectRelativePath().toString());
+    if (line != null) {
+      PHPIdentifierLocation ident;
+      ArrayList allClassNames = new ArrayList();
+      addLine(treeMap, null, line, allClassNames);
+      int i = 0;
+      while (i < allClassNames.size()) {
+        String clazz = (String) allClassNames.get(i++);
+        addClassName(treeMap, clazz, allClassNames);
+      }
+    }
+  }
+
+  public void addIdentifiers(TreeMap treeMap, String startClazz) {
+    PHPIdentifierLocation ident;
+    ArrayList allClassNames = new ArrayList();
+    addClassName(treeMap, startClazz, allClassNames);
+    int i = 0;
+    while (i < allClassNames.size()) {
+      String clazz = (String) allClassNames.get(i++);
+      addClassName(treeMap, clazz, allClassNames);
+    }
+  }
+
+  /**
+   * @param treeMap
+   * @param clazz
+   * @param allClassNames
+   */
+  private boolean addClassName(TreeMap treeMap, String clazz, List allClassNames) {
+    String line;
+    PHPIdentifierLocation ident;
+    List list = getLocations(clazz);
+    if (list == null) {
+      return false;
+    }
+    boolean result = false;
+    for (int i = 0; i < list.size(); i++) {
+      ident = (PHPIdentifierLocation) list.get(i);
+      if (ident.isClass()) {
+        line = (String) fFileMap.get(ident.getFilename());
+        addLine(treeMap, null, line, allClassNames);
+        result = true;
+      }
+    }
+    return result;
+  }
+
+  /**
+   * Adds a line of the index file for function, class, class-method and class-variable names
+   *
+   * @param line
+   */
+  public void addLine(TreeMap treeMap, HashMap fileMap, String line, List allClassNames) {
     StringTokenizer tokenizer;
     String phpFileName = null;
     String token;
@@ -448,6 +588,18 @@ public class IdentifierIndexManager {
           identifier = token.substring(1);
           phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.DEFINE, phpFileName);
           break;
+        case 'e':
+          // extends <class name>
+          // not in map
+          identifier = null;
+          phpIdentifier = null;
+          if (allClassNames != null) {
+            String extName = token.substring(1);
+            if (!allClassNames.contains(extName)) {
+              allClassNames.add(extName);
+            }
+          }
+          break;
         case 'f':
           // function name
           identifier = token.substring(1);
@@ -458,6 +610,18 @@ public class IdentifierIndexManager {
           identifier = token.substring(1);
           phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.GLOBAL_VARIABLE, phpFileName);
           break;
+        case 'i':
+          // implements <class name>
+          // not in map
+          identifier = null;
+          phpIdentifier = null;
+          if (allClassNames != null) {
+            String implName = token.substring(1);
+            if (!allClassNames.contains(implName)) {
+              allClassNames.add(implName);
+            }
+          }
+          break;
         case 'k':
           // constructor function name
           identifier = token.substring(1);
@@ -505,11 +669,11 @@ public class IdentifierIndexManager {
         }
         if (identifier != null && phpIdentifier != null) {
           tokenExists = true;
-          ArrayList list = (ArrayList) fIndentifierMap.get(identifier);
+          ArrayList list = (ArrayList) treeMap.get(identifier);
           if (list == null) {
             list = new ArrayList();
             list.add(phpIdentifier);
-            fIndentifierMap.put(identifier, list);
+            treeMap.put(identifier, list);
           } else {
             boolean flag = false;
             for (int i = 0; i < list.size(); i++) {
@@ -524,19 +688,21 @@ public class IdentifierIndexManager {
           }
         }
       }
-      fFileMap.put(phpFileName, line);
+      if (fileMap != null) {
+        fileMap.put(phpFileName, line);
+      }
     } catch (Throwable e) {
       // write to workspace/.metadata/.log file
       PHPeclipsePlugin.log(e);
     }
     //    if (tokenExists) {
-    
+
     //    }
   }
 
   /**
    * Change the information for a given IFile resource
-   *  
+   *
    */
   public void changeFile(IFile fileToParse) {
     removeFile(fileToParse);
@@ -545,17 +711,21 @@ public class IdentifierIndexManager {
 
   /**
    * Get a list of all PHPIdentifierLocation object's associated with an identifier
-   * 
+   *
    * @param identifier
    * @return
    */
   public List getLocations(String identifier) {
-    return (List) fIndentifierMap.get(identifier);
+         List list=(List) fIndentifierMap.get(identifier);
+         if (list!=null) {
+                 return list;
+         }
+         return new ArrayList();
   }
 
   /**
    * Initialize (i.e. clear) the current index information
-   *  
+   *
    */
   public void initialize() {
     fIndentifierMap = new TreeMap(new StringComparator());
@@ -586,7 +756,7 @@ public class IdentifierIndexManager {
 
   /**
    * Remove the information for a given IFile resource
-   *  
+   *
    */
   public void removeFile(IFile fileToParse) {
     //    String line = (String)
@@ -599,7 +769,7 @@ public class IdentifierIndexManager {
 
   /**
    * Removes a line of the index file for function, class, class-method and class-variable names
-   * 
+   *
    * @param line
    */
   private void removeLine(String line) {
@@ -635,6 +805,11 @@ public class IdentifierIndexManager {
         identifier = token.substring(1);
         phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.DEFINE, phpFileName);
         break;
+      case 'e':
+        // extends <class name>
+        identifier = null;
+        phpIdentifier = null;
+        break;
       case 'f':
         // function name
         identifier = token.substring(1);
@@ -645,6 +820,11 @@ public class IdentifierIndexManager {
         identifier = token.substring(1);
         phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.GLOBAL_VARIABLE, phpFileName);
         break;
+      case 'i':
+        // implements <class name>
+        identifier = null;
+        phpIdentifier = null;
+        break;
       case 'k':
         // constructor function name
         identifier = token.substring(1);
@@ -699,7 +879,7 @@ public class IdentifierIndexManager {
 
   /**
    * Save the current index information in the projects index file
-   *  
+   *
    */
   public void writeFile() {
     FileWriter fileWriter;