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