|
3 | 3 |
|
4 | 4 | package software.aws.toolkits.jetbrains.services.amazonq.lsp.dependencies |
5 | 5 |
|
| 6 | +import com.intellij.openapi.Disposable |
6 | 7 | import com.intellij.openapi.project.Project |
| 8 | +import com.intellij.openapi.module.Module |
| 9 | +import com.intellij.openapi.roots.ModuleRootEvent |
| 10 | +import com.intellij.openapi.roots.ModuleRootListener |
| 11 | +import com.intellij.openapi.module.ModuleManager |
| 12 | +import com.intellij.openapi.projectRoots.JavaSdkType |
| 13 | +import com.intellij.openapi.roots.ModuleRootManager |
| 14 | +import com.intellij.openapi.roots.OrderRootType |
| 15 | +import com.intellij.openapi.vfs.VfsUtil |
| 16 | +import com.jetbrains.python.packaging.management.PythonPackageManager |
| 17 | +import com.jetbrains.python.sdk.PythonSdkUtil |
7 | 18 | import software.aws.toolkits.jetbrains.services.amazonq.lsp.AmazonQLspService |
8 | 19 | import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.dependencies.SyncModuleDependenciesParams |
9 | 20 |
|
10 | | -class DefaultModuleDependenciesService(private val project: Project) : ModuleDependenciesService { |
| 21 | +class DefaultModuleDependenciesService( |
| 22 | + private val project: Project, |
| 23 | + serverInstance: Disposable |
| 24 | +) : ModuleDependenciesService, |
| 25 | + ModuleRootListener |
| 26 | +{ |
| 27 | + |
| 28 | + init { |
| 29 | + project.messageBus.connect(serverInstance).subscribe( |
| 30 | + ModuleRootListener.TOPIC, |
| 31 | + this |
| 32 | + ) |
| 33 | + // project initiation with initial list of dependencies |
| 34 | + syncAllModules() |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + override fun rootsChanged(event: ModuleRootEvent) { |
| 39 | + if (event.isCausedByFileTypesChange) return |
| 40 | + // call on change with updated dependencies |
| 41 | + syncAllModules() |
| 42 | + } |
| 43 | + |
11 | 44 |
|
12 | | -// init { |
13 | | - // TODO: add moduleRootListener to make function call that eventually sends message w/ params |
14 | | -// } |
15 | 45 |
|
16 | 46 | override fun syncModuleDependencies(params: SyncModuleDependenciesParams) { |
17 | 47 | AmazonQLspService.executeIfRunning(project) { languageServer -> |
18 | 48 | languageServer.syncModuleDependencies(params) |
19 | 49 | } |
20 | 50 | } |
21 | 51 |
|
22 | | -// fun createSyncModuleDependenciesParams() : SyncModuleDependenciesParams { |
23 | | -// return SyncModuleDependenciesParams() |
24 | | -// } |
| 52 | + private fun syncAllModules() { |
| 53 | + ModuleManager.getInstance(project).modules.forEach { module -> |
| 54 | + val language = getModuleLanguage(module) |
| 55 | + val params = when (language) { |
| 56 | + "Java" -> createJavaParams(module) |
| 57 | + "Python" -> createPythonParams(module) |
| 58 | + else -> return |
| 59 | + } |
| 60 | + syncModuleDependencies(params) |
| 61 | + } |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + private fun getModuleLanguage(module: Module): String { |
| 66 | + return when { |
| 67 | + ModuleRootManager.getInstance(module).sdk?.sdkType is JavaSdkType -> "Java" |
| 68 | + PythonSdkUtil.findPythonSdk(module) != null -> "Python" |
| 69 | + else -> "Unknown" |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private fun getSourceRoots(module: Module): List<String> { |
| 74 | + val sourceRoots = mutableListOf<String>() |
| 75 | + |
| 76 | + // Get all source roots from content entries |
| 77 | + ModuleRootManager.getInstance(module).contentEntries |
| 78 | + .flatMap { contentEntry -> |
| 79 | + contentEntry.sourceFolders |
| 80 | + .filter { !it.isTestSource } |
| 81 | + .mapNotNull { it.file?.path } |
| 82 | + } |
| 83 | + .forEach { path -> sourceRoots.add(path) } |
| 84 | + |
| 85 | + return sourceRoots |
| 86 | + } |
| 87 | + |
| 88 | + private fun createJavaParams(module: Module): SyncModuleDependenciesParams { |
| 89 | + val sourceRoots = getSourceRoots(module) |
| 90 | + val dependencies = mutableListOf<String>() |
| 91 | + |
| 92 | + // Get library dependencies |
| 93 | + ModuleRootManager.getInstance(module).orderEntries().forEachLibrary { library -> |
| 94 | + library.getUrls(OrderRootType.CLASSES).forEach { url -> |
| 95 | + dependencies.add(VfsUtil.urlToPath(url)) |
| 96 | + } |
| 97 | + true |
| 98 | + } |
| 99 | + |
| 100 | + return SyncModuleDependenciesParams( |
| 101 | + moduleName = module.name, |
| 102 | + programmingLanguage = "Java", |
| 103 | + files = sourceRoots, |
| 104 | + dirs = dependencies, |
| 105 | + includePatterns = emptyList(), |
| 106 | + excludePatterns = emptyList() |
| 107 | + ) |
| 108 | + } |
| 109 | + |
| 110 | + private fun createPythonParams(module: Module): SyncModuleDependenciesParams { |
| 111 | + val sourceRoots = getSourceRoots(module) |
| 112 | + val dependencies = mutableListOf<String>() |
| 113 | + |
| 114 | + // Get Python packages |
| 115 | + PythonSdkUtil.findPythonSdk(module)?.let { sdk -> |
| 116 | + val packageManager = PythonPackageManager.forSdk(module.project, sdk) |
| 117 | + packageManager.installedPackages.forEach { pkg -> |
| 118 | + dependencies.add(pkg.name) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return SyncModuleDependenciesParams( |
| 123 | + moduleName = module.name, |
| 124 | + programmingLanguage = "Python", |
| 125 | + files = sourceRoots, |
| 126 | + dirs = dependencies, |
| 127 | + includePatterns = emptyList(), |
| 128 | + excludePatterns = emptyList() |
| 129 | + ) |
| 130 | + } |
25 | 131 | } |
0 commit comments