Skip to content

Commit 5bc252d

Browse files
committed
Improving the Names Recommended by Extract Local Variable Refactoring. Addressing Issue 745.
1 parent 96a3310 commit 5bc252d

File tree

4 files changed

+450
-5
lines changed

4 files changed

+450
-5
lines changed

core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/extractlocalvariable/ExtractLocalVariableRefactoringTest.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2008, 2016 Institute for Software, HSR Hochschule fuer Technik
2+
* Copyright (c) 2008, 2024 Institute for Software, HSR Hochschule fuer Technik
33
* Rapperswil, University of applied sciences and others
44
*
55
* This program and the accompanying materials
@@ -14,6 +14,7 @@
1414
* Sergey Prigogin (Google)
1515
* Marc-Andre Laperle (Ericsson)
1616
* Thomas Corbat (IFS)
17+
* Taiming Wang - Name recommendation improvement
1718
*******************************************************************************/
1819
package org.eclipse.cdt.ui.tests.refactoring.extractlocalvariable;
1920

@@ -593,4 +594,36 @@ public void testSuggestedNameCFile_Bug412032_2() throws Exception {
593594
public void testTemplateWithFunctionArgument_487186() throws Exception {
594595
assertRefactoringSuccess();
595596
}
597+
598+
//A.cpp
599+
//using namespace std;
600+
//String getProgramResolveLink(String program){
601+
//return program;
602+
//}
603+
//void useProgram(String program)
604+
//{
605+
//cout << getProgramResolveLink(program);
606+
//}
607+
//====================
608+
//using namespace std;
609+
//String getProgramResolveLink(String program){
610+
//return program;
611+
//}
612+
//void useProgram(String program)
613+
//{
614+
// void programResolveLink = getProgramResolveLink(program);
615+
// cout << programResolveLink;
616+
//}
617+
618+
//refScript.xml
619+
//<?xml version="1.0" encoding="UTF-8"?>
620+
//<session version="1.0">
621+
//<refactoring comment="Extract getProgramResolveLink(program)" description="Extract Local Variable Refactoring"
622+
// fileName="file:${projectPath}/A.cpp" flags="4"
623+
// id="org.eclipse.cdt.internal.ui.refactoring.extractlocalvariable.ExtractLocalVariableRefactoring"
624+
// name="programResolveLink" project="RegressionTestProject" selection="120,39"/>
625+
//</session>
626+
public void testIfRecommend() throws Exception {
627+
assertRefactoringSuccess();
628+
}
596629
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Taiming Wang - Initial implementation
13+
*******************************************************************************/
14+
package org.eclipse.cdt.internal.ui.refactoring.extractlocalvariable;
15+
16+
public class AbortSearchException extends RuntimeException {
17+
private static final long serialVersionUID = 8809979732051907351L;
18+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Taiming Wang - Initial implementation
13+
*******************************************************************************/
14+
package org.eclipse.cdt.internal.ui.refactoring.extractlocalvariable;
15+
16+
import java.util.AbstractMap;
17+
import java.util.Map;
18+
import java.util.Set;
19+
import java.util.stream.Collectors;
20+
import java.util.stream.Stream;
21+
22+
public class ConvertLoopOperation {
23+
protected static final String FOR_LOOP_ELEMENT_IDENTIFIER = "element"; //$NON-NLS-1$
24+
25+
private static final Map<String, String> IRREG_NOUNS = Stream
26+
.of(new AbstractMap.SimpleImmutableEntry<>("Children", "Child"), //$NON-NLS-1$ //$NON-NLS-2$
27+
new AbstractMap.SimpleImmutableEntry<>("Entries", "Entry"), //$NON-NLS-1$ //$NON-NLS-2$
28+
new AbstractMap.SimpleImmutableEntry<>("Proxies", "Proxy"), //$NON-NLS-1$ //$NON-NLS-2$
29+
new AbstractMap.SimpleImmutableEntry<>("Indices", "Index"), //$NON-NLS-1$ //$NON-NLS-2$
30+
new AbstractMap.SimpleImmutableEntry<>("People", "Person"), //$NON-NLS-1$ //$NON-NLS-2$
31+
new AbstractMap.SimpleImmutableEntry<>("Properties", "Property"), //$NON-NLS-1$ //$NON-NLS-2$
32+
new AbstractMap.SimpleImmutableEntry<>("Factories", "Factory"), //$NON-NLS-1$ //$NON-NLS-2$
33+
new AbstractMap.SimpleImmutableEntry<>("Archives", "archive"), //$NON-NLS-1$ //$NON-NLS-2$
34+
new AbstractMap.SimpleImmutableEntry<>("Aliases", "Alias"), //$NON-NLS-1$ //$NON-NLS-2$
35+
new AbstractMap.SimpleImmutableEntry<>("Alternatives", "Alternative"), //$NON-NLS-1$ //$NON-NLS-2$
36+
new AbstractMap.SimpleImmutableEntry<>("Capabilities", "Capability"), //$NON-NLS-1$ //$NON-NLS-2$
37+
new AbstractMap.SimpleImmutableEntry<>("Hashes", "Hash"), //$NON-NLS-1$ //$NON-NLS-2$
38+
new AbstractMap.SimpleImmutableEntry<>("Directories", "Directory"), //$NON-NLS-1$ //$NON-NLS-2$
39+
new AbstractMap.SimpleImmutableEntry<>("Statuses", "Status"), //$NON-NLS-1$ //$NON-NLS-2$
40+
new AbstractMap.SimpleImmutableEntry<>("Instances", "Instance"), //$NON-NLS-1$ //$NON-NLS-2$
41+
new AbstractMap.SimpleImmutableEntry<>("Classes", "Class"), //$NON-NLS-1$ //$NON-NLS-2$
42+
new AbstractMap.SimpleImmutableEntry<>("Deliveries", "Delivery"), //$NON-NLS-1$ //$NON-NLS-2$
43+
new AbstractMap.SimpleImmutableEntry<>("Vertices", "Vertex")) //$NON-NLS-1$ //$NON-NLS-2$
44+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
45+
46+
private static final Set<String> NO_BASE_TYPES = Stream.of("integers", //$NON-NLS-1$
47+
"floats", //$NON-NLS-1$
48+
"doubles", //$NON-NLS-1$
49+
"booleans", //$NON-NLS-1$
50+
"bytes", //$NON-NLS-1$
51+
"chars", //$NON-NLS-1$
52+
"shorts", //$NON-NLS-1$
53+
"longs") //$NON-NLS-1$
54+
.collect(Collectors.toSet());
55+
56+
private static final Set<String> CUT_PREFIX = Stream.of("all") //$NON-NLS-1$
57+
.collect(Collectors.toSet());
58+
59+
private static final Set<String> IRREG_ENDINGS = Stream.of("xes", //$NON-NLS-1$
60+
"ies", //$NON-NLS-1$
61+
"oes", //$NON-NLS-1$
62+
"ses", //$NON-NLS-1$
63+
"hes", //$NON-NLS-1$
64+
"zes", //$NON-NLS-1$
65+
"ves", //$NON-NLS-1$
66+
"ces", //$NON-NLS-1$
67+
"ss", //$NON-NLS-1$
68+
"is", //$NON-NLS-1$
69+
"us", //$NON-NLS-1$
70+
"os", //$NON-NLS-1$
71+
"as") //$NON-NLS-1$
72+
.collect(Collectors.toSet());
73+
74+
public static String modifyBaseName(String suggestedName) {
75+
String name = suggestedName;
76+
for (String prefix : CUT_PREFIX) {
77+
if (prefix.length() >= suggestedName.length()) {
78+
continue;
79+
}
80+
char afterPrefix = suggestedName.charAt(prefix.length());
81+
if (Character.isUpperCase(afterPrefix) || afterPrefix == '_') {
82+
if (suggestedName.toLowerCase().startsWith(prefix)) {
83+
String nameWithoutPrefix = suggestedName.substring(prefix.length());
84+
if (nameWithoutPrefix.startsWith("_") && nameWithoutPrefix.length() > 1) { //$NON-NLS-1$
85+
name = nameWithoutPrefix.substring(1);
86+
} else {
87+
name = nameWithoutPrefix;
88+
}
89+
if (name.length() == 1) {
90+
return name;
91+
}
92+
break;
93+
}
94+
}
95+
}
96+
for (Map.Entry<String, String> entry : IRREG_NOUNS.entrySet()) {
97+
String suffix = entry.getKey();
98+
if (name.toLowerCase().endsWith(suffix.toLowerCase())) {
99+
String firstPart = name.substring(0, name.length() - suffix.length());
100+
return firstPart + entry.getValue();
101+
}
102+
}
103+
for (String varname : NO_BASE_TYPES) {
104+
if (name.equalsIgnoreCase(varname)) {
105+
return FOR_LOOP_ELEMENT_IDENTIFIER;
106+
}
107+
}
108+
for (String suffix : IRREG_ENDINGS) {
109+
if (name.toLowerCase().endsWith(suffix)) {
110+
return FOR_LOOP_ELEMENT_IDENTIFIER;
111+
}
112+
}
113+
if (name.length() > 2 && name.endsWith("s")) { //$NON-NLS-1$
114+
return name.substring(0, name.length() - 1);
115+
}
116+
return FOR_LOOP_ELEMENT_IDENTIFIER;
117+
}
118+
}

0 commit comments

Comments
 (0)