introduce functional: don't treat all parameters as ignored cause they could belong to for loops, nested classes not available at call site, etc

This commit is contained in:
Anna Kozlova
2015-12-14 10:30:48 +01:00
parent 7cf8995a23
commit e426d5ac3a
4 changed files with 87 additions and 4 deletions
@@ -558,7 +558,9 @@ public class IntroduceParameterHandler extends IntroduceHandlerBase {
? new PsiElement[] {exprInRange}
: CodeInsightUtil.findStatementsInRange(copy, elements[0].getTextRange().getStartOffset(),
elements[elements.length - 1].getTextRange().getEndOffset());
final MyExtractMethodProcessor processor = new MyExtractMethodProcessor(project, editor, elementsCopy);
final List<PsiMethod> enclosingMethodsInCopy = getEnclosingMethods(Util.getContainingMethod(elementsCopy[0]));
final MyExtractMethodProcessor processor = new MyExtractMethodProcessor(project, editor, elementsCopy,
enclosingMethodsInCopy.get(enclosingMethodsInCopy.size() - 1));
try {
if (!processor.prepare()) return false;
processor.showDialog();
@@ -700,8 +702,11 @@ public class IntroduceParameterHandler extends IntroduceHandlerBase {
}
private static class MyExtractMethodProcessor extends ExtractMethodProcessor {
public MyExtractMethodProcessor(Project project, Editor editor, PsiElement[] elements) {
private final PsiMethod myTopEnclosingMethod;
public MyExtractMethodProcessor(Project project, Editor editor, PsiElement[] elements, PsiMethod topEnclosing) {
super(project, editor, elements, null, REFACTORING_NAME, null, null);
myTopEnclosingMethod = topEnclosing;
}
@Override
@@ -755,7 +760,8 @@ public class IntroduceParameterHandler extends IntroduceHandlerBase {
final InputVariables inputVariables = getInputVariables();
List<VariableData> datas = new ArrayList<VariableData>();
for (VariableData data : inputVariables.getInputVariables()) {
if (data.variable instanceof PsiParameter) {
final PsiVariable variable = data.variable;
if (variable instanceof PsiParameter && myTopEnclosingMethod.equals(((PsiParameter)variable).getDeclarationScope())) {
continue;
}
datas.add(data);
@@ -0,0 +1,31 @@
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
class Test {
public void run() {
final Normalizer normalizer = new Normalizer();
final List<String> input = new ArrayList<>();
final List<String> output = new ArrayList<>();
doNormalize(input, output, new Function<String,String>() {
public String apply(String s) {
return normalizer.normalize(s);
}
});
}
private void doNormalize(final List<String> input, final List<String> output, Function<String, String> anObject) {
for (final String s : input) {
output.add(anObject.apply(s));
}
}
public static class Normalizer {
public String normalize(final String s) {
return s;
}
}
}
@@ -0,0 +1,26 @@
import java.util.ArrayList;
import java.util.List;
class Test {
public void run() {
final Normalizer normalizer = new Normalizer();
final List<String> input = new ArrayList<>();
final List<String> output = new ArrayList<>();
doNormalize(input, normalizer, output);
}
private void doNormalize(final List<String> input, final Normalizer normalizer, final List<String> output) {
for (final String s : input) {
output.add(<selection>normalizer.normalize(s)</selection>);
}
}
public static class Normalizer {
public String normalize(final String s) {
return s;
}
}
}
@@ -16,8 +16,12 @@
package com.intellij.refactoring;
import com.intellij.JavaTestUtil;
import com.intellij.codeInsight.CodeInsightUtil;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiExpression;
import com.intellij.refactoring.introduceParameter.IntroduceParameterHandler;
import com.intellij.testFramework.IdeaTestUtil;
import com.intellij.testFramework.TestDataPath;
@@ -57,6 +61,10 @@ public class IntroduceFunctionalParameterTest extends LightRefactoringTestCase
doTest();
}
public void testInsideForLoop() throws Exception {
doTest();
}
public void testInsideAnonymous() throws Exception {
doTest();
}
@@ -87,7 +95,19 @@ public class IntroduceFunctionalParameterTest extends LightRefactoringTestCase
configureByFile("/refactoring/introduceFunctionalParameter/before" + getTestName(false) + ".java");
enabled = myEditor.getSettings().isVariableInplaceRenameEnabled();
myEditor.getSettings().setVariableInplaceRenameEnabled(false);
new IntroduceParameterHandler().introduceStrategy(getProject(), getEditor(), getFile());
final SelectionModel selectionModel = getEditor().getSelectionModel();
if (selectionModel.hasSelection()) {
final int selectionStart = selectionModel.getSelectionStart();
final int selectionEnd = selectionModel.getSelectionEnd();
PsiElement[] elements = CodeInsightUtil.findStatementsInRange(getFile(), selectionStart, selectionEnd);
if (elements.length == 0) {
final PsiExpression expression = CodeInsightUtil.findExpressionInRange(getFile(), selectionStart, selectionEnd);
if (expression != null) {
elements = new PsiElement[] {expression};
}
}
new IntroduceParameterHandler().introduceStrategy(getProject(), getEditor(), getFile(), elements);
}
checkResultByFile("/refactoring/introduceFunctionalParameter/after" + getTestName(false) + ".java");
if (conflict != null) {
fail("Conflict expected");