[java-highlighting] Finish moving module access errors

Part of IDEA-365344 Create a new Java error highlighter with minimal dependencies (PSI only)

GitOrigin-RevId: 0f89a7cd9609aea2780e132118aae892a6b0bafd
This commit is contained in:
Tagir Valeev
2025-02-25 17:24:00 +00:00
committed by intellij-monorepo-bot
parent f976c0b04c
commit 2250bb64ee
25 changed files with 248 additions and 404 deletions
@@ -3,11 +3,11 @@ package com.intellij.java.codeserver.core
import com.intellij.openapi.module.Module
import com.intellij.openapi.module.ModuleUtilCore
import com.intellij.psi.JavaModuleSystem
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFileSystemItem
import com.intellij.psi.PsiJavaModule
import com.intellij.openapi.roots.ProjectFileIndex
import com.intellij.pom.java.JavaFeature
import com.intellij.psi.*
import com.intellij.psi.impl.light.LightJavaModule
import com.intellij.psi.util.PsiUtil
/**
* Represents a JPMS module and the corresponding module in IntelliJ project model
@@ -55,4 +55,49 @@ sealed interface JpmsModuleInfo {
return JpmsModuleAccessInfo(current, this)
}
}
companion object {
/**
* Find module info structures when accessing a given location.
*
* @param targetPackageName package name which about to be accessed
* @param targetFile concrete target file which is about to be accessed; null if not known (in this case,
* multiple results could be returned, as multiple source roots may define a given package)
* @param place source place from where the access is requested
* @return list of TargetModuleInfo structures that describe the possible target; empty list if the target package is empty
* (which is generally an error), null if not applicable (e.g., modules are not supported at place;
* target does not belong to any module; etc.). In this case, no access problem should be reported.
*/
@JvmStatic
fun findTargetModuleInfos(targetPackageName: String, targetFile: PsiFile?, place: PsiFile): List<TargetModuleInfo>? {
val originalTargetFile = targetFile?.originalFile
if (!PsiUtil.isAvailable(JavaFeature.MODULES, place)) return null
val useVFile = place.virtualFile
val project = place.project
val index = ProjectFileIndex.getInstance(project)
if (useVFile != null && index.isInLibrarySource(useVFile)) return null
if (originalTargetFile != null && originalTargetFile.isPhysical) {
return listOf(TargetModuleInfo(originalTargetFile, targetPackageName))
}
if (useVFile == null) return null
val target = JavaPsiFacade.getInstance(project).findPackage(targetPackageName) ?: return null
val module = index.getModuleForFile(useVFile) ?: return null
val test = index.isInTestSourceContent(useVFile)
val moduleScope = module.getModuleWithDependenciesAndLibrariesScope(test)
val dirs = target.getDirectories(moduleScope)
val packageName = target.qualifiedName
if (dirs.isEmpty()) {
return if (target.getFiles(moduleScope).isEmpty()) {
listOf()
}
else {
null
}
}
return dirs.map { dir -> TargetModuleInfo(dir, packageName) }
}
}
}