From 58e5dd61e0f9d87379ae7c71d166feb4af4eb010 Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Wed, 12 Oct 2011 10:10:34 -0700 Subject: [PATCH] Bug 360378 - Confusing behavior when the current file is not part of the refactoring working set. Also show preview whenever there are warnings. --- .../tests/refactoring/rename/RenameTests.java | 7 +- .../RefactoringExecutionHelper.java | 96 ++++++--- .../rename/CRefactoringArgument.java | 10 +- .../ui/refactoring/rename/CRefactory.java | 86 ++------ .../rename/CRenameGlobalProcessor.java | 9 +- .../rename/CRenameIncludeProcessor.java | 8 +- .../rename/CRenameLocalProcessor.java | 4 +- .../rename/CRenameMacroProcessor.java | 10 +- .../refactoring/rename/CRenameProcessor.java | 95 +++++---- .../rename/CRenameProcessorDelegate.java | 75 ++++--- .../rename/CRenameRefactoringInputPage.java | 191 +++++++++--------- .../rename/CRenameRefactoringPreferences.java | 11 +- .../refactoring/rename/RenameLinkedMode.java | 10 +- .../ui/refactoring/rename/RenameMessages.java | 1 - .../rename/RenameMessages.properties | 5 +- .../ui/refactoring/rename/RenameSupport.java | 131 ++++++++++-- .../refactoring/rename/TextSearchWrapper.java | 113 +++++------ 17 files changed, 485 insertions(+), 377 deletions(-) diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/rename/RenameTests.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/rename/RenameTests.java index 938d873e7c2..978507cb480 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/rename/RenameTests.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/rename/RenameTests.java @@ -74,7 +74,7 @@ public class RenameTests extends RefactoringTests { CRenameProcessor proc= new CRenameProcessor(CRefactory.getInstance(), arg); proc.setReplacementText( newName ); proc.setSelectedOptions(-1); - proc.setScope(TextSearchWrapper.SCOPE_WORKSPACE); + proc.setExhaustiveSearchScope(TextSearchWrapper.SCOPE_WORKSPACE); return new CRenameRefactoring(proc); } @@ -124,8 +124,7 @@ public class RenameTests extends RefactoringTests { ((CRenameProcessor) proc.getProcessor()).lockIndex(); try { RefactoringStatus rs = checkConditions(proc); - - return (rs.getSeverity()); + return rs.getSeverity(); } finally { ((CRenameProcessor) proc.getProcessor()).unlockIndex(); } @@ -134,7 +133,7 @@ public class RenameTests extends RefactoringTests { protected int countOccurrences(String contents, String lookup) { int idx= contents.indexOf(lookup); int count= 0; - while (idx >=0) { + while (idx >= 0) { count++; idx= contents.indexOf(lookup, idx+lookup.length()); } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/RefactoringExecutionHelper.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/RefactoringExecutionHelper.java index 2e81f379479..545b93a4522 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/RefactoringExecutionHelper.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/RefactoringExecutionHelper.java @@ -57,14 +57,16 @@ public class RefactoringExecutionHelper { private final int fSaveMode; private class Operation implements IWorkspaceRunnable { - public Change fChange; - public PerformChangeOperation fPerformChangeOperation; - private final boolean fForked; - private final boolean fForkChangeExecution; + Change fChange; + PerformChangeOperation fPerformChangeOperation; + final boolean fForked; + final boolean fForkChangeExecution; + final boolean fCancelable; - public Operation(boolean forked, boolean forkChangeExecution) { + public Operation(boolean forked, boolean forkChangeExecution, boolean cancelable) { fForked= forked; fForkChangeExecution= forkChangeExecution; + this.fCancelable = cancelable; } public void run(IProgressMonitor pm) throws CoreException { @@ -72,7 +74,8 @@ public class RefactoringExecutionHelper { pm.beginTask("", fForked && !fForkChangeExecution ? 7 : 11); //$NON-NLS-1$ pm.subTask(""); //$NON-NLS-1$ - final RefactoringStatus status= fRefactoring.checkAllConditions(new SubProgressMonitor(pm, 4, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK)); + final RefactoringStatus status= fRefactoring.checkAllConditions( + new SubProgressMonitor(pm, 4, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK)); if (status.getSeverity() >= fStopSeverity) { final boolean[] canceled= { false }; if (fForked) { @@ -92,10 +95,7 @@ public class RefactoringExecutionHelper { fChange= fRefactoring.createChange(new SubProgressMonitor(pm, 2, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK)); fChange.initializeValidationData(new SubProgressMonitor(pm, 1, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK)); - fPerformChangeOperation= new PerformChangeOperation(fChange);//RefactoringUI.createUIAwareChangeOperation(fChange); - fPerformChangeOperation.setUndoManager(RefactoringCore.getUndoManager(), fRefactoring.getName()); - if (fRefactoring instanceof IScheduledRefactoring) - fPerformChangeOperation.setSchedulingRule(((IScheduledRefactoring) fRefactoring).getSchedulingRule()); + fPerformChangeOperation = createPerformChangeOperation(fChange); if (!fForked || fForkChangeExecution) fPerformChangeOperation.run(new SubProgressMonitor(pm, 4, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK)); @@ -157,15 +157,31 @@ public class RefactoringExecutionHelper { * @throws InterruptedException thrown when the operation is canceled * @throws InvocationTargetException thrown when the operation failed to execute */ - public void perform(boolean fork, boolean forkChangeExecution, boolean cancelable) throws InterruptedException, InvocationTargetException { + public void perform(boolean fork, boolean forkChangeExecution, boolean cancelable) + throws InterruptedException, InvocationTargetException { + Operation operation = new Operation(fork, forkChangeExecution, cancelable); + performOperation(operation, null, fork); + } + + public void performChange(Change change, boolean fork) + throws InterruptedException, InvocationTargetException { + PerformChangeOperation operation = createPerformChangeOperation(change); + performOperation(null, operation, fork); + } + + /** + * Executes either a complete refactoring operation or a change operation. + * @param operation The refactoring operation. Can be null. + * @param changeOperation The change operation. Has to be null if {@code operation} + * is not null and not null otherwise. + * @param fork If set, the execution will be forked. + */ + private void performOperation(Operation operation, PerformChangeOperation changeOperation, boolean fork) + throws InterruptedException, InvocationTargetException { + Assert.isTrue((operation == null) != (changeOperation == null)); Assert.isTrue(Display.getCurrent() != null); final IJobManager manager= Job.getJobManager(); - final ISchedulingRule rule; - if (fRefactoring instanceof IScheduledRefactoring) { - rule= ((IScheduledRefactoring) fRefactoring).getSchedulingRule(); - } else { - rule= ResourcesPlugin.getWorkspace().getRoot(); - } + final ISchedulingRule rule = getSchedulingRule(); try { try { Runnable r= new Runnable() { @@ -179,17 +195,22 @@ public class RefactoringExecutionHelper { } RefactoringSaveHelper saveHelper= new RefactoringSaveHelper(fSaveMode); - if (!saveHelper.saveEditors(fParent)) - throw new InterruptedException(); - final Operation op= new Operation(fork, forkChangeExecution); + if (operation != null) { + if (!saveHelper.saveEditors(fParent)) + throw new InterruptedException(); + } fRefactoring.setValidationContext(fParent); try { - fExecContext.run(fork, cancelable, new WorkbenchRunnableAdapter(op, rule, true)); - if (fork && !forkChangeExecution && op.fPerformChangeOperation != null) - fExecContext.run(false, false, new WorkbenchRunnableAdapter(op.fPerformChangeOperation, rule, true)); - - if (op.fPerformChangeOperation != null) { - RefactoringStatus validationStatus= op.fPerformChangeOperation.getValidationStatus(); + if (operation != null) { + fExecContext.run(fork, operation.fCancelable, new WorkbenchRunnableAdapter(operation, rule, true)); + changeOperation = operation.fPerformChangeOperation; + fork = fork && !operation.fForkChangeExecution; + } + + if (changeOperation != null) { + if (fork) + fExecContext.run(false, false, new WorkbenchRunnableAdapter(changeOperation, rule, true)); + RefactoringStatus validationStatus= changeOperation.getValidationStatus(); if (validationStatus != null && validationStatus.hasFatalError()) { MessageDialog.openError(fParent, fRefactoring.getName(), NLS.bind(Messages.RefactoringExecutionHelper_cannot_execute, @@ -198,14 +219,13 @@ public class RefactoringExecutionHelper { } } } catch (InvocationTargetException e) { - PerformChangeOperation pco= op.fPerformChangeOperation; - if (pco != null && pco.changeExecutionFailed()) { + if (changeOperation != null && changeOperation.changeExecutionFailed()) { ChangeExceptionHandler handler= new ChangeExceptionHandler(fParent, fRefactoring); Throwable inner= e.getTargetException(); if (inner instanceof RuntimeException) { - handler.handle(pco.getChange(), (RuntimeException)inner); + handler.handle(changeOperation.getChange(), (RuntimeException)inner); } else if (inner instanceof CoreException) { - handler.handle(pco.getChange(), (CoreException)inner); + handler.handle(changeOperation.getChange(), (CoreException)inner); } else { throw e; } @@ -222,4 +242,20 @@ public class RefactoringExecutionHelper { fRefactoring.setValidationContext(null); } } + + private ISchedulingRule getSchedulingRule() { + if (fRefactoring instanceof IScheduledRefactoring) { + return ((IScheduledRefactoring) fRefactoring).getSchedulingRule(); + } else { + return ResourcesPlugin.getWorkspace().getRoot(); + } + } + + private PerformChangeOperation createPerformChangeOperation(Change change) { + PerformChangeOperation operation = new PerformChangeOperation(change); + operation.setUndoManager(RefactoringCore.getUndoManager(), fRefactoring.getName()); + if (fRefactoring instanceof IScheduledRefactoring) + operation.setSchedulingRule(((IScheduledRefactoring) fRefactoring).getSchedulingRule()); + return operation; + } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRefactoringArgument.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRefactoringArgument.java index 96e57d03d0d..ff9eee6f3db 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRefactoringArgument.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRefactoringArgument.java @@ -6,7 +6,7 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Markus Schorn - initial API and implementation + * Markus Schorn - initial API and implementation ******************************************************************************/ package org.eclipse.cdt.internal.ui.refactoring.rename; @@ -39,8 +39,8 @@ import org.eclipse.cdt.ui.CUIPlugin; * can be calculated from the AST. */ public class CRefactoringArgument { - private int fOffset= 0; - private int fLength= 0; + private int fOffset; + private int fLength; private String fText= ""; //$NON-NLS-1$ private int fKind= CRefactory.ARGUMENT_UNKNOWN; private IFile fFile; @@ -72,22 +72,18 @@ public class CRefactoringArgument { } } - // overrider public String getName() { return fText; } - // overrider public IFile getSourceFile() { return fFile; } - // overrider public int getArgumentKind() { return fKind; } - // overrider public int getOffset() { return fOffset; } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRefactory.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRefactory.java index e0514e8eefd..3bbcfce2558 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRefactory.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRefactory.java @@ -6,8 +6,8 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Markus Schorn - initial API and implementation - * Sergey Prigogin (Google) + * Markus Schorn - initial API and implementation + * Sergey Prigogin (Google) ******************************************************************************/ package org.eclipse.cdt.internal.ui.refactoring.rename; @@ -18,8 +18,6 @@ import java.util.Iterator; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.ResourcesPlugin; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.content.IContentType; import org.eclipse.jface.text.ITextSelection; @@ -29,24 +27,20 @@ import org.eclipse.ui.ide.IDE; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.IWorkingCopy; -import org.eclipse.cdt.ui.CUIPlugin; - -import org.eclipse.cdt.internal.ui.refactoring.RefactoringStarter; /** * Serves to launch the various refactorings. */ public class CRefactory { - public static final int OPTION_ASK_SCOPE = 0x01; - public static final int OPTION_IN_COMMENT = 0x02; - public static final int OPTION_IN_STRING_LITERAL = 0x04; - public static final int OPTION_IN_INCLUDE_DIRECTIVE = 0x08; - public static final int OPTION_IN_MACRO_DEFINITION = 0x10; - public static final int OPTION_IN_PREPROCESSOR_DIRECTIVE = 0x20; - public static final int OPTION_IN_INACTIVE_CODE = 0x40; - public static final int OPTION_IN_CODE = 0x80; - public static final int OPTION_DO_VIRTUAL = 0x100; - public static final int OPTION_EXHAUSTIVE_FILE_SEARCH = 0x200; + public static final int OPTION_IN_CODE_REFERENCES = 0x01; + public static final int OPTION_IN_INACTIVE_CODE = 0x02; + public static final int OPTION_IN_COMMENT = 0x04; + public static final int OPTION_IN_STRING_LITERAL = 0x08; + public static final int OPTION_IN_INCLUDE_DIRECTIVE = 0x10; + public static final int OPTION_IN_MACRO_DEFINITION = 0x20; + public static final int OPTION_IN_PREPROCESSOR_DIRECTIVE = 0x40; + public static final int OPTION_DO_VIRTUAL = 0x80; + public static final int OPTION_EXHAUSTIVE_FILE_SEARCH = 0x100; public static final int ARGUMENT_UNKNOWN = 0; public static final int ARGUMENT_LOCAL_VAR = 1; @@ -64,7 +58,7 @@ public class CRefactory { public static final int ARGUMENT_ENUMERATOR = 13; public static final int ARGUMENT_CLASS_TYPE = 14; public static final int ARGUMENT_NAMESPACE = 15; - + private static CRefactory sInstance= new CRefactory(); private TextSearchWrapper fTextSearch; @@ -83,7 +77,7 @@ public class CRefactory { CRefactoringArgument iarg= new CRefactoringArgument(arg); final CRenameProcessor processor = new CRenameProcessor(this, iarg); CRenameRefactoring refactoring= new CRenameRefactoring(processor); - openDialog(shell, refactoring, false); + RenameSupport.openDialog(shell, refactoring); } public void rename(Shell shell, IWorkingCopy workingCopy, ITextSelection selection) { @@ -98,51 +92,7 @@ public class CRefactory { new CRefactoringArgument((IFile) res, selection.getOffset(), selection.getLength()); final CRenameProcessor processor = new CRenameProcessor(this, iarg); CRenameRefactoring refactoring= new CRenameRefactoring(processor); - openDialog(shell, refactoring, false); - } - - /** - * Opens the refactoring dialog. - * - *

- * This method has to be called from within the UI thread. - *

- * - * @param shell a shell used as a parent for the refactoring, preview, or error dialog - * @param showPreviewOnly if true, the dialog skips all user input pages and - * directly shows the preview or error page. Otherwise, shows all pages. - * @return true if the refactoring has been executed successfully, - * or false if it has been canceled. - */ - static boolean openDialog(Shell shell, CRenameRefactoring refactoring, boolean showPreviewOnly) { - try { - CRenameRefactoringWizard wizard; - if (!showPreviewOnly) { - wizard = new CRenameRefactoringWizard(refactoring); - } else { - wizard = new CRenameRefactoringWizard(refactoring) { - @Override - protected void addUserInputPages() { - // nothing to add - } - }; - wizard.setForcePreviewReview(showPreviewOnly); - } - RefactoringStarter starter = new RefactoringStarter(); - CRenameProcessor processor = (CRenameProcessor) refactoring.getProcessor(); - processor.lockIndex(); - try { - processor.checkInitialConditions(new NullProgressMonitor()); - return starter.activate(wizard, shell, RenameMessages.CRefactory_title_rename, processor.getSaveMode()); - } finally { - processor.unlockIndex(); - } - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } catch (CoreException e) { - CUIPlugin.log(e); - } - return false; + RenameSupport.openDialog(shell, refactoring); } public TextSearchWrapper getTextSearch() { @@ -156,19 +106,19 @@ public class CRefactory { IContentType[] cts= Platform.getContentTypeManager().getAllContentTypes(); HashSet all= new HashSet(); for (IContentType type : cts) { - boolean useit= false; - while (!useit && type != null) { + boolean useIt= false; + while (!useIt && type != null) { String id= type.getId(); if (id.equals(CCorePlugin.CONTENT_TYPE_CHEADER) || id.equals(CCorePlugin.CONTENT_TYPE_CSOURCE) || id.equals(CCorePlugin.CONTENT_TYPE_CXXHEADER) || id.equals(CCorePlugin.CONTENT_TYPE_CXXSOURCE)) { - useit= true; + useIt= true; } else { type= type.getBaseType(); } } - if (useit) { + if (useIt) { String exts[] = type.getFileSpecs(IContentType.FILE_EXTENSION_SPEC); all.addAll(Arrays.asList(exts)); } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameGlobalProcessor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameGlobalProcessor.java index ccc62c5d0d2..b12b7562684 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameGlobalProcessor.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameGlobalProcessor.java @@ -13,7 +13,6 @@ package org.eclipse.cdt.internal.ui.refactoring.rename; import org.eclipse.cdt.internal.ui.refactoring.RefactoringSaveHelper; - /** * Rename processor that sets up the input page for renaming a global entity. */ @@ -21,11 +20,11 @@ public class CRenameGlobalProcessor extends CRenameProcessorDelegate { public CRenameGlobalProcessor(CRenameProcessor processor, String name) { super(processor, name); - setAvailableOptions(CRefactory.OPTION_ASK_SCOPE | - CRefactory.OPTION_EXHAUSTIVE_FILE_SEARCH | - CRefactory.OPTION_IN_CODE | + setAvailableOptions( + CRefactory.OPTION_IN_CODE_REFERENCES | CRefactory.OPTION_IN_COMMENT | - CRefactory.OPTION_IN_MACRO_DEFINITION); + CRefactory.OPTION_IN_MACRO_DEFINITION | + CRefactory.OPTION_EXHAUSTIVE_FILE_SEARCH); } @Override diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameIncludeProcessor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameIncludeProcessor.java index 3ebe3d1808f..65eb58d296c 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameIncludeProcessor.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameIncludeProcessor.java @@ -20,12 +20,12 @@ public class CRenameIncludeProcessor extends CRenameProcessorDelegate { public CRenameIncludeProcessor(CRenameProcessor input, String kind) { super(input, kind); - setAvailableOptions(CRefactory.OPTION_ASK_SCOPE | - CRefactory.OPTION_EXHAUSTIVE_FILE_SEARCH | + setAvailableOptions( CRefactory.OPTION_IN_COMMENT | - CRefactory.OPTION_IN_MACRO_DEFINITION); + CRefactory.OPTION_IN_MACRO_DEFINITION | + CRefactory.OPTION_EXHAUSTIVE_FILE_SEARCH); setOptionsForcingPreview(-1); - setOptionsEnablingScope(-1); + setOptionsEnablingExhaustiveSearch(-1); } @Override diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameLocalProcessor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameLocalProcessor.java index 6baeebdc024..dee0b02c1ea 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameLocalProcessor.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameLocalProcessor.java @@ -40,13 +40,11 @@ public class CRenameLocalProcessor extends CRenameProcessorDelegate { setOptionsForcingPreview(0); } - // overrider @Override protected int getAcceptedLocations(int selectedOptions) { - return CRefactory.OPTION_IN_CODE | CRefactory.OPTION_IN_MACRO_DEFINITION | selectedOptions; + return CRefactory.OPTION_IN_CODE_REFERENCES | CRefactory.OPTION_IN_MACRO_DEFINITION | selectedOptions; } - // overrider @Override protected int getSearchScope() { return TextSearchWrapper.SCOPE_FILE; diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameMacroProcessor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameMacroProcessor.java index 3bebc37ce79..c71e7e15a88 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameMacroProcessor.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameMacroProcessor.java @@ -26,13 +26,13 @@ public class CRenameMacroProcessor extends CRenameGlobalProcessor { public CRenameMacroProcessor(CRenameProcessor processor, String name) { super(processor, name); - setAvailableOptions(CRefactory.OPTION_ASK_SCOPE | - CRefactory.OPTION_EXHAUSTIVE_FILE_SEARCH | - CRefactory.OPTION_IN_CODE | + setAvailableOptions( + CRefactory.OPTION_IN_CODE_REFERENCES | CRefactory.OPTION_IN_COMMENT | - CRefactory.OPTION_IN_PREPROCESSOR_DIRECTIVE); + CRefactory.OPTION_IN_PREPROCESSOR_DIRECTIVE | + CRefactory.OPTION_EXHAUSTIVE_FILE_SEARCH); } - + @Override protected int getAcceptedLocations(int selectedOptions) { return selectedOptions | CRefactory.OPTION_IN_MACRO_DEFINITION; diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameProcessor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameProcessor.java index 2b9c0b5b617..1b7393e3165 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameProcessor.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameProcessor.java @@ -52,13 +52,16 @@ public class CRenameProcessor extends RenameProcessor { private final CRefactoringArgument fArgument; private CRenameProcessorDelegate fDelegate; private String fReplacementText; - private String fWorkingSet; - private int fScope; + private String fWorkingSetName; + private int fExhaustiveSearchScope; private int fSelectedOptions; private final CRefactory fManager; private final ASTManager fAstManager; private IIndex fIndex; + private int indexLockCount; private RefactoringStatus fInitialConditionsStatus; + + private Change fChange; public CRenameProcessor(CRefactory refactoringManager, CRefactoringArgument arg) { fManager= refactoringManager; @@ -70,13 +73,11 @@ public class CRenameProcessor extends RenameProcessor { return fArgument; } - // overrider @Override public Object[] getElements() { return new Object[] { fArgument.getBinding() }; } - // overrider @Override public String getProcessorName() { String result= null; @@ -139,15 +140,15 @@ public class CRenameProcessor extends RenameProcessor { private CRenameProcessorDelegate createDelegate() { switch (fArgument.getArgumentKind()) { case CRefactory.ARGUMENT_LOCAL_VAR: - return new CRenameLocalProcessor(this, + return new CRenameLocalProcessor(this, RenameMessages.CRenameTopProcessor_localVar, fArgument.getScope()); case CRefactory.ARGUMENT_PARAMETER: - return new CRenameLocalProcessor(this, + return new CRenameLocalProcessor(this, RenameMessages.CRenameTopProcessor_parameter, fArgument.getScope()); case CRefactory.ARGUMENT_FILE_LOCAL_VAR: - return new CRenameLocalProcessor(this, + return new CRenameLocalProcessor(this, RenameMessages.CRenameTopProcessor_filelocalVar, null); case CRefactory.ARGUMENT_GLOBAL_VAR: @@ -157,7 +158,7 @@ public class CRenameProcessor extends RenameProcessor { case CRefactory.ARGUMENT_FIELD: return new CRenameGlobalProcessor(this, RenameMessages.CRenameTopProcessor_field); case CRefactory.ARGUMENT_FILE_LOCAL_FUNCTION: - return new CRenameLocalProcessor(this, + return new CRenameLocalProcessor(this, RenameMessages.CRenameTopProcessor_filelocalFunction, null); case CRefactory.ARGUMENT_GLOBAL_FUNCTION: @@ -189,10 +190,18 @@ public class CRenameProcessor extends RenameProcessor { @Override public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException { - return fDelegate.createChange(pm); + fChange = fDelegate.createChange(pm); + return fChange; } - @Override + /** + * @return the change if it has been created, or null otherwise. + */ + Change getChange() { + return fChange; + } + + @Override public RefactoringParticipant[] loadParticipants(RefactoringStatus status, SharableParticipants sharedParticipants) throws CoreException { RenameArguments arguments= new RenameArguments(getReplacementText(), true); @@ -206,7 +215,9 @@ public class CRenameProcessor extends RenameProcessor { return result.toArray(new RefactoringParticipant[result.size()]); } - // options for the input page in the refactoring wizard + /** + * Options for the input page in the refactoring wizard + */ public int getAvailableOptions() { if (fDelegate == null) { return 0; @@ -214,7 +225,9 @@ public class CRenameProcessor extends RenameProcessor { return fDelegate.getAvailableOptions(); } - // options for the input page that trigger the preview + /** + * Options for the input page that trigger the preview + */ public int getOptionsForcingPreview() { if (fDelegate == null) { return 0; @@ -222,12 +235,16 @@ public class CRenameProcessor extends RenameProcessor { return fDelegate.getOptionsForcingPreview(); } - // options for the input page that trigger the preview - public int getOptionsEnablingScope() { + /** + * The options that may need exhaustive file search since index lookup is not guaranteed to + * return all files participating in refactoring. When one of these options is selected, + * the exhaustive file search is enabled. + */ + public int getOptionsEnablingExhaustiveSearch() { if (fDelegate == null) { return 0; } - return fDelegate.getOptionsEnablingScope(); + return fDelegate.getOptionsEnablingExhaustiveSearch(); } @Override @@ -235,12 +252,13 @@ public class CRenameProcessor extends RenameProcessor { return IDENTIFIER; } - public int getScope() { - return fScope; + public int getExhaustiveSearchScope() { + return (fSelectedOptions & CRefactory.OPTION_EXHAUSTIVE_FILE_SEARCH) != 0 ? + fExhaustiveSearchScope : TextSearchWrapper.SCOPE_FILE; } - public void setScope(int scope) { - fScope = scope; + public void setExhaustiveSearchScope(int scope) { + fExhaustiveSearchScope = scope; } public int getSelectedOptions() { @@ -251,16 +269,20 @@ public class CRenameProcessor extends RenameProcessor { fSelectedOptions = selectedOptions; } - public String getWorkingSet() { - return fWorkingSet; + public boolean isPreviewRequired() { + return (fSelectedOptions & getOptionsForcingPreview()) != 0; + } + + public String getWorkingSetName() { + return fWorkingSetName; } /** * Sets the name of the working set. If the name of the working set is invalid, * it's set to an empty string. */ - public void setWorkingSet(String workingSet) { - fWorkingSet = checkWorkingSet(workingSet); + public void setWorkingSetName(String workingSet) { + fWorkingSetName = checkWorkingSet(workingSet); } public String getReplacementText() { @@ -280,21 +302,26 @@ public class CRenameProcessor extends RenameProcessor { } public void lockIndex() throws CoreException, InterruptedException { - if (fIndex == null) { - ICProject[] projects= CoreModel.getDefault().getCModel().getCProjects(); - fIndex= CCorePlugin.getIndexManager().getIndex(projects); + if (indexLockCount == 0) { + if (fIndex == null) { + ICProject[] projects= CoreModel.getDefault().getCModel().getCProjects(); + fIndex= CCorePlugin.getIndexManager().getIndex(projects); + } + fIndex.acquireReadLock(); } - fIndex.acquireReadLock(); + indexLockCount++; } public void unlockIndex() { - if (fAstManager != null) { - fAstManager.dispose(); + if (--indexLockCount <= 0) { + if (fAstManager != null) { + fAstManager.dispose(); + } + if (fIndex != null) { + fIndex.releaseReadLock(); + } + fIndex= null; } - if (fIndex != null) { - fIndex.releaseReadLock(); - } - fIndex= null; } public IIndex getIndex() { @@ -307,7 +334,7 @@ public class CRenameProcessor extends RenameProcessor { public int getSaveMode() { return fDelegate.getSaveMode(); } - + private String checkWorkingSet(String workingSet) { if (workingSet != null && workingSet.length() > 0) { IWorkingSetManager wsManager= PlatformUI.getWorkbench().getWorkingSetManager(); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameProcessorDelegate.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameProcessorDelegate.java index 4d7fd99504e..f88b03f1f1e 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameProcessorDelegate.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameProcessorDelegate.java @@ -47,7 +47,6 @@ import org.eclipse.cdt.core.index.IIndexFileLocation; import org.eclipse.cdt.core.index.IIndexName; import org.eclipse.cdt.ui.refactoring.CTextFileChange; - /** * Abstract base for all different rename processors used by the top processor. */ @@ -55,23 +54,21 @@ public abstract class CRenameProcessorDelegate { private CRenameProcessor fTopProcessor; private ArrayList fMatches; protected String fProcessorBaseName; - private int fAvailableOptions= - CRefactory.OPTION_ASK_SCOPE | - CRefactory.OPTION_EXHAUSTIVE_FILE_SEARCH | - CRefactory.OPTION_IN_CODE | + private int fAvailableOptions= + CRefactory.OPTION_IN_CODE_REFERENCES | CRefactory.OPTION_IN_COMMENT | CRefactory.OPTION_IN_MACRO_DEFINITION | - CRefactory.OPTION_IN_STRING_LITERAL; + CRefactory.OPTION_IN_STRING_LITERAL | + CRefactory.OPTION_EXHAUSTIVE_FILE_SEARCH; private int fOptionsForcingPreview= - CRefactory.OPTION_IN_INACTIVE_CODE | - CRefactory.OPTION_IN_COMMENT | + CRefactory.OPTION_IN_COMMENT | CRefactory.OPTION_IN_MACRO_DEFINITION | CRefactory.OPTION_IN_PREPROCESSOR_DIRECTIVE | CRefactory.OPTION_IN_STRING_LITERAL; - - private int fOptionsEnablingScope= fOptionsForcingPreview; + private int fOptionsEnablingExhaustiveSearch= fOptionsForcingPreview | + CRefactory.OPTION_IN_INACTIVE_CODE | CRefactory.OPTION_IN_CODE_REFERENCES; protected CRenameProcessorDelegate(CRenameProcessor topProcessor, String name) { fTopProcessor= topProcessor; @@ -87,7 +84,7 @@ public abstract class CRenameProcessorDelegate { } final public int getSelectedScope() { - return fTopProcessor.getScope(); + return fTopProcessor.getExhaustiveSearchScope(); } final public int getSelectedOptions() { @@ -95,7 +92,7 @@ public abstract class CRenameProcessorDelegate { } final public String getSelectedWorkingSet() { - return fTopProcessor.getWorkingSet(); + return fTopProcessor.getWorkingSetName(); } final public CRefactory getManager() { @@ -142,15 +139,20 @@ public abstract class CRenameProcessorDelegate { } /** - * The options that need the scope definition. When one of them is - * selected, the scope options are enabled. + * Sets the options that enable exhaustive file search. + * @see #getOptionsEnablingExhaustiveSearch() */ - public void setOptionsEnablingScope(int options) { - fOptionsEnablingScope= options; + void setOptionsEnablingExhaustiveSearch(int options) { + fOptionsEnablingExhaustiveSearch= options; } - - final int getOptionsEnablingScope() { - return fOptionsEnablingScope; + + /** + * The options that may need exhaustive file search since index lookup is not guaranteed to + * return all files participating in refactoring. When one of these options is selected, + * the exhaustive file search is enabled. + */ + final int getOptionsEnablingExhaustiveSearch() { + return fOptionsEnablingExhaustiveSearch; } protected int getSearchScope() { @@ -159,17 +161,15 @@ public abstract class CRenameProcessorDelegate { /** * Builds an index-based file filter for the name search. + * * @param bindings bindings being renamed - * @return A set of files containing references to the bindings, or null if - * exhaustive file search is requested. + * @return A set of files containing references to the bindings. */ - private Collection getFileFilter(IBinding[] bindings) { - if ((getSelectedOptions() & CRefactory.OPTION_EXHAUSTIVE_FILE_SEARCH) != 0) { - return null; - } + private Collection getReferringFiles(IBinding[] bindings) { + ArrayList files = new ArrayList(); IIndex index = getIndex(); if (index == null) { - return null; + return files; } Set locations = new HashSet(); try { @@ -182,21 +182,21 @@ public abstract class CRenameProcessorDelegate { } } } catch (InterruptedException e) { - return null; + return files; } catch (CoreException e) { - return null; + return files; } finally { index.releaseReadLock(); } - ArrayList files = new ArrayList(locations.size()); + files.ensureCapacity(locations.size()); IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); for (IIndexFileLocation location : locations) { String fullPath= location.getFullPath(); if (fullPath != null) { IResource file= workspaceRoot.findMember(fullPath); - if (file != null) { - files.add(file); + if (file instanceof IFile) { + files.add((IFile) file); } } } @@ -220,13 +220,12 @@ public abstract class CRenameProcessorDelegate { // perform text-search fMatches= new ArrayList(); TextSearchWrapper txtSearch= getManager().getTextSearch(); - Collection fileFilter = getFileFilter(renameBindings); - if (fileFilter != null && !fileFilter.contains(file)) { - fileFilter.add(file); + Collection filesToSearch = getReferringFiles(renameBindings); + if (!filesToSearch.contains(file)) { + filesToSearch.add(file); } - IStatus stat= txtSearch.searchWord(getSearchScope(), file, getSelectedWorkingSet(), - fileFilter != null ? fileFilter.toArray(new IResource[fileFilter.size()]) : null, - getManager().getCCppPatterns(), + IStatus stat= txtSearch.searchWord(filesToSearch.toArray(new IFile[filesToSearch.size()]), + getSearchScope(), file, getSelectedWorkingSet(), getManager().getCCppPatterns(), getArgument().getName(), new SubProgressMonitor(monitor, 1), fMatches); if (monitor.isCanceled()) { throw new OperationCanceledException(); @@ -354,7 +353,7 @@ public abstract class CRenameProcessorDelegate { } if (match.getAstInformation() != CRefactoringMatch.AST_REFERENCE_OTHER) { IFile mfile= match.getFile(); - if (file==null || !file.equals(mfile) || fileEdit == null || fileChange == null) { + if (file == null || !file.equals(mfile) || fileEdit == null || fileChange == null) { file= mfile; fileEdit= new MultiTextEdit(); fileChange = new CTextFileChange(file.getName(), file); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameRefactoringInputPage.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameRefactoringInputPage.java index b875cf2ecfd..4ff71ad86af 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameRefactoringInputPage.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameRefactoringInputPage.java @@ -6,9 +6,9 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Markus Schorn - initial API and implementation - * Emanuel Graf (Institute for Software, HSR Hochschule fuer Technik) - * Sergey Prigogin (Google) + * Markus Schorn - initial API and implementation + * Emanuel Graf (Institute for Software, HSR Hochschule fuer Technik) + * Sergey Prigogin (Google) ******************************************************************************/ package org.eclipse.cdt.internal.ui.refactoring.rename; @@ -45,7 +45,7 @@ public class CRenameRefactoringInputPage extends UserInputWizardPage { private String fSearchString; private int fOptions; private int fForcePreviewOptions= 0; - private int fEnableScopeOptions; + private int fExhaustiveSearchEnablingOptions; private Text fNewName; private Button fDoVirtual; @@ -79,7 +79,7 @@ public class CRenameRefactoringInputPage extends UserInputWizardPage { fSearchString= processor.getArgument().getName(); fOptions= processor.getAvailableOptions(); fForcePreviewOptions= processor.getOptionsForcingPreview(); - fEnableScopeOptions= processor.getOptionsEnablingScope(); + fExhaustiveSearchEnablingOptions= processor.getOptionsEnablingExhaustiveSearch(); Composite top= new Composite(parent, SWT.NONE); initializeDialogUnits(top); @@ -87,7 +87,7 @@ public class CRenameRefactoringInputPage extends UserInputWizardPage { top.setLayout(new GridLayout(2, false)); - // new name + // New name Composite group= top; GridData gd; GridLayout gl; @@ -95,7 +95,11 @@ public class CRenameRefactoringInputPage extends UserInputWizardPage { Label label= new Label(top, SWT.NONE); label.setText(RenameMessages.CRenameRefactoringInputPage_label_newName); fNewName= new Text(top, SWT.BORDER); - fNewName.setText(fSearchString); + String name = processor.getReplacementText(); + if (name == null) { + name = fSearchString; + } + fNewName.setText(name); fNewName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); fNewName.selectAll(); @@ -106,52 +110,9 @@ public class CRenameRefactoringInputPage extends UserInputWizardPage { gd.horizontalSpan= 2; } - if (hasOption(CRefactory.OPTION_ASK_SCOPE)) { - // Specify the scope. - skipLine(top); - label = new Label(top, SWT.NONE); - label.setText(RenameMessages.CRenameRefactoringInputPage_label_scope); - label.setLayoutData(gd= new GridData(GridData.FILL_HORIZONTAL)); - gd.horizontalSpan= 2; - - group= new Composite(top, SWT.NONE); - group.setLayoutData(gd= new GridData(GridData.FILL_HORIZONTAL)); - gd.horizontalSpan= 2; - group.setLayout(gl= new GridLayout(4, false)); - gl.marginHeight= 0; - gl.marginLeft = gl.marginWidth; - gl.marginWidth = 0; - - fWorkspace= new Button(group, SWT.RADIO); - fWorkspace.setText(RenameMessages.CRenameRefactoringInputPage_button_workspace); - fWorkspace.setLayoutData(gd= new GridData()); - - fDependent= new Button(group, SWT.RADIO); - fDependent.setText(RenameMessages.CRenameRefactoringInputPage_button_relatedProjects); - fDependent.setLayoutData(gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING)); - gd.horizontalIndent= 8; - - fSingle= new Button(group, SWT.RADIO); - fSingle.setText(RenameMessages.CRenameRefactoringInputPage_button_singleProject); - fSingle.setLayoutData(gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING)); - gd.horizontalIndent= 8; - gd.horizontalSpan= 2; - - fWorkingSet= new Button(group, SWT.RADIO); - fWorkingSet.setText(RenameMessages.CRenameRefactoringInputPage_button_workingSet); - - fWorkingSetSpec= new Text(group, SWT.SINGLE|SWT.BORDER|SWT.READ_ONLY); - fWorkingSetSpec.setLayoutData(gd= new GridData(GridData.FILL_HORIZONTAL)); - gd.horizontalIndent= 8; - gd.horizontalSpan= 2; - fWorkingSetButton= new Button(group, SWT.PUSH); - fWorkingSetButton.setText(RenameMessages.CRenameRefactoringInputPage_button_chooseWorkingSet); - setButtonLayoutData(fWorkingSetButton); - } - boolean skippedLine= false; group= null; - if (hasOption(CRefactory.OPTION_IN_CODE)) { + if (hasOption(CRefactory.OPTION_IN_CODE_REFERENCES)) { group= createLabelAndGroup(group, skippedLine, top); fReferences= new Button(group, SWT.CHECK); fReferences.setText(RenameMessages.CRenameRefactoringInputPage_button_sourceCode); @@ -194,7 +155,43 @@ public class CRenameRefactoringInputPage extends UserInputWizardPage { fExhausiveFileSearch.setLayoutData(gd= new GridData()); gd.horizontalIndent= 5; gd.horizontalSpan= 2; - } + + // Specify the scope. + group= new Composite(top, SWT.NONE); + group.setLayoutData(gd= new GridData(GridData.FILL_HORIZONTAL)); + gd.horizontalSpan= 2; + gd.horizontalIndent= 16; + group.setLayout(gl= new GridLayout(4, false)); + gl.marginHeight= 0; + gl.marginLeft = gl.marginWidth; + gl.marginWidth = 0; + + fWorkspace= new Button(group, SWT.RADIO); + fWorkspace.setText(RenameMessages.CRenameRefactoringInputPage_button_workspace); + fWorkspace.setLayoutData(gd= new GridData()); + + fDependent= new Button(group, SWT.RADIO); + fDependent.setText(RenameMessages.CRenameRefactoringInputPage_button_relatedProjects); + fDependent.setLayoutData(gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING)); + gd.horizontalIndent= 8; + + fSingle= new Button(group, SWT.RADIO); + fSingle.setText(RenameMessages.CRenameRefactoringInputPage_button_singleProject); + fSingle.setLayoutData(gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING)); + gd.horizontalIndent= 8; + gd.horizontalSpan= 2; + + fWorkingSet= new Button(group, SWT.RADIO); + fWorkingSet.setText(RenameMessages.CRenameRefactoringInputPage_button_workingSet); + + fWorkingSetSpec= new Text(group, SWT.SINGLE|SWT.BORDER|SWT.READ_ONLY); + fWorkingSetSpec.setLayoutData(gd= new GridData(GridData.FILL_HORIZONTAL)); + gd.horizontalIndent= 8; + gd.horizontalSpan= 2; + fWorkingSetButton= new Button(group, SWT.PUSH); + fWorkingSetButton.setText(RenameMessages.CRenameRefactoringInputPage_button_chooseWorkingSet); + setButtonLayoutData(fWorkingSetButton); + } Dialog.applyDialogFont(top); hookSelectionListeners(); @@ -283,7 +280,7 @@ public class CRenameRefactoringInputPage extends UserInputWizardPage { } protected void onSelectedScope(int scope) { - getRenameProcessor().setScope(scope); + getRenameProcessor().setExhaustiveSearchScope(scope); updateEnablement(); } @@ -328,11 +325,11 @@ public class CRenameRefactoringInputPage extends UserInputWizardPage { fDependent.setSelection(true); break; } - processor.setScope(scope); + processor.setExhaustiveSearchScope(scope); String workingSet= fPreferences.getWorkingSet(); - processor.setWorkingSet(workingSet); // CRenameProcessor validates the working set name. - fWorkingSetSpec.setText(processor.getWorkingSet()); + processor.setWorkingSetName(workingSet); // CRenameProcessor validates the working set name. + fWorkingSetSpec.setText(processor.getWorkingSetName()); } if (fDoVirtual != null) { @@ -343,40 +340,46 @@ public class CRenameRefactoringInputPage extends UserInputWizardPage { boolean val= !fPreferences.getBoolean(CRenameRefactoringPreferences.KEY_REFERENCES_INV); fReferences.setSelection(val); } + initOption(fInInactiveCode, CRenameRefactoringPreferences.KEY_INACTIVE); initOption(fInComment, CRenameRefactoringPreferences.KEY_COMMENT); initOption(fInString, CRenameRefactoringPreferences.KEY_STRING); initOption(fInInclude, CRenameRefactoringPreferences.KEY_INCLUDE); initOption(fInMacro, CRenameRefactoringPreferences.KEY_MACRO_DEFINITION); initOption(fInPreprocessor, CRenameRefactoringPreferences.KEY_PREPROCESSOR); - initOption(fInInactiveCode, CRenameRefactoringPreferences.KEY_INACTIVE); initOption(fExhausiveFileSearch, CRenameRefactoringPreferences.KEY_EXHAUSTIVE_FILE_SEARCH); } private int computeSelectedOptions() { - int options= 0; - options |= computeOption(fDoVirtual, CRefactory.OPTION_DO_VIRTUAL); - options |= computeOption(fReferences, CRefactory.OPTION_IN_CODE); - options |= computeOption(fInComment, CRefactory.OPTION_IN_COMMENT); - options |= computeOption(fInString, CRefactory.OPTION_IN_STRING_LITERAL); - options |= computeOption(fInInclude, CRefactory.OPTION_IN_INCLUDE_DIRECTIVE); - options |= computeOption(fInMacro, CRefactory.OPTION_IN_MACRO_DEFINITION); - options |= computeOption(fInPreprocessor, CRefactory.OPTION_IN_PREPROCESSOR_DIRECTIVE); - options |= computeOption(fInInactiveCode, CRefactory.OPTION_IN_INACTIVE_CODE); - options |= computeOption(fExhausiveFileSearch, CRefactory.OPTION_EXHAUSTIVE_FILE_SEARCH); + int options= fPreferences.getOptions(); + options = updateOptions(options, fDoVirtual, CRefactory.OPTION_DO_VIRTUAL); + options = updateOptions(options, fInInactiveCode, CRefactory.OPTION_IN_INACTIVE_CODE); + options = updateOptions(options, fInComment, CRefactory.OPTION_IN_COMMENT); + options = updateOptions(options, fInString, CRefactory.OPTION_IN_STRING_LITERAL); + options = updateOptions(options, fInInclude, CRefactory.OPTION_IN_INCLUDE_DIRECTIVE); + options = updateOptions(options, fInMacro, CRefactory.OPTION_IN_MACRO_DEFINITION); + options = updateOptions(options, fInPreprocessor, CRefactory.OPTION_IN_PREPROCESSOR_DIRECTIVE); + options = updateOptions(options, fReferences, CRefactory.OPTION_IN_CODE_REFERENCES); + options = updateOptions(options, fExhausiveFileSearch, CRefactory.OPTION_EXHAUSTIVE_FILE_SEARCH); return options; } - private int computeOption(Button button, int option) { - if (button != null && button.getSelection()) { - return option; + private int updateOptions(int options, Button button, int optionMask) { + if (button == null) + return options; + if (button.getSelection()) { + return options | optionMask; + } else { + return options & ~optionMask; } - return 0; } private void initOption(Button button, String key) { - boolean val= false; + initOption(button, key, false); + } + + private void initOption(Button button, String key, boolean defaultValue) { if (button != null) { - val= fPreferences.getBoolean(key); + boolean val= fPreferences.getBoolean(key, defaultValue); button.setSelection(val); } } @@ -444,13 +447,13 @@ public class CRenameRefactoringInputPage extends UserInputWizardPage { fDependent.setSelection(false); fSingle.setSelection(false); fWorkingSet.setSelection(true); - processor.setScope(TextSearchWrapper.SCOPE_WORKING_SET); + processor.setExhaustiveSearchScope(TextSearchWrapper.SCOPE_WORKING_SET); wsName= ws.getName(); } } - processor.setWorkingSet(wsName); // CRenameProcessor validates the working set name. - fWorkingSetSpec.setText(processor.getWorkingSet()); + processor.setWorkingSetName(wsName); // CRenameProcessor validates the working set name. + fWorkingSetSpec.setText(processor.getWorkingSetName()); updateEnablement(); } @@ -469,22 +472,26 @@ public class CRenameRefactoringInputPage extends UserInputWizardPage { } protected void updateEnablement() { - boolean enable= fEnableScopeOptions == -1 || - (computeSelectedOptions() & fEnableScopeOptions) != 0; - - if (fWorkspace != null) { - fWorkspace.setEnabled(enable); - fDependent.setEnabled(enable); - fSingle.setEnabled(enable); - - boolean enableSpec= false; - fWorkingSet.setEnabled(enable); - if (enable && fWorkingSet.getSelection()) { - enableSpec= true; - } - fWorkingSetSpec.setEnabled(enableSpec); - fWorkingSetButton.setEnabled(enable); - } + if (fExhausiveFileSearch != null) { + boolean enable= fExhaustiveSearchEnablingOptions == -1 || + (computeSelectedOptions() & fExhaustiveSearchEnablingOptions) != 0; + + fExhausiveFileSearch.setEnabled(enable); + enable = enable && fExhausiveFileSearch.getSelection(); + if (fWorkspace != null) { + fWorkspace.setEnabled(enable); + fDependent.setEnabled(enable); + fSingle.setEnabled(enable); + + boolean enableSpec= false; + fWorkingSet.setEnabled(enable); + if (enable && fWorkingSet.getSelection()) { + enableSpec= true; + } + fWorkingSetSpec.setEnabled(enableSpec); + fWorkingSetButton.setEnabled(enable); + } + } } private CRenameProcessor getRenameProcessor() { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameRefactoringPreferences.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameRefactoringPreferences.java index 1aa3681f467..557d30c75d3 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameRefactoringPreferences.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/CRenameRefactoringPreferences.java @@ -45,6 +45,11 @@ public class CRenameRefactoringPreferences { return fDialogSettings.getBoolean(key); } + public boolean getBoolean(String key, boolean defaultValue) { + String value = fDialogSettings.get(key); + return value != null ? Boolean.parseBoolean(value) : defaultValue; + } + public void put(String key, int value) { fDialogSettings.put(key, value); } @@ -74,7 +79,9 @@ public class CRenameRefactoringPreferences { if (!getBoolean(KEY_IGNORE_VIRTUAL)) options |= CRefactory.OPTION_DO_VIRTUAL; if (!getBoolean(KEY_REFERENCES_INV)) - options |= CRefactory.OPTION_IN_CODE; + options |= CRefactory.OPTION_IN_CODE_REFERENCES; + if (getBoolean(KEY_INACTIVE)) + options |= CRefactory.OPTION_IN_INACTIVE_CODE; if (getBoolean(KEY_COMMENT)) options |= CRefactory.OPTION_IN_COMMENT; if (getBoolean(KEY_STRING)) @@ -85,8 +92,6 @@ public class CRenameRefactoringPreferences { options |= CRefactory.OPTION_IN_MACRO_DEFINITION; if (getBoolean(KEY_PREPROCESSOR)) options |= CRefactory.OPTION_IN_PREPROCESSOR_DIRECTIVE; - if (getBoolean(KEY_INACTIVE)) - options |= CRefactory.OPTION_IN_INACTIVE_CODE; if (getBoolean(KEY_EXHAUSTIVE_FILE_SEARCH)) options |= CRefactory.OPTION_EXHAUSTIVE_FILE_SEARCH; return options; diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameLinkedMode.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameLinkedMode.java index a8e95920a9a..dfb2d6e86ae 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameLinkedMode.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameLinkedMode.java @@ -334,8 +334,7 @@ public class RenameLinkedMode { if (fShowPreview) { // could have been updated by undoAndCreateRenameSupport(..) executed= renameSupport.openDialog(shell, true); } else { - renameSupport.perform(shell, fEditor.getSite().getWorkbenchWindow()); - executed= true; + executed= renameSupport.perform(shell, fEditor.getSite().getWorkbenchWindow()); } if (executed) { restoreFullSelection(); @@ -429,10 +428,9 @@ public class RenameLinkedMode { processor.setReplacementText(newName); CRenameRefactoringPreferences preferences = new CRenameRefactoringPreferences(); processor.setSelectedOptions(preferences.getOptions()); - processor.setScope(preferences.getScope()); - processor.setWorkingSet(preferences.getWorkingSet()); - RenameSupport renameSupport= RenameSupport.create(processor); - return renameSupport; + processor.setExhaustiveSearchScope(preferences.getScope()); + processor.setWorkingSetName(preferences.getWorkingSet()); + return RenameSupport.create(processor); } private void reconcile() throws CModelException { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameMessages.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameMessages.java index b4c6489068e..e570db8fcb4 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameMessages.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameMessages.java @@ -77,7 +77,6 @@ public class RenameMessages extends NLS { public static String CRenameRefactoringInputPage_button_workingSet; public static String CRenameRefactoringInputPage_errorInvalidIdentifier; public static String CRenameRefactoringInputPage_label_newName; - public static String CRenameRefactoringInputPage_label_scope; public static String CRenameRefactoringInputPage_label_updateWithin; public static String CRenameRefactoringInputPage_renameBaseAndDerivedMethods; public static String CRenameTopProcessor_enumerator; diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameMessages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameMessages.properties index dcd80f152da..e3c27856bd7 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameMessages.properties +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameMessages.properties @@ -66,15 +66,14 @@ CRenameRefactoringInputPage_button_includes=Include directives CRenameRefactoringInputPage_button_macroDefinitions=Macro definitions CRenameRefactoringInputPage_button_preprocessor=Other preprocessor directives CRenameRefactoringInputPage_button_strings=String literals -CRenameRefactoringInputPage_button_exhaustiveFileSearch=Exhaustive file search (slow) +CRenameRefactoringInputPage_button_exhaustiveFileSearch=Exhaustive (slow) file search in: CRenameRefactoringInputPage_button_singleProject=Project CRenameRefactoringInputPage_button_relatedProjects=Related projects CRenameRefactoringInputPage_button_workspace=All projects CRenameRefactoringInputPage_button_workingSet=Working set: CRenameRefactoringInputPage_errorInvalidIdentifier=''{0}'' is not a valid identifier CRenameRefactoringInputPage_label_newName=New Name: -CRenameRefactoringInputPage_label_scope=Scope of Rename: -CRenameRefactoringInputPage_label_updateWithin=Update within: +CRenameRefactoringInputPage_label_updateWithin=Update occurrences in: CRenameRefactoringInputPage_renameBaseAndDerivedMethods=Rename virtual methods in base and derived classes, also (if any). CRenameTopProcessor_enumerator=enumerator CRenameTopProcessor_error_invalidName=The selected name cannot be renamed. diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameSupport.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameSupport.java index e65517fa5ae..061495f2895 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameSupport.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/RenameSupport.java @@ -13,19 +13,25 @@ package org.eclipse.cdt.internal.ui.refactoring.rename; import java.lang.reflect.InvocationTargetException; +import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Status; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.operation.IRunnableContext; +import org.eclipse.jface.wizard.IWizardPage; +import org.eclipse.ltk.core.refactoring.Change; import org.eclipse.ltk.core.refactoring.RefactoringCore; import org.eclipse.ltk.core.refactoring.RefactoringStatus; import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.internal.ui.refactoring.RefactoringExecutionHelper; +import org.eclipse.cdt.internal.ui.refactoring.RefactoringStarter; /** * Central access point to execute rename refactorings. @@ -49,6 +55,13 @@ public class RenameSupport { /** Flag indicating that the setter method is to be updated as well. */ public static final int UPDATE_SETTER_METHOD= 1 << 5; + /** @see #openDialog(Shell, CRenameRefactoring, DialogMode) */ + private enum DialogMode { ALL_PAGES, PREVIEW_ONLY, CONDITIONAL_PREVIEW } + /** @see #openDialog(Shell, CRenameRefactoring, DialogMode) */ + private enum DialogResult { OK, CANCELED, SKIPPED } + // Same as org.eclipse.ltk.internal.ui.refactoring.IErrorWizardPage#PAGE_NAME + private static final String ERROR_PAGE_NAME = "ErrorPage"; //$NON-NLS-1$ + private CRenameRefactoring fRefactoring; private RefactoringStatus fPreCheckStatus; @@ -67,7 +80,7 @@ public class RenameSupport { * @throws CoreException if an unexpected exception occurs while performing the checking. * * @see #openDialog(Shell) - * @see #perform(Shell, IRunnableContext) + * @see #perform(Shell, IWorkbenchWindow) */ public IStatus preCheck() throws CoreException { ensureChecked(); @@ -80,14 +93,13 @@ public class RenameSupport { /** * Opens the refactoring dialog for this rename support. * - * @param parent a shell used as a parent for the refactoring dialog. - * @throws CoreException if an unexpected exception occurs while opening the - * dialog. + * @param shell a shell used as a parent for the refactoring dialog. + * @throws CoreException if an unexpected exception occurs while opening the dialog. * * @see #openDialog(Shell, boolean) */ - public void openDialog(Shell parent) throws CoreException { - openDialog(parent, false); + public boolean openDialog(Shell shell) throws CoreException { + return openDialog(shell, false); } /** @@ -116,7 +128,84 @@ public class RenameSupport { return false; } - return CRefactory.openDialog(shell, fRefactoring, showPreviewOnly); + DialogMode mode = showPreviewOnly ? DialogMode.PREVIEW_ONLY : DialogMode.ALL_PAGES; + return openDialog(shell, fRefactoring, mode) == DialogResult.OK; + } + + /** + * Opens the refactoring dialog for a given rename refactoring. + * + * @param shell a shell used as a parent for the refactoring dialog. + * @param refactoring the refactoring object. + * + * @see #openDialog(Shell, boolean) + */ + public static void openDialog(Shell shell, CRenameRefactoring refactoring) { + openDialog(shell, refactoring, DialogMode.ALL_PAGES); + } + + /** + * Opens the refactoring dialog. + * + *

+ * This method has to be called from within the UI thread. + *

+ * + * @param shell A shell used as a parent for the refactoring, preview, or error dialog + * @param refactoring The refactoring. + * @param mode One of DialogMode values. ALL_PAGES opens wizard with all pages shown; + * PREVIEW_ONLY opens the preview page only; CONDITIONAL_PREVIEW opens the wizard with + * preview page only and only if a warning was generated during the final conditions check. + * @return One of DialogResult values. OK is returned if the dialog was shown and + * the refactoring change was applied; CANCELED is returned if the refactoring was + * cancelled. SKIPPED is returned if the dialog was skipped in CONDITIONAL_PREVIEW mode and + * the refactoring change has not been applied yet. + */ + static DialogResult openDialog(Shell shell, CRenameRefactoring refactoring, final DialogMode mode) { + try { + final boolean[] dialogSkipped = new boolean[1]; + CRenameRefactoringWizard wizard; + if (mode == DialogMode.ALL_PAGES) { + wizard = new CRenameRefactoringWizard(refactoring); + } else { + wizard = new CRenameRefactoringWizard(refactoring) { + @Override + protected void addUserInputPages() { + // Nothing to add + } + + @Override + public IWizardPage getStartingPage() { + IWizardPage startingPage = super.getStartingPage(); + if (mode == DialogMode.CONDITIONAL_PREVIEW && + !startingPage.getName().equals(ERROR_PAGE_NAME)) { + dialogSkipped[0] = true; + return null; + } + return startingPage; + } + }; + wizard.setForcePreviewReview(mode != DialogMode.ALL_PAGES); + } + RefactoringStarter starter = new RefactoringStarter(); + CRenameProcessor processor = (CRenameProcessor) refactoring.getProcessor(); + processor.lockIndex(); + try { + processor.checkInitialConditions(new NullProgressMonitor()); + if (starter.activate(wizard, shell, RenameMessages.CRefactory_title_rename, + processor.getSaveMode())) { + return DialogResult.OK; + } + return dialogSkipped[0] ? DialogResult.SKIPPED : DialogResult.CANCELED; + } finally { + processor.unlockIndex(); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } catch (CoreException e) { + CUIPlugin.log(e); + } + return DialogResult.CANCELED; } /** @@ -139,26 +228,38 @@ public class RenameSupport { * @see #openDialog(Shell) * @see IRunnableContext#run(boolean, boolean, org.eclipse.jface.operation.IRunnableWithProgress) */ - public void perform(Shell parent, IRunnableContext context) throws InterruptedException, InvocationTargetException { + public boolean perform(Shell parent, IWorkbenchWindow context) throws InterruptedException, InvocationTargetException { try { ensureChecked(); if (fPreCheckStatus.hasFatalError()) { showInformation(parent, fPreCheckStatus); - return; + return false; } CRenameProcessor renameProcessor = getRenameProcessor(); + renameProcessor.lockIndex(); try { - renameProcessor.lockIndex(); fPreCheckStatus = renameProcessor.checkInitialConditions(new NullProgressMonitor()); if (fPreCheckStatus.hasFatalError()) { showInformation(parent, fPreCheckStatus); - return; + return false; + } + DialogResult result = openDialog(context.getShell(), fRefactoring, + DialogMode.CONDITIONAL_PREVIEW); + switch (result) { + case OK: + return true; + case SKIPPED: + RefactoringExecutionHelper helper= new RefactoringExecutionHelper(fRefactoring, + RefactoringCore.getConditionCheckingFailedSeverity(), + renameProcessor.getSaveMode(), parent, context); + Change change = renameProcessor.getChange(); + Assert.isNotNull(change); + helper.performChange(change, true); + return true; + default: + return false; } - RefactoringExecutionHelper helper= new RefactoringExecutionHelper(fRefactoring, - RefactoringCore.getConditionCheckingFailedSeverity(), renameProcessor.getSaveMode(), - parent, context); - helper.perform(true, true); } finally { renameProcessor.unlockIndex(); } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/TextSearchWrapper.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/TextSearchWrapper.java index 57af2ad3349..6c32f9ece07 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/TextSearchWrapper.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/TextSearchWrapper.java @@ -6,8 +6,8 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Markus Schorn - initial API and implementation - * Sergey Prigogin (Google) + * Markus Schorn - initial API and implementation + * Sergey Prigogin (Google) *******************************************************************************/ package org.eclipse.cdt.internal.ui.refactoring.rename; @@ -42,7 +42,6 @@ import org.eclipse.search.core.text.TextSearchMatchAccess; import org.eclipse.search.core.text.TextSearchRequestor; import org.eclipse.search.core.text.TextSearchScope; import org.eclipse.ui.IWorkingSet; -import org.eclipse.ui.IWorkingSetManager; import org.eclipse.ui.PlatformUI; import org.eclipse.cdt.core.model.CoreModel; @@ -65,28 +64,29 @@ public class TextSearchWrapper { public final static int SCOPE_WORKING_SET = 5; private static class SearchScope extends TextSearchScope { - public static SearchScope newSearchScope(IWorkingSet ws, IResource[] filter) { + public static SearchScope newSearchScope(IFile[] files, IWorkingSet ws) { IAdaptable[] adaptables= ws.getElements(); ArrayList resources = new ArrayList(); for (int i = 0; i < adaptables.length; i++) { IAdaptable adaptable = adaptables[i]; - IResource r= (IResource) adaptable.getAdapter(IResource.class); - if (r != null) { - resources.add(r); + IResource resource= (IResource) adaptable.getAdapter(IResource.class); + if (resource != null) { + resources.add(resource); } } - return newSearchScope(resources.toArray(new IResource[resources.size()]), filter); + return newSearchScope(files, resources.toArray(new IResource[resources.size()])); } - public static SearchScope newSearchScope(IResource[] roots, IResource[] filter) { - if (filter != null) { - ArrayList files = new ArrayList(filter.length); - for (IResource file : filter) { - if (isInForest(file, roots)) { - files.add(file); + public static SearchScope newSearchScope(IFile[] files, IResource[] roots) { + if (files != null) { + ArrayList resources = new ArrayList(files.length + roots.length); + for (IFile file : files) { + if (!isInForest(file, roots)) { + resources.add(file); } } - roots = files.toArray(new IResource[files.size()]); + Collections.addAll(resources, roots); + roots = resources.toArray(new IResource[resources.size()]); } return new SearchScope(roots); } @@ -181,93 +181,88 @@ public class TextSearchWrapper { public TextSearchWrapper() { } - private TextSearchScope createSearchScope(IFile file, int scope, String workingSetName, - IResource[] filter, String[] patterns) { + private TextSearchScope createSearchScope(IFile[] files, int scope, IFile file, + String workingSetName, String[] patterns) { switch (scope) { case SCOPE_WORKSPACE: - return defineSearchScope(file.getWorkspace().getRoot(), filter, patterns); + return defineSearchScope(files, file.getWorkspace().getRoot(), patterns); case SCOPE_SINGLE_PROJECT: - return defineSearchScope(file.getProject(), filter, patterns); + return defineSearchScope(files, file.getProject(), patterns); case SCOPE_FILE: - return defineSearchScope(file, filter, patterns); + return defineSearchScope(files, file, patterns); case SCOPE_WORKING_SET: { - TextSearchScope result= defineWorkingSetAsSearchScope(workingSetName, filter, patterns); - if (result == null) { - result= defineSearchScope(file.getWorkspace().getRoot(), filter, patterns); - } - return result; + return defineWorkingSetAsSearchScope(files, workingSetName, patterns); } } - return defineRelatedProjectsAsSearchScope(file.getProject(), filter, patterns); + return defineRelatedProjectsAsSearchScope(files, file.getProject(), patterns); } - private TextSearchScope defineRelatedProjectsAsSearchScope(IProject project, IResource[] filter, String[] patterns) { + private TextSearchScope defineRelatedProjectsAsSearchScope(IFile[] files, IProject project, String[] patterns) { HashSet projects= new HashSet(); LinkedList workThrough= new LinkedList(); workThrough.add(project); while (!workThrough.isEmpty()) { - IProject prj= workThrough.removeLast(); - if (projects.add(prj)) { + IProject proj= workThrough.removeLast(); + if (projects.add(proj)) { try { - workThrough.addAll(Arrays.asList(prj.getReferencedProjects())); - workThrough.addAll(Arrays.asList(prj.getReferencingProjects())); + workThrough.addAll(Arrays.asList(proj.getReferencedProjects())); + workThrough.addAll(Arrays.asList(proj.getReferencingProjects())); } catch (CoreException e) { // need to ignore } } } IResource[] roots= projects.toArray(new IResource[projects.size()]); - return defineSearchScope(roots, filter, patterns); + return defineSearchScope(files, roots, patterns); } - private TextSearchScope defineWorkingSetAsSearchScope(String workingSetName, IResource[] filter, String[] patterns) { - if (workingSetName == null) { - return null; - } - IWorkingSetManager wsManager= PlatformUI.getWorkbench().getWorkingSetManager(); - IWorkingSet ws= wsManager.getWorkingSet(workingSetName); - if (ws == null) { - return null; - } - SearchScope result= SearchScope.newSearchScope(ws, filter); + private TextSearchScope defineWorkingSetAsSearchScope(IFile[] files, String workingSetName, String[] patterns) { + IWorkingSet workingSet = workingSetName != null ? + PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSet(workingSetName) : + null; + SearchScope result= workingSet != null ? + SearchScope.newSearchScope(files, workingSet) : + SearchScope.newSearchScope(files, new IResource[0]); applyFilePatterns(result, patterns); return result; } private void applyFilePatterns(SearchScope scope, String[] patterns) { - for (int i = 0; i < patterns.length; i++) { - String pattern = patterns[i]; + for (String pattern : patterns) { scope.addFileNamePattern(pattern); } } - private TextSearchScope defineSearchScope(IResource root, IResource[] filter, String[] patterns) { - SearchScope result= SearchScope.newSearchScope(new IResource[] { root }, filter); + private TextSearchScope defineSearchScope(IFile[] files, IResource root, String[] patterns) { + SearchScope result= SearchScope.newSearchScope(files, new IResource[] { root }); applyFilePatterns(result, patterns); return result; } - private TextSearchScope defineSearchScope(IResource[] roots, IResource[] filter, String[] patterns) { - SearchScope result= SearchScope.newSearchScope(roots, filter); + private TextSearchScope defineSearchScope(IFile[] files, IResource[] roots, String[] patterns) { + SearchScope result= SearchScope.newSearchScope(files, roots); applyFilePatterns(result, patterns); return result; } /** * Searches for a given word. - * - * @param scope One of SCOPE_FILE, SCOPE_WORKSPACE, SCOPE_RELATED_PROJECTS, SCOPE_SINGLE_PROJECT, - * or SCOPE_WORKING_SET. - * @param file The file used as an anchor for the scope. - * @param workingSet The name of a working set. Ignored is scope is not SCOPE_WORKING_SET. - * @param filter If not null, further limits the scope of the search. + * + * @param filesToSearch The files to search. + * @param scope Together with {@code file} and {@code workingSet} defines set of additional + * file to search. One of SCOPE_FILE, SCOPE_WORKSPACE, SCOPE_RELATED_PROJECTS, + * SCOPE_SINGLE_PROJECT, or SCOPE_WORKING_SET. + * @param scopeAnchor The file used as an anchor for the scope. + * @param workingSet The name of a working set. Ignored if {@code scope} is not + * SCOPE_WORKING_SET. * @param patterns File name patterns. * @param word The word to search for. * @param monitor A progress monitor. * @param target The list that gets populated with search results. */ - public IStatus searchWord(int scope, IFile file, String workingSet, IResource[] filter, String[] patterns, - String word, IProgressMonitor monitor, final List target) { + public IStatus searchWord(IFile[] filesToSearch, int scope, IFile scopeAnchor, String workingSet, + String[] patterns, String word, IProgressMonitor monitor, + final List target) { int startPos= target.size(); TextSearchEngine engine= TextSearchEngine.create(); StringBuilder searchPattern= new StringBuilder(word.length() + 8); @@ -279,7 +274,7 @@ public class TextSearchWrapper { Pattern pattern= Pattern.compile(searchPattern.toString()); - TextSearchScope searchscope= createSearchScope(file, scope, workingSet, filter, patterns); + TextSearchScope searchscope= createSearchScope(filesToSearch, scope, scopeAnchor, workingSet, patterns); TextSearchRequestor requestor= new TextSearchRequestor() { @Override public boolean acceptPatternMatch(TextSearchMatchAccess access) { @@ -360,7 +355,7 @@ public class TextSearchWrapper { Token token; int lastState= 0; while ((token= scanner.nextToken()) != null) { - int state= CRefactory.OPTION_IN_CODE; + int state= CRefactory.OPTION_IN_CODE_REFERENCES; switch (token.getType()) { case Token.tLINECOMMENT: case Token.tBLOCKCOMMENT: @@ -382,7 +377,7 @@ public class TextSearchWrapper { break; } if (state != lastState) { - locations.add(new int[] {token.getOffset(), state}); + locations.add(new int[] { token.getOffset(), state }); lastState= state; } }