[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
This commit is contained in:
Sergei Tachenov
2025-09-11 16:15:24 +00:00
committed by intellij-monorepo-bot
parent dc8b07ef9c
commit 4d6ab68e09
@@ -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));
}