mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-28 11:25:35 +02:00
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
This commit is contained in:
parent
c81916affe
commit
e27e34dd13
2 changed files with 91 additions and 12 deletions
|
@ -546,3 +546,53 @@ int main() {
|
||||||
return 0;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -320,21 +320,15 @@ public class ExtractLocalVariableRefactoring extends CRefactoring {
|
||||||
if (proposals.length == 0) {
|
if (proposals.length == 0) {
|
||||||
return info.getName();
|
return info.getName();
|
||||||
} else {
|
} else {
|
||||||
String name = getLastCamelCasePart(proposals[proposals.length - 1]);
|
String name = proposals[proposals.length - 1];
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getLastCamelCasePart(String string) {
|
private String[] getPrefixes() {
|
||||||
if (string.length() == 0) {
|
// In Future we could use user preferences to define the prefixes
|
||||||
return string;
|
String[] prefixes = { "get", "is" }; //$NON-NLS-1$//$NON-NLS-2$
|
||||||
}
|
return prefixes;
|
||||||
int index = string.length() - 1;
|
|
||||||
while (index > 0
|
|
||||||
&& (Character.isLowerCase(string.charAt(index)) || Character.isDigit(string.charAt(index)))) {
|
|
||||||
--index;
|
|
||||||
}
|
|
||||||
return string.substring(index).toLowerCase();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -408,7 +402,7 @@ public class ExtractLocalVariableRefactoring extends CRefactoring {
|
||||||
tmpName[len++] = c;
|
tmpName[len++] = c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
name = getLastCamelCasePart(new String(tmpName, 0, len));
|
name = trimPrefixes(new String(tmpName, 0, len));
|
||||||
if (name.length() > 0) {
|
if (name.length() > 0) {
|
||||||
if (nameAvailable(name, guessedTempNames, scope)) {
|
if (nameAvailable(name, guessedTempNames, scope)) {
|
||||||
guessedTempNames.add(name);
|
guessedTempNames.add(name);
|
||||||
|
@ -417,6 +411,7 @@ public class ExtractLocalVariableRefactoring extends CRefactoring {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (guessedTempNames.isEmpty()) {
|
if (guessedTempNames.isEmpty()) {
|
||||||
|
@ -428,6 +423,40 @@ public class ExtractLocalVariableRefactoring extends CRefactoring {
|
||||||
return guessedTempNames.toArray(new String[0]);
|
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) {
|
private boolean nameAvailable(String name, List<String> guessedNames, IScope scope) {
|
||||||
if (guessedNames.contains(name) || info.getUsedNames().contains(name)) {
|
if (guessedNames.contains(name) || info.getUsedNames().contains(name)) {
|
||||||
return false;
|
return false;
|
||||||
|
|
Loading…
Add table
Reference in a new issue