Fixed some bugs in the syntax editor preference page
[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     Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor;
13
14 import java.io.FileReader;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Vector;
18
19 import net.sourceforge.phpdt.internal.corext.phpdoc.PHPDocCharArrayCommentReader;
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> 
35  * which hovers over PHP code.
36  */
37 public class PHPTextHover implements ITextHover {
38   public static HashMap functionDescriptions = null;
39
40   private static PHPWordExtractor phpWordDetector = new PHPWordExtractor();
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   /* (non-Javadoc)
50    * Method declared on ITextHover
51    */
52   public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
53     if (hoverRegion != null) {
54       try {
55         if (hoverRegion.getLength() > -1) {
56           String word = textViewer.getDocument().get(hoverRegion.getOffset(), hoverRegion.getLength());
57           if (functionDescriptions == null) {
58             functionDescriptions = new HashMap();
59             //                  PHPSyntaxRdr syntaxRdr = new PHPSyntaxRdr();
60             //                  syntaxRdr.readInSyntax();
61
62             //                  Vector syntaxbuffer = syntaxRdr.getsyntaxdata();
63             Vector syntaxbuffer = PHPSyntaxRdr.getsyntaxdata();
64             String strbuffer = null;
65             PHPElement elbuffer = null;
66             while ((syntaxbuffer != null)
67               && (!syntaxbuffer.isEmpty() && ((elbuffer = (PHPElement) syntaxbuffer.remove(0)) != null))) {
68               functionDescriptions.put(elbuffer.getName(), elbuffer.getHoverText());
69             }
70
71             //            functionDescriptions = new HashMap(997);
72             //            for (int i=0; i<PHPFunctionNames.FUNCTION_NAMES.length;i++) {
73             //              functionDescriptions.put(PHPFunctionNasmes.FUNCTION_NAMES[i],PHPFunctionDescription.FUNCTION_DESCRIPTION[i]);
74             //            }
75           }
76           String hoverInfo = (String) functionDescriptions.get(word);
77           if (hoverInfo == null && fProject != null) {
78             // get the possible PHPDoc information from the index file
79             IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
80             List list = indexManager.getLocations(word);
81             if (list.size() > 0) {
82               try {
83                 PHPIdentifierLocation location;
84                 String filename;
85                 FileReader phpdocFileReader;
86                 PHPDocCharArrayCommentReader phpdocConverter;
87                 StringBuffer hoverInfoBuffer = new StringBuffer();
88                 String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
89                 //                boolean foundPHPdoc = false;
90                 for (int i = 0; i < list.size(); i++) {
91                   location = (PHPIdentifierLocation) list.get(i);
92                                                                         filename = workspaceLocation + location.getFilename();
93                                                                         hoverInfoBuffer.append(location.toString());
94                                                                         hoverInfoBuffer.append('\n');
95                   if (location.getPHPDocOffset() >= 0) {
96                     //                    foundPHPdoc = true;
97                     phpdocFileReader = new FileReader(filename);
98                     char[] charArray = new char[location.getPHPDocLength()];
99                     phpdocFileReader.skip(location.getPHPDocOffset());
100                     phpdocFileReader.read(charArray, 0, location.getPHPDocLength());
101                     phpdocConverter = new PHPDocCharArrayCommentReader(charArray);
102                     hoverInfoBuffer.append(phpdocConverter.getString());
103                     hoverInfoBuffer.append('\n');
104                   }
105                 }
106                 //                if (foundPHPdoc) {
107                 hoverInfo = hoverInfoBuffer.toString();
108                 //                }
109               } catch (Throwable e) {
110                 // ignore exceptions
111                 // e.printStackTrace();
112               }
113             }
114           }
115           return hoverInfo;
116         }
117         //      } catch (BadLocationException x) {
118       } catch (Exception x) {
119       }
120     }
121     return "empty selection";
122   }
123
124   /* (non-Javadoc)
125    * Method declared on ITextHover
126    */
127   public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
128     Point selection = PHPWordExtractor.findWord(textViewer.getDocument(), offset);
129     //  show the extracted word as a tooltip
130     if (selection != null && selection.x <= offset && offset < selection.x + selection.y)
131       return new Region(selection.x, selection.y);
132     return new Region(offset, 0);
133   }
134 }