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;
13 import java.io.FileReader;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
17 import net.sourceforge.phpdt.internal.corext.phpdoc.PHPDocUtil;
18 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
20 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
21 import net.sourceforge.phpeclipse.phpeditor.php.PHPElement;
22 import net.sourceforge.phpeclipse.phpeditor.php.PHPWordExtractor;
23 import org.eclipse.core.resources.IProject;
24 import org.eclipse.jface.text.IRegion;
25 import org.eclipse.jface.text.ITextHover;
26 import org.eclipse.jface.text.ITextViewer;
27 import org.eclipse.jface.text.Region;
28 import org.eclipse.swt.graphics.Point;
30 * Implementation for an <code>ITextHover</code> which hovers over PHP code.
32 public class PHPTextHover implements ITextHover {
33 public static HashMap functionDescriptions = null;
34 private static PHPWordExtractor phpWordDetector = new PHPWordExtractor();
36 * The current project; maybe <code>null</code> for preference pages
38 private IProject fProject;
39 public PHPTextHover(IProject project) {
43 * (non-Javadoc) Method declared on ITextHover
45 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
46 if (hoverRegion != null) {
48 if (hoverRegion.getLength() > -1) {
49 String word = textViewer.getDocument().get(hoverRegion.getOffset(),
50 hoverRegion.getLength());
51 if (functionDescriptions == null) {
52 functionDescriptions = new HashMap();
53 ArrayList syntaxbuffer = PHPSyntaxRdr.getSyntaxData();
54 String strbuffer = null;
55 PHPElement elbuffer = null;
56 if (syntaxbuffer != null) {
57 for (int i = 0; i < syntaxbuffer.size(); i++) {
58 elbuffer = (PHPElement) syntaxbuffer.get(i);
59 functionDescriptions.put(elbuffer.getName(), elbuffer
64 // while ((syntaxbuffer != null)
65 // && (!syntaxbuffer.isEmpty() && ((elbuffer = (PHPElement)
66 // syntaxbuffer.remove(0)) != null))) {
67 // functionDescriptions.put(elbuffer.getName(),
68 // elbuffer.getHoverText());
71 String hoverInfo = (String) functionDescriptions.get(word);
72 if (hoverInfo == null && fProject != null) {
73 // get the possible PHPDoc information from the index file
74 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault()
75 .getIndexManager(fProject);
76 List list = indexManager.getLocations(word);
77 if (list.size() > 0) {
79 PHPIdentifierLocation location;
81 FileReader phpdocFileReader;
82 StringBuffer hoverInfoBuffer = new StringBuffer();
83 String workspaceLocation = PHPeclipsePlugin.getWorkspace()
84 .getRoot().getLocation().toString();
85 // boolean foundPHPdoc = false;
86 for (int i = 0; i < list.size(); i++) {
87 location = (PHPIdentifierLocation) list.get(i);
88 filename = workspaceLocation + location.getFilename();
89 PHPDocUtil.appendPHPDoc(hoverInfoBuffer, filename, location);
91 hoverInfo = hoverInfoBuffer.toString();
92 } catch (Throwable e) {
94 // e.printStackTrace();
100 // } catch (BadLocationException x) {
101 } catch (Exception x) {
105 // don't show this annoying text
106 // return "empty selection";
109 * (non-Javadoc) Method declared on ITextHover
111 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
112 Point selection = PHPWordExtractor.findWord(textViewer.getDocument(),
114 // show the extracted word as a tooltip
115 if (selection != null && selection.x <= offset
116 && offset < selection.x + selection.y)
117 return new Region(selection.x, selection.y);
118 return new Region(offset, 0);