some bugfixes
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPTextHover.java
index 884a557..032adf0 100644 (file)
@@ -11,40 +11,124 @@ Contributors:
 **********************************************************************/
 package net.sourceforge.phpeclipse.phpeditor;
 
+import java.io.FileReader;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Vector;
 
-import org.eclipse.swt.graphics.Point;
-import org.eclipse.jface.text.BadLocationException;
+import net.sourceforge.phpdt.internal.corext.phpdoc.PHPDocCharArrayCommentReader;
+import net.sourceforge.phpeclipse.PHPeclipsePlugin;
+import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
+import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
+import net.sourceforge.phpeclipse.phpeditor.php.PHPElement;
+import net.sourceforge.phpeclipse.phpeditor.php.PHPWordExtractor;
+
+import org.eclipse.core.resources.IProject;
 import org.eclipse.jface.text.IRegion;
 import org.eclipse.jface.text.ITextHover;
 import org.eclipse.jface.text.ITextViewer;
 import org.eclipse.jface.text.Region;
+import org.eclipse.swt.graphics.Point;
 
 /**
- * Example implementation for an <code>ITextHover</code> which hovers over Java code.
+ * Implementation for an <code>ITextHover</code> 
+ * which hovers over PHP code.
  */
 public class PHPTextHover implements ITextHover {
+  public static HashMap functionDescriptions = null;
+
+  private static PHPWordExtractor phpWordDetector = new PHPWordExtractor();
+  /**
+   * The current project; maybe <code>null</code> for preference pages
+   */
+  private IProject fProject;
+
+  public PHPTextHover(IProject project) {
+    fProject = project;
+  }
+  /* (non-Javadoc)
+   * Method declared on ITextHover
+   */
+  public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
+    if (hoverRegion != null) {
+      try {
+        if (hoverRegion.getLength() > -1) {
+          String word = textViewer.getDocument().get(hoverRegion.getOffset(), hoverRegion.getLength());
+          if (functionDescriptions == null) {
+            functionDescriptions = new HashMap();
+            //                 PHPSyntaxRdr syntaxRdr = new PHPSyntaxRdr();
+            //                 syntaxRdr.readInSyntax();
+
+            //                 Vector syntaxbuffer = syntaxRdr.getsyntaxdata();
+            Vector syntaxbuffer = PHPSyntaxRdr.getsyntaxdata();
+            String strbuffer = null;
+            PHPElement elbuffer = null;
+            while ((syntaxbuffer != null)
+              && (!syntaxbuffer.isEmpty() && ((elbuffer = (PHPElement) syntaxbuffer.remove(0)) != null))) {
+              functionDescriptions.put(elbuffer.getName(), elbuffer.getHoverText());
+            }
+
+            //            functionDescriptions = new HashMap(997);
+            //            for (int i=0; i<PHPFunctionNames.FUNCTION_NAMES.length;i++) {
+            //              functionDescriptions.put(PHPFunctionNasmes.FUNCTION_NAMES[i],PHPFunctionDescription.FUNCTION_DESCRIPTION[i]);
+            //            }
+          }
+          String hoverInfo = (String) functionDescriptions.get(word);
+          if (hoverInfo == null && fProject != null) {
+            // get the possible PHPDoc information from the index file
+            IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
+            List list = indexManager.getLocations(word);
+            if (list.size() > 0) {
+              try {
+                PHPIdentifierLocation location;
+                String filename;
+                FileReader phpdocFileReader;
+                PHPDocCharArrayCommentReader phpdocConverter;
+                StringBuffer hoverInfoBuffer = new StringBuffer();
+                String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
+                //                boolean foundPHPdoc = false;
+                for (int i = 0; i < list.size(); i++) {
+                  location = (PHPIdentifierLocation) list.get(i);
+                                                                       filename = workspaceLocation + location.getFilename();
+                                                                       hoverInfoBuffer.append(location.toString());
+                                                                       hoverInfoBuffer.append('\n');
+                  if (location.getPHPDocOffset() >= 0) {
+                    //                    foundPHPdoc = true;
+                    phpdocFileReader = new FileReader(filename);
+                    char[] charArray = new char[location.getPHPDocLength()];
+                    phpdocFileReader.skip(location.getPHPDocOffset());
+                    phpdocFileReader.read(charArray, 0, location.getPHPDocLength());
+                    phpdocConverter = new PHPDocCharArrayCommentReader(charArray);
+                    hoverInfoBuffer.append(phpdocConverter.getString());
+                    hoverInfoBuffer.append('\n');
+                  }
+                }
+                //                if (foundPHPdoc) {
+                hoverInfo = hoverInfoBuffer.toString();
+                //                }
+              } catch (Throwable e) {
+                // ignore exceptions
+                // e.printStackTrace();
+              }
+            }
+          }
+          return hoverInfo;
+        }
+        //      } catch (BadLocationException x) {
+      } catch (Exception x) {
+      }
+    }
+    return "empty selection";
+  }
 
-       /* (non-Javadoc)
-        * Method declared on ITextHover
-        */
-       public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
-               if (hoverRegion != null) {
-                       try {
-                               if (hoverRegion.getLength() > -1)
-                                       return textViewer.getDocument().get(hoverRegion.getOffset(), hoverRegion.getLength());
-                       } catch (BadLocationException x) {
-                       }
-               }
-               return "empty selection";
-       }
-       
-       /* (non-Javadoc)
-        * Method declared on ITextHover
-        */
-       public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
-               Point selection= textViewer.getSelectedRange();
-               if (selection.x <= offset && offset < selection.x + selection.y)
-                       return new Region(selection.x, selection.y);
-               return new Region(offset, 0);
-       }
+  /* (non-Javadoc)
+   * Method declared on ITextHover
+   */
+  public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
+    Point selection = PHPWordExtractor.findWord(textViewer.getDocument(), offset);
+    //  show the extracted word as a tooltip
+    if (selection != null && selection.x <= offset && offset < selection.x + selection.y)
+      return new Region(selection.x, selection.y);
+    return new Region(offset, 0);
+  }
 }