1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-08 16:55:38 +02:00

Visibility ordering within a class declaration.

This commit is contained in:
Sergey Prigogin 2012-01-05 20:23:58 -08:00
parent 76e962ee9b
commit 046c25fadb
7 changed files with 245 additions and 210 deletions

View file

@ -397,7 +397,6 @@ public:
}; };
#endif /*A_H_*/ #endif /*A_H_*/
//= //=
#ifndef A_H_ #ifndef A_H_
#define A_H_ #define A_H_
@ -413,7 +412,6 @@ private:
}; };
#endif /*A_H_*/ #endif /*A_H_*/
//@A.cpp //@A.cpp
#include "A.h" #include "A.h"
@ -426,7 +424,6 @@ A::~A() {
int A::foo() { int A::foo() {
return /*$*/42/*$$*/; return /*$*/42/*$$*/;
} }
//= //=
#include "A.h" #include "A.h"
@ -439,8 +436,7 @@ A::~A() {
int A::foo() { int A::foo() {
return theAnswer; return theAnswer;
} }
//!Bug 246062 - Extracting a constant from an inlined method
//!Bug 246062 [Refactoring] NPE extracting a constant from an inlined method
//#org.eclipse.cdt.ui.tests.refactoring.extractconstant.ExtractConstantRefactoringTest //#org.eclipse.cdt.ui.tests.refactoring.extractconstant.ExtractConstantRefactoringTest
//@.config //@.config
filename=A.h filename=A.h
@ -450,18 +446,17 @@ class X {
int a = /*$*/42/*$$*/; int a = /*$*/42/*$$*/;
} }
}; };
//= //=
class X { class X {
public:
static const int theAnswer = 42;
private:
void method() { void method() {
int a = theAnswer; int a = theAnswer;
} }
public:
static const int theAnswer = 42;
}; };
//!Extract constant string
//!ExtractConstantString
//#org.eclipse.cdt.ui.tests.refactoring.extractconstant.ExtractConstantRefactoringTest //#org.eclipse.cdt.ui.tests.refactoring.extractconstant.ExtractConstantRefactoringTest
//@.config //@.config
visibility=private visibility=private

View file

