added old hover behaviour;
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPTextHover.java
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
7
8  Contributors:
9  IBM Corporation - Initial implementation
10  Klaus Hartlage - www.eclipseproject.de
11  **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor;
13 import java.io.FileReader;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
17 import net.sourceforge.phpdt.internal.corext.phpdoc.PHPDocUtil;
18 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
20 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
21 import net.sourceforge.phpeclipse.phpeditor.php.PHPElement;
22 import net.sourceforge.phpeclipse.phpeditor.php.PHPWordExtractor;
23 import org.eclipse.core.resources.IProject;
24 import org.eclipse.jface.text.IRegion;
25 import org.eclipse.jface.text.ITextHover;
26 import org.eclipse.jface.text.ITextViewer;
27 import org.eclipse.jface.text.Region;
28 import org.eclipse.swt.graphics.Point;
29 /**
30  * Implementation for an <code>ITextHover</code> which hovers over PHP code.
31  */
32 public class PHPTextHover implements ITextHover {
33   public static HashMap functionDescriptions = null;
34   private static PHPWordExtractor phpWordDetector = new PHPWordExtractor();
35   /**
36    * The current project; maybe <code>null</code> for preference pages
37    */
38   private IProject fProject;
39   public PHPTextHover(IProject project) {
40     fProject = project;
41   }
42   /*
43    * (non-Javadoc) Method declared on ITextHover
44    */
45   public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
46     if (hoverRegion != null) {
47       try {
48         if (hoverRegion.getLength() > -1) {
49           String word = textViewer.getDocument().get(hoverRegion.getOffset(),
50               hoverRegion.getLength());
51           if (functionDescriptions == null) {
52             functionDescriptions = new HashMap();
53             ArrayList syntaxbuffer = PHPSyntaxRdr.getSyntaxData();
54             String strbuffer = null;
55             PHPElement elbuffer = null;
56             if (syntaxbuffer != null) {
57               for (int i = 0; i < syntaxbuffer.size(); i++) {
58                 elbuffer = (PHPElement) syntaxbuffer.get(i);
59                 functionDescriptions.put(elbuffer.getName(), elbuffer
60                     .getHoverText());
61               }
62             }
63             //            
64             //            while ((syntaxbuffer != null)
65             //              && (!syntaxbuffer.isEmpty() && ((elbuffer = (PHPElement)
66             // syntaxbuffer.remove(0)) != null))) {
67             //              functionDescriptions.put(elbuffer.getName(),
68             // elbuffer.getHoverText());
69             //            }
70           }
71           String hoverInfo = (String) functionDescriptions.get(word);
72           if (hoverInfo == null && fProject != null) {
73             // get the possible PHPDoc information from the index file
74             IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault()
75                 .getIndexManager(fProject);
76             List list = indexManager.getLocations(word);
77             if (list.size() > 0) {
78               try {
79                 PHPIdentifierLocation location;
80                 String filename;
81                 FileReader phpdocFileReader;
82                 StringBuffer hoverInfoBuffer = new StringBuffer();
83                 String workspaceLocation = PHPeclipsePlugin.getWorkspace()
84                     .getRoot().getLocation().toString();
85                 //                boolean foundPHPdoc = false;
86                 for (int i = 0; i < list.size(); i++) {
87                   location = (PHPIdentifierLocation) list.get(i);
88                   filename = workspaceLocation + location.getFilename();
89                   PHPDocUtil.appendPHPDoc(hoverInfoBuffer, filename, location);
90                 }
91                 hoverInfo = hoverInfoBuffer.toString();
92               } catch (Throwable e) {
93                 // ignore exceptions
94                 // e.printStackTrace();
95               }
96             }
97           }
98           return hoverInfo;
99         }
100         //      } catch (BadLocationException x) {
101       } catch (Exception x) {
102       }
103     }
104     return null;
105     // don't show this annoying text
106     //    return "empty selection";
107   }
108   /*
109    * (non-Javadoc) Method declared on ITextHover
110    */
111   public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
112     Point selection = PHPWordExtractor.findWord(textViewer.getDocument(),
113         offset);
114     //  show the extracted word as a tooltip
115     if (selection != null && selection.x <= offset
116         && offset < selection.x + selection.y)
117       return new Region(selection.x, selection.y);
118     return new Region(offset, 0);
119   }
120 }