Skip to content

Commit

Permalink
TestSourcesFilter: update after review
Browse files Browse the repository at this point in the history
  • Loading branch information
zolotov committed Jul 20, 2016
1 parent 8b87739 commit b9e8f28
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 47 deletions.
8 changes: 3 additions & 5 deletions java/openapi/src/com/intellij/util/xml/DomJavaUtil.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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()));
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -22,21 +22,20 @@
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;
import com.intellij.psi.PsiFile;
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;
Expand Down Expand Up @@ -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()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,16 @@ public interface FileIndex {

/**
* Returns true if <code>fileOrDir</code> is a file or directory from the test content source
* <p>
* Use this method when you really need to check whether the file is under test roots according to project configuration.
* <p>
* 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* By default, IntelliJ Platform considers files as tests only if they are located under test
* sources root {@link FileIndex#isInTestSourceContent(VirtualFile)}.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());

}

Expand Down
28 changes: 21 additions & 7 deletions plugins/coverage/src/com/intellij/coverage/JavaCoverageEngine.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -160,7 +175,7 @@ public boolean coverageEditorHighlightingApplicableTo(@NotNull final PsiFile psi
final Module module = ApplicationManager.getApplication().runReadAction(new Computable<Module>() {
@Nullable
public Module compute() {
return ModuleUtil.findModuleForPsiElement(psiFile);
return ModuleUtilCore.findModuleForPsiElement(psiFile);
}
});
return module != null;
Expand All @@ -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;
}

Expand Down Expand Up @@ -605,7 +619,7 @@ public Collection<ClassInfo> getClasses() {
final Collection<ClassInfo> 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<ClassInfo> iterator = classes.iterator(); iterator.hasNext();) {
final ClassInfo aClass = iterator.next();
final PsiClass psiClass = DumbService.getInstance(project).runReadActionInSmartMode(() -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b9e8f28

Please sign in to comment.