Commiting more changes to fix RSE issues with PHP projects.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / ltk / core / RenameLocalVariableDelegate.java
index dfa9ce1..3f2571f 100644 (file)
@@ -31,7 +31,7 @@ import org.eclipse.ltk.core.refactoring.participants.ValidateEditChecker;
  * <p>
  * delegate object that contains the logic used by the processor.
  * </p>
- *
+ * 
  */
 public class RenameLocalVariableDelegate extends RenameIdentifierDelegate {
 
@@ -53,7 +53,8 @@ public class RenameLocalVariableDelegate extends RenameIdentifierDelegate {
                return result;
        }
 
-       RefactoringStatus checkFinalConditions(final IProgressMonitor pm, final CheckConditionsContext ctxt) {
+       RefactoringStatus checkFinalConditions(final IProgressMonitor pm,
+                       final CheckConditionsContext ctxt) {
                RefactoringStatus result = new RefactoringStatus();
                pm.beginTask(CoreTexts.renamePropertyDelegate_checking, 100);
                // do something long-running here: traverse the entire project (or even
@@ -65,13 +66,16 @@ public class RenameLocalVariableDelegate extends RenameIdentifierDelegate {
                        SourceMethod method = info.getMethod();
                        ISourceRange range = method.getSourceRange();
                        if (project.isNatureEnabled(PHPeclipsePlugin.PHP_NATURE_ID)) {
-                               determineMethodOffsets(file, range.getOffset(), range.getLength(), result);
+                               determineMethodOffsets(file, range.getOffset(), range
+                                               .getLength(), result);
                        }
                } catch (CoreException e) {
-                       String msg = "Project: " + project.getLocation().toOSString() + " CoreException " + e.getMessage();
+                       String msg = "Project: " + project.getFullPath().toOSString()
+                                       + " CoreException " + e.getMessage();
                        result.addError(msg);
                } catch (Exception e) {
-                       String msg = "Project: " + project.getLocation().toOSString() + " Exception " + e.getMessage();
+                       String msg = "Project: " + project.getFullPath().toOSString()
+                                       + " Exception " + e.getMessage();
                        result.addError(msg);
                }
 
@@ -80,7 +84,8 @@ public class RenameLocalVariableDelegate extends RenameIdentifierDelegate {
                if (ctxt != null) {
                        IFile[] files = new IFile[phpFiles.size()];
                        phpFiles.keySet().toArray(files);
-                       IConditionChecker checker = ctxt.getChecker(ValidateEditChecker.class);
+                       IConditionChecker checker = ctxt
+                                       .getChecker(ValidateEditChecker.class);
                        ValidateEditChecker editChecker = (ValidateEditChecker) checker;
                        editChecker.addFiles(files);
                }
@@ -88,9 +93,11 @@ public class RenameLocalVariableDelegate extends RenameIdentifierDelegate {
                return result;
        }
 
-       protected void createChange(final IProgressMonitor pm, final CompositeChange rootChange) {
+       protected void createChange(final IProgressMonitor pm,
+                       final CompositeChange rootChange) {
                try {
-                       pm.beginTask(CoreTexts.renamePropertyDelegate_collectingChanges, 100);
+                       pm.beginTask(CoreTexts.renamePropertyDelegate_collectingChanges,
+                                       100);
                        // all files in the same bundle
                        rootChange.addAll(createChangesForContainer(pm));
                } finally {
@@ -98,54 +105,129 @@ public class RenameLocalVariableDelegate extends RenameIdentifierDelegate {
                }
        }
 
-       // finds the offsets of the identifier to rename
-       // usually, this would be the job of a proper parser;
-       // using a primitive brute-force approach here
-       private void determineMethodOffsets(final IFile file, int offset, int length, final RefactoringStatus status) {
+       private void determineMethodOffsets(final IFile file, int offset,
+                       int length, final RefactoringStatus status) {
                ArrayList matches = new ArrayList();
                try {
                        String content = readFileContent(file, status);
+
+                       //
+                       // Find a PHPdoc directly before the method
+                       //
+                       Scanner firstScanner = new Scanner(true, false);
+                       firstScanner.setSource(content.toCharArray());
+                       int fToken = ITerminalSymbols.TokenNameEOF;
+                       int start = 0;
+                       int phpdocStart = -1;
+                       try {
+                               fToken = firstScanner.getNextToken();
+                               while (fToken != ITerminalSymbols.TokenNameEOF
+                                               && start < offset) {
+                                       if (fToken == ITerminalSymbols.TokenNameCOMMENT_PHPDOC) {
+                                               phpdocStart = firstScanner
+                                                               .getCurrentTokenStartPosition();
+                                       } else {
+                                               phpdocStart = -1;
+                                       }
+                                       fToken = firstScanner.getNextToken();
+                                       start = firstScanner.getCurrentTokenStartPosition();
+                               }
+
+                       } catch (InvalidInputException e) {
+                               String msg = "File: " + file.getFullPath().toOSString()
+                                               + " InvalidInputException " + e.getMessage();
+                               status.addError(msg);
+                       } catch (SyntaxError e) {
+                               String msg = "File: " + file.getFullPath().toOSString()
+                                               + " SyntaxError " + e.getMessage();
+                               status.addError(msg);
+                       }
+
+                       //
+                       // Find matches for the word in the PHPdoc+method declaration
+                       //
+                       if (phpdocStart >= 0 && phpdocStart < offset) {
+                               length += offset - phpdocStart;
+                               offset = phpdocStart;
+                       }
                        String methodString = content.substring(offset, offset + length);
-                       Scanner scanner = new Scanner(true, false);
-                       scanner.setSource(methodString.toCharArray());
-                       scanner.setPHPMode(true);
+                       Scanner secondScanner = new Scanner(true, false);
+                       secondScanner.setSource(methodString.toCharArray());
+                       secondScanner.setPHPMode(true);
                        String wordStr = info.getOldName();
+                       boolean renameDQString = info.isRenameDQString();
+                       boolean renamePHPdoc = info.isRenamePHPdoc();
+                       boolean renameOtherComments = info.isRenameOtherComments();
                        char[] word = wordStr.toCharArray();
 
-                       int fToken = ITerminalSymbols.TokenNameEOF;
-                       String dqStr;
-                       int dqOffset;
+                       fToken = ITerminalSymbols.TokenNameEOF;
+                       // double quoted string
+                       String tokenString;
+                       // double quoted string offset
+                       int tokenOffset;
                        int index;
                        try {
-                               fToken = scanner.getNextToken();
+                               fToken = secondScanner.getNextToken();
                                while (fToken != ITerminalSymbols.TokenNameEOF) {
                                        if (fToken == ITerminalSymbols.TokenNameVariable) {
-                                               if (scanner.equalsCurrentTokenSource(word)) {
-                                                       // the current variable token is equal to the given word
-                                                       matches.add(Integer.valueOf(scanner.getCurrentTokenStartPosition() + offset));
+                                               if (secondScanner.equalsCurrentTokenSource(word)) {
+                                                       // the current variable token is equal to the given
+                                                       // word
+                                                       matches.add(new Integer(secondScanner
+                                                                       .getCurrentTokenStartPosition()
+                                                                       + offset));
                                                }
-                                       } else if (fToken == ITerminalSymbols.TokenNameStringDoubleQuote) {
+                                       } else if (fToken == ITerminalSymbols.TokenNameStringDoubleQuote
+                                                       && renameDQString) {
                                                // determine the word in double quoted strings:
-                                               dqStr = new String(scanner.getCurrentTokenSource());
-                                               dqOffset = scanner.getCurrentTokenStartPosition();
+                                               tokenString = new String(secondScanner
+                                                               .getCurrentTokenSource());
+                                               tokenOffset = secondScanner
+                                                               .getCurrentTokenStartPosition();
+                                               index = -1;
+                                               while ((index = tokenString.indexOf(wordStr, index + 1)) >= 0) {
+                                                       matches.add(new Integer(offset + tokenOffset
+                                                                       + index));
+                                               }
+                                       } else if (fToken == ITerminalSymbols.TokenNameCOMMENT_PHPDOC
+                                                       && renamePHPdoc) {
+                                               tokenString = new String(secondScanner
+                                                               .getCurrentTokenSource());
+                                               tokenOffset = secondScanner
+                                                               .getCurrentTokenStartPosition();
+                                               index = -1;
+                                               while ((index = tokenString.indexOf(wordStr, index + 1)) >= 0) {
+                                                       matches.add(new Integer(offset + tokenOffset
+                                                                       + index));
+                                               }
+                                       } else if ((fToken == ITerminalSymbols.TokenNameCOMMENT_BLOCK || fToken == ITerminalSymbols.TokenNameCOMMENT_LINE)
+                                                       && renameOtherComments) {
+                                               tokenString = new String(secondScanner
+                                                               .getCurrentTokenSource());
+                                               tokenOffset = secondScanner
+                                                               .getCurrentTokenStartPosition();
                                                index = -1;
-                                               while ((index = dqStr.indexOf(wordStr, index + 1)) >= 0) {
-                                                       matches.add(Integer.valueOf(offset + dqOffset + index));
+                                               while ((index = tokenString.indexOf(wordStr, index + 1)) >= 0) {
+                                                       matches.add(new Integer(offset + tokenOffset
+                                                                       + index));
                                                }
                                        }
-                                       fToken = scanner.getNextToken();
+                                       fToken = secondScanner.getNextToken();
                                }
 
                        } catch (InvalidInputException e) {
-                               String msg = "File: " + file.getLocation().toOSString() + " InvalidInputException " + e.getMessage();
+                               String msg = "File: " + file.getFullPath().toOSString()
+                                               + " InvalidInputException " + e.getMessage();
                                status.addError(msg);
                        } catch (SyntaxError e) {
-                               String msg = "File: " + file.getLocation().toOSString() + " SyntaxError " + e.getMessage();
+                               String msg = "File: " + file.getFullPath().toOSString()
+                                               + " SyntaxError " + e.getMessage();
                                status.addError(msg);
                        }
 
                } catch (Exception e) {
-                       String msg = "File: " + file.getLocation().toOSString() + " Exception " + e.getMessage();
+                       String msg = "File: " + file.getFullPath().toOSString()
+                                       + " Exception " + e.getMessage();
                        status.addError(msg);
                }
                if (matches.size() > 0) {
@@ -153,7 +235,8 @@ public class RenameLocalVariableDelegate extends RenameIdentifierDelegate {
                }
        }
 
-       private String readFileContent(final IFile file, final RefactoringStatus refStatus) {
+       private String readFileContent(final IFile file,
+                       final RefactoringStatus refStatus) {
                String result = null;
                try {
                        InputStream is = file.getContents();