Skip to content

Commit eafcf83

Browse files
committed
fix(endpoints): handle progress indicator for async API collection
Ensure API collection runs under a progress indicator to avoid null indicator errors. Refactor endpoint collection logic to use `ProgressManager` for asynchronous execution.
1 parent 9e0969d commit eafcf83

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

docs/development/development-faq.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,20 @@ typealias DockerFileExposeCommand = com.intellij.docker.dockerFile.parser.psi.Do
4949
typealias DockerFileFromCommand = com.intellij.docker.dockerFile.parser.psi.DockerFileFromCommand
5050
typealias DockerFileWorkdirCommand = com.intellij.docker.dockerFile.parser.psi.DockerFileWorkdirCommand
5151
```
52+
53+
## java.lang.Throwable: Must be executed under progress indicator: com.intellij.openapi.progress.EmptyProgressIndicator@6c3fd0d8 but the process is running under null indicator instead. Please see e.g. ProgressManager.runProcess()
54+
55+
```kotlin
56+
val future = CompletableFuture<String>()
57+
val task = object : Task.Backgroundable(project, "Loading", false) {
58+
override fun run(indicator: ProgressIndicator) {
59+
// collectApis point to your long time operation
60+
future.complete(this.collectApis(project, endpointsProviderList))
61+
}
62+
}
63+
64+
ProgressManager.getInstance()
65+
.runProcessWithProgressAsynchronously(task, BackgroundableProcessIndicator(task))
66+
67+
return future.get()
68+
```

exts/ext-endpoints/src/233/main/kotlin/cc/unitmesh/endpoints/bridge/WebApiViewFunctionProvider.kt

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ import com.intellij.microservices.endpoints.EndpointsProvider
66
import com.intellij.microservices.endpoints.EndpointsUrlTargetProvider
77
import com.intellij.microservices.endpoints.ModuleEndpointsFilter
88
import com.intellij.openapi.application.runReadAction
9+
import com.intellij.openapi.progress.ProgressIndicator
10+
import com.intellij.openapi.progress.ProgressManager
11+
import com.intellij.openapi.progress.Task
12+
import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator
913
import com.intellij.openapi.project.Project
1014
import com.intellij.openapi.project.modules
1115
import com.intellij.spring.model.SpringBeanPointer
1216
import com.intellij.spring.mvc.mapping.UrlMappingElement
17+
import java.util.concurrent.CompletableFuture
1318

1419
class WebApiViewFunctionProvider : ToolchainFunctionProvider {
1520
override fun funcNames(): List<String> = listOf(ArchViewCommand.WebApiView.name)
@@ -22,9 +27,23 @@ class WebApiViewFunctionProvider : ToolchainFunctionProvider {
2227
args: List<Any>,
2328
allVariables: Map<String, Any?>
2429
): Any {
25-
val model = runReadAction { EndpointsProvider.getAvailableProviders(project).toList() }
26-
if (model.isEmpty()) return "Cannot find any endpoints"
30+
val endpointsProviderList = runReadAction { EndpointsProvider.getAvailableProviders(project).toList() }
31+
if (endpointsProviderList.isEmpty()) return "Cannot find any endpoints"
2732

33+
val future = CompletableFuture<String>()
34+
val task = object : Task.Backgroundable(project, "Loading", false) {
35+
override fun run(indicator: ProgressIndicator) {
36+
future.complete(this@WebApiViewFunctionProvider.collectApis(project, endpointsProviderList))
37+
}
38+
}
39+
40+
ProgressManager.getInstance()
41+
.runProcessWithProgressAsynchronously(task, BackgroundableProcessIndicator(task))
42+
43+
return future.get()
44+
}
45+
46+
private fun collectApis(project: Project, model: List<EndpointsProvider<*, *>>): String = runReadAction {
2847
val availableProviders = model
2948
.filter { it.getStatus(project) == EndpointsProvider.Status.HAS_ENDPOINTS }
3049
.filterIsInstance<EndpointsUrlTargetProvider<SpringBeanPointer<*>, UrlMappingElement>>()
@@ -43,8 +62,10 @@ class WebApiViewFunctionProvider : ToolchainFunctionProvider {
4362
}.flatten()
4463
}.flatten()
4564

46-
return """Here is current project web api endpoints, ${map.size}:""" + map.joinToString("\n") {
47-
"endpoint: ${it.method} - ${it.urlPath}"
65+
"""Here is current project web api endpoints, ${map.size}:""" + map.joinToString("\n") { url ->
66+
url.method.joinToString("\n") {
67+
"$it - ${url.urlPath.toStringWithStars()}"
68+
}
4869
}
4970
}
5071
}

0 commit comments

Comments
 (0)