fix #774 infinite loop in net.sourceforge.phpeclipse.builder.IdentifierIndexManager...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / java / hover / JavaSourceHover.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation 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 API and implementation
10  *******************************************************************************/
11
12 package net.sourceforge.phpdt.internal.ui.text.java.hover;
13
14 import java.io.IOException;
15
16 import net.sourceforge.phpdt.core.IJavaElement;
17 import net.sourceforge.phpdt.core.IMember;
18 import net.sourceforge.phpdt.core.ISourceReference;
19 import net.sourceforge.phpdt.core.JavaModelException;
20 import net.sourceforge.phpdt.internal.ui.text.HTMLPrinter;
21 import net.sourceforge.phpdt.internal.ui.text.PHPCodeReader;
22 import net.sourceforge.phpdt.internal.ui.viewsupport.JavaElementLabels;
23 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
24
25 import org.eclipse.jface.text.Document;
26 import org.eclipse.jface.text.IDocument;
27
28 /**
29  * Provides source as hover info for Java elements.
30  */
31 public class JavaSourceHover extends AbstractJavaEditorTextHover {
32
33         private final int LABEL_FLAGS = JavaElementLabels.ALL_FULLY_QUALIFIED
34                         | JavaElementLabels.M_PRE_RETURNTYPE
35                         | JavaElementLabels.M_PARAMETER_TYPES
36                         | JavaElementLabels.M_PARAMETER_NAMES
37                         | JavaElementLabels.M_EXCEPTIONS
38                         | JavaElementLabels.F_PRE_TYPE_SIGNATURE;
39
40         /*
41          * @see JavaElementHover
42          */
43         protected String getHoverInfo(IJavaElement[] result) {
44                 int nResults = result.length;
45                 StringBuffer buffer = new StringBuffer();
46
47                 if (nResults > 1) {
48
49                         for (int i = 0; i < result.length; i++) {
50                                 HTMLPrinter.startBulletList(buffer);
51                                 IJavaElement curr = result[i];
52                                 if (curr instanceof IMember)
53                                         HTMLPrinter.addBullet(buffer, getInfoText((IMember) curr));
54                                 HTMLPrinter.endBulletList(buffer);
55                         }
56
57                 } else {
58
59                         IJavaElement curr = result[0];
60                         if (curr instanceof IMember && curr instanceof ISourceReference) {
61                                 HTMLPrinter.addSmallHeader(buffer,
62                                                 getInfoText(((IMember) curr)));
63                                 try {
64                                         String source = ((ISourceReference) curr).getSource();
65                                         source = removeLeadingComments(source);
66                                         HTMLPrinter.addParagraph(buffer, "<pre>"); //$NON-NLS-1$
67                                         HTMLPrinter.addParagraph(buffer, source);
68                                         HTMLPrinter.addParagraph(buffer, "</pre>"); //$NON-NLS-1$
69                                 } catch (JavaModelException ex) {
70                                         // only write small header
71                                 }
72                         }
73                 }
74
75                 if (buffer.length() > 0) {
76                         HTMLPrinter.insertPageProlog(buffer, 0);
77                         HTMLPrinter.addPageEpilog(buffer);
78                         return buffer.toString();
79                 }
80
81                 return null;
82         }
83
84         private String getInfoText(IMember member) {
85                 return JavaElementLabels.getElementLabel(member, LABEL_FLAGS);
86         }
87
88         private String removeLeadingComments(String source) {
89                 PHPCodeReader reader = new PHPCodeReader();
90                 IDocument document = new Document(source);
91                 int i;
92                 try {
93                         reader.configureForwardReader(document, 0, document.getLength(),
94                                         true, false);
95                         int c = reader.read();
96                         while (c != -1 && (c == '\r' || c == '\n')) {
97                                 c = reader.read();
98                         }
99                         i = reader.getOffset();
100                         reader.close();
101                 } catch (IOException ex) {
102                         i = 0;
103                 } finally {
104                         try {
105                                 if (reader != null)
106                                         reader.close();
107                         } catch (IOException ex) {
108                                 PHPeclipsePlugin.log(ex);
109                         }
110                 }
111
112                 if (i < 0)
113                         return source;
114                 return source.substring(i);
115         }
116 }