1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Bug 352257 - Getter and Setter in Name Style preview can differ from

generated code
This commit is contained in:
Marc-Andre Laperle 2011-07-19 12:26:52 -04:00
parent 11963d3ec5
commit 0c0e19f36e
6 changed files with 98 additions and 109 deletions

View file

@ -1,58 +0,0 @@
/*******************************************************************************
* Copyright (c) 2010 Marc-Andre Laperle 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:
* Marc-Andre Laperle - Initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.ui.tests.refactoring.utils;
import junit.framework.TestCase;
import org.eclipse.cdt.internal.ui.refactoring.gettersandsetters.GetterSetterNameGenerator;
public class AccessorNameGeneratorTest extends TestCase {
public void testTrimFieldName() {
assertEquals("f", GetterSetterNameGenerator.trimFieldName("f_"));
assertEquals("F", GetterSetterNameGenerator.trimFieldName("F_"));
assertEquals("oo", GetterSetterNameGenerator.trimFieldName("F_oo"));
assertEquals("o", GetterSetterNameGenerator.trimFieldName("f_o"));
assertEquals("M", GetterSetterNameGenerator.trimFieldName("a_M_"));
assertEquals("bs", GetterSetterNameGenerator.trimFieldName("a_bs_"));
assertEquals("foo_bar", GetterSetterNameGenerator.trimFieldName("foo_bar"));
assertEquals("foo_bar", GetterSetterNameGenerator.trimFieldName("foo_bar_"));
assertEquals("foo_b", GetterSetterNameGenerator.trimFieldName("foo_b_"));
assertEquals("foo", GetterSetterNameGenerator.trimFieldName("foo"));
assertEquals("foo", GetterSetterNameGenerator.trimFieldName("_foo"));
assertEquals("bar", GetterSetterNameGenerator.trimFieldName("_f_bar"));
assertEquals("f", GetterSetterNameGenerator.trimFieldName("f__"));
assertEquals("f", GetterSetterNameGenerator.trimFieldName("__f"));
assertEquals("O__b", GetterSetterNameGenerator.trimFieldName("fO__b"));
assertEquals("Oo", GetterSetterNameGenerator.trimFieldName("fOo"));
assertEquals("O", GetterSetterNameGenerator.trimFieldName("fO"));
assertEquals("MyStatic", GetterSetterNameGenerator.trimFieldName("sMyStatic"));
assertEquals("MyMember", GetterSetterNameGenerator.trimFieldName("mMyMember"));
assertEquals("8", GetterSetterNameGenerator.trimFieldName("_8"));
assertEquals("8bar", GetterSetterNameGenerator.trimFieldName("_8bar_"));
assertEquals("8bar_8", GetterSetterNameGenerator.trimFieldName("_8bar_8"));
assertEquals("8bAr", GetterSetterNameGenerator.trimFieldName("_8bAr"));
assertEquals("b8Ar", GetterSetterNameGenerator.trimFieldName("_b8Ar"));
assertEquals("Id", GetterSetterNameGenerator.trimFieldName("Id"));
assertEquals("ID", GetterSetterNameGenerator.trimFieldName("ID"));
assertEquals("IDS", GetterSetterNameGenerator.trimFieldName("IDS"));
assertEquals("ID", GetterSetterNameGenerator.trimFieldName("bID"));
assertEquals("Id", GetterSetterNameGenerator.trimFieldName("MId"));
assertEquals("IdA", GetterSetterNameGenerator.trimFieldName("IdA"));
}
}

View file

@ -30,7 +30,6 @@ public class IdentifierHelperTest extends TestSuite {
suite.addTest(new EmptyCaseTest()); suite.addTest(new EmptyCaseTest());
suite.addTest(new IllegalCharCaseTest()); suite.addTest(new IllegalCharCaseTest());
suite.addTest(new KeywordCaseTest()); suite.addTest(new KeywordCaseTest());
suite.addTestSuite(AccessorNameGeneratorTest.class);
return suite; return suite;
} }
} }

View file

@ -23,7 +23,7 @@ public class NameComposerTest extends TestCase {
private static final int CAPITALIZATION_CAMEL_CASE = PreferenceConstants.NAME_STYLE_CAPITALIZATION_CAMEL_CASE; private static final int CAPITALIZATION_CAMEL_CASE = PreferenceConstants.NAME_STYLE_CAPITALIZATION_CAMEL_CASE;
private static final int CAPITALIZATION_LOWER_CAMEL_CASE = PreferenceConstants.NAME_STYLE_CAPITALIZATION_LOWER_CAMEL_CASE; private static final int CAPITALIZATION_LOWER_CAMEL_CASE = PreferenceConstants.NAME_STYLE_CAPITALIZATION_LOWER_CAMEL_CASE;
public void testTrimFieldName() { public void testCompose() {
NameComposer composer = new NameComposer(CAPITALIZATION_ORIGINAL, "", "", ".h"); NameComposer composer = new NameComposer(CAPITALIZATION_ORIGINAL, "", "", ".h");
assertEquals("MyClass.h", composer.compose("MyClass")); assertEquals("MyClass.h", composer.compose("MyClass"));
composer = new NameComposer(CAPITALIZATION_LOWER_CASE, "-", "", ".cc"); composer = new NameComposer(CAPITALIZATION_LOWER_CASE, "-", "", ".cc");
@ -44,4 +44,44 @@ public class NameComposerTest extends TestCase {
composer = new NameComposer(CAPITALIZATION_ORIGINAL, "_", "", ""); composer = new NameComposer(CAPITALIZATION_ORIGINAL, "_", "", "");
assertEquals("RGB_Value", composer.compose("RGBValue")); assertEquals("RGB_Value", composer.compose("RGBValue"));
} }
public void testTrimFieldName() {
assertEquals("f", NameComposer.trimFieldName("f_"));
assertEquals("F", NameComposer.trimFieldName("F_"));
assertEquals("oo", NameComposer.trimFieldName("F_oo"));
assertEquals("o", NameComposer.trimFieldName("f_o"));
assertEquals("M", NameComposer.trimFieldName("a_M_"));
assertEquals("bs", NameComposer.trimFieldName("a_bs_"));
assertEquals("foo_bar", NameComposer.trimFieldName("foo_bar"));
assertEquals("foo_bar", NameComposer.trimFieldName("foo_bar_"));
assertEquals("foo_b", NameComposer.trimFieldName("foo_b_"));
assertEquals("foo", NameComposer.trimFieldName("foo"));
assertEquals("foo", NameComposer.trimFieldName("_foo"));
assertEquals("bar", NameComposer.trimFieldName("_f_bar"));
assertEquals("f", NameComposer.trimFieldName("f__"));
assertEquals("f", NameComposer.trimFieldName("__f"));
assertEquals("O__b", NameComposer.trimFieldName("fO__b"));
assertEquals("Oo", NameComposer.trimFieldName("fOo"));
assertEquals("O", NameComposer.trimFieldName("fO"));
assertEquals("MyStatic", NameComposer.trimFieldName("sMyStatic"));
assertEquals("MyMember", NameComposer.trimFieldName("mMyMember"));
assertEquals("8", NameComposer.trimFieldName("_8"));
assertEquals("8bar", NameComposer.trimFieldName("_8bar_"));
assertEquals("8bar_8", NameComposer.trimFieldName("_8bar_8"));
assertEquals("8bAr", NameComposer.trimFieldName("_8bAr"));
assertEquals("b8Ar", NameComposer.trimFieldName("_b8Ar"));
assertEquals("Id", NameComposer.trimFieldName("Id"));
assertEquals("ID", NameComposer.trimFieldName("ID"));
assertEquals("IDS", NameComposer.trimFieldName("IDS"));
assertEquals("ID", NameComposer.trimFieldName("bID"));
assertEquals("Id", NameComposer.trimFieldName("MId"));
assertEquals("IdA", NameComposer.trimFieldName("IdA"));
}
} }

View file

@ -191,7 +191,8 @@ public class NameStyleBlock extends OptionsConfigurationBlock {
.setAlternativePrefixKey(KEY_GETTER_PREFIX_FOR_BOOLEAN) .setAlternativePrefixKey(KEY_GETTER_PREFIX_FOR_BOOLEAN)
.setSuffixKey(KEY_GETTER_SUFFIX) .setSuffixKey(KEY_GETTER_SUFFIX)
.setSeedNameGenerator(fieldCategory) .setSeedNameGenerator(fieldCategory)
.setNameValidator(IDENTIFIER_VALIDATOR); .setNameValidator(IDENTIFIER_VALIDATOR)
.setTrimFieldName(true);
new Category(PreferencesMessages.NameStyleBlock_setter_node, new Category(PreferencesMessages.NameStyleBlock_setter_node,
PreferencesMessages.NameStyleBlock_setter_node_description, EXAMPLE_FIELD_NAME, PreferencesMessages.NameStyleBlock_setter_node_description, EXAMPLE_FIELD_NAME,
codeCategory) codeCategory)
@ -200,7 +201,8 @@ public class NameStyleBlock extends OptionsConfigurationBlock {
.setPrefixKey(KEY_SETTER_PREFIX) .setPrefixKey(KEY_SETTER_PREFIX)
.setSuffixKey(KEY_SETTER_SUFFIX) .setSuffixKey(KEY_SETTER_SUFFIX)
.setSeedNameGenerator(fieldCategory) .setSeedNameGenerator(fieldCategory)
.setNameValidator(IDENTIFIER_VALIDATOR); .setNameValidator(IDENTIFIER_VALIDATOR)
.setTrimFieldName(true);
Category fileCategory = new Category(PreferencesMessages.NameStyleBlock_files_node); Category fileCategory = new Category(PreferencesMessages.NameStyleBlock_files_node);
new Category(PreferencesMessages.NameStyleBlock_cpp_header_node, new Category(PreferencesMessages.NameStyleBlock_cpp_header_node,
PreferencesMessages.NameStyleBlock_cpp_header_node_description, EXAMPLE_CLASS_NAME, PreferencesMessages.NameStyleBlock_cpp_header_node_description, EXAMPLE_CLASS_NAME,
@ -444,6 +446,7 @@ public class NameStyleBlock extends OptionsConfigurationBlock {
private Text previewText; private Text previewText;
private Composite editorArea; private Composite editorArea;
private boolean trimFieldName = false;
Category(String name, String description, String seedName, Category parent) { Category(String name, String description, String seedName, Category parent) {
this.name = name; this.name = name;
@ -568,8 +571,15 @@ public class NameStyleBlock extends OptionsConfigurationBlock {
NameComposer composer = new NameComposer(capitalization, wordDelimiter, prefix, suffix); NameComposer composer = new NameComposer(capitalization, wordDelimiter, prefix, suffix);
String name = seedNameGenerator != null ? String name = seedNameGenerator != null ?
seedNameGenerator.composeExampleName(settings) : seedName; seedNameGenerator.composeExampleName(settings) : seedName;
if (trimFieldName) {
name = NameComposer.trimFieldName(name);
}
return composer.compose(name); return composer.compose(name);
} }
void setTrimFieldName(boolean trimSeedName) {
this.trimFieldName = trimSeedName;
}
} }
private abstract static class NameValidator { private abstract static class NameValidator {

View file

@ -16,7 +16,6 @@ import java.util.Set;
import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.IPreferencesService; import org.eclipse.core.runtime.preferences.IPreferencesService;
import com.ibm.icu.text.BreakIterator;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator; import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTName;
@ -27,7 +26,6 @@ import org.eclipse.cdt.ui.PreferenceConstants;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor; import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.cdt.internal.ui.text.CBreakIterator;
import org.eclipse.cdt.internal.ui.util.NameComposer; import org.eclipse.cdt.internal.ui.util.NameComposer;
public class GetterSetterNameGenerator { public class GetterSetterNameGenerator {
@ -72,7 +70,7 @@ public class GetterSetterNameGenerator {
String suffix = preferences.getString(CUIPlugin.PLUGIN_ID, String suffix = preferences.getString(CUIPlugin.PLUGIN_ID,
PreferenceConstants.NAME_STYLE_GETTER_SUFFIX, "", null); //$NON-NLS-1$ PreferenceConstants.NAME_STYLE_GETTER_SUFFIX, "", null); //$NON-NLS-1$
NameComposer composer = new NameComposer(capitalization, wordDelimiter, prefix, suffix); NameComposer composer = new NameComposer(capitalization, wordDelimiter, prefix, suffix);
String name = GetterSetterNameGenerator.trimFieldName(fieldName.toString()); String name = NameComposer.trimFieldName(fieldName.toString());
return composer.compose(name); return composer.compose(name);
} }
@ -99,7 +97,7 @@ public class GetterSetterNameGenerator {
String suffix = preferences.getString(CUIPlugin.PLUGIN_ID, String suffix = preferences.getString(CUIPlugin.PLUGIN_ID,
PreferenceConstants.NAME_STYLE_SETTER_SUFFIX, "", null); //$NON-NLS-1$ PreferenceConstants.NAME_STYLE_SETTER_SUFFIX, "", null); //$NON-NLS-1$
NameComposer composer = new NameComposer(capitalization, wordDelimiter, prefix, suffix); NameComposer composer = new NameComposer(capitalization, wordDelimiter, prefix, suffix);
String name = GetterSetterNameGenerator.trimFieldName(fieldName.toString()); String name = NameComposer.trimFieldName(fieldName.toString());
return composer.compose(name); return composer.compose(name);
} }
@ -115,49 +113,7 @@ public class GetterSetterNameGenerator {
String suffix = preferences.getString(CUIPlugin.PLUGIN_ID, String suffix = preferences.getString(CUIPlugin.PLUGIN_ID,
PreferenceConstants.NAME_STYLE_VARIABLE_SUFFIX, "", null); //$NON-NLS-1$ PreferenceConstants.NAME_STYLE_VARIABLE_SUFFIX, "", null); //$NON-NLS-1$
NameComposer composer = new NameComposer(capitalization, wordDelimiter, prefix, suffix); NameComposer composer = new NameComposer(capitalization, wordDelimiter, prefix, suffix);
String name = GetterSetterNameGenerator.trimFieldName(fieldName.toString()); String name = NameComposer.trimFieldName(fieldName.toString());
return composer.compose(name); return composer.compose(name);
} }
/**
* 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.
*
* @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);
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;
if (firstWordStart < 0) {
firstWordStart = start;
firstWordEnd = lastWordEnd;
} else if (secondWordStart < 0) {
secondWordStart = start;
}
}
}
// 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 {
return fieldName.substring(firstWordStart, lastWordEnd);
}
}
} }

View file

@ -146,4 +146,46 @@ public class NameComposer {
Character.toUpperCase(word.charAt(i)) : Character.toLowerCase(word.charAt(i))); Character.toUpperCase(word.charAt(i)) : Character.toLowerCase(word.charAt(i)));
} }
} }
/**
* 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.
*
* @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);
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;
if (firstWordStart < 0) {
firstWordStart = start;
firstWordEnd = lastWordEnd;
} else if (secondWordStart < 0) {
secondWordStart = start;
}
}
}
// 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 {
return fieldName.substring(firstWordStart, lastWordEnd);
}
}
} }