mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-26 02:15:31 +02:00
Bug 551761 - GetterSetterRefactoring single char prefix
Instead of dropping single character prefixes in getter/setter name generation, we just drop the class field prefix as defined in the coding style. Change-Id: I3c3853b1b0f675ad1802b1ebbddd68dc8c0c5b33 Signed-off-by: Hannes Vogt <hannes@havogt.de> Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
This commit is contained in:
parent
aa08b85b6f
commit
a4b27b3638
3 changed files with 119 additions and 43 deletions
|
@ -20,8 +20,11 @@ import org.eclipse.cdt.internal.ui.refactoring.CRefactoring;
|
|||
import org.eclipse.cdt.internal.ui.refactoring.gettersandsetters.AccessorDescriptor.AccessorKind;
|
||||
import org.eclipse.cdt.internal.ui.refactoring.gettersandsetters.GenerateGettersAndSettersRefactoring;
|
||||
import org.eclipse.cdt.internal.ui.refactoring.gettersandsetters.GetterSetterContext;
|
||||
import org.eclipse.cdt.ui.CUIPlugin;
|
||||
import org.eclipse.cdt.ui.PreferenceConstants;
|
||||
import org.eclipse.cdt.ui.tests.refactoring.RefactoringTestBase;
|
||||
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
|
||||
import org.eclipse.core.runtime.preferences.InstanceScope;
|
||||
|
||||
import junit.framework.Test;
|
||||
|
||||
|
@ -1576,7 +1579,7 @@ public class GenerateGettersAndSettersTest extends RefactoringTestBase {
|
|||
//
|
||||
//class getClass {
|
||||
//private:
|
||||
// int /*$*/mClass/*$$*/;
|
||||
// int /*$*/class_/*$$*/;
|
||||
//};
|
||||
//#endif /* A_H_ */
|
||||
//====================
|
||||
|
@ -1586,20 +1589,20 @@ public class GenerateGettersAndSettersTest extends RefactoringTestBase {
|
|||
//class getClass {
|
||||
//public:
|
||||
// int getClass1() const {
|
||||
// return mClass;
|
||||
// return class_;
|
||||
// }
|
||||
//
|
||||
// void setClass(int clazz) {
|
||||
// mClass = clazz;
|
||||
// class_ = clazz;
|
||||
// }
|
||||
//
|
||||
//private:
|
||||
// int mClass;
|
||||
// int class_;
|
||||
//};
|
||||
//#endif /* A_H_ */
|
||||
public void testAvoidingReservedNames_Bug352258() throws Exception {
|
||||
selectedGetters = new String[] { "mClass" };
|
||||
selectedSetters = new String[] { "mClass" };
|
||||
selectedGetters = new String[] { "class_" };
|
||||
selectedSetters = new String[] { "class_" };
|
||||
assertRefactoringSuccess();
|
||||
}
|
||||
|
||||
|
@ -1670,4 +1673,88 @@ public class GenerateGettersAndSettersTest extends RefactoringTestBase {
|
|||
selectedGetters = new String[] { "a" };
|
||||
assertRefactoringSuccess();
|
||||
}
|
||||
|
||||
//Bug551761.h
|
||||
//#ifndef BUG551761_H_
|
||||
//#define BUG551761_H_
|
||||
//
|
||||
//class Bug551761 {
|
||||
//private:
|
||||
// int /*$*/aVar/*$$*/;
|
||||
//};
|
||||
//
|
||||
//#endif /* BUG551761_H_ */
|
||||
//
|
||||
//====================
|
||||
//#ifndef BUG551761_H_
|
||||
//#define BUG551761_H_
|
||||
//
|
||||
//class Bug551761 {
|
||||
//public:
|
||||
// int getAVar() const {
|
||||
// return aVar;
|
||||
// }
|
||||
//
|
||||
// void setAVar(int aVar) {
|
||||
// this->aVar = aVar;
|
||||
// }
|
||||
//
|
||||
//private:
|
||||
// int aVar;
|
||||
//};
|
||||
//
|
||||
//#endif /* BUG551761_H_ */
|
||||
//
|
||||
public void testSingleLetterPrefix_Bug551761() throws Exception {
|
||||
selectedGetters = new String[] { "aVar" };
|
||||
selectedSetters = new String[] { "aVar" };
|
||||
assertRefactoringSuccess();
|
||||
}
|
||||
|
||||
//Bug551761b.h
|
||||
//#ifndef BUG551761B_H_
|
||||
//#define BUG551761B_H_
|
||||
//
|
||||
//class Bug551761b {
|
||||
//private:
|
||||
// int /*$*/m_member/*$$*/;
|
||||
//};
|
||||
//
|
||||
//#endif /* BUG551761B_H_ */
|
||||
//
|
||||
//====================
|
||||
//#ifndef BUG551761B_H_
|
||||
//#define BUG551761B_H_
|
||||
//
|
||||
//class Bug551761b {
|
||||
//public:
|
||||
// int getMember() const {
|
||||
// return m_member;
|
||||
// }
|
||||
//
|
||||
// void setMember(int member) {
|
||||
// m_member = member;
|
||||
// }
|
||||
//
|
||||
//private:
|
||||
// int m_member;
|
||||
//};
|
||||
//
|
||||
//#endif /* BUG551761B_H_ */
|
||||
//
|
||||
public void testSingleLetterPrefix_Bug551761b() throws Exception {
|
||||
IEclipsePreferences node = InstanceScope.INSTANCE.getNode(CUIPlugin.PLUGIN_ID);
|
||||
String oldValue = node.get(PreferenceConstants.NAME_STYLE_FIELD_PREFIX, null);
|
||||
try {
|
||||
node.put(PreferenceConstants.NAME_STYLE_FIELD_PREFIX, "m");
|
||||
selectedGetters = new String[] { "m_member" };
|
||||
selectedSetters = new String[] { "m_member" };
|
||||
assertRefactoringSuccess();
|
||||
} finally {
|
||||
if (oldValue == null)
|
||||
node.remove(PreferenceConstants.NAME_STYLE_FIELD_PREFIX);
|
||||
else
|
||||
node.put(PreferenceConstants.NAME_STYLE_FIELD_PREFIX, oldValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,10 @@ import static org.eclipse.cdt.internal.corext.codemanipulation.StubUtility.trimF
|
|||
import static org.eclipse.cdt.internal.ui.util.NameComposer.createByExample;
|
||||
|
||||
import org.eclipse.cdt.internal.ui.util.NameComposer;
|
||||
import org.eclipse.cdt.ui.CUIPlugin;
|
||||
import org.eclipse.cdt.ui.PreferenceConstants;
|
||||
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
|
||||
import org.eclipse.core.runtime.preferences.InstanceScope;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
|
@ -68,11 +71,7 @@ public class NameComposerTest extends TestCase {
|
|||
public void testTrimFieldName() {
|
||||
assertEquals("f", trimFieldName("f_"));
|
||||
assertEquals("F", trimFieldName("F_"));
|
||||
assertEquals("oo", trimFieldName("F_oo"));
|
||||
assertEquals("o", trimFieldName("f_o"));
|
||||
|
||||
assertEquals("M", trimFieldName("a_M_"));
|
||||
assertEquals("bs", trimFieldName("a_bs_"));
|
||||
assertEquals("foo_bar", trimFieldName("foo_bar"));
|
||||
assertEquals("foo_bar", trimFieldName("foo_bar_"));
|
||||
|
||||
|
@ -80,15 +79,9 @@ public class NameComposerTest extends TestCase {
|
|||
|
||||
assertEquals("foo", trimFieldName("foo"));
|
||||
assertEquals("foo", trimFieldName("_foo"));
|
||||
assertEquals("bar", trimFieldName("_f_bar"));
|
||||
|
||||
assertEquals("f", trimFieldName("f__"));
|
||||
assertEquals("f", trimFieldName("__f"));
|
||||
assertEquals("O__b", trimFieldName("fO__b"));
|
||||
assertEquals("Oo", trimFieldName("fOo"));
|
||||
assertEquals("O", trimFieldName("fO"));
|
||||
assertEquals("MyStatic", trimFieldName("sMyStatic"));
|
||||
assertEquals("MyMember", trimFieldName("mMyMember"));
|
||||
|
||||
assertEquals("8", trimFieldName("_8"));
|
||||
|
||||
|
@ -100,8 +93,18 @@ public class NameComposerTest extends TestCase {
|
|||
assertEquals("Id", trimFieldName("Id"));
|
||||
assertEquals("ID", trimFieldName("ID"));
|
||||
assertEquals("IDS", trimFieldName("IDS"));
|
||||
assertEquals("ID", trimFieldName("bID"));
|
||||
assertEquals("Id", trimFieldName("MId"));
|
||||
assertEquals("IdA", trimFieldName("IdA"));
|
||||
|
||||
IEclipsePreferences node = InstanceScope.INSTANCE.getNode(CUIPlugin.PLUGIN_ID);
|
||||
String oldValue = node.get(PreferenceConstants.NAME_STYLE_FIELD_PREFIX, null);
|
||||
try {
|
||||
node.put(PreferenceConstants.NAME_STYLE_FIELD_PREFIX, "m");
|
||||
assertEquals("MyMember", trimFieldName("mMyMember"));
|
||||
} finally {
|
||||
if (oldValue == null)
|
||||
node.remove(PreferenceConstants.NAME_STYLE_FIELD_PREFIX);
|
||||
else
|
||||
node.put(PreferenceConstants.NAME_STYLE_FIELD_PREFIX, oldValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,7 +41,6 @@ import org.eclipse.cdt.internal.corext.template.c.CodeTemplateContextType;
|
|||
import org.eclipse.cdt.internal.corext.template.c.FileTemplateContext;
|
||||
import org.eclipse.cdt.internal.corext.template.c.FileTemplateContextType;
|
||||
import org.eclipse.cdt.internal.corext.util.Strings;
|
||||
import org.eclipse.cdt.internal.ui.text.CBreakIterator;
|
||||
import org.eclipse.cdt.internal.ui.util.NameComposer;
|
||||
import org.eclipse.cdt.internal.ui.viewsupport.ProjectTemplateStore;
|
||||
import org.eclipse.cdt.ui.CUIPlugin;
|
||||
|
@ -74,8 +73,6 @@ import org.eclipse.text.edits.MalformedTreeException;
|
|||
import org.eclipse.text.edits.MultiTextEdit;
|
||||
import org.eclipse.text.templates.TemplatePersistenceData;
|
||||
|
||||
import com.ibm.icu.text.BreakIterator;
|
||||
|
||||
public class StubUtility {
|
||||
private static final String[] EMPTY = {};
|
||||
|
||||
|
@ -935,39 +932,28 @@ public class StubUtility {
|
|||
|
||||
/**
|
||||
* Returns the trimmed field name. Leading and trailing non-alphanumeric characters are trimmed.
|
||||
* If the first word of the name consists of a single letter and the name contains more than
|
||||
* one word, the first word is removed.
|
||||
* If the field name starts with the prefix defined in the coding style, the prefix is removed.
|
||||
*
|
||||
* @param fieldName a field name to trim
|
||||
* @return the trimmed field name
|
||||
*/
|
||||
public static String trimFieldName(String fieldName) {
|
||||
CBreakIterator iterator = new CBreakIterator();
|
||||
iterator.setText(fieldName);
|
||||
IPreferencesService preferences = Platform.getPreferencesService();
|
||||
String prefix = preferences.getString(CUIPlugin.PLUGIN_ID, PreferenceConstants.NAME_STYLE_FIELD_PREFIX, "", //$NON-NLS-1$
|
||||
null);
|
||||
if (fieldName.startsWith(prefix))
|
||||
fieldName = fieldName.substring(prefix.length());
|
||||
|
||||
int firstWordStart = -1;
|
||||
int firstWordEnd = -1;
|
||||
int secondWordStart = -1;
|
||||
int lastWordEnd = -1;
|
||||
int end;
|
||||
for (int start = iterator.first(); (end = iterator.next()) != BreakIterator.DONE; start = end) {
|
||||
if (Character.isLetterOrDigit(fieldName.charAt(start))) {
|
||||
int pos = end;
|
||||
while (--pos >= start && !Character.isLetterOrDigit(fieldName.charAt(pos))) {
|
||||
}
|
||||
lastWordEnd = pos + 1;
|
||||
for (int i = 0; i < fieldName.length(); ++i) {
|
||||
if (Character.isLetterOrDigit(fieldName.charAt(i))) {
|
||||
lastWordEnd = i + 1;
|
||||
if (firstWordStart < 0) {
|
||||
firstWordStart = start;
|
||||
firstWordEnd = lastWordEnd;
|
||||
} else if (secondWordStart < 0) {
|
||||
secondWordStart = start;
|
||||
firstWordStart = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Skip the first word if it consists of a single letter and the name contains more than
|
||||
// one word.
|
||||
if (firstWordStart >= 0 && firstWordStart + 1 == firstWordEnd && secondWordStart >= 0) {
|
||||
firstWordStart = secondWordStart;
|
||||
}
|
||||
if (firstWordStart < 0) {
|
||||
return fieldName;
|
||||
} else {
|
||||
|
|
Loading…
Add table
Reference in a new issue