mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Bug 428237 Added "Replace all" option to Extract Constant dialog
Change-Id: I65b9b8dd86bfe6431e9bb5ea4894b234ab26e004 Signed-off-by: Thomas Corbat <tcorbat@hsr.ch>
This commit is contained in:
parent
e52031c676
commit
7c80ce009c
6 changed files with 70 additions and 20 deletions
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
return super.visit(expression);
|
||||
}
|
||||
});
|
||||
|
||||
@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);
|
||||
}
|
||||
});
|
||||
} 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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Add table
Reference in a new issue