mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-05-06 05:10:22 +07:00
inline method: allow to inline one liners in loop conditions fixed (IDEA-255057)
GitOrigin-RevId: 72c7c6ec9865984b997a05c2b8756771aa388ddb
This commit is contained in:
committed by
intellij-monorepo-bot
parent
838558d8f1
commit
5697a07aae
@@ -1151,8 +1151,8 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor {
|
||||
}
|
||||
return checkUnableToInsertCodeBlock(methodBody, element,
|
||||
expr -> {
|
||||
PsiElement parent = expr.getParent();
|
||||
return parent instanceof PsiLoopStatement && PsiUtil.isCondition(expr, parent);
|
||||
PsiConditionalLoopStatement loopStatement = PsiTreeUtil.getParentOfType(expr, PsiConditionalLoopStatement.class);
|
||||
return loopStatement != null && PsiTreeUtil.isAncestor(loopStatement.getCondition(), expr, false);
|
||||
})
|
||||
? JavaRefactoringBundle.message("inline.method.multiline.method.in.loop.condition")
|
||||
: null;
|
||||
@@ -1162,7 +1162,9 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor {
|
||||
final PsiElement element,
|
||||
final Predicate<? super PsiMethodCallExpression> errorCondition) {
|
||||
PsiStatement[] statements = methodBody.getStatements();
|
||||
if (statements.length > 1 || statements.length == 1 && !(statements[0] instanceof PsiExpressionStatement)) {
|
||||
if (statements.length > 1 || statements.length == 1 &&
|
||||
!(statements[0] instanceof PsiExpressionStatement) &&
|
||||
!(statements[0] instanceof PsiReturnStatement)) {
|
||||
PsiMethodCallExpression expr = PsiTreeUtil.getParentOfType(element, PsiMethodCallExpression.class, true, PsiStatement.class);
|
||||
while (expr != null) {
|
||||
if (errorCondition.test(expr)) {
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
class MyWorld {
|
||||
public boolean isH<caret>ello(Object o) {
|
||||
return o instanceof String && ((String)o).startsWith("hello");
|
||||
}
|
||||
|
||||
public void process(Object[] o) {
|
||||
int i = 0;
|
||||
while (isHello(o[i])) {
|
||||
i++;
|
||||
}
|
||||
i = 0;
|
||||
while (!isHello(o[i])) {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
class MyWorld {
|
||||
|
||||
public void process(Object[] o) {
|
||||
int i = 0;
|
||||
while (o[i] instanceof String && ((String) o[i]).startsWith("hello")) {
|
||||
i++;
|
||||
}
|
||||
i = 0;
|
||||
while (!(o[i] instanceof String && ((String) o[i]).startsWith("hello"))) {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -237,6 +237,10 @@ public class InlineMethodTest extends LightRefactoringTestCase {
|
||||
doTestConflict("Inlined method is used reflectively");
|
||||
}
|
||||
|
||||
public void testInlineOnelinerToCondition() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testNotAStatement() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user