From 4d6ab68e09de4e127cbf38bd99829ce15c58a8d3 Mon Sep 17 00:00:00 2001 From: Sergei Tachenov Date: Thu, 11 Sep 2025 16:24:11 +0300 Subject: [PATCH] [platform, core] IJPL-206569 Don't defer PSI icons in tests For some reason, a couple of tests in CustomScopePaneTest failed in a really strange way: the top directory of the project stopped being recognized by the project index as a part of the project content. It's not clear why that happened, but it seems like a nice idea to disable icon deferring in tests, because unlike in production, test may never actually try to paint the returned icon, and therefore the actual icon computation won't be tested. GitOrigin-RevId: 11f91e5395ce47afc2356e562bfd214e08f8c8e2 --- .../src/com/intellij/psi/impl/ElementBase.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/platform/core-impl/src/com/intellij/psi/impl/ElementBase.java b/platform/core-impl/src/com/intellij/psi/impl/ElementBase.java index 5c4d37cf363a..f79da01abbc7 100644 --- a/platform/core-impl/src/com/intellij/psi/impl/ElementBase.java +++ b/platform/core-impl/src/com/intellij/psi/impl/ElementBase.java @@ -3,6 +3,7 @@ package com.intellij.psi.impl; import com.intellij.ide.FileIconUtil; import com.intellij.navigation.ItemPresentation; +import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.fileTypes.FileType; import com.intellij.openapi.fileTypes.INativeFileType; @@ -74,7 +75,7 @@ public abstract class ElementBase extends UserDataHolderBase implements Iconable PsiElement psiElement = (PsiElement)this; if (!psiElement.isValid()) return null; - if (Registry.is("psi.deferIconLoading", true)) { + if (shouldDeferIcon()) { Icon baseIcon = LastComputedIconCache.get(psiElement, flags); if (baseIcon == null) { baseIcon = AstLoadingFilter.disallowTreeLoading(() -> computeBaseIcon(flags)); @@ -89,6 +90,14 @@ public abstract class ElementBase extends UserDataHolderBase implements Iconable return computeIconNow(psiElement, flags); } + private static boolean shouldDeferIcon() { + if (!Registry.is("psi.deferIconLoading", true)) return false; + if (EDT.isCurrentThreadEdt()) return true; + // Unit tests often don't create actual UI, so the deferred icon is never resolved. + // Sometimes this causes tests to fail in really unpredictable ways. + return !ApplicationManager.getApplication().isUnitTestMode(); + } + private static @Nullable Icon computeIconNow(@NotNull PsiElement element, @Iconable.IconFlags int flags) { return AstLoadingFilter.disallowTreeLoading(() -> doComputeIconNow(element, flags)); }