[java] prettify text/fix highlighting suggested for the target placement (IDEA-286811)

GitOrigin-RevId: d77132fdf02f5663ef5f1e70de623aef00664225
This commit is contained in:
Anna Kozlova
2022-01-18 11:22:47 +01:00
committed by intellij-monorepo-bot
parent 02b1c9c1e2
commit 81f7d44702
4 changed files with 26 additions and 8 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.refactoring.introduceVariable;
import com.intellij.java.JavaBundle;
@@ -57,27 +57,28 @@ final class IntroduceVariableTargetBlockChooser {
}
PsiElement lastItem = ContainerUtil.getLastItem(containers);
LOG.assertTrue(lastItem instanceof PsiLambdaExpression);
PsiElement parent = PsiTreeUtil.getParentOfType(lastItem, PsiStatement.class, PsiField.class);
PsiElement parent = PsiTreeUtil.getParentOfType(lastItem, PsiStatement.class, PsiField.class, PsiLambdaExpression.class);
if (parent instanceof PsiLambdaExpression) return parent;
return parent != null ? parent.getParent() : lastItem.getParent();
}
static List<PsiElement> getContainers(PsiElement anchor, PsiExpression expr) {
List<PsiElement> containers = new ArrayList<>();
containers.add(anchor);
PsiElement container = PsiTreeUtil.getParentOfType(anchor, PsiLambdaExpression.class, true, PsiCodeBlock.class);
PsiLambdaExpression container = PsiTreeUtil.getParentOfType(anchor, PsiLambdaExpression.class, true, PsiCodeBlock.class);
if (container == null) return containers;
Set<PsiElement> dependencies =
PsiTreeUtil.collectElementsOfType(expr, PsiReferenceExpression.class)
.stream()
.map(ref -> ref.resolve())
.filter(Objects::nonNull)
.filter(PsiParameter.class::isInstance)
.collect(Collectors.toSet());
while (container instanceof PsiLambdaExpression) {
if (ContainerUtil.intersects(dependencies, Arrays.asList(((PsiLambdaExpression)container).getParameterList().getParameters()))) {
while (container != null) {
if (ContainerUtil.intersects(dependencies, Arrays.asList(container.getParameterList().getParameters()))) {
break;
}
containers.add(container);
container = container.getParent();
container = PsiTreeUtil.getParentOfType(container, PsiLambdaExpression.class, true, PsiCodeBlock.class);
}
return containers;
}

View File

@@ -0,0 +1,5 @@
import java.util.stream.Stream;
class Test {
Object f = Stream.of("x").filter(s -> s.chars().anyMatch(c -> c == <selection>s.charAt(0)</selection>)).findFirst();
}

View File

@@ -0,0 +1,8 @@
import java.util.stream.Stream;
class Test {
Object f = Stream.of("x").filter(s -> {
char first = s.charAt(0);
return s.chars().anyMatch(c -> c == first);
}).findFirst();
}

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.java.refactoring;
import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
@@ -101,6 +101,10 @@ public class InplaceIntroduceVariableTest extends AbstractJavaInplaceIntroduceTe
doTestReplaceChoice("Runnable: () -> {...}", introducer -> type("expr"));
}
public void testPlaceInsideLambdaBody1() {
doTestReplaceChoice("Predicate<String>: s -> {...}", introducer -> type("first"));
}
public void testPlaceInsideLambdaBodyMultipleOccurrences1() {
doTestReplaceChoice("Replace all 0 occurrences", "Runnable: () -> {...}", introducer -> type("expr"), null);
}