postpone isJsProject() check

#KT-9026 Fixed
This commit is contained in:
Dmitry Jemerov
2016-02-01 20:36:01 +01:00
parent abcdae8e69
commit 37976e516c
2 changed files with 24 additions and 14 deletions
@@ -19,11 +19,11 @@ package org.jetbrains.kotlin.idea.stubindex;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.search.DelegatingGlobalSearchScope;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.idea.caches.resolve.JsProjectDetector;
import org.jetbrains.kotlin.idea.util.ProjectRootsUtil;
public class KotlinSourceFilterScope extends DelegatingGlobalSearchScope {
@@ -84,7 +84,9 @@ public class KotlinSourceFilterScope extends DelegatingGlobalSearchScope {
private final boolean includeProjectSourceFiles;
private final boolean includeLibrarySourceFiles;
private final boolean includeClassFiles;
private final boolean isJsProject;
//NOTE: avoid recomputing in potentially bottleneck 'contains' method
private final Ref<Boolean> isJsProjectRef = new Ref<Boolean>();
private KotlinSourceFilterScope(
@NotNull GlobalSearchScope delegate,
@@ -98,9 +100,7 @@ public class KotlinSourceFilterScope extends DelegatingGlobalSearchScope {
this.includeProjectSourceFiles = includeProjectSourceFiles;
this.includeLibrarySourceFiles = includeLibrarySourceFiles;
this.includeClassFiles = includeClassFiles;
//NOTE: avoid recomputing in potentially bottleneck 'contains' method
this.index = ProjectRootManager.getInstance(project).getFileIndex();
this.isJsProject = JsProjectDetector.isJsProject(project);
}
@Override
@@ -112,7 +112,7 @@ public class KotlinSourceFilterScope extends DelegatingGlobalSearchScope {
public boolean contains(@NotNull VirtualFile file) {
if (!super.contains(file)) return false;
return ProjectRootsUtil.isInContent(
project, file, includeProjectSourceFiles, includeLibrarySourceFiles, includeClassFiles, index, isJsProject
project, file, includeProjectSourceFiles, includeLibrarySourceFiles, includeClassFiles, index, isJsProjectRef
);
}
}
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.idea.util
import com.intellij.ide.highlighter.JavaClassFileType
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectFileIndex
import com.intellij.openapi.util.Ref
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiElement
@@ -33,21 +34,30 @@ object ProjectRootsUtil {
@JvmStatic fun isInContent(project: Project, file: VirtualFile, includeProjectSource: Boolean,
includeLibrarySource: Boolean, includeLibraryClasses: Boolean,
fileIndex: ProjectFileIndex = ProjectFileIndex.SERVICE.getInstance(project),
isJsProject: Boolean? = null): Boolean {
isJsProjectRef: Ref<Boolean?>? = null): Boolean {
if (includeProjectSource && fileIndex.isInSourceContent(file)) {
return true
}
if (!includeLibraryClasses && !includeLibrarySource) return false
//NOTE: avoid computing isJsProject if redundant
if (isJsProject ?: JsProjectDetector.isJsProject(project)) {
return (includeLibrarySource && fileIndex.isInLibrarySource(file))
|| (includeLibraryClasses && fileIndex.isLibraryClassFile(file))
}
// NOTE: the following is a workaround for cases when class files are under library source roots and source files are under class roots
val isClassFile = file.fileType in classFileLike
return (includeLibraryClasses && isClassFile && fileIndex.isInLibraryClasses(file))
|| (includeLibrarySource && !isClassFile && fileIndex.isInLibrarySource(file))
if ((includeLibraryClasses && isClassFile && fileIndex.isInLibraryClasses(file)) ||
(includeLibrarySource && !isClassFile && fileIndex.isInLibrarySource(file))) {
return true
}
if ((includeLibraryClasses && fileIndex.isInLibraryClasses(file)) ||
(includeLibrarySource && fileIndex.isInLibrarySource(file))) {
//NOTE: avoid computing isJsProject if redundant
val isJsProject = isJsProjectRef?.get() ?: JsProjectDetector.isJsProject(project)
isJsProjectRef?.set(isJsProject)
return isJsProject
}
return false
}
@JvmStatic fun isInContent(