diff options
6 files changed, 69 insertions, 19 deletions
diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/extractconstant/ExtractConstantRefactoringTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/extractconstant/ExtractConstantRefactoringTest.java index d99cc6daf13..68aadf4a9e8 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/extractconstant/ExtractConstantRefactoringTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/extractconstant/ExtractConstantRefactoringTest.java @@ -34,6 +34,7 @@ import org.eclipse.cdt.internal.ui.refactoring.utils.VisibilityEnum; public class ExtractConstantRefactoringTest extends RefactoringTestBase { private String extractedConstantName; private VisibilityEnum visibility; + private boolean replaceAllLiterals; private ExtractConstantRefactoring refactoring; public ExtractConstantRefactoringTest() { @@ -52,6 +53,7 @@ public class ExtractConstantRefactoringTest extends RefactoringTestBase { public void setUp() throws Exception { extractedConstantName = "EXTRACTED"; visibility = VisibilityEnum.v_private; + replaceAllLiterals = true; super.setUp(); } @@ -67,6 +69,7 @@ public class ExtractConstantRefactoringTest extends RefactoringTestBase { ExtractConstantInfo info = refactoring.getRefactoringInfo(); info.setName(extractedConstantName); info.setVisibility(visibility); + info.setReplaceAllLiterals(replaceAllLiterals); } //A.h @@ -670,7 +673,7 @@ public class ExtractConstantRefactoringTest extends RefactoringTestBase { //<refactoring comment="Create constant for 42" description="Extract Constant Refactoring" //fileName="file:${projectPath}/A.cpp" flags="4" //id="org.eclipse.cdt.ui.refactoring.extractconstant.ExtractConstantRefactoring" name="EXTRACTED" - //project="RegressionTestProject" selection="64,2" visibility="public"/> + //project="RegressionTestProject" selection="64,2" visibility="public" replaceAll="true"/> //</session> // public void testHistoryExtractConstantInt() throws Exception { @@ -1013,4 +1016,27 @@ public class ExtractConstantRefactoringTest extends RefactoringTestBase { refactoring.setContext(new CRefactoringContext(refactoring)); refactoring.checkInitialConditions(npm()); } + + //A.cpp + //int h = 42; + //void foo() { + // int j = 42; + // int i = /*$*/42/*$$*/; + //} + //= + //namespace { + // + //const int EXTRACTED = 42; + // + //} + // + //int h = 42; + //void foo() { + // int j = 42; + // int i = EXTRACTED; + //} + public void testExtractOnlyOneOccurrence() throws Exception { + replaceAllLiterals = false; + assertRefactoringSuccess(); + } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoring.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoring.java index 5b05ed2306c..ef222be5942 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoring.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoring.java @@ -315,24 +315,29 @@ public class ExtractConstantRefactoring extends CRefactoring { SubMonitor subMonitor = SubMonitor.convert(monitor, 5); final Collection<IASTExpression> result = new ArrayList<>(); - IASTTranslationUnit ast = getAST(tu, subMonitor.split(4)); - subMonitor.split(1); - ast.accept(new ASTVisitor() { - { - shouldVisitExpressions = true; - } - - @Override - public int visit(IASTExpression expression) { - if (isSameExpressionTree(expression, target)) { - if (!(expression.getNodeLocations().length == 1 && - expression.getNodeLocations()[0] instanceof IASTMacroExpansionLocation)) { - result.add(expression); + if (info.isReplaceAllOccurences()) { + IASTTranslationUnit ast = getAST(tu, subMonitor.split(4)); + subMonitor.split(1); + ast.accept(new ASTVisitor() { + { + shouldVisitExpressions = true; + } + + @Override + public int visit(IASTExpression expression) { + if (isSameExpressionTree(expression, target)) { + if (!(expression.getNodeLocations().length == 1 && + expression.getNodeLocations()[0] instanceof IASTMacroExpansionLocation)) { + result.add(expression); + } } + return super.visit(expression); } - return super.visit(expression); - } - }); + }); + } else { + subMonitor.split(5); + result.add(target); + } return result; } @@ -385,6 +390,7 @@ public class ExtractConstantRefactoring extends CRefactoring { arguments.put(CRefactoringDescriptor.SELECTION, selectedRegion.getOffset() + "," + selectedRegion.getLength()); //$NON-NLS-1$ arguments.put(ExtractConstantRefactoringDescriptor.NAME, info.getName()); arguments.put(ExtractConstantRefactoringDescriptor.VISIBILITY, info.getVisibility().toString()); + arguments.put(ExtractConstantRefactoringDescriptor.REPLACE_ALL, Boolean.toString(info.isReplaceAllOccurences())); return arguments; } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoringDescriptor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoringDescriptor.java index 52f9e12837b..ebfb2115296 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoringDescriptor.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoringDescriptor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2012 Institute for Software, HSR Hochschule fuer Technik + * Copyright (c) 2009, 2016 Institute for Software, HSR Hochschule fuer Technik * Rapperswil, University of applied sciences and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -9,6 +9,7 @@ * Contributors: * Institute for Software (IFS)- initial API and implementation * Sergey Prigogin (Google) + * Thomas Corbat (IFS) ******************************************************************************/ package org.eclipse.cdt.internal.ui.refactoring.extractconstant; @@ -31,6 +32,7 @@ import org.eclipse.cdt.internal.ui.refactoring.utils.VisibilityEnum; public class ExtractConstantRefactoringDescriptor extends CRefactoringDescriptor { protected static final String NAME = "name"; //$NON-NLS-1$ protected static final String VISIBILITY = "visibility"; //$NON-NLS-1$ + protected static final String REPLACE_ALL = "replaceAll"; //$NON-NLS-1$ protected ExtractConstantRefactoringDescriptor(String project, String description, String comment, Map<String, String> arguments) { @@ -47,6 +49,7 @@ public class ExtractConstantRefactoringDescriptor extends CRefactoringDescriptor ExtractConstantInfo info = refactoring.getRefactoringInfo(); info.setName(arguments.get(NAME)); info.setVisibility(VisibilityEnum.getEnumForStringRepresentation(arguments.get(VISIBILITY))); + info.setReplaceAllLiterals(Boolean.parseBoolean(arguments.get(REPLACE_ALL))); return refactoring; } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/InputPage.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/InputPage.java index 7bb25df892d..c518de4fb3c 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/InputPage.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/InputPage.java @@ -14,10 +14,13 @@ package org.eclipse.cdt.internal.ui.refactoring.extractconstant; import org.eclipse.ltk.ui.refactoring.UserInputWizardPage; import org.eclipse.osgi.util.NLS; +import org.eclipse.swt.SWT; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.events.MouseAdapter; import org.eclipse.swt.events.MouseEvent; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; @@ -64,6 +67,16 @@ public class InputPage extends UserInputWizardPage { }); } + Button replaceAllOccurencesButton = new Button(control, SWT.CHECK); + replaceAllOccurencesButton.setSelection(info.isReplaceAllOccurences()); + replaceAllOccurencesButton.setText(Messages.InputPage_ReplaceAllOccurrences); + replaceAllOccurencesButton.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + info.setReplaceAllLiterals(replaceAllOccurencesButton.getSelection()); + } + }); + checkName(); control.getVisibiltyGroup().setVisible(showVisibilityPane); setControl(control); @@ -83,7 +96,7 @@ public class InputPage extends UserInputWizardPage { } private void verifyName(String name) { - if (info.getUsedNames().contains(name)) { + if (info.isNameUsed(name)) { setErrorMessage(NLS.bind(Messages.InputPage_NameAlreadyDefined, name)); setPageComplete(false); } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/Messages.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/Messages.java index e16241c69fe..8f08845cfd8 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/Messages.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/Messages.java @@ -17,6 +17,7 @@ final class Messages extends NLS { public static String InputPage_ConstName; public static String InputPage_EnterConstName; public static String InputPage_NameAlreadyDefined; + public static String InputPage_ReplaceAllOccurrences; public static String ExtractConstantRefactoring_ExtractConst; public static String ExtractConstantRefactoring_LiteralMustBeSelected; public static String ExtractConstantRefactoring_NoLiteralSelected; diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/Messages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/Messages.properties index 33b6323c963..18359657958 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/Messages.properties +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/Messages.properties @@ -13,6 +13,7 @@ InputPage_ConstName=Constant &name: InputPage_EnterConstName=Enter a name for the new constant InputPage_NameAlreadyDefined=An element named ''{0}'' is already defined in this scope. +InputPage_ReplaceAllOccurrences=Replace all occurrences ExtractConstantRefactoring_ExtractConst=Extract Constant ExtractConstantRefactoring_LiteralMustBeSelected=An literal expression must be selected to activate this refactoring. ExtractConstantRefactoring_NoLiteralSelected=No selected literal. |