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.util.HashMap;
15 import java.util.Vector;
17 import net.sourceforge.phpeclipse.phpeditor.php.PHPElement;
18 import net.sourceforge.phpeclipse.phpeditor.php.PHPWordExtractor;
19 import org.eclipse.jface.text.IRegion;
20 import org.eclipse.jface.text.ITextHover;
21 import org.eclipse.jface.text.ITextViewer;
22 import org.eclipse.jface.text.Region;
23 import org.eclipse.swt.graphics.Point;
26 * Example implementation for an <code>ITextHover</code>
27 * which hovers over PHP code.
29 public class PHPTextHover implements ITextHover {
30 public static HashMap functionDescriptions = null;
32 private static PHPWordExtractor phpWordDetector = new PHPWordExtractor();
35 * Method declared on ITextHover
37 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
38 if (hoverRegion != null) {
40 if (hoverRegion.getLength() > -1) {
41 String word = textViewer.getDocument().get(hoverRegion.getOffset(), hoverRegion.getLength());
42 if (functionDescriptions == null) {
43 functionDescriptions = new HashMap();
44 // PHPSyntaxRdr syntaxRdr = new PHPSyntaxRdr();
45 // syntaxRdr.readInSyntax();
47 // Vector syntaxbuffer = syntaxRdr.getsyntaxdata();
48 Vector syntaxbuffer = PHPSyntaxRdr.getsyntaxdata();
49 String strbuffer = null;
50 PHPElement elbuffer = null;
51 while ((syntaxbuffer != null)
52 && (!syntaxbuffer.isEmpty() &&
53 ((elbuffer = (PHPElement) syntaxbuffer.remove(0)) != null))) {
54 functionDescriptions.put(elbuffer.getName(), elbuffer.getHoverText());
57 // functionDescriptions = new HashMap(997);
58 // for (int i=0; i<PHPFunctionNames.FUNCTION_NAMES.length;i++) {
59 // functionDescriptions.put(PHPFunctionNasmes.FUNCTION_NAMES[i],PHPFunctionDescription.FUNCTION_DESCRIPTION[i]);
62 return (String) functionDescriptions.get(word);
64 // } catch (BadLocationException x) {
65 } catch (Exception x) {
68 return "empty selection";
72 * Method declared on ITextHover
74 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
75 Point selection = PHPWordExtractor.findWord(textViewer.getDocument(), offset);
76 // show the extracted word as a tooltip
77 if (selection != null && selection.x <= offset && offset < selection.x + selection.y)
78 return new Region(selection.x, selection.y);
79 return new Region(offset, 0);