Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving the Names Recommended by Extract Local Variable Refactoring #754

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2016 Institute for Software, HSR Hochschule fuer Technik
* Copyright (c) 2008, 2024 Institute for Software, HSR Hochschule fuer Technik
* Rapperswil, University of applied sciences and others
*
* This program and the accompanying materials
Expand All @@ -14,6 +14,7 @@
* Sergey Prigogin (Google)
* Marc-Andre Laperle (Ericsson)
* Thomas Corbat (IFS)
* Taiming Wang - Name recommendation improvement
*******************************************************************************/
package org.eclipse.cdt.ui.tests.refactoring.extractlocalvariable;

Expand Down Expand Up @@ -593,4 +594,36 @@ public void testSuggestedNameCFile_Bug412032_2() throws Exception {
public void testTemplateWithFunctionArgument_487186() throws Exception {
assertRefactoringSuccess();
}

//A.cpp
//using namespace std;
//String getProgramResolveLink(String program){
//return program;
//}
//void useProgram(String program)
//{
//cout << getProgramResolveLink(program);
//}
//====================
//using namespace std;
//String getProgramResolveLink(String program){
//return program;
//}
//void useProgram(String program)
//{
// void programResolveLink = getProgramResolveLink(program);
// cout << programResolveLink;
//}

//refScript.xml
//<?xml version="1.0" encoding="UTF-8"?>
//<session version="1.0">
//<refactoring comment="Extract getProgramResolveLink(program)" description="Extract Local Variable Refactoring"
// fileName="file:${projectPath}/A.cpp" flags="4"
// id="org.eclipse.cdt.internal.ui.refactoring.extractlocalvariable.ExtractLocalVariableRefactoring"
// name="programResolveLink" project="RegressionTestProject" selection="120,39"/>
//</session>
public void testIfRecommend() throws Exception {
assertRefactoringSuccess();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*******************************************************************************
* Copyright (c) 2024
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Taiming Wang - Initial implementation
*******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.extractlocalvariable;

public class AbortSearchException extends RuntimeException {
private static final long serialVersionUID = 8809979732051907351L;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*******************************************************************************
* Copyright (c) 2024
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Taiming Wang - Initial implementation
*******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.extractlocalvariable;

import java.util.AbstractMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ConvertLoopOperation {
protected static final String FOR_LOOP_ELEMENT_IDENTIFIER = "element"; //$NON-NLS-1$

private static final Map<String, String> IRREG_NOUNS = Stream
.of(new AbstractMap.SimpleImmutableEntry<>("Children", "Child"), //$NON-NLS-1$ //$NON-NLS-2$
new AbstractMap.SimpleImmutableEntry<>("Entries", "Entry"), //$NON-NLS-1$ //$NON-NLS-2$
new AbstractMap.SimpleImmutableEntry<>("Proxies", "Proxy"), //$NON-NLS-1$ //$NON-NLS-2$
new AbstractMap.SimpleImmutableEntry<>("Indices", "Index"), //$NON-NLS-1$ //$NON-NLS-2$
new AbstractMap.SimpleImmutableEntry<>("People", "Person"), //$NON-NLS-1$ //$NON-NLS-2$
new AbstractMap.SimpleImmutableEntry<>("Properties", "Property"), //$NON-NLS-1$ //$NON-NLS-2$
new AbstractMap.SimpleImmutableEntry<>("Factories", "Factory"), //$NON-NLS-1$ //$NON-NLS-2$
new AbstractMap.SimpleImmutableEntry<>("Archives", "archive"), //$NON-NLS-1$ //$NON-NLS-2$
new AbstractMap.SimpleImmutableEntry<>("Aliases", "Alias"), //$NON-NLS-1$ //$NON-NLS-2$
new AbstractMap.SimpleImmutableEntry<>("Alternatives", "Alternative"), //$NON-NLS-1$ //$NON-NLS-2$
new AbstractMap.SimpleImmutableEntry<>("Capabilities", "Capability"), //$NON-NLS-1$ //$NON-NLS-2$
new AbstractMap.SimpleImmutableEntry<>("Hashes", "Hash"), //$NON-NLS-1$ //$NON-NLS-2$
new AbstractMap.SimpleImmutableEntry<>("Directories", "Directory"), //$NON-NLS-1$ //$NON-NLS-2$
new AbstractMap.SimpleImmutableEntry<>("Statuses", "Status"), //$NON-NLS-1$ //$NON-NLS-2$
new AbstractMap.SimpleImmutableEntry<>("Instances", "Instance"), //$NON-NLS-1$ //$NON-NLS-2$
new AbstractMap.SimpleImmutableEntry<>("Classes", "Class"), //$NON-NLS-1$ //$NON-NLS-2$
new AbstractMap.SimpleImmutableEntry<>("Deliveries", "Delivery"), //$NON-NLS-1$ //$NON-NLS-2$
new AbstractMap.SimpleImmutableEntry<>("Vertices", "Vertex")) //$NON-NLS-1$ //$NON-NLS-2$
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

private static final Set<String> NO_BASE_TYPES = Stream.of("integers", //$NON-NLS-1$
"floats", //$NON-NLS-1$
"doubles", //$NON-NLS-1$
"booleans", //$NON-NLS-1$
"bytes", //$NON-NLS-1$
"chars", //$NON-NLS-1$
"shorts", //$NON-NLS-1$
"longs") //$NON-NLS-1$
.collect(Collectors.toSet());

private static final Set<String> CUT_PREFIX = Stream.of("all") //$NON-NLS-1$
.collect(Collectors.toSet());

private static final Set<String> IRREG_ENDINGS = Stream.of("xes", //$NON-NLS-1$
"ies", //$NON-NLS-1$
"oes", //$NON-NLS-1$
"ses", //$NON-NLS-1$
"hes", //$NON-NLS-1$
"zes", //$NON-NLS-1$
"ves", //$NON-NLS-1$
"ces", //$NON-NLS-1$
"ss", //$NON-NLS-1$
"is", //$NON-NLS-1$
"us", //$NON-NLS-1$
"os", //$NON-NLS-1$
"as") //$NON-NLS-1$
.collect(Collectors.toSet());

public static String modifyBaseName(String suggestedName) {
String name = suggestedName;
for (String prefix : CUT_PREFIX) {
if (prefix.length() >= suggestedName.length()) {
continue;
}
char afterPrefix = suggestedName.charAt(prefix.length());
if (Character.isUpperCase(afterPrefix) || afterPrefix == '_') {
if (suggestedName.toLowerCase().startsWith(prefix)) {
String nameWithoutPrefix = suggestedName.substring(prefix.length());
if (nameWithoutPrefix.startsWith("_") && nameWithoutPrefix.length() > 1) { //$NON-NLS-1$
name = nameWithoutPrefix.substring(1);
} else {
name = nameWithoutPrefix;
}
if (name.length() == 1) {
return name;
}
break;
}
}
}
for (Map.Entry<String, String> entry : IRREG_NOUNS.entrySet()) {
String suffix = entry.getKey();
if (name.toLowerCase().endsWith(suffix.toLowerCase())) {
String firstPart = name.substring(0, name.length() - suffix.length());
return firstPart + entry.getValue();
}
}
for (String varname : NO_BASE_TYPES) {
if (name.equalsIgnoreCase(varname)) {
return FOR_LOOP_ELEMENT_IDENTIFIER;
}
}
for (String suffix : IRREG_ENDINGS) {
if (name.toLowerCase().endsWith(suffix)) {
return FOR_LOOP_ELEMENT_IDENTIFIER;
}
}
if (name.length() > 2 && name.endsWith("s")) { //$NON-NLS-1$
return name.substring(0, name.length() - 1);
}
return FOR_LOOP_ELEMENT_IDENTIFIER;
}
}
Loading