package net.sourceforge.phpdt.internal.corext.phpdoc;
import java.io.FileInputStream;
+import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
import net.sourceforge.phpeclipse.PHPeclipsePlugin;
import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
// read the phpdoc for the function
if (location.getPHPDocOffset() >= 0) {
- String encoding = getEncoding(filename);
- InputStreamReader phpFileReader = new InputStreamReader(
- new FileInputStream(filename), encoding);
+ InputStreamReader phpFileReader = createReader(filename);
+ if (phpFileReader == null)
+ return;
char[] phpDocDeclarationCharArray = new char[location
.getPHPDocLength()];
phpFileReader.skip(location.getPHPDocOffset());
usage = "";
try {
- String encoding = getEncoding(filename);
- InputStreamReader phpFileReader = new InputStreamReader(
- new FileInputStream(filename), encoding);
+ InputStreamReader phpFileReader = createReader(filename);
+ if (phpFileReader == null)
+ return "";
// read the function declaration
if (location.getOffset() >= 0
&& (location.isMethod() || location.isConstructor()
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;
+ if (location.isDefine()) {
+ length = getClosingParenthesis(functionDeclarationCharArray);
+ if (length < 0)
+ return "";
+ } else {
+ 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);
+ // cache the usage string:
+ location.setUsage(usage);
}
phpFileReader.close();
+
} catch (IOException e) {
// do nothing
}
- // cache the usage string:
- location.setUsage(usage);
return usage;
}
+
+ private static InputStreamReader createReader(String filename) {
+ IFile file = PHPeclipsePlugin.getWorkspace().getRoot()
+ .getFileForLocation(new Path(filename));
+ if (file != null) {
+ try {
+ return new InputStreamReader(new FileInputStream(file
+ .getFullPath().toString()), file.getCharset());
+ } catch (UnsupportedEncodingException e) {
+ // do nothing
+ } catch (FileNotFoundException e) {
+ // do nothing
+ } catch (CoreException e) {
+ // do nothing
+ }
+ }
+ return null;
+ }
+
+ private static int getClosingParenthesis(char[] buffer) {
+ int p = 0;
+ boolean dq = false;
+ boolean sq = false;
+
+ for (int i = 0; i < buffer.length; i++) {
+ if (buffer[i] == '\\') {
+ i++;
+ continue;
+ }
+ if (dq) {
+ dq = (buffer[i] != '"');
+ continue;
+ }
+ if (sq) {
+ sq = (buffer[i] != '\'');
+ continue;
+ }
+ switch (buffer[i]) {
+ case '(':
+ p++;
+ break;
+ case ')':
+ p--;
+ if (p < 0)
+ return i;
+ break;
+ case '"':
+ dq = true;
+ break;
+ case '\'':
+ sq = true;
+ break;
+ }
+ }
+ return -1;
+ }
+
}