// 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;
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;
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) {
// 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) {
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();
}
// 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()]);
}
// 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();
}
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);
}
}
- 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;
// 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 org.eclipse.core.runtime.*;
import org.eclipse.ltk.core.refactoring.*;
import org.eclipse.ltk.core.refactoring.participants.*;
-/** <p>The processor is where the work is delegated to if participants are
+/** <p>The processor is where the work is delegated to if participants are
* involved. The processor loads the participants and manages the lifecycle
- * of the refactoring. In order to do that, the refactoring entry point
+ * of the refactoring. In order to do that, the refactoring entry point
* methods must be implemented.</p>
*
* @author Leif Frenzel
delegate = new RenamePropertyDelegate( info );
}
-
+
// interface methods of RefactoringProcessor
////////////////////////////////////////////
public Object[] getElements() {
// usually, this would be some element object in the object model on which
- // we work (e.g. a Java element if we were in the Java Model); in this case
+ // we work (e.g. a Java element if we were in the Java Model); in this case
// we have only the property name
return new Object[] { info.getOldName() };
}
return delegate.checkInitialConditions();
}
- public RefactoringStatus checkFinalConditions(
+ public RefactoringStatus checkFinalConditions(
final IProgressMonitor pm, final CheckConditionsContext context ) {
return delegate.checkFinalConditions( pm, context );
}
public Change createChange( final IProgressMonitor pm ) {
- CompositeChange result = new CompositeChange( getProcessorName() );
+ CompositeChange result = new CompositeChange( getProcessorName() );
delegate.createChange( pm, result );
return result;
}
- public RefactoringParticipant[] loadParticipants(
- final RefactoringStatus status,
+ public RefactoringParticipant[] loadParticipants(
+ final RefactoringStatus status,
final SharableParticipants sharedParticipants ) {
- // This would be the place to load the participants via the
+ // This would be the place to load the participants via the
// ParticipantManager and decide which of them are allowed to participate.
return new RefactoringParticipant[ 0 ];
}