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.HashMap;
16 import java.util.List;
17 import java.util.Vector;
19 import net.sourceforge.phpdt.internal.corext.phpdoc.PHPDocCharArrayCommentReader;
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 PHPDocCharArrayCommentReader phpdocConverter;
84 StringBuffer hoverInfoBuffer = new StringBuffer();
85 String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
86 // boolean foundPHPdoc = false;
87 for (int i = 0; i < list.size(); i++) {
88 location = (PHPIdentifierLocation) list.get(i);
89 filename = workspaceLocation + location.getFilename();
90 hoverInfoBuffer.append(location.toString());
91 hoverInfoBuffer.append('\n');
92 if (location.getPHPDocOffset() >= 0) {
93 // foundPHPdoc = true;
94 phpdocFileReader = new FileReader(filename);
95 char[] charArray = new char[location.getPHPDocLength()];
96 phpdocFileReader.skip(location.getPHPDocOffset());
97 phpdocFileReader.read(charArray, 0, location.getPHPDocLength());
98 phpdocConverter = new PHPDocCharArrayCommentReader(charArray);
99 hoverInfoBuffer.append(phpdocConverter.getString());
100 hoverInfoBuffer.append('\n');
103 // if (foundPHPdoc) {
104 hoverInfo = hoverInfoBuffer.toString();
106 } catch (Throwable e) {
108 // e.printStackTrace();
114 // } catch (BadLocationException x) {
115 } catch (Exception x) {
118 return "empty selection";
122 * Method declared on ITextHover
124 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
125 Point selection = PHPWordExtractor.findWord(textViewer.getDocument(), offset);
126 // show the extracted word as a tooltip
127 if (selection != null && selection.x <= offset && offset < selection.x + selection.y)
128 return new Region(selection.x, selection.y);
129 return new Region(offset, 0);