ProjectRootsUtil: fix check for library class and source file for js projects
Update JetSourceFilterScope Extract JsProjectDetector
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2010-2014 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 org.jetbrains.jet.plugin.caches.resolve
|
||||
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.module.ModuleManager
|
||||
import org.jetbrains.jet.plugin.project.ProjectStructureUtil
|
||||
import kotlin.platform.platformStatic
|
||||
import com.intellij.psi.util.CachedValuesManager
|
||||
import com.intellij.psi.util.CachedValueProvider
|
||||
import com.intellij.openapi.roots.ProjectRootModificationTracker
|
||||
|
||||
//TODO: this should go away to support cross-platform projects
|
||||
public object JsProjectDetector {
|
||||
platformStatic public fun isJsProject(project: Project): Boolean {
|
||||
return CachedValuesManager.getManager(project).getCachedValue(project) {
|
||||
val result = ModuleManager.getInstance(project).getModules().any { ProjectStructureUtil.isJsKotlinModule(it) }
|
||||
CachedValueProvider.Result(result, ProjectRootModificationTracker.getInstance(project))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,7 @@ import org.jetbrains.jet.asJava.KotlinLightClassForPackage
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.openapi.roots.ModuleRootManager
|
||||
import org.jetbrains.jet.plugin.util.ProjectRootsUtil
|
||||
|
||||
fun PsiElement.getModuleInfo(): IdeaModuleInfo {
|
||||
fun logAndReturnDefault(message: String): IdeaModuleInfo {
|
||||
@@ -90,10 +91,10 @@ private fun getModuleInfoByVirtualFile(project: Project, virtualFile: VirtualFil
|
||||
when (orderEntry) {
|
||||
is LibraryOrderEntry -> {
|
||||
val library = orderEntry.getLibrary() ?: continue @entries
|
||||
if (projectFileIndex.isInLibraryClasses(virtualFile) && !isDecompiledFile) {
|
||||
if (ProjectRootsUtil.isLibraryClassFile(project, virtualFile) && !isDecompiledFile) {
|
||||
return LibraryInfo(project, library)
|
||||
}
|
||||
else if (projectFileIndex.isInLibrarySource(virtualFile) || isDecompiledFile) {
|
||||
else if (ProjectRootsUtil.isLibraryFile(project, virtualFile) || isDecompiledFile) {
|
||||
return LibrarySourceInfo(project, library)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ 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.jet.plugin.caches.resolve.JsProjectDetector;
|
||||
import org.jetbrains.jet.plugin.util.ProjectRootsUtil;
|
||||
|
||||
public class JetSourceFilterScope extends DelegatingGlobalSearchScope {
|
||||
@@ -54,6 +55,7 @@ public class JetSourceFilterScope extends DelegatingGlobalSearchScope {
|
||||
private final Project project;
|
||||
private final boolean includeLibrarySourceFiles;
|
||||
private final boolean includeClassFiles;
|
||||
private final boolean isJsProject;
|
||||
|
||||
private JetSourceFilterScope(
|
||||
@NotNull GlobalSearchScope delegate,
|
||||
@@ -62,10 +64,12 @@ public class JetSourceFilterScope extends DelegatingGlobalSearchScope {
|
||||
@NotNull Project project
|
||||
) {
|
||||
super(delegate);
|
||||
this.index = ProjectRootManager.getInstance(project).getFileIndex();
|
||||
this.project = project;
|
||||
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
|
||||
@@ -74,6 +78,6 @@ public class JetSourceFilterScope extends DelegatingGlobalSearchScope {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ProjectRootsUtil.isInContent(project, file, true, includeLibrarySourceFiles, includeClassFiles, index);
|
||||
return ProjectRootsUtil.isInContent(project, file, true, includeLibrarySourceFiles, includeClassFiles, index, isJsProject);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-8
@@ -23,8 +23,7 @@ import org.jetbrains.jet.lang.resolve.lazy.declarations.DeclarationProviderFacto
|
||||
import org.jetbrains.jet.lang.resolve.lazy.declarations.DeclarationProviderFactoryService
|
||||
import org.jetbrains.jet.storage.StorageManager
|
||||
import org.jetbrains.jet.plugin.stubindex.JetSourceFilterScope
|
||||
import com.intellij.openapi.module.ModuleManager
|
||||
import org.jetbrains.jet.plugin.project.ProjectStructureUtil
|
||||
import org.jetbrains.jet.plugin.caches.resolve.JsProjectDetector
|
||||
|
||||
public class PluginDeclarationProviderFactoryService : DeclarationProviderFactoryService() {
|
||||
|
||||
@@ -34,7 +33,7 @@ public class PluginDeclarationProviderFactoryService : DeclarationProviderFactor
|
||||
syntheticFiles: Collection<JetFile>,
|
||||
filesScope: GlobalSearchScope
|
||||
): DeclarationProviderFactory {
|
||||
val scope = if (isJsProject(project)) {
|
||||
val scope = if (JsProjectDetector.isJsProject(project)) {
|
||||
//NOTE: we include libraries here to support analyzing JavaScript libraries which are kotlin sources in classes root
|
||||
JetSourceFilterScope.kotlinSourcesAndLibraries(filesScope, project)
|
||||
}
|
||||
@@ -43,9 +42,4 @@ public class PluginDeclarationProviderFactoryService : DeclarationProviderFactor
|
||||
}
|
||||
return PluginDeclarationProviderFactory(project, scope, storageManager, syntheticFiles)
|
||||
}
|
||||
|
||||
//TODO: (module refactoring) get rid of this when there is no single module resolve in plugin
|
||||
private fun isJsProject(project: Project): Boolean {
|
||||
return ModuleManager.getInstance(project).getModules().any { ProjectStructureUtil.isJsKotlinModule(it) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,17 +25,24 @@ import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.psi.PsiDirectory
|
||||
import com.intellij.ide.highlighter.JavaClassFileType
|
||||
import org.jetbrains.jet.plugin.caches.resolve.JsProjectDetector
|
||||
|
||||
public object ProjectRootsUtil {
|
||||
platformStatic
|
||||
public fun isInContent(project: Project, file: VirtualFile, includeProjectSource: Boolean,
|
||||
includeLibrarySource: Boolean, includeLibraryClasses: Boolean,
|
||||
fileIndex: ProjectFileIndex = ProjectFileIndex.SERVICE.getInstance(project)): Boolean {
|
||||
fileIndex: ProjectFileIndex = ProjectFileIndex.SERVICE.getInstance(project),
|
||||
isJsProject: Boolean? = null): Boolean {
|
||||
if (includeProjectSource && fileIndex.isInSourceContent(file)) {
|
||||
return !JetModuleTypeManager.getInstance()!!.isKtFileInGradleProjectInWrongFolder(file, project)
|
||||
}
|
||||
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.getFileType() == JavaClassFileType.INSTANCE
|
||||
return (includeLibraryClasses && isClassFile && fileIndex.isInLibraryClasses(file))
|
||||
|
||||
Reference in New Issue
Block a user