1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-09 10:46:02 +02:00

FIXED - bug 246061: [Refactoring] why can I select a visibility when extracting a const

https://bugs.eclipse.org/bugs/show_bug.cgi?id=246061
This commit is contained in:
Emanuel Graf 2008-09-10 14:36:22 +00:00
parent 4e8f520b7c
commit 06eb3b7f29
9 changed files with 101 additions and 27 deletions

View file

@ -456,18 +456,17 @@ filename=A.h
//@A.h
class X {
void method() {
int a= /*$*/1/*$$*/;
int a= /*$*/42/*$$*/;
}
};
//=
namespace
{
const int theAnswer = 1;
}
class X {
void method() {
int a= theAnswer;
}
}
public:
static const int theAnswer = 42;
};

View file

@ -21,7 +21,7 @@ import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.cdt.ui.tests.refactoring.RefactoringTest;
import org.eclipse.cdt.ui.tests.refactoring.TestSourceFile;
import org.eclipse.cdt.internal.ui.refactoring.NameNVisibilityInformation;
import org.eclipse.cdt.internal.ui.refactoring.extractconstant.ExtractConstantInfo;
import org.eclipse.cdt.internal.ui.refactoring.extractconstant.ExtractConstantRefactoring;
import org.eclipse.cdt.internal.ui.refactoring.utils.VisibilityEnum;
@ -40,7 +40,7 @@ public class ExtractConstantRefactoringTest extends RefactoringTest {
@Override
protected void runTest() throws Throwable {
IFile refFile = project.getFile(fileName);
NameNVisibilityInformation info = new NameNVisibilityInformation();
ExtractConstantInfo info = new ExtractConstantInfo();
ExtractConstantRefactoring refactoring = new ExtractConstantRefactoring( refFile, selection, info);
try {
refactoring.lockIndex();

View file

@ -12,9 +12,9 @@
package org.eclipse.cdt.internal.ui.refactoring;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
@ -57,10 +57,10 @@ public class MethodContext {
return declarationName;
}
public IASTSimpleDeclaration getMethodDeclaration(){
public IASTDeclaration getMethodDeclaration(){
IASTNode parent = declarationName.getParent().getParent();
if (parent instanceof IASTSimpleDeclaration) {
return (IASTSimpleDeclaration) parent;
if (parent instanceof IASTDeclaration) {
return (IASTDeclaration) parent;
}
return null;
}
@ -199,4 +199,19 @@ public class MethodContext {
ICPPClassType bind = (ICPPClassType)classname.resolveBinding();
return bind;
}
public static boolean isSameClass(IASTName declName1,
IASTName declName2) {
ICPPClassType bind1 = getClassBinding(declName1);
ICPPClassType bind2 = getClassBinding(declName2);
return bind1.equals(bind2);
}
private static ICPPClassType getClassBinding(IASTName declName1) {
if (declName1.getParent().getParent().getParent() instanceof ICPPASTCompositeTypeSpecifier) {
ICPPASTCompositeTypeSpecifier compTypeSpec = (ICPPASTCompositeTypeSpecifier) declName1.getParent().getParent().getParent();
return (ICPPClassType) compTypeSpec.getName().resolveBinding();
}
return null;
}
}

View file

@ -0,0 +1,34 @@
/*******************************************************************************
* Copyright (c) 2008 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
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Institute for Software (IFS)- initial API and implementation
******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.extractconstant;
import org.eclipse.cdt.internal.ui.refactoring.MethodContext;
import org.eclipse.cdt.internal.ui.refactoring.NameNVisibilityInformation;
/**
* @author Emanuel Graf IFS
*
*/
public class ExtractConstantInfo extends NameNVisibilityInformation{
private MethodContext mContext;
public MethodContext getMContext() {
return mContext;
}
public void setMContext(MethodContext context) {
mContext = context;
}
}

View file

@ -62,7 +62,6 @@ import org.eclipse.cdt.internal.ui.refactoring.AddDeclarationNodeToClassChange;
import org.eclipse.cdt.internal.ui.refactoring.CRefactoring;
import org.eclipse.cdt.internal.ui.refactoring.MethodContext;
import org.eclipse.cdt.internal.ui.refactoring.ModificationCollector;
import org.eclipse.cdt.internal.ui.refactoring.NameNVisibilityInformation;
import org.eclipse.cdt.internal.ui.refactoring.utils.NodeHelper;
import org.eclipse.cdt.internal.ui.refactoring.utils.SelectionHelper;
import org.eclipse.cdt.internal.ui.refactoring.utils.TranslationUnitHelper;
@ -78,9 +77,9 @@ public class ExtractConstantRefactoring extends CRefactoring {
private IASTLiteralExpression target = null;
private final ArrayList<IASTExpression> literalsToReplace = new ArrayList<IASTExpression>();
private final NameNVisibilityInformation info;
private final ExtractConstantInfo info;
public ExtractConstantRefactoring(IFile file, ISelection selection, NameNVisibilityInformation info){
public ExtractConstantRefactoring(IFile file, ISelection selection, ExtractConstantInfo info){
super(file,selection, null);
this.info = info;
name = Messages.ExtractConstantRefactoring_ExtractConst;
@ -120,6 +119,7 @@ public class ExtractConstantRefactoring extends CRefactoring {
info.addNamesToUsedNames(findAllDeclaredNames());
info.setName(getDefaultName(target));
info.setMContext(NodeHelper.findMethodContext(target, getIndex()));
sm.done();
return initStatus;
}
@ -254,7 +254,7 @@ public class ExtractConstantRefactoring extends CRefactoring {
protected void collectModifications(IProgressMonitor pm, ModificationCollector collector)
throws CoreException, OperationCanceledException{
MethodContext context = NodeHelper.findMethodContext(target, getIndex());
MethodContext context = info.getMContext();
Collection<IASTExpression> locLiteralsToReplace = new ArrayList<IASTExpression>();
if(context.getType() == MethodContext.ContextType.METHOD){
@ -262,8 +262,14 @@ public class ExtractConstantRefactoring extends CRefactoring {
for (IASTExpression expression : literalsToReplace) {
MethodContext exprContext = NodeHelper.findMethodContext(expression, getIndex());
if(exprContext.getType() == MethodContext.ContextType.METHOD){
if( MethodContext.isSameClass(exprContext.getMethodQName(), context.getMethodQName())){
locLiteralsToReplace.add(expression);
if(context.getMethodQName() != null) {
if( MethodContext.isSameClass(exprContext.getMethodQName(), context.getMethodQName())){
locLiteralsToReplace.add(expression);
}
}else {
if( MethodContext.isSameClass(exprContext.getMethodDeclarationName(), context.getMethodDeclarationName())){
locLiteralsToReplace.add(expression);
}
}
}
}

View file

@ -20,7 +20,6 @@ import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.internal.ui.refactoring.CRefactoring;
import org.eclipse.cdt.internal.ui.refactoring.NameNVisibilityInformation;
import org.eclipse.cdt.internal.ui.refactoring.RefactoringRunner;
/**
@ -35,7 +34,7 @@ public class ExtractConstantRefactoringRunner extends RefactoringRunner {
@Override
public void run() {
NameNVisibilityInformation info = new NameNVisibilityInformation();
ExtractConstantInfo info = new ExtractConstantInfo();
CRefactoring refactoring = new ExtractConstantRefactoring(file,selection,info);
ExtractConstantRefactoringWizard wizard = new ExtractConstantRefactoringWizard(refactoring, info);
RefactoringWizardOpenOperation operator = new RefactoringWizardOpenOperation(wizard);

View file

@ -14,7 +14,7 @@ package org.eclipse.cdt.internal.ui.refactoring.extractconstant;
import org.eclipse.ltk.core.refactoring.Refactoring;
import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
import org.eclipse.cdt.internal.ui.refactoring.NameNVisibilityInformation;
import org.eclipse.cdt.internal.ui.refactoring.MethodContext.ContextType;
import org.eclipse.cdt.internal.ui.refactoring.dialogs.ExtractInputPage;
/**
@ -23,16 +23,16 @@ import org.eclipse.cdt.internal.ui.refactoring.dialogs.ExtractInputPage;
public class ExtractConstantRefactoringWizard extends RefactoringWizard {
private ExtractInputPage page;
private final NameNVisibilityInformation info;
private final ExtractConstantInfo info;
public ExtractConstantRefactoringWizard(Refactoring refactoring, NameNVisibilityInformation info) {
public ExtractConstantRefactoringWizard(Refactoring refactoring, ExtractConstantInfo info) {
super(refactoring, WIZARD_BASED_USER_INTERFACE);
this.info = info;
}
@Override
protected void addUserInputPages() {
page = new InputPage(Messages.ExtractConstantRefactoring_ExtractConst, info);
page = new InputPage(Messages.ExtractConstantRefactoring_ExtractConst, info, info.getMContext().getType() == ContextType.METHOD);
addPage(page);
}

View file

@ -14,6 +14,7 @@ package org.eclipse.cdt.internal.ui.refactoring.extractconstant;
import java.util.ArrayList;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.cdt.internal.ui.refactoring.NameNVisibilityInformation;
import org.eclipse.cdt.internal.ui.refactoring.dialogs.ExtractInputPage;
@ -21,12 +22,26 @@ import org.eclipse.cdt.internal.ui.refactoring.dialogs.ExtractInputPage;
public class InputPage extends ExtractInputPage {
private final ArrayList<String> usedNames;
private boolean showVisibilityPane;
public InputPage(String name, NameNVisibilityInformation info) {
this(name, info, true);
}
public InputPage(String name, NameNVisibilityInformation info, boolean showVisibilityPane) {
super(name, info);
label = Messages.InputPage_ConstName;
errorLabel = Messages.InputPage_EnterContName;
label = Messages.InputPage_ConstName;
errorLabel = Messages.InputPage_EnterContName;
usedNames = info.getUsedNames();
this.showVisibilityPane = showVisibilityPane;
}
@Override
public void createControl(Composite parent) {
super.createControl(parent);
control.getVisibiltyGroup().setVisible(showVisibilityPane);
}
@Override

View file

@ -37,6 +37,7 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTranslationUnit;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.cdt.internal.ui.refactoring.MethodContext;
import org.eclipse.cdt.internal.ui.refactoring.MethodContext.ContextType;
/**
* General class for common Node operations.
@ -133,7 +134,7 @@ public class NodeHelper {
ICPPASTQualifiedName qname =( ICPPASTQualifiedName )name;
context.setMethodQName(qname);
IBinding bind = qname.resolveBinding();
IASTName[] decl = translationUnit.getDeclarationsInAST(bind);//TODO HSR works only for names in the current translationUnit
IASTName[] decl = translationUnit.getDeclarationsInAST(bind);
for (IASTName tmpname : decl) {
IASTNode methoddefinition = tmpname.getParent().getParent();
if (methoddefinition instanceof IASTSimpleDeclaration) {
@ -172,6 +173,11 @@ public class NodeHelper {
}
}
}
}else {
if (name.getParent().getParent().getParent() instanceof ICPPASTCompositeTypeSpecifier) {
context.setType(ContextType.METHOD);
context.setMethodDeclarationName(name);
}
}
}