Fix bugs #725 and releted #744
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / actions / OpenDeclarationEditorAction.java
index 37f7d5d..6aeb43f 100644 (file)
@@ -12,6 +12,7 @@ import java.util.List;
 import java.util.Set;
 
 import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil;
+import net.sourceforge.phpdt.internal.ui.viewsupport.ListContentProvider;
 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
@@ -27,237 +28,280 @@ import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.ITextSelection;
 import org.eclipse.jface.viewers.LabelProvider;
 import org.eclipse.jface.window.Window;
-import net.sourceforge.phpdt.internal.ui.viewsupport.ListContentProvider;
 import org.eclipse.swt.graphics.Point;
 import org.eclipse.ui.IFileEditorInput;
 import org.eclipse.ui.dialogs.ListSelectionDialog;
 
 public class OpenDeclarationEditorAction {
 
-  private PHPEditor fEditor;
+       private PHPEditor fEditor;
+
+       private IProject fProject;
 
-  private IProject fProject;
+       private boolean isIncludeString;
 
-  private boolean isIncludeString;
+       public OpenDeclarationEditorAction(PHPEditor editor) {
+               fEditor = editor;
+               fProject = null;
+               isIncludeString = false;
+       }
 
-  public OpenDeclarationEditorAction(PHPEditor editor) {
-    fEditor = editor;
-    fProject = null;
-    isIncludeString = false;
-  }
+       /**
+        * @param selection
+        */
+       protected void openSelectedElement(ITextSelection selection) {
+               IDocument doc = fEditor.getDocumentProvider().getDocument(
+                               fEditor.getEditorInput());
+               int pos = selection.getOffset();
+               openSelectedPosition(doc, pos);
+       }
 
-  /**
-   * @param selection
-   */
-  protected void openSelectedElement(ITextSelection selection) {
-    IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
-    int pos = selection.getOffset();
-    openSelectedPosition(doc, pos);
-  }
+       protected void openSelectedPosition(IDocument doc, int position) {
+               IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
+               fProject = f.getProject();
+               // System.out.println(selection.getText());
+               String identifierOrInclude = getIdentifierOrInclude(doc, position);
+               // System.out.println(word);
+               if (identifierOrInclude != null && !identifierOrInclude.equals("")) {
+                       if (isIncludeString) {
+                               openIncludeFile(identifierOrInclude);
+                       } else {
+                               openIdentifierDeclaration(f, identifierOrInclude);
+                       }
+               }
+       }
 
-  protected void openSelectedPosition(IDocument doc, int position) {
-    IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
-    fProject = f.getProject();
-    //  System.out.println(selection.getText());
-    String identifierOrInclude = getIdentifierOrInclude(doc, position);
-    //      System.out.println(word);
-    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) {
-         IFile file = PHPFileUtil.createFile(path, fProject);
-          if (file != null && file.exists()) {
-            PHPeclipsePlugin.getDefault().openFileInTextEditor(file.getLocation().toString());
-            return;
-          }
-        }
-      } catch (Exception e) {
-        // ignore
-      }
+       /**
+        * @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) {
+                                       //IFile file = PHPFileUtil.createFile(path, fProject);
+                                       //if (file != null && file.exists()) {
+                                       //      PHPeclipsePlugin.getDefault().openFileInTextEditor(
+                                       //                      file.getLocation().toString());
+                                       //      return;
+                                       //}
+                                       PHPeclipsePlugin.getDefault().openFileInTextEditor(
+                                                       path.toString());
+                                       return;
+                               }
+                       } catch (Exception e) {
+                               // ignore
+                       }
 
-      try {
+                       try {
 
-        IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
-        //            filename = StringUtil.replaceRegExChars(filename);
-        List list = indexManager.getFileList(filename);
-        if (list != null && list.size() > 0) {
-          //String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
-          String workspaceLocation = fProject.getLocation().toString() + java.io.File.separatorChar;
+                               IdentifierIndexManager indexManager = PHPeclipsePlugin
+                                               .getDefault().getIndexManager(fProject);
+                               // filename = StringUtil.replaceRegExChars(filename);
+                               List list = indexManager.getFileList(filename);
+                               if (list != null && list.size() > 0) {
+                                       // String workspaceLocation =
+                                       // PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
+                                       String workspaceLocation = fProject.getFullPath()
+                                                       .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();
-              }
-            }
-          }
+                                       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();
+                                                       }
+                                               }
+                                       }
 
-        }
-      } catch (Exception e) {
-      }
+                               }
+                       } catch (Exception e) {
+                       }
 
-    }
-    return;
-  }
+               }
+               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) {
+       /**
+        * @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 =
+                               // 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();
+                               String workspaceLocation = fProject.getFullPath().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);
+                                       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();
-              }
-            }
-          }
-        } 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();
-          }
-        }
-      }
-    }
-  }
+                                       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();
+                                                       }
+                                               }
+                                       }
+                               } 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();
+                                       }
+                               }
+                       }
+               }
+       }
 
-  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 = ' ';
+       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;
+                       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;
+                               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;
-          }
-        }
-      }
+                                       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) {
-    }
+                       // 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);
-      } catch (BadLocationException e) {
-      }
-    }
-    return "";
-  }
+               if (word != null) {
+                       try {
+                               return doc.get(word.x, word.y);
+                       } catch (BadLocationException e) {
+                       }
+               }
+               return "";
+       }
 
 }
\ No newline at end of file