@ -1480,7 +1480,6 @@ private:
}; };
#endif /*A_H_*/ #endif /*A_H_*/
//= //=
#ifndef A_H_ #ifndef A_H_
#define A_H_ #define A_H_
@ -1491,15 +1490,14 @@ public:
virtual ~A(); virtual ~A();
int foo(); int foo();
private:
int help();
protected: protected:
void exp(int& i); void exp(int& i);
private:
int help();
}; };
#endif /*A_H_*/ #endif /*A_H_*/
//@A.cpp //@A.cpp
#include "A.h" #include "A.h"
@ -1519,7 +1517,6 @@ int A::foo() {
int A::help() { int A::help() {
return 42; return 42;
} }
//= //=
#include "A.h" #include "A.h"
@ -1543,7 +1540,6 @@ int A::foo() {
int A::help() { int A::help() {
return 42; return 42;
} }
//@.config //@.config
filename=A.cpp filename=A.cpp
methodname=exp methodname=exp

View file

@ -71,10 +71,6 @@ public:
int GetUniqueId(); int GetUniqueId();
char* getName() const {
return name;
}
int getSystemId() { int getSystemId() {
return systemId; return systemId;
} }
@ -82,6 +78,10 @@ public:
void setSystemId(int systemId) { void setSystemId(int systemId) {
this->systemId = systemId; this->systemId = systemId;
} }
char* getName() const {
return name;
}
}; };
int gooo = 1; int gooo = 1;
@ -176,10 +176,6 @@ public:
int GetUniqueId(); int GetUniqueId();
char* getName() const {
return name;
}
int getSystemId() { int getSystemId() {
return systemId; return systemId;
} }
@ -187,6 +183,10 @@ public:
void setSystemId(int systemId) { void setSystemId(int systemId) {
this->systemId = systemId; this->systemId = systemId;
} }
char* getName() const {
return name;
}
}; };
} }
@ -246,7 +246,6 @@ public:
} }
}; };
int gooo = 1; int gooo = 1;
#endif /* A_H_ */ #endif /* A_H_ */
@ -278,10 +277,6 @@ public:
int GetUniqueId(); int GetUniqueId();
void setName(char* name) {
this->name = name;
}
int getSystemId() { int getSystemId() {
return systemId; return systemId;
} }
@ -289,8 +284,11 @@ public:
void setSystemId(int systemId) { void setSystemId(int systemId) {
this->systemId = systemId; this->systemId = systemId;
} }
};
void setName(char* name) {
this->name = name;
}
};
int gooo = 1; int gooo = 1;
@ -379,14 +377,6 @@ public:
int GetUniqueId(); int GetUniqueId();
char* getName() const {
return name;
}
void setName(char* name) {
this->name = name;
}
int getSystemId() { int getSystemId() {
return systemId; return systemId;
} }
@ -394,6 +384,14 @@ public:
void setSystemId(int systemId) { void setSystemId(int systemId) {
this->systemId = systemId; this->systemId = systemId;
} }
char* getName() const {
return name;
}
void setName(char* name) {
this->name = name;
}
}; };
int gooo = 1; int gooo = 1;
@ -463,6 +461,18 @@ public:
Person myFriend; Person myFriend;
Person(int socSecNo); // constructor
~Person(); // destructor
char* Name();
void Print();
int SocSecNo();
int GetUniqueId();
char* getName() const { char* getName() const {
return name; return name;
} }
@ -478,18 +488,6 @@ public:
void setSystemId(int systemId) { void setSystemId(int systemId) {
this->systemId = systemId; this->systemId = systemId;
} }
Person(int socSecNo); // constructor
~Person(); // destructor
char* Name();
void Print();
int SocSecNo();
int GetUniqueId();
}; };
int gooo = 1; int gooo = 1;
@ -567,7 +565,7 @@ GaS::Getters() {
GaS::~Getters() { GaS::~Getters() {
} }
//!Generate Getters and Setters no Methods //!Generate Getters and Setters no methods
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest //#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
//@.config //@.config
filename=A.h filename=A.h
@ -588,9 +586,6 @@ private:
#define A_H_ #define A_H_
class Person { class Person {
private:
int id;
public: public:
int getId() const { int getId() const {
return id; return id;
@ -599,15 +594,19 @@ public:
void setId(int id) { void setId(int id) {
this->id = id; this->id = id;
} }
private:
int id;
}; };
#endif /* A_H_ */ #endif /* A_H_ */
//!Generate Getters and Setters no Methods //!Generate Getters and Setters no methods ascending visibility order
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest //#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
//@.config //@.config
filename=A.h filename=A.h
getters=i getters=i
setters=i setters=i
ascendingVisibilityOrder=true
//@A.h //@A.h
/* /*
* test.h * test.h
@ -624,7 +623,6 @@ class test {
}; };
#endif /* TEST_H_ */ #endif /* TEST_H_ */
//= //=
/* /*
* test.h * test.h
@ -650,8 +648,7 @@ public:
}; };
#endif /* TEST_H_ */ #endif /* TEST_H_ */
//!Generate Getters and Setters no fields
//!Generate Getters and Setters no Fields
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest //#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
//@.config //@.config
filename=A.h filename=A.h
@ -759,8 +756,6 @@ public:
Person myFriend; Person myFriend;
const FullName& getName() const;
void setName(const FullName& name);
Person(int socSecNo); // constructor Person(int socSecNo); // constructor
~Person(); // destructor ~Person(); // destructor
@ -776,6 +771,8 @@ public:
int getSystemId(); int getSystemId();
void setSystemId(int systemId); void setSystemId(int systemId);
const FullName& getName() const;
void setName(const FullName& name);
}; };
int gooo = 1; int gooo = 1;
@ -886,7 +883,6 @@ public:
int SocSecNo(); int SocSecNo();
int GetUniqueId(); int GetUniqueId();
char* getName() const;
int getSystemId() { int getSystemId() {
return systemId; return systemId;
@ -895,6 +891,8 @@ public:
void setSystemId(int systemId) { void setSystemId(int systemId) {
this->systemId = systemId; this->systemId = systemId;
} }
char* getName() const;
}; };
} }
@ -1005,7 +1003,6 @@ public:
int SocSecNo(); int SocSecNo();
int GetUniqueId(); int GetUniqueId();
void setName(char* name);
int getSystemId() { int getSystemId() {
return systemId; return systemId;
@ -1014,6 +1011,8 @@ public:
void setSystemId(int systemId) { void setSystemId(int systemId) {
this->systemId = systemId; this->systemId = systemId;
} }
void setName(char* name);
}; };
int gooo = 1; int gooo = 1;
@ -1115,8 +1114,6 @@ public:
int SocSecNo(); int SocSecNo();
int GetUniqueId(); int GetUniqueId();
char* getName() const;
void setName(char* name);
int getSystemId() { int getSystemId() {
return systemId; return systemId;
@ -1125,6 +1122,9 @@ public:
void setSystemId(int systemId) { void setSystemId(int systemId) {
this->systemId = systemId; this->systemId = systemId;
} }
char* getName() const;
void setName(char* name);
}; };
int gooo = 1; int gooo = 1;
@ -1178,12 +1178,12 @@ private:
#define A_H_ #define A_H_
class Person { class Person {
private:
int id;
public: public:
int getId() const; int getId() const;
void setId(int id); void setId(int id);
private:
int id;
}; };
inline int Person::getId() const { inline int Person::getId() const {
@ -1228,13 +1228,14 @@ class test {
//comment1 //comment1
class test { class test {
int i; //comment2
char* b;
//comment3
public: public:
int getI() const; int getI() const;
void setI(int i); void setI(int i);
private:
int i; //comment2
char* b;
//comment3
}; };
inline int test::getI() const { inline int test::getI() const {
@ -1275,12 +1276,13 @@ class Test {
namespace foo { namespace foo {
class Test { class Test {
int testField;
void foo();
public: public:
int getTestField() const; int getTestField() const;
void setTestField(int testField); void setTestField(int testField);
private:
int testField;
void foo();
}; };
} // namespace foo } // namespace foo
@ -1334,11 +1336,12 @@ class Test {
#define TEST_H_ #define TEST_H_
class Test { class Test {
int testField;
public: public:
int getTestField() const; int getTestField() const;
void setTestField(int testField); void setTestField(int testField);
private:
int testField;
}; };
#endif #endif
@ -1374,11 +1377,12 @@ class Test {
#define TEST_H_ #define TEST_H_
class Test { class Test {
int testField;
public: public:
int getTestField() const; int getTestField() const;
void setTestField(int testField); void setTestField(int testField);
private:
int testField;
}; };
#endif #endif
@ -1464,13 +1468,13 @@ private:
#define A_H_ #define A_H_
class A { class A {
private:
int a[2];
public: public:
const int* getA() const { const int* getA() const {
return a; return a;
} }
private:
int a[2];
}; };
#endif /* A_H_ */ #endif /* A_H_ */
//!Bug 352258 - Avoiding reserved names //!Bug 352258 - Avoiding reserved names
@ -1493,9 +1497,6 @@ private:
#define A_H_ #define A_H_
class getClass { class getClass {
private:
int mClass;
public: public:
int getClass1() const { int getClass1() const {
return mClass; return mClass;
@ -1504,5 +1505,8 @@ public:
void setClass(int _class) { void setClass(int _class) {
mClass = _class; mClass = _class;
} }
private:
int mClass;
}; };
#endif /* A_H_ */ #endif /* A_H_ */

View file

@ -18,11 +18,14 @@ import java.util.Properties;
import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ltk.core.refactoring.Change; import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.RefactoringStatus; import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.cdt.core.model.CoreModel; import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.PreferenceConstants;
import org.eclipse.cdt.ui.tests.refactoring.RefactoringTest; import org.eclipse.cdt.ui.tests.refactoring.RefactoringTest;
import org.eclipse.cdt.ui.tests.refactoring.TestSourceFile; import org.eclipse.cdt.ui.tests.refactoring.TestSourceFile;
@ -41,6 +44,7 @@ public class GenerateGettersAndSettersTest extends RefactoringTest {
private String[] selectedSetters; private String[] selectedSetters;
private GenerateGettersAndSettersRefactoring refactoring; private GenerateGettersAndSettersRefactoring refactoring;
private boolean definitionSeparate; private boolean definitionSeparate;
private String ascendingVisibilityOrder;
/** /**
* @param name * @param name
@ -52,21 +56,30 @@ public class GenerateGettersAndSettersTest extends RefactoringTest {
@Override @Override
protected void runTest() throws Throwable { protected void runTest() throws Throwable {
IFile file = project.getFile(fileName); try {
ICElement element = CoreModel.getDefault().create(file); IFile file = project.getFile(fileName);
refactoring = new GenerateGettersAndSettersRefactoring(element, selection, cproject, astCache); ICElement element = CoreModel.getDefault().create(file);
RefactoringStatus initialConditions = refactoring.checkInitialConditions(NULL_PROGRESS_MONITOR); refactoring = new GenerateGettersAndSettersRefactoring(element, selection, cproject, astCache);
RefactoringStatus initialConditions = refactoring.checkInitialConditions(NULL_PROGRESS_MONITOR);
if (fatalError) {
assertConditionsFatalError(initialConditions); if (fatalError) {
return; assertConditionsFatalError(initialConditions);
} else { return;
assertConditionsOk(initialConditions); } else {
executeRefactoring(); assertConditionsOk(initialConditions);
executeRefactoring();
}
} finally {
IPreferenceStore store= CUIPlugin.getDefault().getPreferenceStore();
store.setToDefault(PreferenceConstants.CLASS_MEMBER_ASCENDING_VISIBILITY_ORDER);
} }
} }
private void executeRefactoring() throws CoreException, Exception { private void executeRefactoring() throws CoreException, Exception {
if (ascendingVisibilityOrder != null) {
IPreferenceStore store= CUIPlugin.getDefault().getPreferenceStore();
store.setValue(PreferenceConstants.CLASS_MEMBER_ASCENDING_VISIBILITY_ORDER, ascendingVisibilityOrder);
}
selectFields(); selectFields();
refactoring.getContext().setDefinitionSeparate(definitionSeparate); refactoring.getContext().setDefinitionSeparate(definitionSeparate);
RefactoringStatus finalConditions = refactoring.checkFinalConditions(NULL_PROGRESS_MONITOR); RefactoringStatus finalConditions = refactoring.checkFinalConditions(NULL_PROGRESS_MONITOR);
@ -102,6 +115,7 @@ public class GenerateGettersAndSettersTest extends RefactoringTest {
String getters = refactoringProperties.getProperty("getters", ""); //$NON-NLS-1$ //$NON-NLS-2$ String getters = refactoringProperties.getProperty("getters", ""); //$NON-NLS-1$ //$NON-NLS-2$
String setters = refactoringProperties.getProperty("setters", ""); //$NON-NLS-1$ //$NON-NLS-2$ String setters = refactoringProperties.getProperty("setters", ""); //$NON-NLS-1$ //$NON-NLS-2$
definitionSeparate = Boolean.valueOf(refactoringProperties.getProperty("definitionSeparate", "false")); definitionSeparate = Boolean.valueOf(refactoringProperties.getProperty("definitionSeparate", "false"));
ascendingVisibilityOrder = refactoringProperties.getProperty("ascendingVisibilityOrder", null);
selectedGetters = getters.split(","); selectedGetters = getters.split(",");
selectedSetters = setters.split(","); selectedSetters = setters.split(",");

View file

@ -8,12 +8,17 @@
* *
* Contributors: * Contributors:
* Institute for Software - initial API and implementation * Institute for Software - initial API and implementation
* Sergey Prigogin (Google)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring; package org.eclipse.cdt.internal.ui.refactoring;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.IPreferencesService;
import org.eclipse.osgi.util.NLS; import org.eclipse.osgi.util.NLS;
import org.eclipse.text.edits.TextEditGroup; import org.eclipse.text.edits.TextEditGroup;
@ -23,9 +28,14 @@ import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator; import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTVisibilityLabel; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTVisibilityLabel;
import org.eclipse.cdt.core.dom.rewrite.ASTRewrite; import org.eclipse.cdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.PreferenceConstants;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTVisibilityLabel; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTVisibilityLabel;
@ -58,7 +68,7 @@ public class AddDeclarationNodeToClassChange {
private AddDeclarationNodeToClassChange(ICPPASTCompositeTypeSpecifier classNode, private AddDeclarationNodeToClassChange(ICPPASTCompositeTypeSpecifier classNode,
VisibilityEnum visibility, List<IASTNode> nodesToAdd, ModificationCollector collector, VisibilityEnum visibility, List<IASTNode> nodesToAdd, ModificationCollector collector,
boolean isField) { boolean isField) {
this.nodesToAdd = nodesToAdd; this.nodesToAdd = new ArrayList<IASTNode>(nodesToAdd);
this.classNode = classNode; this.classNode = classNode;
this.visibility = visibility; this.visibility = visibility;
this.collector = collector; this.collector = collector;
@ -66,83 +76,65 @@ public class AddDeclarationNodeToClassChange {
} }
private void createRewrites(boolean isField) { private void createRewrites(boolean isField) {
int lastFunctionDeclaration = -1; VisibilityEnum defaultVisibility = classNode.getKey() == IASTCompositeTypeSpecifier.k_struct ?
int lastFieldDeclaration = -1; VisibilityEnum.v_public : VisibilityEnum.v_private;
VisibilityEnum currentVisibility = defaultVisibility;
boolean ascendingVisibilityOrder = isAscendingVisibilityOrder();
int lastFunctionIndex = -1;
int lastFieldIndex = -1;
int lastMatchingVisibilityIndex = -1;
int lastPrecedingVisibilityIndex = -1;
IASTDeclaration[] members = classNode.getMembers(); IASTDeclaration[] members = classNode.getMembers();
VisibilityEnum currentVisibility = classNode.getKey() == IASTCompositeTypeSpecifier.k_struct ?
VisibilityEnum.v_public : VisibilityEnum.v_private;
// Find the insert location by iterating over the elements of the class // Find the insert location by iterating over the elements of the class
// and remembering the last element with the matching visibility // and remembering the last element with the matching visibility and the last element
// with preceding visibility (according to the visibility order preference).
for (int i = 0; i < members.length; i++) { for (int i = 0; i < members.length; i++) {
IASTDeclaration declaration = members[i]; IASTDeclaration declaration = members[i];
if (declaration instanceof ICPPASTVisibilityLabel) { if (declaration instanceof ICPPASTVisibilityLabel) {
currentVisibility = VisibilityEnum.from((ICPPASTVisibilityLabel) declaration); currentVisibility = VisibilityEnum.from((ICPPASTVisibilityLabel) declaration);
} else if (declaration instanceof IASTSimpleDeclaration && currentVisibility.equals(visibility)) { }
IASTSimpleDeclaration simple = (IASTSimpleDeclaration) declaration; if (currentVisibility == visibility) {
IASTDeclarator[] declarators = simple.getDeclarators(); lastMatchingVisibilityIndex = i;
if (declarators.length > 0 && declarators[0] != null) { if (declaration instanceof IASTSimpleDeclaration) {
if (declarators[0] instanceof IASTFunctionDeclarator) { IASTDeclarator[] declarators = ((IASTSimpleDeclaration) declaration).getDeclarators();
lastFunctionDeclaration = i; if (declarators.length > 0 && declarators[0] != null) {
} else { if (declarators[0] instanceof IASTFunctionDeclarator) {
lastFieldDeclaration = i; lastFunctionIndex = i;
} else {
lastFieldIndex = i;
}
} }
} else if (declaration instanceof ICPPASTFunctionDefinition) {
lastFunctionIndex = i;
} }
} else if (currentVisibility.compareTo(visibility) < 0 == ascendingVisibilityOrder) {
lastPrecedingVisibilityIndex = i;
} }
} }
IASTDeclaration nextFunctionDeclaration = null; int index = isField && lastFieldIndex >= 0 || !isField && lastFunctionIndex < 0 ?
if (lastFunctionDeclaration < members.length - 1 && lastFunctionDeclaration >= 0) lastFieldIndex : lastFunctionIndex;
nextFunctionDeclaration = members[lastFunctionDeclaration + 1]; if (index < 0)
index = lastMatchingVisibilityIndex;
IASTDeclaration nextFieldDeclaration = null; if (index < 0)
if (lastFieldDeclaration < members.length - 1 && lastFieldDeclaration >= 0) index = lastPrecedingVisibilityIndex;
nextFieldDeclaration = members[lastFieldDeclaration + 1]; index++;
IASTNode nextNode = index < members.length ? members[index] : null;
createInsert(isField, nextFunctionDeclaration, nextFieldDeclaration, currentVisibility); if (lastMatchingVisibilityIndex < 0 &&
} !(index == 0 && classNode.getKey() == IASTCompositeTypeSpecifier.k_struct && visibility == defaultVisibility)) {
nodesToAdd.add(0, new CPPASTVisibilityLabel(visibility.getVisibilityLabelValue()));
private void createInsert(boolean isField, IASTDeclaration nextFunctionDeclaration, if (index == 0 && nextNode != null && !(nextNode instanceof ICPPASTVisibilityLabel)) {
IASTDeclaration nextFieldDeclaration, VisibilityEnum currentVisibility) { nodesToAdd.add(new CPPASTVisibilityLabel(defaultVisibility.getVisibilityLabelValue()));
if (isField) {
if (nextFieldDeclaration != null) {
insertBefore(nextFieldDeclaration);
} else if (nextFunctionDeclaration != null) {
insertBefore(nextFunctionDeclaration);
} else {
insertAtTheEnd(currentVisibility);
}
} else {
if (nextFunctionDeclaration != null) {
insertBefore(nextFunctionDeclaration);
} else if (nextFieldDeclaration != null) {
insertBefore(nextFieldDeclaration);
} else {
insertAtTheEnd(currentVisibility);
} }
} }
}
private void insertBefore(IASTNode nextNode) {
ASTRewrite rewrite = collector.rewriterForTranslationUnit(nextNode.getTranslationUnit());
for (IASTNode node : nodesToAdd) {
rewrite.insertBefore(nextNode.getParent(), nextNode, node, createEditDescription());
}
}
private void insertAtTheEnd(VisibilityEnum currentVisibility) {
ASTRewrite rewrite = collector.rewriterForTranslationUnit(classNode.getTranslationUnit()); ASTRewrite rewrite = collector.rewriterForTranslationUnit(classNode.getTranslationUnit());
if (!currentVisibility.equals(visibility)) {
ICPPASTVisibilityLabel label =
new CPPASTVisibilityLabel(visibility.getICPPASTVisiblityLabelVisibility());
rewrite.insertBefore(classNode, null, label, createEditDescription());
}
for (IASTNode node : nodesToAdd) { for (IASTNode node : nodesToAdd) {
rewrite.insertBefore(classNode, null, node, createEditDescription()); rewrite.insertBefore(classNode, nextNode, node, createEditDescription());
} }
} }
@ -150,4 +142,14 @@ public class AddDeclarationNodeToClassChange {
return new TextEditGroup(NLS.bind(Messages.AddDeclarationNodeToClassChange_AddDeclaration, return new TextEditGroup(NLS.bind(Messages.AddDeclarationNodeToClassChange_AddDeclaration,
classNode.getName())); classNode.getName()));
} }
private boolean isAscendingVisibilityOrder() {
IPreferencesService preferences = Platform.getPreferencesService();
IASTTranslationUnit ast = classNode.getTranslationUnit();
ITranslationUnit tu = ast.getOriginatingTranslationUnit();
IProject project = tu != null ? tu.getCProject().getProject() : null;
return preferences.getBoolean(CUIPlugin.PLUGIN_ID,
PreferenceConstants.CLASS_MEMBER_ASCENDING_VISIBILITY_ORDER, false,
PreferenceConstants.getPreferenceScopes(project));
}
} }

View file

@ -7,7 +7,7 @@
* http://www.eclipse.org/legal/epl-v10.html * http://www.eclipse.org/legal/epl-v10.html
* *
* Contributors: * Contributors:
* Institute for Software - initial API and implementation * Institute for Software - initial API and implementation
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.utils; package org.eclipse.cdt.internal.ui.refactoring.utils;
@ -17,22 +17,26 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBas
/** /**
* Enum that represents C++ visibilities, with methods to convert to * Enum that represents C++ visibilities, with methods to convert to
* and from ICPPASTVisiblityLabel. * and from ICPPASTVisiblityLabel.
*
*/ */
public enum VisibilityEnum { public enum VisibilityEnum {
// The values are ordered by increasing visibility.
v_public(Messages.VisibilityEnum_public), v_private(Messages.VisibilityEnum_private, ICPPASTVisibilityLabel.v_private, ICPPASTBaseSpecifier.v_private),
v_protected(Messages.VisibilityEnum_protected), v_protected(Messages.VisibilityEnum_protected, ICPPASTVisibilityLabel.v_protected, ICPPASTBaseSpecifier.v_protected),
v_private(Messages.VisibilityEnum_private); v_public(Messages.VisibilityEnum_public, ICPPASTVisibilityLabel.v_public, ICPPASTBaseSpecifier.v_public);
private final String stringRepresentation; private final String stringRepresentation;
private final int visibilityLabelValue;
private final int baseSpecifierValue;
VisibilityEnum(String stringRepresentation) { private VisibilityEnum(String stringRepresentation, int visibilityLabelValue,
int baseSpecifierValue) {
this.stringRepresentation = stringRepresentation; this.stringRepresentation = stringRepresentation;
this.visibilityLabelValue = visibilityLabelValue;
this.baseSpecifierValue = baseSpecifierValue;
} }
public static VisibilityEnum from(ICPPASTVisibilityLabel visibility) { public static VisibilityEnum from(ICPPASTVisibilityLabel visibility) {
switch(visibility.getVisibility()){ switch (visibility.getVisibility()) {
case ICPPASTVisibilityLabel.v_private: case ICPPASTVisibilityLabel.v_private:
return VisibilityEnum.v_private; return VisibilityEnum.v_private;
case ICPPASTVisibilityLabel.v_protected: case ICPPASTVisibilityLabel.v_protected:
@ -43,36 +47,20 @@ public enum VisibilityEnum {
return null; return null;
} }
public int getASTBaseSpecifierVisibility() { public int getBaseSpecifierValue() {
switch (this) { return baseSpecifierValue;
case v_private:
return ICPPASTBaseSpecifier.v_private;
case v_protected:
return ICPPASTBaseSpecifier.v_protected;
case v_public:
return ICPPASTBaseSpecifier.v_public;
}
return 0;
} }
public int getICPPASTVisiblityLabelVisibility() { public int getVisibilityLabelValue() {
switch (this) { return visibilityLabelValue;
case v_private:
return ICPPASTVisibilityLabel.v_private;
case v_protected:
return ICPPASTVisibilityLabel.v_protected;
case v_public:
return ICPPASTVisibilityLabel.v_public;
}
return 0;
} }
public static VisibilityEnum getEnumForStringRepresentation(String visibility){ public static VisibilityEnum getEnumForStringRepresentation(String visibility) {
if( VisibilityEnum.v_private.toString().equals( visibility ) ) { if (VisibilityEnum.v_private.toString().equals(visibility)) {
return VisibilityEnum.v_private; return VisibilityEnum.v_private;
}else if( VisibilityEnum.v_protected.toString().equals( visibility ) ) { } else if (VisibilityEnum.v_protected.toString().equals(visibility)) {
return VisibilityEnum.v_protected; return VisibilityEnum.v_protected;
}else if ( VisibilityEnum.v_public.toString().equals( visibility ) ) { } else if (VisibilityEnum.v_public.toString().equals(visibility)) {
return VisibilityEnum.v_public; return VisibilityEnum.v_public;
} }
return null; return null;

View file

@ -17,9 +17,12 @@ package org.eclipse.cdt.ui;
import java.util.Locale; import java.util.Locale;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope; import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.preferences.ConfigurationScope;
import org.eclipse.core.runtime.preferences.DefaultScope; import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences; import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.core.runtime.preferences.InstanceScope; import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.action.Action; import org.eclipse.jface.action.Action;
import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.jface.preference.IPreferenceStore;
@ -597,7 +600,8 @@ public class PreferenceConstants {
public final static String EDITOR_TEXT_FONT= "org.eclipse.cdt.ui.editors.textfont"; //$NON-NLS-1$ public final static String EDITOR_TEXT_FONT= "org.eclipse.cdt.ui.editors.textfont"; //$NON-NLS-1$
/** /**
* A named preference that controls whether the cview's selection is linked to the active editor. * A named preference that controls whether the cview's selection is linked to the active
* editor.
* <p> * <p>
* Value is of type <code>Boolean</code>. * Value is of type <code>Boolean</code>.
* </p> * </p>
@ -605,7 +609,8 @@ public class PreferenceConstants {
public static final String PREF_LINK_TO_EDITOR= "org.eclipse.cdt.ui.editor.linkToEditor"; //$NON-NLS-1$ public static final String PREF_LINK_TO_EDITOR= "org.eclipse.cdt.ui.editor.linkToEditor"; //$NON-NLS-1$
/** /**
* A named preference that speficies whether children of a translation unit are shown in the CView. * A named preference that specifies whether children of a translation unit are shown in
* the CView.
* <p> * <p>
* Value is of type <code>Boolean</code>. * Value is of type <code>Boolean</code>.
* </p> * </p>
@ -613,7 +618,8 @@ public class PreferenceConstants {
public static final String PREF_SHOW_CU_CHILDREN= "org.eclipse.cdt.ui.editor.CUChildren"; //$NON-NLS-1$ public static final String PREF_SHOW_CU_CHILDREN= "org.eclipse.cdt.ui.editor.CUChildren"; //$NON-NLS-1$
/** /**
* A named preference that speficies whether to use the parser's structural mode to build the CModel. * A named preference that speficies whether to use the parser's structural mode to build
* the CModel.
* <p> * <p>
* Value is of type <code>Boolean</code>. * Value is of type <code>Boolean</code>.
* </p> * </p>
@ -621,7 +627,8 @@ public class PreferenceConstants {
public static final String PREF_USE_STRUCTURAL_PARSE_MODE= "org.eclipse.cdt.ui.editor.UseStructuralMode"; //$NON-NLS-1$ public static final String PREF_USE_STRUCTURAL_PARSE_MODE= "org.eclipse.cdt.ui.editor.UseStructuralMode"; //$NON-NLS-1$
/** /**
* A named preference that controls if segmented view (show selected element only) is turned on or off. * A named preference that controls if segmented view (show selected element only) is turned
* on or off.
* <p> * <p>
* Value is of type <code>Boolean</code>. * Value is of type <code>Boolean</code>.
* </p> * </p>
@ -818,8 +825,8 @@ public class PreferenceConstants {
public static final String OUTLINE_LINK_TO_EDITOR = "org.eclipse.cdt.ui.outline.linktoeditor"; //$NON-NLS-1$ public static final String OUTLINE_LINK_TO_EDITOR = "org.eclipse.cdt.ui.outline.linktoeditor"; //$NON-NLS-1$
/** /**
* A named preference that controls whether include directives should be grouped in the * A named preference that controls whether include directives should be grouped in
* C/C++ Projects view and the Project Explorer view. * the C/C++ Projects view and the Project Explorer view.
* <p> * <p>
* Value is of type <code>Boolean</code>. * Value is of type <code>Boolean</code>.
* </p> * </p>
@ -827,8 +834,8 @@ public class PreferenceConstants {
public static final String CVIEW_GROUP_INCLUDES= "org.eclipse.cdt.ui.cview.groupincludes"; //$NON-NLS-1$ public static final String CVIEW_GROUP_INCLUDES= "org.eclipse.cdt.ui.cview.groupincludes"; //$NON-NLS-1$
/** /**
* A named preference that controls whether macro defintions should be grouped in the * A named preference that controls whether macro definitions should be grouped in
* C/C++ Projects view and the Project Explorer view. * the C/C++ Projects view and the Project Explorer view.
* <p> * <p>
* Value is of type <code>Boolean</code>. * Value is of type <code>Boolean</code>.
* </p> * </p>
@ -837,8 +844,8 @@ public class PreferenceConstants {
public static final String CVIEW_GROUP_MACROS= "org.eclipse.cdt.ui.cview.groupmacros"; //$NON-NLS-1$ public static final String CVIEW_GROUP_MACROS= "org.eclipse.cdt.ui.cview.groupmacros"; //$NON-NLS-1$
/** /**
* A named preference that controls whether header and source files should be separated in the * A named preference that controls whether header and source files should be separated in
* C/C++ Projects view and the Project Explorer view. * the C/C++ Projects view and the Project Explorer view.
* <p> * <p>
* Value is of type <code>Boolean</code>. * Value is of type <code>Boolean</code>.
* </p> * </p>
@ -1878,6 +1885,18 @@ public class PreferenceConstants {
*/ */
public static final int NAME_STYLE_CAPITALIZATION_LOWER_CAMEL_CASE = 4; public static final int NAME_STYLE_CAPITALIZATION_LOWER_CAMEL_CASE = 4;
/**
* A named preference that controls order of private/protected/public class members in generated
* code.
* <p>
* Value is of type <code>Boolean</code>. The <code>true</code> value means that private members
* are before public ones. The default is to put public members before private ones.
*
* @since 5.4
*/
public static final String CLASS_MEMBER_ASCENDING_VISIBILITY_ORDER = "class_member_ascending_visibility_order"; //$NON-NLS-1$
/** /**
* Returns the CDT-UI preference store. * Returns the CDT-UI preference store.
* *
@ -2089,9 +2108,9 @@ public class PreferenceConstants {
/** /**
* Returns the node in the preference in the given context. * Returns the node in the preference in the given context.
* @param key The preference key. * @param key The preference key.
* @param project The current context or <code>null</code> if no context is available and the * @param project The current context or <code>null</code> if no context is available and
* workspace setting should be taken. Note that passing <code>null</code> should * the workspace setting should be taken. Note that passing <code>null</code> should
* be avoided. * be avoided.
* @return Returns the node matching the given context. * @return Returns the node matching the given context.
*/ */
private static IEclipsePreferences getPreferenceNode(String key, ICProject project) { private static IEclipsePreferences getPreferenceNode(String key, ICProject project) {
@ -2108,15 +2127,20 @@ public class PreferenceConstants {
return node; return node;
} }
node = ConfigurationScope.INSTANCE.getNode(CUIPlugin.PLUGIN_ID);
if (node.get(key, null) != null) {
return node;
}
return DefaultScope.INSTANCE.getNode(CUIPlugin.PLUGIN_ID); return DefaultScope.INSTANCE.getNode(CUIPlugin.PLUGIN_ID);
} }
/** /**
* Returns the string value for the given key in the given context. * Returns the string value for the given key in the given context.
* @param key The preference key * @param key The preference key
* @param project The current context or <code>null</code> if no context is available and the * @param project The current context or <code>null</code> if no context is available and
* workspace setting should be taken. Note that passing <code>null</code> should * the workspace setting should be taken. Note that passing <code>null</code> should
* be avoided. * be avoided.
* @return Returns the current value for the string. * @return Returns the current value for the string.
* @since 5.0 * @since 5.0
*/ */
@ -2127,9 +2151,9 @@ public class PreferenceConstants {
/** /**
* Returns the integer value for the given key in the given context. * Returns the integer value for the given key in the given context.
* @param key The preference key * @param key The preference key
* @param project The current context or <code>null</code> if no context is available and the * @param project The current context or <code>null</code> if no context is available and
* workspace setting should be taken. Note that passing <code>null</code> should * the workspace setting should be taken. Note that passing <code>null</code> should
* be avoided. * be avoided.
* @param defaultValue The default value if not specified in the preferences. * @param defaultValue The default value if not specified in the preferences.
* @return Returns the current value for the string. * @return Returns the current value for the string.
* @since 5.1 * @since 5.1
@ -2141,9 +2165,9 @@ public class PreferenceConstants {
/** /**
* Returns the boolean value for the given key in the given context. * Returns the boolean value for the given key in the given context.
* @param key The preference key * @param key The preference key
* @param project The current context or <code>null</code> if no context is available and the * @param project The current context or <code>null</code> if no context is available and
* workspace setting should be taken. Note that passing <code>null</code> should * the workspace setting should be taken. Note that passing <code>null</code> should
* be avoided. * be avoided.
* @param defaultValue The default value if not specified in the preferences. * @param defaultValue The default value if not specified in the preferences.
* @return Returns the current value for the string. * @return Returns the current value for the string.
* @since 5.1 * @since 5.1
@ -2152,4 +2176,16 @@ public class PreferenceConstants {
return getPreferenceNode(key, project).getBoolean(key, defaultValue); return getPreferenceNode(key, project).getBoolean(key, defaultValue);
} }
/**
* Returns the scopes for preference lookup.
*
* @param project a project or <code>null</code>
* @return the scopes for preference lookup.
* @since 5.4
*/
public static IScopeContext[] getPreferenceScopes(IProject project) {
return project != null ?
new IScopeContext[] { new ProjectScope(project), InstanceScope.INSTANCE, ConfigurationScope.INSTANCE, DefaultScope.INSTANCE } :
new IScopeContext[] { InstanceScope.INSTANCE, ConfigurationScope.INSTANCE, DefaultScope.INSTANCE };
}
} }