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