RC2 compatibility
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPTextHover.java
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
7
8  Contributors:
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
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;
24
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;
31 /**
32  * Implementation for an <code>ITextHover</code> which hovers over PHP code.
33  */
34 public class PHPTextHover implements ITextHover {
35   public static HashMap functionDescriptions = null;
36   private static PHPWordExtractor phpWordDetector = new PHPWordExtractor();
37   /**
38    * The current project; maybe <code>null</code> for preference pages
39    */
40   private IProject fProject;
41   public PHPTextHover(IProject project) {
42     fProject = project;
43   }
44   /*
45    * (non-Javadoc) Method declared on ITextHover
46    */
47   public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
48     if (hoverRegion != null) {
49       try {
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
62                     .getHoverText());
63               }
64             }
65             //            
66             //            while ((syntaxbuffer != null)
67             //              && (!syntaxbuffer.isEmpty() && ((elbuffer = (PHPElement)
68             // syntaxbuffer.remove(0)) != null))) {
69             //              functionDescriptions.put(elbuffer.getName(),
70             // elbuffer.getHoverText());
71             //            }
72           }
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()
77                 .getIndexManager(fProject);
78             List list = indexManager.getLocations(word);
79             if (list.size() > 0) {
80               try {
81                 PHPIdentifierLocation location;
82                 String filename;
83                 FileReader phpdocFileReader;
84                 StringBuffer hoverInfoBuffer = new StringBuffer();
85                 String workspaceLocation = PHPeclipsePlugin.getWorkspace()
86                     .getRoot().getLocation().toString();
87                 //                boolean foundPHPdoc = false;
88                 for (int i = 0; i < list.size(); i++) {
89                   location = (PHPIdentifierLocation) list.get(i);
90                   filename = workspaceLocation + location.getFilename();
91                   PHPDocUtil.appendPHPDoc(hoverInfoBuffer, filename, location);
92                 }
93                 hoverInfo = hoverInfoBuffer.toString();
94               } catch (Throwable e) {
95                 // ignore exceptions
96                 // e.printStackTrace();
97               }
98             }
99           }
100           return hoverInfo;
101         }
102         //      } catch (BadLocationException x) {
103       } catch (Exception x) {
104       }
105     }
106     return null;
107     // don't show this annoying text
108     //    return "empty selection";
109   }
110   /*
111    * (non-Javadoc) Method declared on ITextHover
112    */
113   public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
114     Point selection = PHPWordExtractor.findWord(textViewer.getDocument(),
115         offset);
116     //  show the extracted word as a tooltip
117     if (selection != null && selection.x <= offset
118         && offset < selection.x + selection.y)
119       return new Region(selection.x, selection.y);
120     return new Region(offset, 0);
121   }
122 }