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.io.IOException;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Vector;
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();
41 private IProject fProject;
43 public PHPTextHover(IProject project) {
47 * Method declared on ITextHover
49 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
50 if (hoverRegion != null) {
52 if (hoverRegion.getLength() > -1) {
53 String word = textViewer.getDocument().get(hoverRegion.getOffset(), hoverRegion.getLength());
54 if (functionDescriptions == null) {
55 functionDescriptions = new HashMap();
56 // PHPSyntaxRdr syntaxRdr = new PHPSyntaxRdr();
57 // syntaxRdr.readInSyntax();
59 // Vector syntaxbuffer = syntaxRdr.getsyntaxdata();
60 Vector syntaxbuffer = PHPSyntaxRdr.getsyntaxdata();
61 String strbuffer = null;
62 PHPElement elbuffer = null;
63 while ((syntaxbuffer != null)
64 && (!syntaxbuffer.isEmpty() && ((elbuffer = (PHPElement) syntaxbuffer.remove(0)) != null))) {
65 functionDescriptions.put(elbuffer.getName(), elbuffer.getHoverText());
68 // functionDescriptions = new HashMap(997);
69 // for (int i=0; i<PHPFunctionNames.FUNCTION_NAMES.length;i++) {
70 // functionDescriptions.put(PHPFunctionNasmes.FUNCTION_NAMES[i],PHPFunctionDescription.FUNCTION_DESCRIPTION[i]);
73 String hoverInfo = (String) functionDescriptions.get(word);
74 if (hoverInfo == null && fProject != null) {
75 // get the possible PHPDoc information from the index file
76 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
77 List list = indexManager.getLocations(word);
78 if (list.size() > 0) {
80 PHPIdentifierLocation location;
82 FileReader phpdocFileReader;
83 StringBuffer hoverInfoBuffer = new StringBuffer();
84 String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
85 boolean foundPHPdoc = false;
86 for (int i = 0; i < list.size(); i++) {
87 location = (PHPIdentifierLocation) list.get(0);
88 if (location.getPHPDocOffset() >= 0) {
90 filename = workspaceLocation + location.getFilename();
91 phpdocFileReader = new FileReader(filename);
92 hoverInfoBuffer.append("PHPdoc found in file: ");
93 hoverInfoBuffer.append(filename);
94 hoverInfoBuffer.append('\n');
95 char[] charArray = new char[location.getPHPDocLength()];
96 phpdocFileReader.skip(location.getPHPDocOffset());
97 phpdocFileReader.read(charArray, 0, location.getPHPDocLength());
98 hoverInfoBuffer.append(charArray);
99 hoverInfoBuffer.append('\n');
103 hoverInfo = hoverInfoBuffer.toString();
105 } catch (Throwable e) {
107 // e.printStackTrace();
113 // } catch (BadLocationException x) {
114 } catch (Exception x) {
117 return "empty selection";
121 * Method declared on ITextHover
123 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
124 Point selection = PHPWordExtractor.findWord(textViewer.getDocument(), offset);
125 // show the extracted word as a tooltip
126 if (selection != null && selection.x <= offset && offset < selection.x + selection.y)
127 return new Region(selection.x, selection.y);
128 return new Region(offset, 0);