[java-intentions] ConvertSwitchToIfIntention: fix brace handling

Fixes EA-795482 - IOE: JavaParserUtil.parseFragment

GitOrigin-RevId: 63fe89da84e4432fab8f8f191c139add97553640
This commit is contained in:
Tagir Valeev
2023-02-13 10:13:31 +01:00
committed by intellij-monorepo-bot
parent e947f7b9a8
commit 2b2d79fb6a
7 changed files with 68 additions and 2 deletions

View File

@@ -477,8 +477,8 @@ public class ConvertSwitchToIfIntention implements IntentionActionWithFixAllOpti
for (PsiElement bodyStatement : bodyStatements) {
if (bodyStatement instanceof PsiBlockStatement blockStatement) {
final PsiCodeBlock codeBlock = blockStatement.getCodeBlock();
PsiElement start = PsiTreeUtil.skipWhitespacesForward(codeBlock.getFirstBodyElement());
PsiElement end = PsiTreeUtil.skipWhitespacesBackward(codeBlock.getLastBodyElement());
PsiElement start = PsiTreeUtil.skipWhitespacesForward(codeBlock.getLBrace());
PsiElement end = PsiTreeUtil.skipWhitespacesBackward(codeBlock.getRBrace());
if (start != null && end != null && start != codeBlock.getRBrace()) {
for (PsiElement child = start; child != null; child = child.getNextSibling()) {
out.append(commentTracker.text(child));

View File

@@ -0,0 +1,12 @@
// "Replace 'switch' with 'if'" "true-preview"
class X {
void test(int x) {
if (x == 1) {
{
System.out.println("hello");
}
} else if (x == 2) {
System.out.println("oops");
}
}
}

View File

@@ -0,0 +1,12 @@
// "Replace 'switch' with 'if'" "true-preview"
class X {
void test(int x) {
if (x == 1) {
{
System.out.println("hello");
}
} else if (x == 2) {
System.out.println("oops");
}
}
}

View File

@@ -0,0 +1,10 @@
// "Replace 'switch' with 'if'" "true-preview"
class X {
void test(int x) {
if (x == 1) {
System.out.println("");
} else if (x == 2) {
System.out.println("oops");
}
}
}

View File

@@ -0,0 +1,11 @@
// "Replace 'switch' with 'if'" "true-preview"
class X {
void test(int x) {
<caret>switch (x) {
case 1 -> {{
System.out.println("hello");
}}
case 2 -> System.out.println("oops");
}
}
}

View File

@@ -0,0 +1,12 @@
// "Replace 'switch' with 'if'" "true-preview"
class X {
void test(int x) {
<caret>switch (x) {
case 1 -> {
{
System.out.println("hello");
}}
case 2 -> System.out.println("oops");
}
}
}

View File

@@ -0,0 +1,9 @@
// "Replace 'switch' with 'if'" "true-preview"
class X {
void test(int x) {
<caret>switch (x) {
case 1 -> { System.out.println("");}
case 2 -> System.out.println("oops");
}
}
}