[gradle][android] IDEA-347852 considers version catalogs in included builds of a composite build.

Each included build (linked project) could have its own version catalogs, different to the catalogs of a root composite build. This change makes possible accessing version catalogs of an included build.

Also, it enables navigation from a build script (either Groovy or Kts) in an included build to a corresponding version catalog.
For example, from `/app/build.gradle` to `/app/gradle/libs.versions.toml`, where `app` is a build included in the settings file of the composite build using `includeBuild("app")`

GitOrigin-RevId: ed5d68cb91e7fe74c0af1755e3cbfa7b3b2d1abf
This commit is contained in:
Nikita Biriukov
2024-08-21 17:47:32 +02:00
committed by intellij-monorepo-bot
parent 77f726391c
commit 9ce2071a66
4 changed files with 41 additions and 2 deletions

View File

@@ -13,5 +13,6 @@
<orderEntry type="module" module-name="intellij.gradle.java" />
<orderEntry type="module" module-name="intellij.java.psi" />
<orderEntry type="module" module-name="intellij.java.psi.impl" />
<orderEntry type="module" module-name="intellij.platform.projectModel" />
</component>
</module>

View File

@@ -2,6 +2,9 @@
package org.jetbrains.plugins.gradle.service.resolve.static
import com.android.tools.idea.gradle.dsl.api.ProjectBuildModel
import com.intellij.openapi.externalSystem.ExternalSystemModulePropertyManager
import com.intellij.openapi.module.Module
import com.intellij.openapi.module.ModuleUtilCore
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiClass
@@ -18,10 +21,23 @@ class GradleDslVersionCatalogHandler : GradleVersionCatalogHandler {
return ProjectBuildModel.get(project).context.versionCatalogFiles.associate { it.catalogName to it.file } ?: emptyMap()
}
override fun getVersionCatalogFiles(module: Module): Map<String, VirtualFile> {
val buildModel = getBuildModel(module) ?: return emptyMap()
return buildModel.context.versionCatalogFiles.associate { it.catalogName to it.file }
}
override fun getAccessorClass(context: PsiElement, catalogName: String): PsiClass? {
val project = context.project
val scope = context.resolveScope
val versionCatalogModel = ProjectBuildModel.get(project).versionCatalogsModel ?: return null
val module = ModuleUtilCore.findModuleForPsiElement(context) ?: return null
val buildModel = getBuildModel(module) ?: return null
val versionCatalogModel = buildModel.versionCatalogsModel
return SyntheticVersionCatalogAccessor(project, scope, versionCatalogModel, catalogName)
}
private fun getBuildModel(module: Module): ProjectBuildModel? {
val buildPath = ExternalSystemModulePropertyManager.getInstance(module)
.getLinkedProjectPath() ?: return null
return ProjectBuildModel.getForCompositeBuild(module.project, buildPath)
}
}

View File

@@ -3,6 +3,7 @@ package org.jetbrains.plugins.gradle.service.resolve
import com.intellij.openapi.extensions.ExtensionPointName
import com.intellij.openapi.project.Project
import com.intellij.openapi.module.Module
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiElement
@@ -16,10 +17,16 @@ interface GradleVersionCatalogHandler {
fun getExternallyHandledExtension(project: Project) : Set<String>
fun getVersionCatalogFiles(project: Project) : Map</*catalog name*/ String, /*catalog file*/ VirtualFile>
fun getVersionCatalogFiles(module: Module) : Map</*catalog name*/ String, /*catalog file*/ VirtualFile>
fun getAccessorClass(context: PsiElement, catalogName: String) : PsiClass?
}
@Deprecated(
"Doesn't work for linked projects in a composite build. It only provides version catalogs for a build in a root project directory.",
ReplaceWith("getVersionCatalogFiles(module)"),
DeprecationLevel.WARNING,
)
fun getVersionCatalogFiles(project: Project) : Map<String, VirtualFile> {
val container = mutableMapOf<String, VirtualFile>()
for (extension in EP_NAME.extensionList) {
@@ -28,6 +35,19 @@ fun getVersionCatalogFiles(project: Project) : Map<String, VirtualFile> {
return container
}
/**
* Provides version catalogs for a Gradle build corresponding to the given module.
* The build could be not only the main (in a root project directory), but also an included build (linked project) of a composite build.
* @return a map between a version catalog name and a file with this catalog.
*/
fun getVersionCatalogFiles(module: Module) : Map<String, VirtualFile> {
val container = mutableMapOf<String, VirtualFile>()
for (extension in EP_NAME.extensionList) {
container.putAll(extension.getVersionCatalogFiles(module))
}
return container
}
fun getGradleStaticallyHandledExtensions(project: Project) : Set<String> {
val container = mutableSetOf<String>()
for (extension in EP_NAME.extensionList) {

View File

@@ -2,6 +2,7 @@
package org.jetbrains.plugins.gradle.toml
import com.intellij.openapi.components.service
import com.intellij.openapi.module.ModuleUtilCore
import com.intellij.openapi.roots.ProjectFileIndex
import com.intellij.openapi.util.NlsSafe
import com.intellij.openapi.util.text.StringUtil
@@ -33,7 +34,8 @@ internal fun getLibraries(context: PsiElement): List<TomlKeySegment> = getTableE
internal fun String.getVersionCatalogParts() : List<String> = split("_", "-")
internal fun findTomlFile(context: PsiElement, name: String) : TomlFile? {
val file = getVersionCatalogFiles(context.project)[name] ?: return null
val module = ModuleUtilCore.findModuleForPsiElement(context) ?: return null
val file = getVersionCatalogFiles(module)[name] ?: return null
return context.manager.findFile(file)?.asSafely<TomlFile>()
}