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