mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-13 09:19:13 +07:00
extract method suggester: unique parameter names (IDEA-152334); update output variables when suggester replaces the initial method
This commit is contained in:
@@ -1728,6 +1728,22 @@ public class ExtractMethodProcessor implements MatchProvider {
|
||||
myExtractedMethod = suggester.getExtractedMethod();
|
||||
myMethodCall = suggester.getMethodCall();
|
||||
myVariableDatum = suggester.getVariableData();
|
||||
|
||||
final List<PsiVariable> outputVariables = new ArrayList<>();
|
||||
for (PsiReturnStatement statement : PsiUtil.findReturnStatements(myExtractedMethod)) {
|
||||
final PsiExpression returnValue = statement.getReturnValue();
|
||||
if (returnValue instanceof PsiReferenceExpression) {
|
||||
final PsiElement resolve = ((PsiReferenceExpression)returnValue).resolve();
|
||||
if (resolve instanceof PsiLocalVariable) {
|
||||
outputVariables.add((PsiVariable)resolve);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (outputVariables.size() == 1) {
|
||||
myOutputVariable = outputVariables.get(0);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
+6
@@ -334,6 +334,12 @@ public class ExtractMethodSignatureSuggester {
|
||||
for (PsiParameter parameter : parameters) {
|
||||
uniqueNameGenerator.addExistingName(parameter.getName());
|
||||
}
|
||||
|
||||
SyntaxTraverser.psiTraverser().withRoot(myExtractedMethod.getBody())
|
||||
.filter(element -> element instanceof PsiVariable)
|
||||
.forEach(element -> uniqueNameGenerator.addExistingName(((PsiVariable)element).getName()));
|
||||
|
||||
|
||||
final THashMap<PsiExpression, String> unique = new THashMap<PsiExpression, String>(ourEquivalenceStrategy);
|
||||
final Map<PsiExpression, String> replacement = new HashMap<PsiExpression, String>();
|
||||
for (PsiExpression expr : exprs) {
|
||||
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
|
||||
class Test {
|
||||
|
||||
private BigDecimal getRevenue() {
|
||||
<selection>final String query = createNamedQuery("revenues");
|
||||
String revenues = "";
|
||||
final String revenue;
|
||||
revenue = "a";
|
||||
</selection>
|
||||
return revenue;
|
||||
}
|
||||
|
||||
public BigDecimal getExpense() {
|
||||
final String query = createNamedQuery("expenses");
|
||||
String expenses = "";
|
||||
final String expense;
|
||||
expense = "a";
|
||||
|
||||
return expense;
|
||||
}
|
||||
|
||||
private String createNamedQuery(String expenses) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
class Test {
|
||||
|
||||
private BigDecimal getRevenue() {
|
||||
final String revenue = newMethod("revenues");
|
||||
|
||||
return revenue;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String newMethod(String revenues2) {
|
||||
final String query = createNamedQuery(revenues2);
|
||||
String revenues = "";
|
||||
final String revenue;
|
||||
revenue = "a";
|
||||
return revenue;
|
||||
}
|
||||
|
||||
public BigDecimal getExpense() {
|
||||
final String expense = newMethod("expenses");
|
||||
|
||||
return expense;
|
||||
}
|
||||
|
||||
private String createNamedQuery(String expenses) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -675,6 +675,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
|
||||
doDuplicatesTest();
|
||||
}
|
||||
|
||||
public void testSuggestChangeSignatureWithOutputVariables() throws Exception {
|
||||
doDuplicatesTest();
|
||||
}
|
||||
|
||||
public void testSuggestChangeSignatureWithChangedParameterName() throws Exception {
|
||||
configureByFile(BASE_PATH + getTestName(false) + ".java");
|
||||
boolean success = performExtractMethod(true, true, getEditor(), getFile(), getProject(), false, null, false, "p");
|
||||
|
||||
Reference in New Issue
Block a user