diff --git a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java index 68865e05fd31..2adbd7d1fd02 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java +++ b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java @@ -225,9 +225,10 @@ public class SimplifyBooleanExpressionFix extends LocalQuickFixOnPsiElement { return name != null && PsiResolveHelper.SERVICE.getInstance(declaration.getProject()).resolveAccessibleReferencedVariable(name, parent) != null; } - private static PsiCodeBlock wrapWithCodeBlock(PsiStatement replacement) { - PsiCodeBlock newBlock = JavaPsiFacade.getElementFactory(replacement.getProject()).createCodeBlock(); - newBlock.add(replacement); + private static PsiBlockStatement wrapWithCodeBlock(PsiStatement replacement) { + PsiBlockStatement newBlock = (PsiBlockStatement) + JavaPsiFacade.getElementFactory(replacement.getProject()).createStatementFromText("{}", null); + newBlock.getCodeBlock().add(replacement); return newBlock; } diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTest.java index f8f9c2691095..05697b4e3cf6 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTest.java @@ -22,6 +22,7 @@ import com.intellij.psi.PsiElement; import com.intellij.psi.PsiField; import com.intellij.testFramework.LightProjectDescriptor; import com.intellij.testFramework.PlatformTestUtil; +import com.intellij.testFramework.PsiTestUtil; import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture; import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; @@ -167,6 +168,7 @@ public class DataFlowInspectionTest extends DataFlowInspectionTestCase { private void checkIntentionResult(String hint) { myFixture.launchAction(myFixture.findSingleIntention(hint)); myFixture.checkResultByFile(getTestName(false) + "_after.java"); + PsiTestUtil.checkPsiMatchesTextIgnoringNonCode(getFile()); } public void testReportConstantReferences_OverloadedCall() { diff --git a/platform/core-impl/src/com/intellij/psi/impl/DebugUtil.java b/platform/core-impl/src/com/intellij/psi/impl/DebugUtil.java index 47db06270316..f22abc8f1240 100644 --- a/platform/core-impl/src/com/intellij/psi/impl/DebugUtil.java +++ b/platform/core-impl/src/com/intellij/psi/impl/DebugUtil.java @@ -28,7 +28,10 @@ import com.intellij.openapi.util.UserDataHolderBase; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.*; import com.intellij.psi.impl.source.SourceTreeToPsiMap; -import com.intellij.psi.impl.source.tree.*; +import com.intellij.psi.impl.source.tree.CompositeElement; +import com.intellij.psi.impl.source.tree.RecursiveTreeElementWalkingVisitor; +import com.intellij.psi.impl.source.tree.SharedImplUtil; +import com.intellij.psi.impl.source.tree.TreeElement; import com.intellij.psi.stubs.ObjectStubSerializer; import com.intellij.psi.stubs.Stub; import com.intellij.psi.tree.IElementType; @@ -124,7 +127,7 @@ public class DebugUtil { @Override protected void visitNode(TreeElement root) { - if (skipWhiteSpaces && root.getElementType() == TokenType.WHITE_SPACE) { + if (shouldSkipNode(root)) { indent += 2; return; } @@ -164,6 +167,10 @@ public class DebugUtil { super.visitNode(root); } + protected boolean shouldSkipNode(TreeElement node) { + return skipWhiteSpaces && node.getElementType() == TokenType.WHITE_SPACE; + } + @Override protected void elementFinished(@NotNull ASTNode e) { PsiElement psiElement = extra != null && usePsi && e instanceof CompositeElement ? e.getPsi() : null; @@ -397,6 +404,19 @@ public class DebugUtil { return buffer.toString(); } + @NotNull + public static String psiToStringIgnoringNonCode(@NotNull PsiElement element) { + StringBuilder buffer = new StringBuilder(); + ((TreeElement)element.getNode()).acceptTree( + new TreeToBuffer(buffer, 0, true, false, false, true, null) { + @Override + protected boolean shouldSkipNode(TreeElement node) { + return super.shouldSkipNode(node) || node instanceof PsiErrorElement || node instanceof PsiComment; + } + }); + return buffer.toString(); + } + private static void psiToBuffer(final Appendable buffer, final PsiElement root, final boolean skipWhiteSpaces, @@ -485,7 +505,7 @@ public class DebugUtil { /** * Marks a start of PSI modification action. Any PSI/AST elements invalidated inside such an action will contain a debug trace - * identifying this transaction, and so will {@link com.intellij.psi.PsiInvalidElementAccessException} thrown when accessing such invalid + * identifying this transaction, and so will {@link PsiInvalidElementAccessException} thrown when accessing such invalid * elements. This should help finding out why a specific PSI element has become invalid. * * @param trace The debug trace that the invalidated elements should be identified by. May be null, then current stack trace is used. diff --git a/platform/testFramework/src/com/intellij/testFramework/PsiTestUtil.java b/platform/testFramework/src/com/intellij/testFramework/PsiTestUtil.java index 041b928928a5..328d35146c39 100644 --- a/platform/testFramework/src/com/intellij/testFramework/PsiTestUtil.java +++ b/platform/testFramework/src/com/intellij/testFramework/PsiTestUtil.java @@ -234,8 +234,8 @@ public class PsiTestUtil { return Objects.requireNonNull(file.getManager().findFile(copy)); } - public static void checkPsiMatchesTextIgnoringWhitespace(PsiFile file) { - compareFromAllRoots(file, f -> DebugUtil.psiTreeToString(f, true)); + public static void checkPsiMatchesTextIgnoringNonCode(PsiFile file) { + compareFromAllRoots(file, f -> DebugUtil.psiToStringIgnoringNonCode(f)); } public static void addLibrary(Module module, String libPath) {