added h4 (====) gheader template
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / actions / PHPOpenDeclarationEditorAction.java
index a582f1f..486f7cf 100644 (file)
@@ -1,26 +1,29 @@
-/**********************************************************************
-Copyright (c) 2000, 2002 IBM Corp. and others.
-All rights reserved. This program and the accompanying materials
-are made available under the terms of the Common Public License v1.0
-which accompanies this distribution, and is available at
-http://www.eclipse.org/legal/cpl-v10.html
-
-Contributors:
-    Klaus Hartlage - www.eclipseproject.de
-**********************************************************************/
+/***********************************************************************************************************************************
+ * Copyright (c) 2000, 2002 IBM Corp. and others. All rights reserved. This program and the accompanying materials are made
+ * available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ * 
+ * Contributors: www.phpeclipse.de
+ **********************************************************************************************************************************/
 package net.sourceforge.phpeclipse.actions;
 
+import java.util.Collections;
 import java.util.List;
+import java.util.Set;
 
+import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil;
 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
 import net.sourceforge.phpeclipse.phpeditor.php.PHPWordExtractor;
 
+import org.eclipse.core.resources.IContainer;
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
+import org.eclipse.core.internal.resources.File;
 import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
 import org.eclipse.jface.action.IAction;
 import org.eclipse.jface.text.BadLocationException;
 import org.eclipse.jface.text.IDocument;
