From 4ec220cca77ab1ac7ef6c2bf2db7c0d927e4f029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20C=C3=A9bron?= Date: Mon, 16 Sep 2024 14:56:14 +0200 Subject: [PATCH] [devkit] add DevKitInspectionUtil.isAllowedIncludingTestSources() GitOrigin-RevId: 79aa1d443b859c91060b005ff48b05daeb3e4353 --- plugins/devkit/README.md | 2 ++ .../src/inspections/DevKitInspectionUtil.java | 15 +++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/plugins/devkit/README.md b/plugins/devkit/README.md index 8cea1dbfacc9..b508c1cc7de1 100644 --- a/plugins/devkit/README.md +++ b/plugins/devkit/README.md @@ -3,6 +3,8 @@ ## Inspections See `org.jetbrains.idea.devkit.inspections.DevKitInspectionUtil` for common utility methods. +By default, files located in _test sources_ are not checked, +use `DevKitInspectionUtil.isAllowedIncludingTestSources()` to include them. Consider marking inspections with _safe-only_ fixes ready for _Code | Code Cleanup..._ (`com.intellij.codeInspection.CleanupLocalInspectionTool`). diff --git a/plugins/devkit/devkit-core/src/inspections/DevKitInspectionUtil.java b/plugins/devkit/devkit-core/src/inspections/DevKitInspectionUtil.java index 42a41460f204..703910ebdb35 100644 --- a/plugins/devkit/devkit-core/src/inspections/DevKitInspectionUtil.java +++ b/plugins/devkit/devkit-core/src/inspections/DevKitInspectionUtil.java @@ -19,23 +19,30 @@ import java.util.function.Predicate; public final class DevKitInspectionUtil { static boolean isAllowedInPluginsOnly(@NotNull PsiFile file) { - return isAllowed(file, DevKitInspectionUtil::isPluginFile); + return isAllowed(file, false, DevKitInspectionUtil::isPluginFile); } + /** + * @see #isAllowedIncludingTestSources(PsiFile) + */ public static boolean isAllowed(@NotNull PsiFile file) { - return isAllowed(file, __ -> true); + return isAllowed(file, false, __ -> true); + } + + public static boolean isAllowedIncludingTestSources(@NotNull PsiFile file) { + return isAllowed(file, true, ___ -> true); } public static boolean isClassAvailable(@NotNull ProblemsHolder holder, @NonNls String classFqn) { return JavaPsiFacade.getInstance(holder.getProject()).findClass(classFqn, holder.getFile().getResolveScope()) != null; } - private static boolean isAllowed(@NotNull PsiFile file, @NotNull Predicate predicate) { + private static boolean isAllowed(@NotNull PsiFile file, boolean allowedInTestSources, @NotNull Predicate predicate) { if (ApplicationManager.getApplication().isUnitTestMode()) return true; // always run in tests VirtualFile vFile = file.getVirtualFile(); if (vFile == null) return false; - if (TestSourcesFilter.isTestSources(vFile, file.getProject())) return false; + if (!allowedInTestSources && TestSourcesFilter.isTestSources(vFile, file.getProject())) return false; if (IntelliJProjectUtil.isIntelliJPlatformProject(file.getProject())) { return predicate.test(file);