Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/AccessorNameGeneratorTest.java58
-rw-r--r--core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/IdentifierHelperTest.java1
-rw-r--r--core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/NameComposerTest.java42
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/NameStyleBlock.java14
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterNameGenerator.java50
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/NameComposer.java42
6 files changed, 98 insertions, 109 deletions
diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/AccessorNameGeneratorTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/AccessorNameGeneratorTest.java
deleted file mode 100644
index df82a4d5dd..0000000000
--- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/AccessorNameGeneratorTest.java
+++ /dev/null
@@ -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"));
- }
-}
diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/IdentifierHelperTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/IdentifierHelperTest.java
index 181b0b7d54..a43cee1d5a 100644
--- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/IdentifierHelperTest.java
+++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/IdentifierHelperTest.java
@@ -30,7 +30,6 @@ public class IdentifierHelperTest extends TestSuite {
suite.addTest(new EmptyCaseTest());
suite.addTest(new IllegalCharCaseTest());
suite.addTest(new KeywordCaseTest());
- suite.addTestSuite(AccessorNameGeneratorTest.class);
return suite;
}
}
diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/NameComposerTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/NameComposerTest.java
index 8fddfe0be1..907c6b036d 100644
--- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/NameComposerTest.java
+++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/NameComposerTest.java
@@ -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_LOWER_CAMEL_CASE = PreferenceConstants.NAME_STYLE_CAPITALIZATION_LOWER_CAMEL_CASE;
- public void testTrimFieldName() {
+ public void testCompose() {
NameComposer composer = new NameComposer(CAPITALIZATION_ORIGINAL, "", "", ".h");
assertEquals("MyClass.h", composer.compose("MyClass"));
composer = new NameComposer(CAPITALIZATION_LOWER_CASE, "-", "", ".cc");
@@ -44,4 +44,44 @@ public class NameComposerTest extends TestCase {
composer = new NameComposer(CAPITALIZATION_ORIGINAL, "_", "", "");
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"));
+ }
}
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/NameStyleBlock.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/NameStyleBlock.java
index c97931e4fe..f0f168ea7e 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/NameStyleBlock.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/NameStyleBlock.java
@@ -191,7 +191,8 @@ public class NameStyleBlock extends OptionsConfigurationBlock {
.setAlternativePrefixKey(KEY_GETTER_PREFIX_FOR_BOOLEAN)
.setSuffixKey(KEY_GETTER_SUFFIX)
.setSeedNameGenerator(fieldCategory)
- .setNameValidator(IDENTIFIER_VALIDATOR);
+ .setNameValidator(IDENTIFIER_VALIDATOR)
+ .setTrimFieldName(true);
new Category(PreferencesMessages.NameStyleBlock_setter_node,
PreferencesMessages.NameStyleBlock_setter_node_description, EXAMPLE_FIELD_NAME,
codeCategory)
@@ -200,7 +201,8 @@ public class NameStyleBlock extends OptionsConfigurationBlock {
.setPrefixKey(KEY_SETTER_PREFIX)
.setSuffixKey(KEY_SETTER_SUFFIX)
.setSeedNameGenerator(fieldCategory)
- .setNameValidator(IDENTIFIER_VALIDATOR);
+ .setNameValidator(IDENTIFIER_VALIDATOR)
+ .setTrimFieldName(true);
Category fileCategory = new Category(PreferencesMessages.NameStyleBlock_files_node);
new Category(PreferencesMessages.NameStyleBlock_cpp_header_node,
PreferencesMessages.NameStyleBlock_cpp_header_node_description, EXAMPLE_CLASS_NAME,
@@ -444,6 +446,7 @@ public class NameStyleBlock extends OptionsConfigurationBlock {
private Text previewText;
private Composite editorArea;
+ private boolean trimFieldName = false;
Category(String name, String description, String seedName, Category parent) {
this.name = name;
@@ -568,8 +571,15 @@ public class NameStyleBlock extends OptionsConfigurationBlock {
NameComposer composer = new NameComposer(capitalization, wordDelimiter, prefix, suffix);
String name = seedNameGenerator != null ?
seedNameGenerator.composeExampleName(settings) : seedName;
+ if (trimFieldName) {
+ name = NameComposer.trimFieldName(name);
+ }
return composer.compose(name);
}
+
+ void setTrimFieldName(boolean trimSeedName) {
+ this.trimFieldName = trimSeedName;
+ }
}
private abstract static class NameValidator {
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterNameGenerator.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterNameGenerator.java
index 3574b0bff4..229fe842b5 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterNameGenerator.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterNameGenerator.java
@@ -16,7 +16,6 @@ import java.util.Set;
import org.eclipse.core.runtime.Platform;
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.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.ui.text.CBreakIterator;
import org.eclipse.cdt.internal.ui.util.NameComposer;
public class GetterSetterNameGenerator {
@@ -72,7 +70,7 @@ public class GetterSetterNameGenerator {
String suffix = preferences.getString(CUIPlugin.PLUGIN_ID,
PreferenceConstants.NAME_STYLE_GETTER_SUFFIX, "", null); //$NON-NLS-1$
NameComposer composer = new NameComposer(capitalization, wordDelimiter, prefix, suffix);
- String name = GetterSetterNameGenerator.trimFieldName(fieldName.toString());
+ String name = NameComposer.trimFieldName(fieldName.toString());
return composer.compose(name);
}
@@ -99,7 +97,7 @@ public class GetterSetterNameGenerator {
String suffix = preferences.getString(CUIPlugin.PLUGIN_ID,
PreferenceConstants.NAME_STYLE_SETTER_SUFFIX, "", null); //$NON-NLS-1$
NameComposer composer = new NameComposer(capitalization, wordDelimiter, prefix, suffix);
- String name = GetterSetterNameGenerator.trimFieldName(fieldName.toString());
+ String name = NameComposer.trimFieldName(fieldName.toString());
return composer.compose(name);
}
@@ -115,49 +113,7 @@ public class GetterSetterNameGenerator {
String suffix = preferences.getString(CUIPlugin.PLUGIN_ID,
PreferenceConstants.NAME_STYLE_VARIABLE_SUFFIX, "", null); //$NON-NLS-1$
NameComposer composer = new NameComposer(capitalization, wordDelimiter, prefix, suffix);
- String name = GetterSetterNameGenerator.trimFieldName(fieldName.toString());
+ String name = NameComposer.trimFieldName(fieldName.toString());
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);
- }
- }
}
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/NameComposer.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/NameComposer.java
index 133d216cdb..4492bcacbc 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/NameComposer.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/NameComposer.java
@@ -146,4 +146,46 @@ public class NameComposer {
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);
+ }
+ }
}

Back to the top