@@ -41,9 +44,13 @@ import org.eclipse.ui.internal.dialogs.ListContentProvider;
 public class PHPOpenDeclarationEditorAction extends ActionDelegate implements IEditorActionDelegate {
 
   private IWorkbenchWindow fWindow;
+
   private PHPEditor fEditor;
+
   private IProject fProject;
 
+  private boolean isIncludeString;
+
   public void dispose() {
   }
 
@@ -77,60 +84,151 @@ public class PHPOpenDeclarationEditorAction extends ActionDelegate implements IE
       ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection();
       IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
       int pos = selection.getOffset();
-    //  System.out.println(selection.getText());
-      String word = getPHPIdentifier(doc, pos);
+      //  System.out.println(selection.getText());
+      String identifierOrInclude = getIdentifierOrInclude(doc, pos);
       //      System.out.println(word);
-      if (word != null && !word.equals("")) {
+      if (identifierOrInclude != null && !identifierOrInclude.equals("")) {
+        if (isIncludeString) {
+          openIncludeFile(identifierOrInclude);
+        } else {
+          openIdentifierDeclaration(f, identifierOrInclude);
+        }
+      }
+    }
+  }
+
+  /**
+   * @param filename
+   */
+  private void openIncludeFile(String filename) {
+    if (filename != null && !filename.equals("")) {
+      try {
+        IFile currentFile = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
+        IPath path = PHPFileUtil.determineFilePath(filename, currentFile, fProject);
+        if (path != null) { 
+//          String projectPath = fProject.getLocation().toString();
+//          String filePath = path.toString().substring(projectPath.length()+1);
+//          IFile file = fProject.getFile(filePath);
+          IFile file = PHPFileUtil.createFile(path, fProject);
+          //        IFile file = getIncludeFile(fProject, (IFileEditorInput) fEditor.getEditorInput(), filename);
+          if (file != null && file.exists()) {
+            PHPeclipsePlugin.getDefault().openFileInTextEditor(file.getLocation().toString());
+            return;
+          }
+        }
+      } catch (Exception e) {
+        // ignore
+      }
+
+      try {
+
         IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
-        List list = indexManager.getLocations(word);
+        //            filename = StringUtil.replaceRegExChars(filename);
+        List list = indexManager.getFileList(filename);
         if (list != null && list.size() > 0) {
-          String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
-          // TODO show all entries of the list in a dialog box
-          // at the moment always the first entry will be opened
-          if (list.size() > 1) {
-            ListSelectionDialog listSelectionDialog =
-              new ListSelectionDialog(
-                PHPeclipsePlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell(),
-                list,
-                new ListContentProvider(),
-                new LabelProvider(),
-                "Select the resources to open.");
-            listSelectionDialog.setTitle("Multiple declarations found");
-            if (listSelectionDialog.open() == Window.OK) {
-              Object[] locations = listSelectionDialog.getResult();
-              if (locations != null) {
-                try {
-                  for (int i = 0; i < locations.length; i++) {
-                    PHPIdentifierLocation location = (PHPIdentifierLocation) locations[i];
-                    String filename = workspaceLocation + location.getFilename();
-                    //                                 System.out.println(filename);
-                    if (location.getOffset() >= 0) {
-                      PHPeclipsePlugin.getDefault().openFileAndGotoOffset(filename, location.getOffset(), word.length());
-                    } else {
-                      PHPeclipsePlugin.getDefault().openFileAndFindString(filename, word);
-                    }
-                  }
-                } catch (CoreException e) {
-                  // TODO Auto-generated catch block
-                  e.printStackTrace();
+          //String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
+          String workspaceLocation = fProject.getLocation().toString() + java.io.File.separatorChar;
+
+          ListSelectionDialog listSelectionDialog = new ListSelectionDialog(PHPeclipsePlugin.getDefault().getWorkbench()
+              .getActiveWorkbenchWindow().getShell(), list, new ListContentProvider(), new LabelProvider(),
+              "Select the includes to open.");
+          listSelectionDialog.setTitle("Multiple includes found");
+          if (listSelectionDialog.open() == Window.OK) {
+            Object[] locations = listSelectionDialog.getResult();
+            if (locations != null) {
+              try {
+                for (int i = 0; i < locations.length; i++) {
+                  //                    PHPIdentifierLocation location = (PHPIdentifierLocation)
+                  // locations[i];
+                  String openFilename = workspaceLocation + ((String) locations[i]);
+                  PHPeclipsePlugin.getDefault().openFileInTextEditor(openFilename);
                 }
+              } catch (CoreException e) {
+                // TODO Auto-generated catch block
+                e.printStackTrace();
               }
             }
-          } else {
-            try {
-              PHPIdentifierLocation location = (PHPIdentifierLocation) list.get(0);
-              String filename = workspaceLocation + location.getFilename();
-              //                                       System.out.println(filename);
-              if (location.getOffset() >= 0) {
-                PHPeclipsePlugin.getDefault().openFileAndGotoOffset(filename, location.getOffset(), word.length());
-              } else {
-                PHPeclipsePlugin.getDefault().openFileAndFindString(filename, word);
+          }
+
+        }
+      } catch (Exception e) {
+      }
+
+    }
+    return;
+  }
+
+  /**
+   * @param f
+   * @param identiifer
+   */
+  private void openIdentifierDeclaration(IFile f, String identiifer) {
+    if (identiifer != null && !identiifer.equals("")) {
+      IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
+      List locationsList = indexManager.getLocations(identiifer);
+      if (locationsList != null && locationsList.size() > 0) {
+
+        //          String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot()
+        //              .getLocation().toString();
+
+        String workspaceLocation = fProject.getLocation().toString() + java.io.File.separatorChar;
+        // TODO show all entries of the list in a dialog box
+        // at the moment always the first entry will be opened
+        if (locationsList.size() > 1) {
+          // determine all includes:
+          IncludesScanner includesScanner = new IncludesScanner(fProject, (IFileEditorInput) fEditor.getEditorInput());
+          includesScanner.addFile(f);
+          Set exactIncludeSet = includesScanner.getSet();
+
+          PHPIdentifierLocation includeName;
+          for (int i = 0; i < locationsList.size(); i++) {
+            includeName = (PHPIdentifierLocation) locationsList.get(i);
+            if (exactIncludeSet.contains(includeName.getFilename())) {
+              includeName.setMatch(PHPIdentifierLocation.EXACT_MATCH);
+            } else {
+              includeName.setMatch(PHPIdentifierLocation.UNDEFINED_MATCH);
+            }
+          }
+          Collections.sort(locationsList);
+
+          ListSelectionDialog listSelectionDialog = new ListSelectionDialog(PHPeclipsePlugin.getDefault().getWorkbench()
+              .getActiveWorkbenchWindow().getShell(), locationsList, new ListContentProvider(), new LabelProvider(),
+              "Select the resources to open.");
+          listSelectionDialog.setTitle("Multiple declarations found");
+          if (listSelectionDialog.open() == Window.OK) {
+            Object[] locations = listSelectionDialog.getResult();
+            if (locations != null) {
+              try {
+                for (int i = 0; i < locations.length; i++) {
+                  PHPIdentifierLocation location = (PHPIdentifierLocation) locations[i];
+                  String filename = workspaceLocation + location.getFilename();
+                  //                                   System.out.println(filename);
+                  if (location.getOffset() >= 0) {
+                    PHPeclipsePlugin.getDefault().openFileAndGotoOffset(filename, location.getOffset(), identiifer.length());
+                  } else {
+                    PHPeclipsePlugin.getDefault().openFileAndFindString(filename, identiifer);
+                  }
+                }
+              } catch (CoreException e) {
+                // TODO Auto-generated catch block
+                e.printStackTrace();
               }
-            } catch (CoreException e) {
-              // TODO Auto-generated catch block
-              e.printStackTrace();
             }
           }
+        } else {
+          try {
+            PHPIdentifierLocation location = (PHPIdentifierLocation) locationsList.get(0);
+            String filename = workspaceLocation + location.getFilename();
+            //                                 System.out.println(filename);
+            if (location.getOffset() >= 0) {
+              PHPeclipsePlugin.getDefault().openFileAndGotoOffset(filename, location.getOffset(), identiifer.length());
+            } else {
+              PHPeclipsePlugin.getDefault().openFileAndFindString(filename, identiifer);
+            }
+          } catch (CoreException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+          }
         }
       }
     }
@@ -142,8 +240,54 @@ public class PHPOpenDeclarationEditorAction extends ActionDelegate implements IE
     }
   }
 
-  private String getPHPIdentifier(IDocument doc, int pos) {
-    Point word = PHPWordExtractor.findWord(doc, pos);
+  private String getIdentifierOrInclude(IDocument doc, int pos) {
+    //    private String getPHPIncludeText(IDocument doc, int pos) {
+    Point word = null;
+    int start = -1;
+    int end = -1;
+    isIncludeString = false;
+    try {
+      //    try to find an include string
+      int position = pos;
+      char character = ' ';
+
+      while (position >= 0) {
+        character = doc.getChar(position);
+        if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
+          break;
+        --position;
+      }
+      if ((character == '\"') || (character == '\'')) {
+        start = position;
+
+        position = pos;
+        int length = doc.getLength();
+        character = ' ';
+        while (position < length) {
+          character = doc.getChar(position);
+          if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
+            break;
+          ++position;
+        }
+        if ((character == '\"') || (character == '\'')) {
+          start++;
+          end = position;
+
+          if (end > start) {
+            word = new Point(start, end - start); // include name found
+            isIncludeString = true;
+          }
+        }
+      }
+
+      // try to find an identifier
+      if (word == null) {
+        word = PHPWordExtractor.findWord(doc, pos); // identifier found
+        isIncludeString = false;
+      }
+    } catch (BadLocationException x) {
+    }
+
     if (word != null) {
       try {
         return doc.get(word.x, word.y);
@@ -152,4 +296,47 @@ public class PHPOpenDeclarationEditorAction extends ActionDelegate implements IE
     }
     return "";
   }
-}
+
+  //    
+  //    
+  //    Point word = PHPWordExtractor.findWord(doc, pos);
+  //    if (word != null) {
+  //      try {
+  //        return doc.get(word.x, word.y);
+  //      } catch (BadLocationException e) {
+  //      }
+  //    }
+  //    return "";
+  //  }
+  private IContainer getWorkingLocation(IFileEditorInput editorInput) {
+    if (editorInput == null || editorInput.getFile() == null) {
+      return null;
+    }
+    return editorInput.getFile().getParent();
+  }
+
+  //  private IFile getIncludeFile(IProject project, IFileEditorInput editorInput, String relativeFilename) {
+  //    IContainer container = getWorkingLocation(editorInput);
+  //    String fullPath = project.getLocation().toString();
+  //    IFile file = null;
+  //    if (relativeFilename.startsWith("../")) {
+  //      Path path = new Path(relativeFilename);
+  //      file = container.getFile(path);
+  //      return file;
+  //    }
+  //    int index = relativeFilename.lastIndexOf('/');
+  //
+  //    if (index >= 0) {
+  //      Path path = new Path(relativeFilename);
+  //      file = project.getFile(path);
+  //      if (file.exists()) {
+  //        return file;
+  //      }
+  //    }
+  //
+  //    Path path = new Path(relativeFilename);
+  //    file = container.getFile(path);
+  //
+  //    return file;
+  //  }
+}
\ No newline at end of file