Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Detect Gradle source folders correctly #187

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 66 additions & 33 deletions src/main/kotlin/com/vaadin/plugin/copilot/CopilotPluginUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.intellij.openapi.application.WriteAction
import com.intellij.openapi.application.runInEdt
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.extensions.PluginId
import com.intellij.openapi.module.Module
import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.project.DumbModeTask
Expand All @@ -16,14 +17,25 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.roots.CompilerModuleExtension
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.vfs.*
import com.vaadin.plugin.copilot.handler.*
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.findFile
import com.intellij.openapi.vfs.findOrCreateDirectory
import com.vaadin.plugin.copilot.handler.GetModulePathsHandler
import com.vaadin.plugin.copilot.handler.Handler
import com.vaadin.plugin.copilot.handler.HandlerResponse
import com.vaadin.plugin.copilot.handler.RedoHandler
import com.vaadin.plugin.copilot.handler.RefreshHandler
import com.vaadin.plugin.copilot.handler.ShowInIdeHandler
import com.vaadin.plugin.copilot.handler.UndoHandler
import com.vaadin.plugin.copilot.handler.WriteBase64FileHandler
import com.vaadin.plugin.copilot.handler.WriteFileHandler
import com.vaadin.plugin.utils.VaadinIcons
import io.netty.handler.codec.http.HttpResponseStatus
import java.io.BufferedWriter
import java.io.IOException
import java.io.StringWriter
import java.util.*
import java.util.Properties
import org.jetbrains.jps.model.java.JavaResourceRootType
import org.jetbrains.jps.model.java.JavaSourceRootType

Expand All @@ -32,11 +44,11 @@ class CopilotPluginUtil {
@JvmRecord
data class ModuleInfo(
val name: String,
val contentRoots: Array<String>,
val javaSourcePaths: Array<String>,
val javaTestSourcePaths: Array<String>,
val resourcePaths: Array<String>,
val testResourcePaths: Array<String>,
val contentRoots: List<String>,
val javaSourcePaths: ArrayList<String>,
val javaTestSourcePaths: ArrayList<String>,
val resourcePaths: ArrayList<String>,
val testResourcePaths: ArrayList<String>,
val outputPath: String?
)

Expand Down Expand Up @@ -189,36 +201,57 @@ class CopilotPluginUtil {
* Returns a list of all modules in the project. Each module contains information about the module name, content
* roots, source paths, test source paths, resource paths, test resource paths, and output path.
*/
fun getModulesInfo(project: Project): ArrayList<ModuleInfo> {
val modules = ArrayList<ModuleInfo>()
ModuleManager.getInstance(project).modules.forEach { module ->
fun getModulesInfo(project: Project): List<ModuleInfo> {
val modules = HashMap<String, ModuleInfo>()
val moduleManager = ModuleManager.getInstance(project)
val projectModules = moduleManager.modules
val moduleMap = projectModules.associateBy({ it.name }, { it })
projectModules.forEach { module: Module ->
val moduleRootManager = ModuleRootManager.getInstance(module)
val contentRoots = moduleRootManager.contentRoots.map { it.path }

val compilerModuleExtension = CompilerModuleExtension.getInstance(module)
val outputPath = compilerModuleExtension?.compilerOutputPath
val dotIndex = module.name.lastIndexOf('.')
var targetModuleName = module.name
var targetModule = module
if (dotIndex > 0) {
val base = module.name.substring(0, dotIndex)
if (moduleMap.containsKey(base)) {
// Add the modules from this module to the main one
targetModuleName = base
targetModule = moduleMap[base]!!
}
}

val targetModuleInfo =
modules.computeIfAbsent(
targetModuleName,
{
val targetModuleRootManager = ModuleRootManager.getInstance(targetModule)
val contentRoots = targetModuleRootManager.contentRoots.map { it.path }

val compilerModuleExtension = CompilerModuleExtension.getInstance(module)
val outputPath = compilerModuleExtension?.compilerOutputPath

ModuleInfo(
targetModuleName,
contentRoots,
ArrayList<String>(),
ArrayList<String>(),
ArrayList<String>(),
ArrayList<String>(),
outputPath?.path)
})

// Note that the JavaSourceRootType.SOURCE also includes Kotlin source folders
// Only include folder if is is not in the output path
val javaSourcePaths = moduleRootManager.getSourceRoots(JavaSourceRootType.SOURCE).map({ it.path })
val javaTestSourcePaths =
moduleRootManager.getSourceRoots(JavaSourceRootType.TEST_SOURCE).map({ it.path })

val resourcePaths = moduleRootManager.getSourceRoots(JavaResourceRootType.RESOURCE).map { it.path }
val testResourcePaths =
moduleRootManager.getSourceRoots(JavaResourceRootType.TEST_RESOURCE).map { it.path }

modules.add(
ModuleInfo(
module.name,
contentRoots.toTypedArray(),
javaSourcePaths.toTypedArray(),
javaTestSourcePaths.toTypedArray(),
resourcePaths.toTypedArray(),
testResourcePaths.toTypedArray(),
outputPath?.path))
targetModuleInfo.javaSourcePaths.addAll(
moduleRootManager.getSourceRoots(JavaSourceRootType.SOURCE).map { it.path })
targetModuleInfo.javaTestSourcePaths.addAll(
moduleRootManager.getSourceRoots(JavaSourceRootType.TEST_SOURCE).map { it.path })
targetModuleInfo.resourcePaths.addAll(
moduleRootManager.getSourceRoots(JavaResourceRootType.RESOURCE).map { it.path })
targetModuleInfo.testResourcePaths.addAll(
moduleRootManager.getSourceRoots(JavaResourceRootType.TEST_RESOURCE).map { it.path })
}
return modules
return modules.values.toList()
}

/**
Expand Down