1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-01 06:05:24 +02:00

Bug 319506 - Allow renaming class via constructor

Change-Id: I3c2f3e5337c7cdea4714732580806713aa28187b
Signed-off-by: Ian Leslie <ian.leslie@lesliesoftware.com>
This commit is contained in:
Nathan Ridge 2018-03-23 21:33:36 -04:00
parent 54287859f0
commit 91a142fcb7
4 changed files with 89 additions and 44 deletions

View file

@ -228,27 +228,6 @@ public class RenameRegressionTests extends RenameTestBase {
assertChange(changes, file, contents.indexOf("method1();//res2"), 7, "ooga"); assertChange(changes, file, contents.indexOf("method1();//res2"), 7, "ooga");
} }
public void testConstructor_26() throws Exception {
StringWriter writer = new StringWriter();
writer.write("class Boo{ \n");
writer.write(" Boo(){}//vp1,res1 \n");
writer.write("}; \n");
writer.write("void f() { \n");
writer.write(" Boo a = new Boo(); \n");
writer.write("} \n");
String contents = writer.toString();
IFile file = importFile("t.cpp", contents);
int offset = contents.indexOf("Boo(){}") ;
try {
getRefactorChanges(file, offset, "ooga");
} catch (AssertionFailedError e) {
assertTrue(e.getMessage().startsWith("Input check on ooga failed."));
return;
}
fail ("An error should have occurred in the input check.");
}
// The constructor name is accepted, but the refactoring doesn't remove the return // The constructor name is accepted, but the refactoring doesn't remove the return
// type and there is a compile error. Renaming to a constructor should be disabled. // type and there is a compile error. Renaming to a constructor should be disabled.
// However, the UI does display the error in the preview panel. Defect 78769 states // However, the UI does display the error in the preview panel. Defect 78769 states
@ -277,28 +256,6 @@ public class RenameRegressionTests extends RenameTestBase {
fail ("An error should have occurred in the input check."); fail ("An error should have occurred in the input check.");
} }
public void testDestructor_28() throws Exception {
StringWriter writer = new StringWriter();
writer.write("class Boo{ \n");
writer.write(" ~Boo(){}//vp1 \n");
writer.write("}; \n");
writer.write("void f() { \n");
writer.write(" Boo a ; \n");
writer.write(" a.~Boo(); \n");
writer.write("} \n");
String contents = writer.toString();
IFile file = importFile("t.cpp", contents);
int offset = contents.indexOf("~Boo(){}") ;
try {
getRefactorChanges(file, offset, "ooga");
} catch (AssertionFailedError e) {
assertTrue(e.getMessage().startsWith("Input check on ooga failed."));
return;
}
fail ("An error should have occurred in the input check.");
}
public void testDestructor_29_72612() throws Exception { public void testDestructor_29_72612() throws Exception {
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
writer.write("class Boo{ \n"); writer.write("class Boo{ \n");

View file

@ -2277,6 +2277,58 @@ public class RenameTypeTests extends RenameTestBase {
assertTotalChanges(countOccurrences(contents, "String"), ch); assertTotalChanges(countOccurrences(contents, "String"), ch);
} }
public void testRenameClassFromCtor() throws Exception {
StringWriter writer = new StringWriter();
writer.write("class String \n"); //$NON-NLS-1$
writer.write("{ \n"); //$NON-NLS-1$
writer.write("public: \n"); //$NON-NLS-1$
writer.write(" String(); \n"); //$NON-NLS-1$
writer.write(" String(const String &other); \n"); //$NON-NLS-1$
writer.write(" ~String(); \n"); //$NON-NLS-1$
writer.write(" String &operator=(const String &other); \n"); //$NON-NLS-1$
writer.write("}; \n"); //$NON-NLS-1$
writer.write(" String::String() {} \n"); //$NON-NLS-1$
writer.write(" String::String(const String &other) {}; \n"); //$NON-NLS-1$
writer.write(" String::~String() {}; \n"); //$NON-NLS-1$
writer.write(" String& String::operator=(const String &other) \n"); //$NON-NLS-1$
writer.write(" {return *this;} \n"); //$NON-NLS-1$
String contents = writer.toString();
IFile cpp= importFile("test.cpp", contents); //$NON-NLS-1$
int offset= contents.indexOf("String()"); //$NON-NLS-1$
RefactoringStatus status= checkConditions(cpp, offset, "CString"); //$NON-NLS-1$
assertRefactoringOk(status);
Change ch= getRefactorChanges(cpp, offset, "CString"); //$NON-NLS-1$
assertTotalChanges(countOccurrences(contents, "String"), ch); //$NON-NLS-1$
}
public void testRenameClassFromDtor() throws Exception {
StringWriter writer = new StringWriter();
writer.write("class String \n"); //$NON-NLS-1$
writer.write("{ \n"); //$NON-NLS-1$
writer.write("public: \n"); //$NON-NLS-1$
writer.write(" String(); \n"); //$NON-NLS-1$
writer.write(" String(const String &other); \n"); //$NON-NLS-1$
writer.write(" ~String(); \n"); //$NON-NLS-1$
writer.write(" String &operator=(const String &other); \n"); //$NON-NLS-1$
writer.write("}; \n"); //$NON-NLS-1$
writer.write(" String::String() {} \n"); //$NON-NLS-1$
writer.write(" String::String(const String &other) {}; \n"); //$NON-NLS-1$
writer.write(" String::~String() {}; \n"); //$NON-NLS-1$
writer.write(" String& String::operator=(const String &other) \n"); //$NON-NLS-1$
writer.write(" {return *this;} \n"); //$NON-NLS-1$
String contents = writer.toString();
IFile cpp= importFile("test.cpp", contents); //$NON-NLS-1$
int offset= contents.indexOf("~String") + 1; //$NON-NLS-1$
RefactoringStatus status= checkConditions(cpp, offset, "CString"); //$NON-NLS-1$
assertRefactoringOk(status);
Change ch= getRefactorChanges(cpp, offset, "CString"); //$NON-NLS-1$
assertTotalChanges(countOccurrences(contents, "String"), ch); //$NON-NLS-1$
}
public void testUsingDeclaration_332895() throws Exception { public void testUsingDeclaration_332895() throws Exception {
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
writer.write("namespace ns { \n"); writer.write("namespace ns { \n");

View file

@ -94,10 +94,14 @@ public class CRefactoringArgument {
return fLength; return fLength;
} }
public void setName(IASTName name) { public void setName(String name) {
fText= name.toString(); fText= name.toString();
} }
public void setName(IASTName name) {
setName(name.toString());
}
public void setBinding(IASTTranslationUnit tu, IBinding binding, IScope scope) { public void setBinding(IASTTranslationUnit tu, IBinding binding, IScope scope) {
fTranslationUnit= tu; fTranslationUnit= tu;
fBinding= binding; fBinding= binding;

View file

@ -30,10 +30,17 @@ import org.eclipse.ui.PlatformUI;
import org.eclipse.cdt.core.CCProjectNature; import org.eclipse.cdt.core.CCProjectNature;
import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.CProjectNature; import org.eclipse.cdt.core.CProjectNature;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMember;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.core.index.IIndex; import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexManager; import org.eclipse.cdt.core.index.IIndexManager;
import org.eclipse.cdt.core.model.CoreModel; import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.ui.CUIPlugin;
/** /**
* This is the processor used for the rename. It decides which of the delegates to * This is the processor used for the rename. It decides which of the delegates to
@ -120,6 +127,8 @@ public class CRenameProcessor extends RenameProcessor {
if (path == null) { if (path == null) {
return RefactoringStatus.createFatalErrorStatus(RenameMessages.CRenameTopProcessor_error_renameWithoutSourceFile); return RefactoringStatus.createFatalErrorStatus(RenameMessages.CRenameTopProcessor_error_renameWithoutSourceFile);
} }
updateBinding();
fDelegate= createDelegate(); fDelegate= createDelegate();
if (fDelegate == null) { if (fDelegate == null) {
@ -131,6 +140,29 @@ public class CRenameProcessor extends RenameProcessor {
return fInitialConditionsStatus; return fInitialConditionsStatus;
} }
/**
* Change the binding for the renaming of constructors and destructor to the class.
*/
private void updateBinding() {
IBinding binding= fArgument.getBinding();
if (binding instanceof ICPPConstructor ||
(binding instanceof ICPPMethod && ((ICPPMethod) binding).isDestructor())) {
// Switch binding to class level when constructor or destructor selected
IBinding newBinding = ((ICPPMember) binding).getClassOwner();
IScope scope = fArgument.getScope();
try {
scope = newBinding.getScope();
} catch (DOMException e) {
CUIPlugin.log(e);
}
fArgument.setBinding(fArgument.getTranslationUnit(), newBinding, scope);
if (fArgument.getName().startsWith("~")) { //$NON-NLS-1$
fArgument.setName(newBinding.getName());
}
}
}
private CRenameProcessorDelegate createDelegate() { private CRenameProcessorDelegate createDelegate() {
switch (fArgument.getArgumentKind()) { switch (fArgument.getArgumentKind()) {
case CRefactory.ARGUMENT_LOCAL_VAR: case CRefactory.ARGUMENT_LOCAL_VAR: