package net.sourceforge.phpdt.internal.corext.phpdoc; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import net.sourceforge.phpeclipse.PHPeclipsePlugin; import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation; import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.Path; /** * Utility class for static PHPdoc helper mehods */ public class PHPDocUtil { /** * Generate a PHPDoc hover text if possible * * @param hoverInfoBuffer * @param filename * @param location */ public static void appendPHPDoc(StringBuffer hoverInfoBuffer, String filename, PHPIdentifierLocation location) { hoverInfoBuffer.append(location.toString()); hoverInfoBuffer.append(" - "); try { hoverInfoBuffer.append(getUsage(filename, location)); hoverInfoBuffer.append("
"); // read the phpdoc for the function if (location.getPHPDocOffset() >= 0) { String encoding = getEncoding(filename); InputStreamReader phpFileReader = new InputStreamReader( new FileInputStream(filename), encoding); char[] phpDocDeclarationCharArray = new char[location .getPHPDocLength()]; phpFileReader.skip(location.getPHPDocOffset()); phpFileReader.read(phpDocDeclarationCharArray, 0, location .getPHPDocLength()); PHPDocCharArrayCommentReader phpdocConverter = new PHPDocCharArrayCommentReader( phpDocDeclarationCharArray); hoverInfoBuffer.append(phpdocConverter.getString()); phpFileReader.close(); } } catch (IOException e) { // TODO: smell return; } } static String getEncoding(String filename) { String encoding = null; IFile file = PHPeclipsePlugin.getWorkspace().getRoot() .getFileForLocation(new Path(filename)); if (file != null) { try { encoding = file.getCharset(); } catch (CoreException e) { // TODO: should log the fact that we could not get the encoding? } } return encoding; } public static String getUsage(String filename, PHPIdentifierLocation location) { String usage = location.getUsage(); if (usage != null) { return usage; } usage = ""; try { String encoding = getEncoding(filename); InputStreamReader phpFileReader = new InputStreamReader( new FileInputStream(filename), encoding); // read the function declaration if (location.getOffset() >= 0 && (location.isMethod() || location.isConstructor() || location.isFunction() || location.isDefine())) { char[] functionDeclarationCharArray = new char[256]; int offset = location.getOffset(); phpFileReader.skip(offset); int length = phpFileReader.read(functionDeclarationCharArray, 0, 256); if (length == -1) { length = 256; } for (int i = 0; i < length; i++) { if (functionDeclarationCharArray[i] == ')') { length = i + 1; break; } if (functionDeclarationCharArray[i] == '{' || functionDeclarationCharArray[i] == '}') { length = i; break; } } usage = new String(functionDeclarationCharArray, 0, length); } phpFileReader.close(); } catch (IOException e) { // do nothing } // cache the usage string: location.setUsage(usage); return usage; } }