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.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
18 import net.sourceforge.phpdt.internal.corext.phpdoc.PHPDocUtil;
19 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
20 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
21 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
22 import net.sourceforge.phpeclipse.phpeditor.php.PHPElement;
23 import net.sourceforge.phpeclipse.phpeditor.php.PHPFunction;
24 import net.sourceforge.phpeclipse.phpeditor.php.PHPWordExtractor;
26 import org.eclipse.core.resources.IProject;
27 import org.eclipse.jface.text.IRegion;
28 import org.eclipse.jface.text.ITextHover;
29 import org.eclipse.jface.text.ITextViewer;
30 import org.eclipse.jface.text.Region;
31 import org.eclipse.swt.graphics.Point;
34 * Implementation for an <code>ITextHover</code> which hovers over PHP code.
36 public class PHPTextHover implements ITextHover {
37 private static HashMap functionDescriptions = null;
39 private static HashMap identDescriptions = null;
42 * The current project; maybe <code>null</code> for preference pages
44 private IProject fProject;
46 public PHPTextHover(IProject project) {
51 * (non-Javadoc) Method declared on ITextHover
53 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
54 if (hoverRegion != null) {
56 if (hoverRegion.getLength() > -1) {
57 String word = textViewer.getDocument().get(
58 hoverRegion.getOffset(), hoverRegion.getLength());
59 if (functionDescriptions == null) {
60 functionDescriptions = new HashMap();
61 identDescriptions = new HashMap();
62 ArrayList syntaxbuffer = PHPSyntaxRdr.getSyntaxData();
63 PHPElement elbuffer = null;
64 if (syntaxbuffer != null) {
65 for (int i = 0; i < syntaxbuffer.size(); i++) {
66 elbuffer = (PHPElement) syntaxbuffer.get(i);
67 if (elbuffer instanceof PHPFunction) {
68 functionDescriptions.put(
69 elbuffer.getName(), elbuffer
72 identDescriptions.put(elbuffer.getName(),
73 elbuffer.getHoverText());
78 // while ((syntaxbuffer != null)
79 // && (!syntaxbuffer.isEmpty() && ((elbuffer =
81 // syntaxbuffer.remove(0)) != null))) {
82 // functionDescriptions.put(elbuffer.getName(),
83 // elbuffer.getHoverText());
86 String hoverInfo = (String) identDescriptions.get(word);
87 if (hoverInfo == null & word.length() > 0) {
88 hoverInfo = (String) functionDescriptions.get(word
91 if (hoverInfo == null && fProject != null) {
92 // get the possible PHPDoc information from the index
94 IdentifierIndexManager indexManager = PHPeclipsePlugin
95 .getDefault().getIndexManager(fProject);
96 List list = indexManager.getLocations(word);
97 if (list.size() > 0) {
99 PHPIdentifierLocation location;
101 StringBuffer hoverInfoBuffer = new StringBuffer();
102 String workspaceLocation;
103 if (fProject != null) {
104 workspaceLocation = fProject.getLocation()
107 // should never happen?
108 workspaceLocation = PHPeclipsePlugin
109 .getWorkspace().getRoot()
110 .getLocation().toString();
112 // boolean foundPHPdoc = false;
113 for (int i = 0; i < list.size(); i++) {
114 location = (PHPIdentifierLocation) list
116 filename = workspaceLocation
117 + location.getFilename();
118 PHPDocUtil.appendPHPDoc(hoverInfoBuffer,
121 hoverInfo = hoverInfoBuffer.toString();
122 } catch (Throwable e) {
124 // e.printStackTrace();
130 // } catch (BadLocationException x) {
131 } catch (Exception x) {
135 // don't show this annoying text
136 // return "empty selection";
140 * (non-Javadoc) Method declared on ITextHover
142 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
143 Point selection = PHPWordExtractor.findWord(textViewer.getDocument(),
145 // show the extracted word as a tooltip
146 if (selection != null && selection.x <= offset
147 && offset < selection.x + selection.y)
148 return new Region(selection.x, selection.y);
149 return new Region(offset, 0);