Some simple improvements
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / ltk / core / RenamePropertyDelegate.java
index 80677a8..ae23722 100644 (file)
@@ -1,5 +1,6 @@
 // Copyright (c) 2005 by Leif Frenzel. All rights reserved.
 // See http://leiffrenzel.de
+// modified for phpeclipse.de project by axelcl
 package net.sourceforge.phpdt.ltk.core;
 
 import java.io.ByteArrayOutputStream;
@@ -10,11 +11,16 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
+import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
+import net.sourceforge.phpdt.core.compiler.InvalidInputException;
+import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
+import net.sourceforge.phpdt.internal.compiler.parser.SyntaxError;
 import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil;
 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 
 import org.eclipse.core.resources.IContainer;
 import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.CoreException;
@@ -44,7 +50,7 @@ class RenamePropertyDelegate {
 
        private final RenamePropertyInfo info;
 
-       // properties file with the key to rename -> offset of the key
+       // PHP file with the identifier to rename -> offset of the key
        private final Map phpFiles;
 
        RenamePropertyDelegate(final RenamePropertyInfo info) {
@@ -73,12 +79,48 @@ class RenamePropertyDelegate {
                // workspace) to look for all *.properties files with the same bundle
                // base name
                IContainer rootContainer;
+               ArrayList phpProjects = new ArrayList();
+               IProject project;
                if (info.isAllProjects()) {
                        rootContainer = ResourcesPlugin.getWorkspace().getRoot();
+                       IResource[] members;
+                       try {
+                               members = rootContainer.members();
+                               for (int i = 0; i < members.length; i++) {
+                                       if (members[i] instanceof IProject) {
+                                               project = (IProject) members[i];
+                                               try {
+                                                       if (project.isNatureEnabled(PHPeclipsePlugin.PHP_NATURE_ID)) {
+                                                               search(project, result);
+                                                       }
+                                               } catch (CoreException e) {
+                                                       String msg = "Project: " + project.getLocation().toOSString() + " CoreException " + e.getMessage();
+                                                       result.addError(msg);
+                                               } catch (Exception e) {
+                                                       String msg = "Project: " + project.getLocation().toOSString() + " Exception " + e.getMessage();
+                                                       result.addError(msg);
+                                               }
+                                       }
+                               }
+                       } catch (CoreException e) {
+                               String msg = "Workspace: " + rootContainer.getLocation().toOSString() + " CoreException " + e.getMessage();
+                               result.addError(msg);
+                       }
                } else {
-                       rootContainer = info.getSourceFile().getProject();
+                       project = info.getSourceFile().getProject();
+                       try {
+                               if (project.isNatureEnabled(PHPeclipsePlugin.PHP_NATURE_ID)) {
+                                       search(project, result);
+                               }
+                       } catch (CoreException e) {
+                               String msg = "Project: " + project.getLocation().toOSString() + " CoreException " + e.getMessage();
+                               result.addError(msg);
+                       } catch (Exception e) {
+                               String msg = "Project: " + project.getLocation().toOSString() + " Exception " + e.getMessage();
+                               result.addError(msg);
+                       }
                }
-               search(rootContainer, result);
+
                pm.worked(50);
 
                if (ctxt != null) {
@@ -95,14 +137,10 @@ class RenamePropertyDelegate {
        void createChange(final IProgressMonitor pm, final CompositeChange rootChange) {
                try {
                        pm.beginTask(CoreTexts.renamePropertyDelegate_collectingChanges, 100);
-                       // the property which was directly selected by the user
-                       rootChange.add(createRenameChange());
-                       pm.worked(10);
                        // all files in the same bundle
-                       if (info.isUpdateBundle()) {
-                               rootChange.addAll(createChangesForBundle());
+                       if (info.isUpdateProject()) {
+                               rootChange.addAll(createChangesForContainer(pm));
                        }
-                       pm.worked(90);
                } finally {
                        pm.done();
                }
@@ -111,37 +149,48 @@ class RenamePropertyDelegate {
        // helping methods
        // ////////////////
 
-       private Change createRenameChange() {
-               // create a change object for the file that contains the property the
-               // user has selected to rename
-               IFile file = info.getSourceFile();
-               TextFileChange result = new TextFileChange(file.getName(), file);
-               // a file change contains a tree of edits, first add the root of them
-               MultiTextEdit fileChangeRootEdit = new MultiTextEdit();
-               result.setEdit(fileChangeRootEdit);
-
-               // edit object for the text replacement in the file, this is the only child
-               ReplaceEdit edit = new ReplaceEdit(info.getOffset(), info.getOldName().length(), info.getNewName());
-               fileChangeRootEdit.addChild(edit);
-               return result;
-       }
+       // private Change createRenameChange() {
+       // // create a change object for the file that contains the property the
+       // // user has selected to rename
+       // IFile file = info.getSourceFile();
+       // TextFileChange result = new TextFileChange(file.getName(), file);
+       // // a file change contains a tree of edits, first add the root of them
+       // MultiTextEdit fileChangeRootEdit = new MultiTextEdit();
+       // result.setEdit(fileChangeRootEdit);
+       //
+       // // edit object for the text replacement in the file, this is the only child
+       // ReplaceEdit edit = new ReplaceEdit(info.getOffset(),
+       // info.getOldName().length(), info.getNewName());
+       // fileChangeRootEdit.addChild(edit);
+       // return result;
+       // }
 
-       private Change[] createChangesForBundle() {
+       private Change[] createChangesForContainer(final IProgressMonitor pm) {
                List result = new ArrayList();
                Iterator it = phpFiles.keySet().iterator();
+               int numberOfFiles = phpFiles.size();
+               double percent = 100 / numberOfFiles;
+               int work = 0;
+               int indx = 0;
                while (it.hasNext()) {
                        IFile file = (IFile) it.next();
+                       List list = getKeyOffsets(file);
+                       if (list != null && list.size() > 0) {
+                               TextFileChange tfc = new TextFileChange(file.getName(), file);
+                               MultiTextEdit fileChangeRootEdit = new MultiTextEdit();
+                               tfc.setEdit(fileChangeRootEdit);
 
-                       TextFileChange tfc = new TextFileChange(file.getName(), file);
-                       MultiTextEdit fileChangeRootEdit = new MultiTextEdit();
-                       tfc.setEdit(fileChangeRootEdit);
-
-                       // edit object for the text replacement in the file, this is the only
-                       // child
-                       ReplaceEdit edit = new ReplaceEdit(getKeyOffset(file), info.getOldName().length(), info.getNewName());
-                       fileChangeRootEdit.addChild(edit);
-
-                       result.add(tfc);
+                               // edit object for the text replacement in the file, there could be
+                               // multiple childs
+                               ReplaceEdit edit;
+                               for (int i = 0; i < list.size(); i++) {
+                                       edit = new ReplaceEdit(((Integer) list.get(i)).intValue(), info.getOldName().length(), info.getNewName());
+                                       fileChangeRootEdit.addChild(edit);
+                               }
+                               result.add(tfc);
+                       }
+                       work = Double.valueOf(++indx * percent).intValue();
+                       pm.worked(work);
                }
                return (Change[]) result.toArray(new Change[result.size()]);
        }
@@ -163,13 +212,13 @@ class RenamePropertyDelegate {
        // return result;
        // }
 
-       // whether the file is a PHP file with the same base name as the
-       // one we refactor and contains the key that interests us
-       private boolean isToRefactor(final IFile file) {
-               return PHPFileUtil.isPHPFile(file);
-               // && !file.equals( info.getSourceFile() )
-               // && isPropertyKey( file, info.getOldName() );
-       }
+       // // whether the file is a PHP file with the same base name as the
+       // // one we refactor and contains the key that interests us
+       // private boolean isToRefactor(final IFile file) {
+       // return PHPFileUtil.isPHPFile(file);
+       // // && !file.equals( info.getSourceFile() )
+       // // && isPropertyKey( file, info.getOldName() );
+       // }
 
        // private String getBundleBaseName() {
        // String result = info.getSourceFile().getName();
@@ -200,7 +249,7 @@ class RenamePropertyDelegate {
        }
 
        private void handleFile(final IFile file, final RefactoringStatus status) {
-               if (isToRefactor(file)) {
+               if (PHPFileUtil.isPHPFile(file)) {
                        determineKeyOffsets(file, status);
                        // if (keyOffsets.size() > 0) {
                        // Integer offset = new Integer(keyOffsets);
@@ -209,19 +258,50 @@ class RenamePropertyDelegate {
                }
        }
 
-       private int getKeyOffset(final IFile file) {
-               return ((Integer) phpFiles.get(file)).intValue();
+       private List getKeyOffsets(final IFile file) {
+               return (List) phpFiles.get(file);
        }
 
-       // finds the offset of the property key to rename
+       // 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 determineKeyOffsets(final IFile file, final RefactoringStatus status) {
-               String content = readFileContent(file, status);
-               int indx = 0;
-               while ((indx = content.indexOf(info.getOldName(), indx)) >= 0) {
-                       phpFiles.put(file, Integer.valueOf(indx++));
+               ArrayList matches = new ArrayList();
+               try {
+                       String content = readFileContent(file, status);
+                       Scanner scanner = new Scanner(true, false);
+                       scanner.setSource(content.toCharArray());
+                       scanner.setPHPMode(false);
+                       char[] word = info.getOldName().toCharArray();
+
+                       int fToken = ITerminalSymbols.TokenNameEOF;
+                       try {
+                               fToken = scanner.getNextToken();
+                               while (fToken != ITerminalSymbols.TokenNameEOF) {
+                                       if (fToken == ITerminalSymbols.TokenNameVariable || fToken == ITerminalSymbols.TokenNameIdentifier) {
+                                               if (scanner.equalsCurrentTokenSource(word)) {
+                                                       matches.add(Integer.valueOf(scanner.getCurrentTokenStartPosition()));
+                                               }
+                                       }
+                                       fToken = scanner.getNextToken();
+                               }
+
+                       } catch (InvalidInputException e) {
+                               String msg = "File: " + file.getLocation().toOSString() + " InvalidInputException " + e.getMessage();
+                               status.addError(msg);
+                       } catch (SyntaxError e) {
+                               String msg = "File: " + file.getLocation().toOSString() + " SyntaxError " + e.getMessage();
+                               status.addError(msg);
+                       }
+
+               } catch (Exception e) {
+                       String msg = "File: " + file.getLocation().toOSString() + " Exception " + e.getMessage();
+                       status.addError(msg);
+               }
+               if (matches.size() > 0) {
+                       phpFiles.put(file, matches);
                }
+
                // int result = -1;
                // int candidateIndex = content.indexOf(info.getOldName());
                // result = candidateIndex;