[extract method] allow to extract a lambda expression with an assignment inside (IDEA-347866)

GitOrigin-RevId: 4258330bf27d70e942dc84d53c62842ac682fba4
This commit is contained in:
Bas Leijdekkers
2025-06-02 09:10:22 +02:00
committed by intellij-monorepo-bot
parent 9dde446e22
commit d5ca38c197
4 changed files with 66 additions and 3 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.refactoring.extractMethod.newImpl
import com.intellij.codeInsight.CodeInsightUtil
@@ -90,7 +90,14 @@ class ExtractSelector {
}
private fun hasAssignmentInside(expression: PsiExpression): Boolean {
return PsiTreeUtil.findChildOfType(expression, PsiAssignmentExpression::class.java, false) != null
val assignment = PsiTreeUtil.findChildOfType(expression, PsiAssignmentExpression::class.java, false)
if (assignment == null) return false
val lhs = assignment.lExpression
if (lhs is PsiReferenceExpression) {
val target = lhs.resolve()
return target != null && !expression.textRange.contains(target.textRange)
}
return false
}
private fun alignStatements(statements: List<PsiElement>): List<PsiElement> {

View File

@@ -0,0 +1,23 @@
class AssignmentInsideLamda {
private final List<Comparator<String>> comparators = new ArrayList<>();
public void test(String[] args) {
Arrays.sort(args, <selection>(o1, o2) -> {
int result = compareForNull(o1, o2);
if (result == 0) {
for (Comparator<String> comparator : comparators) {
result = comparator.compare(o1, o2);
if (result != 0) {
return result;
}
}
}
return result;
}</selection>);
}
private int compareForNull(String o1, String o2) {
return 0;
}
}

View File

@@ -0,0 +1,29 @@
import org.jetbrains.annotations.NotNull;
class AssignmentInsideLamda {
private final List<Comparator<String>> comparators = new ArrayList<>();
public void test(String[] args) {
Arrays.sort(args, newMethod());
}
private @NotNull Object newMethod() {
return (o1, o2) -> {
int result = compareForNull(o1, o2);
if (result == 0) {
for (Comparator<String> comparator : comparators) {
result = comparator.compare(o1, o2);
if (result != 0) {
return result;
}
}
}
return result;
};
}
private int compareForNull(String o1, String o2) {
return 0;
}
}

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-2025 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.JavaTestUtil;
@@ -285,6 +285,10 @@ public class ExtractMethodNewTest extends LightJavaCodeInsightTestCase {
}
}
public void testAssignmentInsideLambda() throws Exception {
doTest();
}
public void testExtractFromTryFinally2() throws Exception {
doTest();
}