1 /**********************************************************************
 
   2  Copyright (c) 2000, 2002 IBM Corp. and others.
 
   3  All rights reserved. This program and the accompanying materials
 
   4  are made available under the terms of the Common Public License v1.0
 
   5  which accompanies this distribution, and is available at
 
   6  http://www.eclipse.org/legal/cpl-v10.html
 
   9  IBM Corporation - Initial implementation
 
  11  **********************************************************************/
 
  12 package net.sourceforge.phpeclipse.phpeditor;
 
  14 import java.io.FileReader;
 
  15 import java.util.ArrayList;
 
  16 import java.util.HashMap;
 
  17 import java.util.List;
 
  19 import net.sourceforge.phpdt.internal.corext.phpdoc.PHPDocUtil;
 
  20 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 
  21 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
 
  22 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
 
  23 import net.sourceforge.phpeclipse.phpeditor.php.PHPElement;
 
  24 import net.sourceforge.phpeclipse.phpeditor.php.PHPFunction;
 
  25 import net.sourceforge.phpeclipse.phpeditor.php.PHPWordExtractor;
 
  27 import org.eclipse.core.resources.IProject;
 
  28 import org.eclipse.jface.text.IRegion;
 
  29 import org.eclipse.jface.text.ITextHover;
 
  30 import org.eclipse.jface.text.ITextViewer;
 
  31 import org.eclipse.jface.text.Region;
 
  32 import org.eclipse.swt.graphics.Point;
 
  35  * Implementation for an <code>ITextHover</code> which hovers over PHP code.
 
  37 public class PHPTextHover implements ITextHover {
 
  38         private static HashMap functionDescriptions = null;
 
  40         private static HashMap identDescriptions = null;
 
  43          * The current project; maybe <code>null</code> for preference pages
 
  45         private IProject fProject;
 
  47         public PHPTextHover(IProject project) {
 
  52          * (non-Javadoc) Method declared on ITextHover
 
  54         public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
 
  55                 if (hoverRegion != null) {
 
  57                                 if (hoverRegion.getLength() > -1) {
 
  58                                         String word = textViewer.getDocument().get(hoverRegion.getOffset(), hoverRegion.getLength());
 
  59                                         if (functionDescriptions == null) {
 
  60                                                 functionDescriptions = new HashMap();
 
  61                                                 identDescriptions = new HashMap();
 
  62                                                 ArrayList syntaxbuffer = PHPSyntaxRdr.getSyntaxData();
 
  63                                                 String strbuffer = null;
 
  64                                                 PHPElement elbuffer = null;
 
  65                                                 if (syntaxbuffer != null) {
 
  66                                                         for (int i = 0; i < syntaxbuffer.size(); i++) {
 
  67                                                                 elbuffer = (PHPElement) syntaxbuffer.get(i);
 
  68                                                                 if (elbuffer instanceof PHPFunction) {
 
  69                                                                         functionDescriptions.put(elbuffer.getName(), elbuffer.getHoverText());
 
  71                                                                         identDescriptions.put(elbuffer.getName(), elbuffer.getHoverText());
 
  76                                                 // while ((syntaxbuffer != null)
 
  77                                                 // && (!syntaxbuffer.isEmpty() && ((elbuffer = (PHPElement)
 
  78                                                 // syntaxbuffer.remove(0)) != null))) {
 
  79                                                 // functionDescriptions.put(elbuffer.getName(),
 
  80                                                 // elbuffer.getHoverText());
 
  83                                         String hoverInfo = (String) identDescriptions.get(word);
 
  84                                         if (hoverInfo == null & word.length() > 0) {
 
  85                                                 hoverInfo = (String) functionDescriptions.get(word.toLowerCase());
 
  87                                         if (hoverInfo == null && fProject != null) {
 
  88                                                 // get the possible PHPDoc information from the index file
 
  89                                                 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
 
  90                                                 List list = indexManager.getLocations(word);
 
  91                                                 if (list.size() > 0) {
 
  93                                                                 PHPIdentifierLocation location;
 
  95                                                                 FileReader phpdocFileReader;
 
  96                                                                 StringBuffer hoverInfoBuffer = new StringBuffer();
 
  97                                                                 String workspaceLocation;
 
  98                                                                 if (fProject != null) {
 
  99                                                                         workspaceLocation = fProject.getLocation().toString() + '/';
 
 101                                                                         // should never happen?
 
 102                                                                         workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
 
 104                                                                 // boolean foundPHPdoc = false;
 
 105                                                                 for (int i = 0; i < list.size(); i++) {
 
 106                                                                         location = (PHPIdentifierLocation) list.get(i);
 
 107                                                                         filename = workspaceLocation + location.getFilename();
 
 108                                                                         PHPDocUtil.appendPHPDoc(hoverInfoBuffer, filename, location);
 
 110                                                                 hoverInfo = hoverInfoBuffer.toString();
 
 111                                                         } catch (Throwable e) {
 
 113                                                                 // e.printStackTrace();
 
 119                                 // } catch (BadLocationException x) {
 
 120                         } catch (Exception x) {
 
 124                 // don't show this annoying text
 
 125                 // return "empty selection";
 
 129          * (non-Javadoc) Method declared on ITextHover
 
 131         public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
 
 132                 Point selection = PHPWordExtractor.findWord(textViewer.getDocument(), offset);
 
 133                 // show the extracted word as a tooltip
 
 134                 if (selection != null && selection.x <= offset && offset < selection.x + selection.y)
 
 135                         return new Region(selection.x, selection.y);
 
 136                 return new Region(offset, 0);