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(hoverRegion.getOffset(), hoverRegion.getLength());
58 if (functionDescriptions == null) {
59 functionDescriptions = new HashMap();
60 identDescriptions = new HashMap();
61 ArrayList syntaxbuffer = PHPSyntaxRdr.getSyntaxData();
62 PHPElement elbuffer = null;
63 if (syntaxbuffer != null) {
64 for (int i = 0; i < syntaxbuffer.size(); i++) {
65 elbuffer = (PHPElement) syntaxbuffer.get(i);
66 if (elbuffer instanceof PHPFunction) {
67 functionDescriptions.put(elbuffer.getName(), elbuffer.getHoverText());
69 identDescriptions.put(elbuffer.getName(), elbuffer.getHoverText());
74 // while ((syntaxbuffer != null)
75 // && (!syntaxbuffer.isEmpty() && ((elbuffer = (PHPElement)
76 // syntaxbuffer.remove(0)) != null))) {
77 // functionDescriptions.put(elbuffer.getName(),
78 // elbuffer.getHoverText());
81 String hoverInfo = (String) identDescriptions.get(word);
82 if (hoverInfo == null & word.length() > 0) {
83 hoverInfo = (String) functionDescriptions.get(word.toLowerCase());
85 if (hoverInfo == null && fProject != null) {
86 // get the possible PHPDoc information from the index file
87 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
88 List list = indexManager.getLocations(word);
89 if (list.size() > 0) {
91 PHPIdentifierLocation location;
93 StringBuffer hoverInfoBuffer = new StringBuffer();
94 String workspaceLocation;
95 if (fProject != null) {
96 workspaceLocation = fProject.getLocation().toString() + '/';
98 // should never happen?
99 workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
101 // boolean foundPHPdoc = false;
102 for (int i = 0; i < list.size(); i++) {
103 location = (PHPIdentifierLocation) list.get(i);
104 filename = workspaceLocation + location.getFilename();
105 PHPDocUtil.appendPHPDoc(hoverInfoBuffer, filename, location);
107 hoverInfo = hoverInfoBuffer.toString();
108 } catch (Throwable e) {
110 // e.printStackTrace();
116 // } catch (BadLocationException x) {
117 } catch (Exception x) {
121 // don't show this annoying text
122 // return "empty selection";
126 * (non-Javadoc) Method declared on ITextHover
128 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
129 Point selection = PHPWordExtractor.findWord(textViewer.getDocument(), offset);
130 // show the extracted word as a tooltip
131 if (selection != null && selection.x <= offset && offset < selection.x + selection.y)
132 return new Region(selection.x, selection.y);
133 return new Region(offset, 0);