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;
25 import net.sourceforge.phpeclipse.ui.WebUI;
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(
59 hoverRegion.getOffset(), hoverRegion.getLength());
60 if (functionDescriptions == null) {
61 functionDescriptions = new HashMap();
62 identDescriptions = new HashMap();
63 ArrayList syntaxbuffer = PHPSyntaxRdr.getSyntaxData();
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(
70 elbuffer.getName(), elbuffer
73 identDescriptions.put(elbuffer.getName(),
74 elbuffer.getHoverText());
79 // while ((syntaxbuffer != null)
80 // && (!syntaxbuffer.isEmpty() && ((elbuffer =
82 // syntaxbuffer.remove(0)) != null))) {
83 // functionDescriptions.put(elbuffer.getName(),
84 // elbuffer.getHoverText());
87 String hoverInfo = (String) identDescriptions.get(word);
88 if (hoverInfo == null & word.length() > 0) {
89 hoverInfo = (String) functionDescriptions.get(word
92 if (hoverInfo == null && fProject != null) {
93 // get the possible PHPDoc information from the index
95 IdentifierIndexManager indexManager = WebUI
96 .getDefault().getIndexManager(fProject);
97 List list = indexManager.getLocations(word);
98 if (list.size() > 0) {
100 PHPIdentifierLocation location;
102 StringBuffer hoverInfoBuffer = new StringBuffer();
103 String workspaceLocation;
104 if (fProject != null) {
105 workspaceLocation = fProject.getLocation()
108 // should never happen?
109 workspaceLocation = PHPeclipsePlugin
110 .getWorkspace().getRoot()
111 .getLocation().toString();
113 // boolean foundPHPdoc = false;
114 for (int i = 0; i < list.size(); i++) {
115 location = (PHPIdentifierLocation) list
117 filename = workspaceLocation
118 + location.getFilename();
119 PHPDocUtil.appendPHPDoc(hoverInfoBuffer,
122 hoverInfo = hoverInfoBuffer.toString();
123 } catch (Throwable e) {
125 // e.printStackTrace();
131 // } catch (BadLocationException x) {
132 } catch (Exception x) {
136 // don't show this annoying text
137 // return "empty selection";
141 * (non-Javadoc) Method declared on ITextHover
143 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
144 Point selection = PHPWordExtractor.findWord(textViewer.getDocument(),
146 // show the extracted word as a tooltip
147 if (selection != null && selection.x <= offset
148 && offset < selection.x + selection.y)
149 return new Region(selection.x, selection.y);
150 return new Region(offset, 0);