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