diff --git a/java/java-impl/src/com/intellij/codeInspection/enhancedSwitch/SwitchLabeledRuleCanBeCodeBlockInspection.java b/java/java-impl/src/com/intellij/codeInspection/enhancedSwitch/SwitchLabeledRuleCanBeCodeBlockInspection.java index 6375149808d1..f45f50fa4bfd 100644 --- a/java/java-impl/src/com/intellij/codeInspection/enhancedSwitch/SwitchLabeledRuleCanBeCodeBlockInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/enhancedSwitch/SwitchLabeledRuleCanBeCodeBlockInspection.java @@ -8,8 +8,10 @@ import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.codeInspection.ProblemsHolder; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.project.Project; +import com.intellij.pom.java.LanguageLevel; import com.intellij.profile.codeInspection.InspectionProjectProfileManager; import com.intellij.psi.*; +import com.intellij.psi.util.PsiUtil; import com.intellij.util.ObjectUtils; import com.siyeh.ig.psiutils.CommentTracker; import org.jetbrains.annotations.Nls; @@ -102,7 +104,8 @@ public class SwitchLabeledRuleCanBeCodeBlockInspection extends LocalInspectionTo private static void wrapExpression(PsiExpressionStatement expressionStatement) { CommentTracker tracker = new CommentTracker(); - tracker.replaceAndRestoreComments(expressionStatement, "{ break " + tracker.text(expressionStatement) + " }"); + String valueKeyword = PsiUtil.getLanguageLevel(expressionStatement) == LanguageLevel.JDK_12_PREVIEW ? PsiKeyword.BREAK : PsiKeyword.YIELD; + tracker.replaceAndRestoreComments(expressionStatement, "{ " + valueKeyword + " " + tracker.text(expressionStatement) + " }"); } private static void wrapStatement(@NotNull PsiStatement statement) { diff --git a/java/java-impl/src/com/intellij/psi/formatter/java/JavaSpacePropertyProcessor.java b/java/java-impl/src/com/intellij/psi/formatter/java/JavaSpacePropertyProcessor.java index 2eb3f79e68ee..c346998cff85 100644 --- a/java/java-impl/src/com/intellij/psi/formatter/java/JavaSpacePropertyProcessor.java +++ b/java/java-impl/src/com/intellij/psi/formatter/java/JavaSpacePropertyProcessor.java @@ -833,6 +833,13 @@ public class JavaSpacePropertyProcessor extends JavaElementVisitor { } } + @Override + public void visitYieldStatement(PsiYieldStatement statement) { + if (myType1 == JavaTokenType.YIELD_KEYWORD && ElementType.EXPRESSION_BIT_SET.contains(myType2)) { + createSpaceProperty(true, false, 0); + } + } + @Override public void visitContinueStatement(PsiContinueStatement statement) { if (myType1 == JavaTokenType.CONTINUE_KEYWORD && myType2 == JavaTokenType.IDENTIFIER) { diff --git a/java/java-tests/testData/inspection/switchLabeledRuleCanBeCodeBlockFix/afterCallInExpression.java b/java/java-tests/testData/inspection/switchLabeledRuleCanBeCodeBlockFix/afterCallInExpression.java index 584529a3c9df..5e831ba8e4ed 100644 --- a/java/java-tests/testData/inspection/switchLabeledRuleCanBeCodeBlockFix/afterCallInExpression.java +++ b/java/java-tests/testData/inspection/switchLabeledRuleCanBeCodeBlockFix/afterCallInExpression.java @@ -3,7 +3,7 @@ class C { String foo(int n) { return switch (n) { case 1 -> /*1*/{ - break Integer.toString(/*2*/n); + yield Integer.toString(/*2*/n); }/*3*/ default -> "b"; }; diff --git a/java/java-tests/testData/inspection/switchLabeledRuleCanBeCodeBlockFix/afterConstInExpression.java b/java/java-tests/testData/inspection/switchLabeledRuleCanBeCodeBlockFix/afterConstInExpression.java index e2a80e650473..099a49067e57 100644 --- a/java/java-tests/testData/inspection/switchLabeledRuleCanBeCodeBlockFix/afterConstInExpression.java +++ b/java/java-tests/testData/inspection/switchLabeledRuleCanBeCodeBlockFix/afterConstInExpression.java @@ -3,7 +3,7 @@ class C { String foo(int n) { return switch (n) { case 1 -> /*1*/ { - break "a"; + yield "a"; } /*2*/ default -> "b"; }; diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/enhancedSwitch/SwitchLabeledRuleCanBeCodeBlockFixTest.kt b/java/java-tests/testSrc/com/intellij/java/codeInspection/enhancedSwitch/SwitchLabeledRuleCanBeCodeBlockFixTest.kt index 1945049e158c..0526ee6c0876 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/enhancedSwitch/SwitchLabeledRuleCanBeCodeBlockFixTest.kt +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/enhancedSwitch/SwitchLabeledRuleCanBeCodeBlockFixTest.kt @@ -14,7 +14,7 @@ class SwitchLabeledRuleCanBeCodeBlockFixTest : LightQuickFixParameterizedTestCas override fun configureLocalInspectionTools(): Array = arrayOf(SwitchLabeledRuleCanBeCodeBlockInspection()) - override fun getProjectDescriptor(): LightProjectDescriptor = LightJavaCodeInsightFixtureTestCase.JAVA_12 + override fun getProjectDescriptor(): LightProjectDescriptor = LightJavaCodeInsightFixtureTestCase.JAVA_13 override fun getBasePath() = "/inspection/switchLabeledRuleCanBeCodeBlockFix" }