1 package net.sourceforge.phpdt.internal.corext.phpdoc;
3 import java.io.FileInputStream;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
7 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
8 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
10 import org.eclipse.core.resources.IFile;
11 import org.eclipse.core.runtime.CoreException;
12 import org.eclipse.core.runtime.Path;
15 * Utility class for static PHPdoc helper mehods
17 public class PHPDocUtil {
20 * Generate a PHPDoc hover text if possible
22 * @param hoverInfoBuffer
26 public static void appendPHPDoc(StringBuffer hoverInfoBuffer,
27 String filename, PHPIdentifierLocation location) {
28 hoverInfoBuffer.append(location.toString());
29 hoverInfoBuffer.append(" - <b>");
31 hoverInfoBuffer.append(getUsage(filename, location));
32 hoverInfoBuffer.append("</b><br>");
34 // read the phpdoc for the function
35 if (location.getPHPDocOffset() >= 0) {
36 String encoding = getEncoding(filename);
37 InputStreamReader phpFileReader = new InputStreamReader(
38 new FileInputStream(filename), encoding);
39 char[] phpDocDeclarationCharArray = new char[location
41 phpFileReader.skip(location.getPHPDocOffset());
42 phpFileReader.read(phpDocDeclarationCharArray, 0, location
44 PHPDocCharArrayCommentReader phpdocConverter = new PHPDocCharArrayCommentReader(
45 phpDocDeclarationCharArray);
46 hoverInfoBuffer.append(phpdocConverter.getString());
47 phpFileReader.close();
50 } catch (IOException e) {
56 static String getEncoding(String filename) {
57 String encoding = null;
58 IFile file = PHPeclipsePlugin.getWorkspace().getRoot()
59 .getFileForLocation(new Path(filename));
62 encoding = file.getCharset();
63 } catch (CoreException e) {
64 // TODO: should log the fact that we could not get the encoding?
70 public static String getUsage(String filename,
71 PHPIdentifierLocation location) {
72 String usage = location.getUsage();
79 String encoding = getEncoding(filename);
80 InputStreamReader phpFileReader = new InputStreamReader(
81 new FileInputStream(filename), encoding);
82 // read the function declaration
83 if (location.getOffset() >= 0
84 && (location.isMethod() || location.isConstructor()
85 || location.isFunction() || location.isDefine())) {
86 char[] functionDeclarationCharArray = new char[256];
87 int offset = location.getOffset();
88 phpFileReader.skip(offset);
89 int length = phpFileReader.read(functionDeclarationCharArray,
94 for (int i = 0; i < length; i++) {
95 if (functionDeclarationCharArray[i] == ')') {
99 if (functionDeclarationCharArray[i] == '{'
100 || functionDeclarationCharArray[i] == '}') {
105 usage = new String(functionDeclarationCharArray, 0, length);
107 phpFileReader.close();
108 } catch (IOException e) {
111 // cache the usage string:
112 location.setUsage(usage);