From 98a7f22fc350e265124f1b949c15ea6355ad96dd Mon Sep 17 00:00:00 2001 From: axelcl Date: Sun, 8 Jan 2006 15:25:50 +0000 Subject: [PATCH 1/1] Some simple improvements --- .../net/sourceforge/phpdt/ltk/core/CoreTexts.java | 1 + .../phpdt/ltk/core/RenamePropertyDelegate.java | 178 ++++++++++++++------ .../phpdt/ltk/core/RenamePropertyInfo.java | 26 ++-- .../phpdt/ltk/core/RenamePropertyProcessor.java | 19 +- .../phpdt/ltk/core/RenamePropertyRefactoring.java | 7 +- .../src/net/sourceforge/phpdt/ltk/ui/UITexts.java | 1 + .../phpdt/ltk/ui/actions/RenamePHPIdentifier.java | 3 +- .../sourceforge/phpdt/ltk/ui/uitexts.properties | 4 +- .../ltk/ui/wizards/RenamePropertyInputPage.java | 4 +- 9 files changed, 164 insertions(+), 79 deletions(-) diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/core/CoreTexts.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/core/CoreTexts.java index 3c8c23f..731454c 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/core/CoreTexts.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/core/CoreTexts.java @@ -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 org.eclipse.osgi.util.NLS; diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/core/RenamePropertyDelegate.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/core/RenamePropertyDelegate.java index 80677a8..ae23722 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/core/RenamePropertyDelegate.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/core/RenamePropertyDelegate.java @@ -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; diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/core/RenamePropertyInfo.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/core/RenamePropertyInfo.java index b1c95a7..32a2c8c 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/core/RenamePropertyInfo.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/core/RenamePropertyInfo.java @@ -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 org.eclipse.core.resources.IFile; @@ -20,22 +21,21 @@ public class RenamePropertyInfo { private String oldName; // the file that contains the property to be renamed private IFile sourceFile; - // whether the refactoring should also change the name of the property - // in corresponding properties files in the same bundle (i.e. which start - // with the same name) - private boolean updateBundle; - // whether the refactoring should also update properties files in other + // whether the refactoring should also change the identifier + // in corresponding PHP files in the same project + private boolean updateProject; + // whether the refactoring should also update PHP files in other // projects than the current one private boolean allProjects; - - + + // interface methods of IRenamePropertyInfo /////////////////////////////////////////// - + public int getOffset() { return offset; } - + public void setOffset( final int offset ) { this.offset = offset; } @@ -72,11 +72,11 @@ public class RenamePropertyInfo { this.allProjects = allProjects; } - public boolean isUpdateBundle() { - return updateBundle; + public boolean isUpdateProject() { + return updateProject; } - public void setUpdateBundle( final boolean updateBundle ) { - this.updateBundle = updateBundle; + public void setUpdateProject( final boolean updateBundle ) { + this.updateProject = updateBundle; } } diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/core/RenamePropertyProcessor.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/core/RenamePropertyProcessor.java index 46cde24..20cf802 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/core/RenamePropertyProcessor.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/core/RenamePropertyProcessor.java @@ -1,14 +1,15 @@ // 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.*; -/**

The processor is where the work is delegated to if participants are +/**

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.

* * @author Leif Frenzel @@ -23,13 +24,13 @@ public class RenamePropertyProcessor extends RefactoringProcessor { 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() }; } @@ -50,21 +51,21 @@ public class RenamePropertyProcessor extends RefactoringProcessor { 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 ]; } diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/core/RenamePropertyRefactoring.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/core/RenamePropertyRefactoring.java index 2cff300..e05e3a6 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/core/RenamePropertyRefactoring.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/core/RenamePropertyRefactoring.java @@ -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 org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring; @@ -7,7 +8,7 @@ import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor; /**

Refactoring for renaming properties in Java Properties files.

* - *

All the actual work is done in the processor, so we just have to + *

All the actual work is done in the processor, so we just have to * keep a reference to one here.

* * @author Leif Frenzel @@ -21,10 +22,10 @@ public class RenamePropertyRefactoring extends ProcessorBasedRefactoring { this.processor = processor; } - + // interface methods of ProcessorBasedRefactoring ///////////////////////////////////////////////// - + public RefactoringProcessor getProcessor() { return processor; } diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/ui/UITexts.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/ui/UITexts.java index 949c04b..c357bf3 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/ui/UITexts.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/ui/UITexts.java @@ -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.ui; import org.eclipse.osgi.util.NLS; diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/ui/actions/RenamePHPIdentifier.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/ui/actions/RenamePHPIdentifier.java index 9e38cd5..4c58891 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/ui/actions/RenamePHPIdentifier.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/ui/actions/RenamePHPIdentifier.java @@ -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.ui.actions; import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil; @@ -46,7 +47,7 @@ import org.eclipse.ui.texteditor.ITextEditor; */ public class RenamePHPIdentifier implements IEditorActionDelegate { - private static final String EXT_PROPERTIES = "properties"; //$NON-NLS-1$ +// private static final String EXT_PROPERTIES = "properties"; //$NON-NLS-1$ private ISelection selection; diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/ui/uitexts.properties b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/ui/uitexts.properties index f3ad862..60084d0 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/ui/uitexts.properties +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/ui/uitexts.properties @@ -1,5 +1,5 @@ renameProperty_refuseDlg_title=Rename PHP identifier renameProperty_refuseDlg_message=This refactoring is only available on PHP files. renamePropertyInputPage_lblNewName=New name\: -renamePropertyInputPage_cbUpdateBundle=Update all properties files in the same bundle -renamePropertyInputPage_cbAllProjects=Update all projects in the workspace (forces preview) \ No newline at end of file +renamePropertyInputPage_cbUpdateBundle=Update all PHP files in the same project +renamePropertyInputPage_cbAllProjects=Update all PHP projects in the workspace (forces preview) \ No newline at end of file diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/ui/wizards/RenamePropertyInputPage.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/ui/wizards/RenamePropertyInputPage.java index 2f7eb74..72f1b0b 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/ui/wizards/RenamePropertyInputPage.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/ui/wizards/RenamePropertyInputPage.java @@ -108,7 +108,7 @@ public class RenamePropertyInputPage extends UserInputWizardPage { public void widgetSelected( final SelectionEvent event ) { boolean selected = cbUpdateBundle.getSelection(); dialogSettings.put( DS_UPDATE_BUNDLE, selected ); - info.setUpdateBundle( selected ); + info.setUpdateProject( selected ); } } ); initUpdateBundleOption(); @@ -165,7 +165,7 @@ public class RenamePropertyInputPage extends UserInputWizardPage { private void initUpdateBundleOption() { boolean updateRefs = dialogSettings.getBoolean( DS_UPDATE_BUNDLE ); cbUpdateBundle.setSelection( updateRefs ); - info.setUpdateBundle( updateRefs ); + info.setUpdateProject( updateRefs ); } private void initAllProjectsOption() { -- 1.7.1