diff options
author | Emanuel Graf | 2011-01-05 10:19:49 +0000 |
---|---|---|
committer | Emanuel Graf | 2011-01-05 10:19:49 +0000 |
commit | 3775d258adeaa3d6062a9daa0534017a18d04201 (patch) | |
tree | d8389cc10f8c4bc6b219a9ddc7d5c9b3553ca601 | |
parent | 15308781a3a60bf7c4915485748bf9eb1f122551 (diff) | |
download | org.eclipse.cdt-3775d258adeaa3d6062a9daa0534017a18d04201.tar.gz org.eclipse.cdt-3775d258adeaa3d6062a9daa0534017a18d04201.tar.xz org.eclipse.cdt-3775d258adeaa3d6062a9daa0534017a18d04201.zip |
bug 330693: Improve suggested variable name in Extract Local Variable
https://bugs.eclipse.org/bugs/show_bug.cgi?id=330693 Improved handling of long names
2 files changed, 91 insertions, 12 deletions
diff --git a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractLocalVariable.rts b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractLocalVariable.rts index 3e6dfa754ce..d621ab9be16 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractLocalVariable.rts +++ b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractLocalVariable.rts @@ -546,3 +546,53 @@ int main() { return 0; } +//!Bug 330693 Improve suggested variable name in Extract Local Variable +//#org.eclipse.cdt.ui.tests.refactoring.extractlocalvariable.ExtractLocalVariableRefactoringTest +//@A.cpp + +struct Foo{ + int getVarWithLongName(); +}; + +void bar(){ + Foo f; + /*$*/f.getVarWithLongName()/*$$*/; +} + +//= + +struct Foo{ + int getVarWithLongName(); +}; + +void bar(){ + Foo f; + int varWithLongName = f.getVarWithLongName(); + varWithLongName; +} + +//!Bug 330693 Improve suggested variable name in Extract Local Variable: name == prefix +//#org.eclipse.cdt.ui.tests.refactoring.extractlocalvariable.ExtractLocalVariableRefactoringTest +//@A.cpp + +struct Foo{ + int get(); +}; + +void bar(){ + Foo f; + /*$*/f.get()/*$$*/; +} + +//= + +struct Foo{ + int get(); +}; + +void bar(){ + Foo f; + int get = f.get(); + get; +} + diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractlocalvariable/ExtractLocalVariableRefactoring.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractlocalvariable/ExtractLocalVariableRefactoring.java index a641fe03a33..0ed6b04a8b7 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractlocalvariable/ExtractLocalVariableRefactoring.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractlocalvariable/ExtractLocalVariableRefactoring.java @@ -336,21 +336,15 @@ public class ExtractLocalVariableRefactoring extends CRefactoring { if (proposals.length == 0) { return info.getName(); } else { - String name = getLastCamelCasePart(proposals[proposals.length - 1]); + String name = proposals[proposals.length - 1]; return name; } } - private String getLastCamelCasePart(String string) { - if (string.length() == 0) { - return string; - } - int index = string.length() - 1; - while (index > 0 - && (Character.isLowerCase(string.charAt(index)) || Character.isDigit(string.charAt(index)))) { - --index; - } - return string.substring(index).toLowerCase(); + private String[] getPrefixes() { + // In Future we could use user preferences to define the prefixes + String[] prefixes = { "get", "is" }; //$NON-NLS-1$//$NON-NLS-2$ + return prefixes; } /** @@ -426,7 +420,7 @@ public class ExtractLocalVariableRefactoring extends CRefactoring { tmpName[len++] = c; } } - name = getLastCamelCasePart(new String(tmpName, 0, len)); + name = trimPrefixes(new String(tmpName, 0, len)); if (name.length() > 0) { if (nameAvailable(name, guessedTempNames, scope)) { guessedTempNames.add(name); @@ -435,6 +429,7 @@ public class ExtractLocalVariableRefactoring extends CRefactoring { } } } + }); } if (guessedTempNames.isEmpty()) { @@ -445,6 +440,40 @@ public class ExtractLocalVariableRefactoring extends CRefactoring { } return guessedTempNames.toArray(new String[0]); } + + private String trimPrefixes(String name) { + String lower = name.toLowerCase(); + int start = 0; + for (String prefix : getPrefixes()) { + if(lower.startsWith(prefix)) { + if (name.length() > prefix.length()) { + start = prefix.length(); + } + } + prefix = prefix + "_"; //$NON-NLS-1$ + if(lower.startsWith(prefix)) { + if (name.length() > prefix.length()) { + start = prefix.length(); + } + } + } + + if (start > 0) { + + String nameWithoutPrefix = name.substring(start); + if (Character.isUpperCase(nameWithoutPrefix.charAt(0))) { + nameWithoutPrefix = nameWithoutPrefix.substring(0, 1).toLowerCase() + + nameWithoutPrefix.substring(1); + } + + if (!Character.isJavaIdentifierStart(nameWithoutPrefix.charAt(0))) { + nameWithoutPrefix = "_" + nameWithoutPrefix; //$NON-NLS-1$ + } + return nameWithoutPrefix; + } else { + return name; + } + } private boolean nameAvailable(String name, List<String> guessedNames, IScope scope) { if (guessedNames.contains(name) || |