[devkit] add DevKitInspectionUtil.isAllowedIncludingTestSources()

GitOrigin-RevId: 79aa1d443b859c91060b005ff48b05daeb3e4353
This commit is contained in:
Yann Cébron
2024-09-16 15:37:31 +00:00
committed by intellij-monorepo-bot
parent c197b13d6e
commit 4ec220cca7
2 changed files with 13 additions and 4 deletions
+2
View File
@@ -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`).
@@ -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<? super PsiFile> predicate) {
private static boolean isAllowed(@NotNull PsiFile file, boolean allowedInTestSources, @NotNull Predicate<? super PsiFile> 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);