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
10 Klaus Hartlage - www.eclipseproject.de
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.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>
35 * which hovers over PHP code.
37 public class PHPTextHover implements ITextHover {
38 public static HashMap functionDescriptions = null;
40 private static PHPWordExtractor phpWordDetector = new PHPWordExtractor();
42 * The current project; maybe <code>null</code> for preference pages
44 private IProject fProject;
46 public PHPTextHover(IProject project) {
50 * Method declared on ITextHover
52 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
53 if (hoverRegion != null) {
55 if (hoverRegion.getLength() > -1) {
56 String word = textViewer.getDocument().get(hoverRegion.getOffset(), hoverRegion.getLength());
57 if (functionDescriptions == null) {
58 functionDescriptions = new HashMap();
60 ArrayList syntaxbuffer = PHPSyntaxRdr.getSyntaxData();
61 String strbuffer = null;
62 PHPElement elbuffer = null;
63 if (syntaxbuffer!=null) {
64 for (int i=0;i<syntaxbuffer.size();i++) {
65 elbuffer = (PHPElement) syntaxbuffer.get(i);
66 functionDescriptions.put(elbuffer.getName(), elbuffer.getHoverText());
70 // while ((syntaxbuffer != null)
71 // && (!syntaxbuffer.isEmpty() && ((elbuffer = (PHPElement) syntaxbuffer.remove(0)) != null))) {
72 // functionDescriptions.put(elbuffer.getName(), elbuffer.getHoverText());
76 String hoverInfo = (String) functionDescriptions.get(word);
77 if (hoverInfo == null && fProject != null) {
78 // get the possible PHPDoc information from the index file
79 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
80 List list = indexManager.getLocations(word);
81 if (list.size() > 0) {
83 PHPIdentifierLocation location;
85 FileReader phpdocFileReader;
86 StringBuffer hoverInfoBuffer = new StringBuffer();
87 String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
88 // boolean foundPHPdoc = false;
89 for (int i = 0; i < list.size(); i++) {
90 location = (PHPIdentifierLocation) list.get(i);
91 filename = workspaceLocation + location.getFilename();
92 PHPDocUtil.appendPHPDoc(hoverInfoBuffer, filename, location);
94 hoverInfo = hoverInfoBuffer.toString();
95 } catch (Throwable e) {
97 // e.printStackTrace();
103 // } catch (BadLocationException x) {
104 } catch (Exception x) {
108 // don't show this annoying text
109 // return "empty selection";
113 * Method declared on ITextHover
115 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
116 Point selection = PHPWordExtractor.findWord(textViewer.getDocument(), offset);
117 // show the extracted word as a tooltip
118 if (selection != null && selection.x <= offset && offset < selection.x + selection.y)
119 return new Region(selection.x, selection.y);
120 return new Region(offset, 0);