void () { // sick and tired of hunting tests leaking documents ((UndoManagerImpl)UndoManager.getInstance(getProject())).flushCurrentCommandMerger(); Set<Object> alreadyLeaking = new ReferenceOpenHashSet<>(); Predicate<Object> isReallyLeak = file -> { if (file instanceof PsiFile) { if (!((PsiFile)file).isPhysical()) { return false; } Project project = ((PsiFile)file).getProject(); if (alreadyLeaking.add(project)) { System.err.println(project + " already leaking; its creation trace: " + ((ProjectEx)project).getCreationTrace()); } } alreadyLeaking.add(file); return false; }; LeakHunter.checkLeak(ApplicationManager.getApplication(), PsiFileImpl.class, isReallyLeak); LeakHunter.checkLeak(ApplicationManager.getApplication(), Document.class, isReallyLeak); @Language("JAVA") String text = "public class X{} //iuggjhfg"; PsiFile psiFile = myFixture.addFileToProject("X.java", text); Usage[] usages = new Usage[100]; for (int i = 0; i < usages.length; i++) { usages[i] = createUsage(psiFile, i); } UsageView usageView = createUsageView(usages); TestApplicationKt.clearEncodingManagerDocumentQueue(ApplicationManager.getApplication()); FileDocumentManager.getInstance().saveAllDocuments(); UIUtil.dispatchAllInvocationEvents(); LeakHunter.checkLeak(usageView, PsiFileImpl.class, file -> !alreadyLeaking.contains(file) && file.isPhysical()); LeakHunter.checkLeak(usageView, Document.class, document -> !alreadyLeaking.contains(document)); }