22e14708879c3b731a16c6fbd8ce41500a067dfa
[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  www.phpeclipse.de
11  **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor;
13
14 import java.io.FileReader;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
18
19 import net.sourceforge.phpdt.internal.corext.phpdoc.PHPDocUtil;
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;
25
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;
32
33 /**
34  * Implementation for an <code>ITextHover</code> which hovers over PHP code.
35  */
36 public class PHPTextHover implements ITextHover {
37         public static HashMap functionDescriptions = null;
38
39         private static PHPWordExtractor phpWordDetector = new PHPWordExtractor();
40
41         /**
42          * The current project; maybe <code>null</code> for preference pages
43          */
44         private IProject fProject;
45
46         public PHPTextHover(IProject project) {
47                 fProject = project;
48         }
49
50         /*
51          * (non-Javadoc) Method declared on ITextHover
52          */
53         public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
54                 if (hoverRegion != null) {
55                         try {
56                                 if (hoverRegion.getLength() > -1) {
57                                         String word = textViewer.getDocument().get(hoverRegion.getOffset(), hoverRegion.getLength());
58                                         if (functionDescriptions == null) {
59                                                 functionDescriptions = new HashMap();
60                                                 ArrayList syntaxbuffer = PHPSyntaxRdr.getSyntaxData();
61                                                 String strbuffer = null;
62                                                 PHPElement elbuffer = null;
63                                                 if (syntaxbuffer != null) {
64                                                         for (int i = 0; i < syntaxbuffer.size(); i++) {
65                                                                 elbuffer = (PHPElement) syntaxbuffer.get(i);
66                                                                 functionDescriptions.put(elbuffer.getName(), elbuffer.getHoverText());
67                                                         }
68                                                 }
69                                                 //
70                                                 // while ((syntaxbuffer != null)
71                                                 // && (!syntaxbuffer.isEmpty() && ((elbuffer = (PHPElement)
72                                                 // syntaxbuffer.remove(0)) != null))) {
73                                                 // functionDescriptions.put(elbuffer.getName(),
74                                                 // elbuffer.getHoverText());
75                                                 // }
76                                         }
77                                         String hoverInfo = (String) functionDescriptions.get(word);
78                                         if (hoverInfo == null & word.length() > 0) {
79                                                 hoverInfo = (String) functionDescriptions.get(word.toLowerCase());
80                                         }
81                                         if (hoverInfo == null && fProject != null) {
82                                                 // get the possible PHPDoc information from the index file
83                                                 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
84                                                 List list = indexManager.getLocations(word);
85                                                 if (list.size() > 0) {
86                                                         try {
87                                                                 PHPIdentifierLocation location;
88                                                                 String filename;
89                                                                 FileReader phpdocFileReader;
90                                                                 StringBuffer hoverInfoBuffer = new StringBuffer();
91                                                                 String workspaceLocation;
92                                                                 if (fProject != null) {
93                                                                         workspaceLocation = fProject.getLocation().toString() + '/';
94                                                                 } else {
95                                                                         // should never happen?
96                                                                         workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
97                                                                 }
98                                                                 // boolean foundPHPdoc = false;
99                                                                 for (int i = 0; i < list.size(); i++) {
100                                                                         location = (PHPIdentifierLocation) list.get(i);
101                                                                         filename = workspaceLocation + location.getFilename();
102                                                                         PHPDocUtil.appendPHPDoc(hoverInfoBuffer, filename, location);
103                                                                 }
104                                                                 hoverInfo = hoverInfoBuffer.toString();
105                                                         } catch (Throwable e) {
106                                                                 // ignore exceptions
107                                                                 // e.printStackTrace();
108                                                         }
109                                                 }
110                                         }
111                                         return hoverInfo;
112                                 }
113                                 // } catch (BadLocationException x) {
114                         } catch (Exception x) {
115                         }
116                 }
117                 return null;
118                 // don't show this annoying text
119                 // return "empty selection";
120         }
121
122         /*
123          * (non-Javadoc) Method declared on ITextHover
124          */
125         public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
126                 Point selection = PHPWordExtractor.findWord(textViewer.getDocument(), offset);
127                 // show the extracted word as a tooltip
128                 if (selection != null && selection.x <= offset && offset < selection.x + selection.y)
129                         return new Region(selection.x, selection.y);
130                 return new Region(offset, 0);
131         }
132 }