bc388b19a92388052fcd18944668e5a061796e81
[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.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.PHPFunction;
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         private static HashMap functionDescriptions = null;
38
39         private static HashMap identDescriptions = null;
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(
58                                                         hoverRegion.getOffset(), hoverRegion.getLength());
59                                         if (functionDescriptions == null) {
60                                                 functionDescriptions = new HashMap();
61                                                 identDescriptions = new HashMap();
62                                                 ArrayList syntaxbuffer = PHPSyntaxRdr.getSyntaxData();
63                                                 PHPElement elbuffer = null;
64                                                 if (syntaxbuffer != null) {
65                                                         for (int i = 0; i < syntaxbuffer.size(); i++) {
66                                                                 elbuffer = (PHPElement) syntaxbuffer.get(i);
67                                                                 if (elbuffer instanceof PHPFunction) {
68                                                                         functionDescriptions.put(
69                                                                                         elbuffer.getName(), elbuffer
70                                                                                                         .getHoverText());
71                                                                 } else {
72                                                                         identDescriptions.put(elbuffer.getName(),
73                                                                                         elbuffer.getHoverText());
74                                                                 }
75                                                         }
76                                                 }
77                                                 //
78                                                 // while ((syntaxbuffer != null)
79                                                 // && (!syntaxbuffer.isEmpty() && ((elbuffer =
80                                                 // (PHPElement)
81                                                 // syntaxbuffer.remove(0)) != null))) {
82                                                 // functionDescriptions.put(elbuffer.getName(),
83                                                 // elbuffer.getHoverText());
84                                                 // }
85                                         }
86                                         String hoverInfo = (String) identDescriptions.get(word);
87                                         if (hoverInfo == null & word.length() > 0) {
88                                                 hoverInfo = (String) functionDescriptions.get(word
89                                                                 .toLowerCase());
90                                         }
91                                         if (hoverInfo == null && fProject != null) {
92                                                 // get the possible PHPDoc information from the index
93                                                 // file
94                                                 IdentifierIndexManager indexManager = PHPeclipsePlugin
95                                                                 .getDefault().getIndexManager(fProject);
96                                                 List list = indexManager.getLocations(word);
97                                                 if (list.size() > 0) {
98                                                         try {
99                                                                 PHPIdentifierLocation location;
100                                                                 String filename;
101                                                                 StringBuffer hoverInfoBuffer = new StringBuffer();
102                                                                 String workspaceLocation;
103                                                                 if (fProject != null) {
104                                                                         workspaceLocation = fProject.getFullPath()
105                                                                                         .toString() + '/';
106                                                                 } else {
107                                                                         // should never happen?
108                                                                         workspaceLocation = PHPeclipsePlugin
109                                                                                         .getWorkspace().getRoot()
110                                                                                         .getFullPath().toString();
111                                                                 }
112                                                                 // boolean foundPHPdoc = false;
113                                                                 for (int i = 0; i < list.size(); i++) {
114                                                                         location = (PHPIdentifierLocation) list
115                                                                                         .get(i);
116                                                                         filename = workspaceLocation
117                                                                                         + location.getFilename();
118                                                                         PHPDocUtil.appendPHPDoc(hoverInfoBuffer,
119                                                                                         filename, location);
120                                                                 }
121                                                                 hoverInfo = hoverInfoBuffer.toString();
122                                                         } catch (Throwable e) {
123                                                                 // ignore exceptions
124                                                                 // e.printStackTrace();
125                                                         }
126                                                 }
127                                         }
128                                         return hoverInfo;
129                                 }
130                                 // } catch (BadLocationException x) {
131                         } catch (Exception x) {
132                         }
133                 }
134                 return null;
135                 // don't show this annoying text
136                 // return "empty selection";
137         }
138
139         /*
140          * (non-Javadoc) Method declared on ITextHover
141          */
142         public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
143                 Point selection = PHPWordExtractor.findWord(textViewer.getDocument(),
144                                 offset);
145                 // show the extracted word as a tooltip
146                 if (selection != null && selection.x <= offset
147                                 && offset < selection.x + selection.y)
148                         return new Region(selection.x, selection.y);
149                 return new Region(offset, 0);
150         }
151 }