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;
13 import java.io.FileReader;
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.PHPWordExtractor;
25 import org.eclipse.core.resources.IProject;
26 import org.eclipse.jface.text.IRegion;
27 import org.eclipse.jface.text.ITextHover;
28 import org.eclipse.jface.text.ITextViewer;
29 import org.eclipse.jface.text.Region;
30 import org.eclipse.swt.graphics.Point;
32 * Implementation for an <code>ITextHover</code> which hovers over PHP code.
34 public class PHPTextHover implements ITextHover {
35 public static HashMap functionDescriptions = null;
36 private static PHPWordExtractor phpWordDetector = new PHPWordExtractor();
38 * The current project; maybe <code>null</code> for preference pages
40 private IProject fProject;
41 public PHPTextHover(IProject project) {
45 * (non-Javadoc) Method declared on ITextHover
47 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
48 if (hoverRegion != null) {
50 if (hoverRegion.getLength() > -1) {
51 String word = textViewer.getDocument().get(hoverRegion.getOffset(),
52 hoverRegion.getLength());
53 if (functionDescriptions == null) {
54 functionDescriptions = new HashMap();
55 ArrayList syntaxbuffer = PHPSyntaxRdr.getSyntaxData();
56 String strbuffer = null;
57 PHPElement elbuffer = null;
58 if (syntaxbuffer != null) {
59 for (int i = 0; i < syntaxbuffer.size(); i++) {
60 elbuffer = (PHPElement) syntaxbuffer.get(i);
61 functionDescriptions.put(elbuffer.getName(), elbuffer
66 // while ((syntaxbuffer != null)
67 // && (!syntaxbuffer.isEmpty() && ((elbuffer = (PHPElement)
68 // syntaxbuffer.remove(0)) != null))) {
69 // functionDescriptions.put(elbuffer.getName(),
70 // elbuffer.getHoverText());
74 (String) functionDescriptions.get(word);
75 if (hoverInfo == null && fProject != null) {
76 // get the possible PHPDoc information from the index file
77 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault()
78 .getIndexManager(fProject);
79 List list = indexManager.getLocations(word);
80 if (list.size() > 0) {
82 PHPIdentifierLocation location;
84 FileReader phpdocFileReader;
85 StringBuffer hoverInfoBuffer = new StringBuffer();
86 String workspaceLocation;
88 workspaceLocation = fProject.getLocation().toString()+'/';
90 // should never happen?
91 workspaceLocation = PHPeclipsePlugin.getWorkspace()
92 .getRoot().getLocation().toString();
94 // boolean foundPHPdoc = false;
95 for (int i = 0; i < list.size(); i++) {
96 location = (PHPIdentifierLocation) list.get(i);
97 filename = workspaceLocation + location.getFilename();
98 PHPDocUtil.appendPHPDoc(hoverInfoBuffer, filename, location);
100 hoverInfo = hoverInfoBuffer.toString();
101 } catch (Throwable e) {
103 // e.printStackTrace();
109 // } catch (BadLocationException x) {
110 } catch (Exception x) {
114 // don't show this annoying text
115 // return "empty selection";
118 * (non-Javadoc) Method declared on ITextHover
120 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
121 Point selection = PHPWordExtractor.findWord(textViewer.getDocument(),
123 // show the extracted word as a tooltip
124 if (selection != null && selection.x <= offset
125 && offset < selection.x + selection.y)
126 return new Region(selection.x, selection.y);
127 return new Region(offset, 0);