X-Git-Url: http://git.phpeclipse.com diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/IdentifierIndexManager.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/IdentifierIndexManager.java index dd2672b..1047779 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/IdentifierIndexManager.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/IdentifierIndexManager.java @@ -8,10 +8,13 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Collection; +import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.SortedMap; import java.util.StringTokenizer; +import java.util.TreeMap; import net.sourceforge.phpdt.core.compiler.ITerminalSymbols; import net.sourceforge.phpdt.core.compiler.InvalidInputException; @@ -34,13 +37,42 @@ public class IdentifierIndexManager { private int fToken; public LineCreator() { - fScanner = new Scanner(false, false); + fScanner = new Scanner(true, false); } + /** - * gets the next token from input + * Add the information of the current identifier to the line + * + * @param typeOfIdentifier the type of the identifier ('c'lass, 'd'efine, 'f'unction, 'm'ethod, 'v'ariable) + * @param identifier current identifier + * @param line Buffer for the current index line + * @param phpdocOffset the offset of the PHPdoc comment if available + * @param phpdocLength the length of the PHPdoc comment if available */ - private void getNextToken() { + private void addIdentifierInformation( + char typeOfIdentifier, + char[] identifier, + StringBuffer line, + int phpdocOffset, + int phpdocLength) { + + line.append('\t'); + line.append(typeOfIdentifier); + line.append(identifier); + line.append("\to"); // Offset + line.append(fScanner.getCurrentTokenStartPosition()); + if (phpdocOffset >= 0) { + line.append("\tp"); // phpdoc offset + line.append(phpdocOffset); + line.append("\tl"); // phpdoc length + line.append(phpdocLength); + } + } + /** + * Get the next token from input + */ + private void getNextToken() { try { fToken = fScanner.getNextToken(); if (Scanner.DEBUG) { @@ -59,17 +91,29 @@ public class IdentifierIndexManager { private void parseDeclarations(StringBuffer buf, boolean goBack) { char[] ident; + char[] classVariable; int counter = 0; + int phpdocOffset = -1; + int phpdocLength = -1; try { while (fToken != TokenNameEOF && fToken != TokenNameERROR) { + phpdocOffset = -1; + if (fToken == TokenNameCOMMENT_PHPDOC) { + phpdocOffset = fScanner.getCurrentTokenStartPosition(); + phpdocLength = fScanner.getCurrentTokenEndPosition() - fScanner.getCurrentTokenStartPosition() + 1; + getNextToken(); + if (fToken == TokenNameEOF || fToken == TokenNameERROR) { + break; + } + } if (fToken == TokenNamevar) { getNextToken(); if (fToken == TokenNameVariable) { ident = fScanner.getCurrentIdentifierSource(); - buf.append("\tv"); - buf.append(ident); - + classVariable = new char[ident.length-1]; + System.arraycopy(ident, 1, classVariable, 0, ident.length-1); + addIdentifierInformation('v', classVariable, buf, phpdocOffset, phpdocLength); getNextToken(); } } else if (fToken == TokenNamefunction) { @@ -79,8 +123,7 @@ public class IdentifierIndexManager { } if (fToken == TokenNameIdentifier) { ident = fScanner.getCurrentIdentifierSource(); - buf.append("\tm"); - buf.append(ident); + addIdentifierInformation('m', ident, buf, phpdocOffset, phpdocLength); getNextToken(); parseDeclarations(buf, true); } @@ -88,8 +131,7 @@ public class IdentifierIndexManager { getNextToken(); if (fToken == TokenNameIdentifier) { ident = fScanner.getCurrentIdentifierSource(); - buf.append("\tc"); - buf.append(ident); + addIdentifierInformation('c', ident, buf, phpdocOffset, phpdocLength); getNextToken(); //skip tokens for classname, extends and others until we have the opening '{' @@ -98,6 +140,16 @@ public class IdentifierIndexManager { } parseDeclarations(buf, true); } + } else if (fToken == TokenNamedefine) { + getNextToken(); + if (fToken == TokenNameLPAREN) { + getNextToken(); + if (fToken == TokenNameStringLiteral) { + ident = fScanner.getCurrentStringLiteralSource(); + addIdentifierInformation('d', ident, buf, phpdocOffset, phpdocLength); + getNextToken(); + } + } } else if ((fToken == TokenNameLBRACE) || (fToken == TokenNameDOLLAR_LBRACE)) { getNextToken(); counter++; @@ -121,6 +173,8 @@ public class IdentifierIndexManager { char[] ident; String identifier; int counter = 0; + int phpdocOffset = -1; + int phpdocLength = -1; fScanner.setSource(charArray); fScanner.setPHPMode(false); @@ -129,6 +183,15 @@ public class IdentifierIndexManager { try { while (fToken != TokenNameEOF && fToken != TokenNameERROR) { + phpdocOffset = -1; + if (fToken == TokenNameCOMMENT_PHPDOC) { + phpdocOffset = fScanner.getCurrentTokenStartPosition(); + phpdocLength = fScanner.getCurrentTokenEndPosition() - fScanner.getCurrentTokenStartPosition() + 1; + getNextToken(); + if (fToken == TokenNameEOF || fToken == TokenNameERROR) { + break; + } + } if (fToken == TokenNamefunction) { getNextToken(); if (fToken == TokenNameAND) { @@ -136,8 +199,7 @@ public class IdentifierIndexManager { } if (fToken == TokenNameIdentifier) { ident = fScanner.getCurrentIdentifierSource(); - buf.append("\tf"); - buf.append(ident); + addIdentifierInformation('f', ident, buf, phpdocOffset, phpdocLength); getNextToken(); parseDeclarations(buf, true); } @@ -145,8 +207,7 @@ public class IdentifierIndexManager { getNextToken(); if (fToken == TokenNameIdentifier) { ident = fScanner.getCurrentIdentifierSource(); - buf.append("\tc"); - buf.append(ident); + addIdentifierInformation('c', ident, buf, phpdocOffset, phpdocLength); getNextToken(); //skip fTokens for classname, extends and others until we have the opening '{' @@ -157,6 +218,16 @@ public class IdentifierIndexManager { parseDeclarations(buf, true); } + } else if (fToken == TokenNamedefine) { + getNextToken(); + if (fToken == TokenNameLPAREN) { + getNextToken(); + if (fToken == TokenNameStringLiteral) { + ident = fScanner.getCurrentStringLiteralSource(); + addIdentifierInformation('d', ident, buf, phpdocOffset, phpdocLength); + getNextToken(); + } + } } else { getNextToken(); } @@ -168,9 +239,22 @@ public class IdentifierIndexManager { } } + class StringComparator implements Comparator { + public int compare(Object o1, Object o2) { + String s1 = (String)o1; + String s2 = (String)o2; + return s1.compareTo(s2); + // return s1.toUpperCase().compareTo(s2.toUpperCase()); + } + public boolean equals(Object o) { + String s = (String)o; + return compare(this, o)==0; + } + } + private HashMap fFileMap; private String fFilename; - private HashMap fIndentifierMap; + private TreeMap fIndentifierMap; public IdentifierIndexManager(String filename) { fFilename = filename; @@ -223,7 +307,8 @@ public class IdentifierIndexManager { String token; String identifier = null; String classname = null; - PHPIdentifier phpIdentifier = null; + String offset = null; + PHPIdentifierLocation phpIdentifier = null; boolean tokenExists = false; tokenizer = new StringTokenizer(line, "\t"); @@ -244,6 +329,10 @@ public class IdentifierIndexManager { classname = identifier; phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.CLASS, phpFileName); break; + case 'd' : // define + identifier = token.substring(1); + phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.DEFINE, phpFileName); + break; case 'f' : // function name identifier = token.substring(1); phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.FUNCTION, phpFileName); @@ -256,6 +345,27 @@ public class IdentifierIndexManager { identifier = token.substring(1); phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.VARIABLE, phpFileName, classname); break; + case 'o' : // offset information + identifier = null; + if (phpIdentifier != null) { + offset = token.substring(1); + phpIdentifier.setOffset(Integer.parseInt(offset)); + } + break; + case 'p' : // PHPdoc offset information + identifier = null; + if (phpIdentifier != null) { + offset = token.substring(1); + phpIdentifier.setPHPDocOffset(Integer.parseInt(offset)); + } + break; + case 'l' : // PHPdoc length information + identifier = null; + if (phpIdentifier != null) { + offset = token.substring(1); + phpIdentifier.setPHPDocLength(Integer.parseInt(offset)); + } + break; default : identifier = null; phpIdentifier = null; @@ -311,7 +421,7 @@ public class IdentifierIndexManager { * */ public void initialize() { - fIndentifierMap = new HashMap(); + fIndentifierMap = new TreeMap(new StringComparator()); fFileMap = new HashMap(); } @@ -386,6 +496,10 @@ public class IdentifierIndexManager { classname = identifier; phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.CLASS, phpFileName); break; + case 'd' : // define + identifier = token.substring(1); + phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.DEFINE, phpFileName); + break; case 'f' : // function name identifier = token.substring(1); phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.FUNCTION, phpFileName); @@ -438,9 +552,21 @@ public class IdentifierIndexManager { fileWriter.write(line + '\n'); } fileWriter.close(); + } catch (FileNotFoundException e) { + // ignore exception; project is deleted by user } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } + + /** + * @param fromKey + * @param toKey + * @return + */ + public SortedMap getIdentifierMap() { + return fIndentifierMap; + } + } \ No newline at end of file