add checkPsiMatchesTextIgnoringNonCode and fix detected code block/block statement confusion

This commit is contained in:
peter
2017-09-02 08:53:37 +02:00
parent 6e9d9c597b
commit 9c1baaeada
4 changed files with 31 additions and 8 deletions
@@ -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;
}
@@ -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() {
@@ -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.
@@ -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) {