From b9e8f2856f53cb79c71b569ee303a744c764d257 Mon Sep 17 00:00:00 2001 From: Alexander Zolotov Date: Wed, 20 Jul 2016 14:09:07 +0300 Subject: [PATCH] TestSourcesFilter: update after review --- .../com/intellij/util/xml/DomJavaUtil.java | 8 ++---- .../values/ClassValueConverter.java | 6 ++-- .../BackwardDependenciesBuilder.java | 10 ++----- .../file/impl/ResolveScopeManagerImpl.java | 2 +- .../ide/util/PlatformPackageUtil.java | 14 ++++------ .../com/intellij/openapi/roots/FileIndex.java | 9 ++++-- .../openapi/roots/TestSourcesFilter.java | 2 +- .../coverage/JavaCoverageAnnotator.java | 25 ++++++++++++----- .../intellij/coverage/JavaCoverageEngine.java | 28 ++++++++++++++----- .../util/xml/AbstractConvertContext.java | 9 ++---- 10 files changed, 66 insertions(+), 47 deletions(-) diff --git a/java/openapi/src/com/intellij/util/xml/DomJavaUtil.java b/java/openapi/src/com/intellij/util/xml/DomJavaUtil.java index f3e2baca83d66..ea773b2a64e4f 100644 --- a/java/openapi/src/com/intellij/util/xml/DomJavaUtil.java +++ b/java/openapi/src/com/intellij/util/xml/DomJavaUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,8 +16,7 @@ package com.intellij.util.xml; import com.intellij.openapi.module.Module; -import com.intellij.openapi.roots.ProjectFileIndex; -import com.intellij.openapi.roots.ProjectRootManager; +import com.intellij.openapi.roots.TestSourcesFilter; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.JavaPsiFacade; import com.intellij.psi.PsiClass; @@ -89,7 +88,6 @@ private static GlobalSearchScope calcScope(@NotNull PsiFile file, @Nullable Modu return GlobalSearchScope.moduleRuntimeScope(module, true); } - ProjectFileIndex fileIndex = ProjectRootManager.getInstance(file.getProject()).getFileIndex(); - return module.getModuleRuntimeScope(fileIndex.isInTestSourceContent(virtualFile)); + return module.getModuleRuntimeScope(TestSourcesFilter.isTestSources(virtualFile, file.getProject())); } } diff --git a/java/openapi/src/com/intellij/util/xml/converters/values/ClassValueConverter.java b/java/openapi/src/com/intellij/util/xml/converters/values/ClassValueConverter.java index d3a70c761f752..360ccfcc230f7 100644 --- a/java/openapi/src/com/intellij/util/xml/converters/values/ClassValueConverter.java +++ b/java/openapi/src/com/intellij/util/xml/converters/values/ClassValueConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,7 +19,7 @@ import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; -import com.intellij.openapi.roots.ProjectRootManager; +import com.intellij.openapi.roots.TestSourcesFilter; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiElement; @@ -62,7 +62,7 @@ public static GlobalSearchScope getScope(Project project, @Nullable Module modul if (file == null) { return ProjectScope.getAllScope(project); } - final boolean inTests = ProjectRootManager.getInstance(project).getFileIndex().isInTestSourceContent(file); + final boolean inTests = TestSourcesFilter.isTestSources(file, project); return module.getModuleRuntimeScope(inTests); } diff --git a/platform/analysis-impl/src/com/intellij/packageDependencies/BackwardDependenciesBuilder.java b/platform/analysis-impl/src/com/intellij/packageDependencies/BackwardDependenciesBuilder.java index 3f72c3fdc55f2..fce508bbc608b 100644 --- a/platform/analysis-impl/src/com/intellij/packageDependencies/BackwardDependenciesBuilder.java +++ b/platform/analysis-impl/src/com/intellij/packageDependencies/BackwardDependenciesBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,14 +23,11 @@ import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.project.Project; -import com.intellij.openapi.roots.ProjectFileIndex; -import com.intellij.openapi.roots.ProjectRootManager; +import com.intellij.openapi.roots.TestSourcesFilter; import com.intellij.openapi.util.Computable; -import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.PsiManager; -import com.intellij.util.Processor; import org.jetbrains.annotations.Nullable; import java.util.HashSet; @@ -88,9 +85,8 @@ public void analyze() { try { final int fileCount = getScope().getFileCount(); final boolean includeTestSource = getScope().isIncludeTestSource(); - final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(getProject()).getFileIndex(); getScope().accept(virtualFile -> { - if (!includeTestSource && fileIndex.isInTestSourceContent(virtualFile)) { + if (!includeTestSource && TestSourcesFilter.isTestSources(virtualFile, getProject())) { return true; } ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator(); diff --git a/platform/indexing-impl/src/com/intellij/psi/impl/file/impl/ResolveScopeManagerImpl.java b/platform/indexing-impl/src/com/intellij/psi/impl/file/impl/ResolveScopeManagerImpl.java index b99a71e5b44a7..a6d381a73da90 100644 --- a/platform/indexing-impl/src/com/intellij/psi/impl/file/impl/ResolveScopeManagerImpl.java +++ b/platform/indexing-impl/src/com/intellij/psi/impl/file/impl/ResolveScopeManagerImpl.java @@ -185,7 +185,7 @@ public GlobalSearchScope getUseScope(@NotNull PsiElement element) { return containingFile == null || virtualFile.isDirectory() || result.contains(virtualFile) ? result : GlobalSearchScope.fileScope(containingFile).uniteWith(result); } - boolean isTest = projectFileIndex.isInTestSourceContent(vDirectory); + boolean isTest = TestSourcesFilter.isTestSources(vDirectory, myProject); GlobalSearchScope scope = isTest ? GlobalSearchScope.moduleTestsWithDependentsScope(module) : GlobalSearchScope.moduleWithDependentsScope(module); diff --git a/platform/lang-impl/src/com/intellij/ide/util/PlatformPackageUtil.java b/platform/lang-impl/src/com/intellij/ide/util/PlatformPackageUtil.java index 696e4e711fa02..c07bc47fc86ac 100644 --- a/platform/lang-impl/src/com/intellij/ide/util/PlatformPackageUtil.java +++ b/platform/lang-impl/src/com/intellij/ide/util/PlatformPackageUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,13 +22,12 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.project.ProjectBundle; import com.intellij.openapi.roots.ModuleRootManager; -import com.intellij.openapi.roots.ProjectFileIndex; import com.intellij.openapi.roots.ProjectRootManager; +import com.intellij.openapi.roots.TestSourcesFilter; import com.intellij.openapi.roots.impl.DirectoryIndex; import com.intellij.openapi.roots.ui.configuration.CommonContentEntriesEditor; import com.intellij.openapi.roots.ui.configuration.ProjectSettingsService; import com.intellij.openapi.ui.Messages; -import com.intellij.openapi.util.Condition; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiElement; @@ -36,7 +35,7 @@ import com.intellij.psi.PsiManager; import com.intellij.psi.impl.source.resolve.FileContextUtil; import com.intellij.psi.search.GlobalSearchScope; -import com.intellij.psi.search.GlobalSearchScopes; +import com.intellij.psi.search.GlobalSearchScopesCore; import com.intellij.util.*; import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; @@ -181,15 +180,14 @@ public static GlobalSearchScope adjustScope(PsiDirectory baseDir, boolean skipSourceDirsForBaseTestDirectory, boolean skipTestDirsForBaseSourceDirectory) { if (baseDir != null) { - final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(baseDir.getProject()).getFileIndex(); - if (fileIndex.isInTestSourceContent(baseDir.getVirtualFile())) { + if (TestSourcesFilter.isTestSources(baseDir.getVirtualFile(), baseDir.getProject())) { if (skipSourceDirsForBaseTestDirectory) { - return scope.intersectWith(GlobalSearchScopes.projectTestScope(baseDir.getProject())); + return scope.intersectWith(GlobalSearchScopesCore.projectTestScope(baseDir.getProject())); } } else { if (skipTestDirsForBaseSourceDirectory) { - return scope.intersectWith(GlobalSearchScopes.projectProductionScope(baseDir.getProject())); + return scope.intersectWith(GlobalSearchScopesCore.projectProductionScope(baseDir.getProject())); } } } diff --git a/platform/projectModel-api/src/com/intellij/openapi/roots/FileIndex.java b/platform/projectModel-api/src/com/intellij/openapi/roots/FileIndex.java index a53a4bfbc7415..d2182f033471f 100644 --- a/platform/projectModel-api/src/com/intellij/openapi/roots/FileIndex.java +++ b/platform/projectModel-api/src/com/intellij/openapi/roots/FileIndex.java @@ -78,11 +78,16 @@ public interface FileIndex { /** * Returns true if fileOrDir is a file or directory from the test content source + *

+ * Use this method when you really need to check whether the file is under test roots according to project configuration. + *

+ * If you want to determine whether file should be considered as test (e.g. for implementing SearchScope) + * you'd better use {@link TestSourcesFilter#isTestSources(VirtualFile, Project)} instead + * which includes {@link ProjectFileIndex#isInTestSourceContent(VirtualFile)} invocation. * - * @see TestSourcesFilter#isTestSources(VirtualFile, Project) - * * @param fileOrDir the file or directory to check. * @return true if the file or directory belongs to a test source root, false otherwise. + * @see TestSourcesFilter#isTestSources(VirtualFile, Project) */ boolean isInTestSourceContent(@NotNull VirtualFile fileOrDir); diff --git a/platform/projectModel-api/src/com/intellij/openapi/roots/TestSourcesFilter.java b/platform/projectModel-api/src/com/intellij/openapi/roots/TestSourcesFilter.java index be20208355334..2a5ce8c2db919 100644 --- a/platform/projectModel-api/src/com/intellij/openapi/roots/TestSourcesFilter.java +++ b/platform/projectModel-api/src/com/intellij/openapi/roots/TestSourcesFilter.java @@ -21,7 +21,7 @@ import org.jetbrains.annotations.NotNull; /** - * Implementations of this extension point can tell IDE whether some particular file is a test file despite project roots configuration. + * Implementations of this extension point can tell IDE whether some particular file is a test file. *

* By default, IntelliJ Platform considers files as tests only if they are located under test * sources root {@link FileIndex#isInTestSourceContent(VirtualFile)}. diff --git a/plugins/coverage/src/com/intellij/coverage/JavaCoverageAnnotator.java b/plugins/coverage/src/com/intellij/coverage/JavaCoverageAnnotator.java index d3db8c84c6df3..0abed78a1fd87 100644 --- a/plugins/coverage/src/com/intellij/coverage/JavaCoverageAnnotator.java +++ b/plugins/coverage/src/com/intellij/coverage/JavaCoverageAnnotator.java @@ -1,11 +1,25 @@ +/* + * Copyright 2000-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.intellij.coverage; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; -import com.intellij.openapi.roots.ProjectFileIndex; -import com.intellij.openapi.roots.ProjectRootManager; +import com.intellij.openapi.roots.TestSourcesFilter; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.*; import com.intellij.psi.search.GlobalSearchScope; @@ -47,16 +61,13 @@ public String getDirCoverageInformationString(@NotNull final PsiDirectory direct final PsiPackage psiPackage = JavaDirectoryService.getInstance().getPackage(directory); if (psiPackage == null) return null; - final ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(directory.getProject()).getFileIndex(); final VirtualFile virtualFile = directory.getVirtualFile(); - final boolean isInTestContent = projectFileIndex.isInTestSourceContent(virtualFile); - + final boolean isInTestContent = TestSourcesFilter.isTestSources(virtualFile, directory.getProject()); if (!currentSuite.isTrackTestFolders() && isInTestContent) { return null; } - return isInTestContent ? getCoverageInformationString(myTestDirCoverageInfos.get(virtualFile), coverageDataManager.isSubCoverageActive()) - : getCoverageInformationString(myDirCoverageInfos.get(virtualFile), coverageDataManager.isSubCoverageActive()); + return getCoverageInformationString(isInTestContent ? myTestDirCoverageInfos.get(virtualFile) : myDirCoverageInfos.get(virtualFile), coverageDataManager.isSubCoverageActive()); } diff --git a/plugins/coverage/src/com/intellij/coverage/JavaCoverageEngine.java b/plugins/coverage/src/com/intellij/coverage/JavaCoverageEngine.java index a10d61c8baf86..e1ff189a47527 100644 --- a/plugins/coverage/src/com/intellij/coverage/JavaCoverageEngine.java +++ b/plugins/coverage/src/com/intellij/coverage/JavaCoverageEngine.java @@ -1,3 +1,18 @@ +/* + * Copyright 2000-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.intellij.coverage; import com.intellij.CommonBundle; @@ -23,7 +38,7 @@ import com.intellij.openapi.extensions.Extensions; import com.intellij.openapi.fileTypes.StdFileTypes; import com.intellij.openapi.module.Module; -import com.intellij.openapi.module.ModuleUtil; +import com.intellij.openapi.module.ModuleUtilCore; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.progress.Task; @@ -32,8 +47,8 @@ import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.roots.CompilerModuleExtension; import com.intellij.openapi.roots.ModuleRootManager; -import com.intellij.openapi.roots.ProjectFileIndex; import com.intellij.openapi.roots.ProjectRootManager; +import com.intellij.openapi.roots.TestSourcesFilter; import com.intellij.openapi.ui.Messages; import com.intellij.openapi.util.Computable; import com.intellij.openapi.util.io.FileUtil; @@ -45,7 +60,7 @@ import com.intellij.psi.controlFlow.*; import com.intellij.psi.impl.source.tree.java.PsiSwitchStatementImpl; import com.intellij.psi.search.GlobalSearchScope; -import com.intellij.psi.search.GlobalSearchScopes; +import com.intellij.psi.search.GlobalSearchScopesCore; import com.intellij.psi.util.ClassUtil; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.rt.coverage.data.JumpData; @@ -160,7 +175,7 @@ public boolean coverageEditorHighlightingApplicableTo(@NotNull final PsiFile psi final Module module = ApplicationManager.getApplication().runReadAction(new Computable() { @Nullable public Module compute() { - return ModuleUtil.findModuleForPsiElement(psiFile); + return ModuleUtilCore.findModuleForPsiElement(psiFile); } }); return module != null; @@ -170,8 +185,7 @@ public boolean acceptedByFilters(@NotNull final PsiFile psiFile, @NotNull final final VirtualFile virtualFile = psiFile.getVirtualFile(); if (virtualFile == null) return false; final Project project = psiFile.getProject(); - final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex(); - if (!suite.isTrackTestFolders() && fileIndex.isInTestSourceContent(virtualFile)) { + if (!suite.isTrackTestFolders() && TestSourcesFilter.isTestSources(virtualFile, project)) { return false; } @@ -605,7 +619,7 @@ public Collection getClasses() { final Collection classes = super.getClasses(); if (!currentSuite.isTrackTestFolders()) { final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); - final GlobalSearchScope productionScope = GlobalSearchScopes.projectProductionScope(project); + final GlobalSearchScope productionScope = GlobalSearchScopesCore.projectProductionScope(project); for (Iterator iterator = classes.iterator(); iterator.hasNext();) { final ClassInfo aClass = iterator.next(); final PsiClass psiClass = DumbService.getInstance(project).runReadActionInSmartMode(() -> { diff --git a/xml/dom-openapi/src/com/intellij/util/xml/AbstractConvertContext.java b/xml/dom-openapi/src/com/intellij/util/xml/AbstractConvertContext.java index 205da1c0ea69e..4f051b8489671 100644 --- a/xml/dom-openapi/src/com/intellij/util/xml/AbstractConvertContext.java +++ b/xml/dom-openapi/src/com/intellij/util/xml/AbstractConvertContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,8 +17,7 @@ import com.intellij.openapi.module.Module; import com.intellij.openapi.module.ModuleUtilCore; -import com.intellij.openapi.roots.ProjectFileIndex; -import com.intellij.openapi.roots.ProjectRootManager; +import com.intellij.openapi.roots.TestSourcesFilter; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiFile; import com.intellij.psi.search.GlobalSearchScope; @@ -72,9 +71,7 @@ public GlobalSearchScope getSearchScope() { file = file.getOriginalFile(); VirtualFile virtualFile = file.getVirtualFile(); if (virtualFile != null) { - ProjectFileIndex fileIndex = ProjectRootManager.getInstance(file.getProject()).getFileIndex(); - boolean tests = fileIndex.isInTestSourceContent(virtualFile); - + boolean tests = TestSourcesFilter.isTestSources(virtualFile, file.getProject()); for (Module module : modules) { if (scope == null) { scope = module.getModuleRuntimeScope(tests);