introduce functional parameter: exclude only selection range from used parameters check as for functional interfaces the whole code block is treated as selection's parent (IDEA-154299)

This commit is contained in:
Anna.Kozlova
2016-09-27 09:22:01 +02:00
parent 31927c0d07
commit 9863deb8d9
6 changed files with 82 additions and 0 deletions
@@ -23,6 +23,7 @@ import com.intellij.openapi.editor.RangeMarker;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Pass;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiExpressionTrimRenderer;
import com.intellij.psi.util.PsiTreeUtil;
@@ -44,6 +45,7 @@ public class ElementToWorkOn {
public static final Key<String> PREFIX = Key.create("prefix");
public static final Key<String> SUFFIX = Key.create("suffix");
public static final Key<RangeMarker> TEXT_RANGE = Key.create("range");
public static final Key<TextRange> EXPR_RANGE = Key.create("expr_range");
public static final Key<Boolean> REPLACE_NON_PHYSICAL = Key.create("replace_non_physical");
public static final Key<Boolean> OUT_OF_CODE_BLOCK= Key.create("out_of_code_block");
@@ -679,6 +679,7 @@ public class IntroduceParameterHandler extends IntroduceHandlerBase {
expression.putUserData(ElementToWorkOn.PREFIX, prefixText.get());
expression.putUserData(ElementToWorkOn.SUFFIX, suffixText.get());
expression.putUserData(ElementToWorkOn.TEXT_RANGE, marker);
expression.putUserData(ElementToWorkOn.EXPR_RANGE, elements.length == 1 ? elements[0].getTextRange() : null);
new Introducer(project, expression, null, editor)
.introduceParameter(methodToIntroduceParameter, methodToSearchFor);
@@ -25,6 +25,7 @@
package com.intellij.refactoring.introduceParameter;
import com.intellij.codeInsight.generation.GenerateMembersUtil;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.search.searches.OverridingMethodsSearch;
import com.intellij.psi.search.searches.ReferencesSearch;
@@ -89,6 +90,10 @@ public class Util {
return PsiTreeUtil.getParentOfType(getPhysical(expr), PsiMethod.class);
}
public static boolean isAncestor(PsiElement ancestor, PsiElement element, boolean strict) {
final TextRange exprRange = ancestor.getUserData(ElementToWorkOn.EXPR_RANGE);
if (exprRange != null) {
return exprRange.contains(element.getTextRange());
}
return PsiTreeUtil.isAncestor(getPhysical(ancestor), getPhysical(element), strict);
}
@@ -0,0 +1,38 @@
import java.util.function.BooleanSupplier;
class A2 {
void method(MyObject obj, BooleanSupplier anObject) {
obj.method1();
if (anObject.getAsBoolean()) {
doOtherStaff();
}
obj.method2();
}
{
final MyObject obj = new MyObject();
method(obj, new BooleanSupplier() {
public boolean getAsBoolean() {
return obj.isCondition1();
}
});
}
private void doOtherStaff() {
}
private class MyObject {
public void method1() {
}
public boolean isCondition1() {
return true;
}
public void method2() {
}
}
}
@@ -0,0 +1,32 @@
class A2 {
void method(MyObject obj) {
obj.method1();
if (<selection>obj.isCondition1()</selection>) {
doOtherStaff();
}
obj.method2();
}
{
method(new MyObject());
}
private void doOtherStaff() {
}
private class MyObject {
public void method1() {
}
public boolean isCondition1() {
return true;
}
public void method2() {
}
}
}
@@ -70,6 +70,10 @@ public class IntroduceFunctionalParameterTest extends LightRefactoringTestCase
doTest();
}
public void testUsedParametersOutsideSelectedFragment() throws Exception {
doTest();
}
@NotNull
@Override
protected String getTestDataPath() {