Skip to content

Commit befd089

Browse files
committed
more io
1 parent 4fce619 commit befd089

File tree

1 file changed

+12
-8
lines changed
  • plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/project

1 file changed

+12
-8
lines changed

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/project/ProjectContextProvider.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ import com.intellij.openapi.vfs.VfsUtilCore
1515
import com.intellij.openapi.vfs.VirtualFile
1616
import com.intellij.openapi.vfs.VirtualFileVisitor
1717
import com.intellij.openapi.vfs.isFile
18+
import com.intellij.util.concurrency.annotations.RequiresBackgroundThread
1819
import kotlinx.coroutines.CoroutineScope
1920
import kotlinx.coroutines.async
2021
import kotlinx.coroutines.delay
2122
import kotlinx.coroutines.launch
2223
import kotlinx.coroutines.runBlocking
24+
import kotlinx.coroutines.withContext
2325
import kotlinx.coroutines.withTimeout
2426
import software.aws.toolkits.core.utils.debug
2527
import software.aws.toolkits.core.utils.getLogger
@@ -34,7 +36,7 @@ import software.aws.toolkits.jetbrains.settings.CodeWhispererSettings
3436
import software.aws.toolkits.telemetry.AmazonqTelemetry
3537
import java.io.OutputStreamWriter
3638
import java.net.HttpURLConnection
37-
import java.net.URL
39+
import java.net.URI
3840
import java.util.concurrent.atomic.AtomicBoolean
3941
import java.util.concurrent.atomic.AtomicInteger
4042
import kotlin.time.Duration.Companion.minutes
@@ -128,13 +130,13 @@ class ProjectContextProvider(val project: Project, private val encoderServer: En
128130
}
129131
}
130132

131-
private fun initEncryption(): Boolean {
133+
private suspend fun initEncryption(): Boolean {
132134
val request = encoderServer.getEncryptionRequest()
133135
val response = sendMsgToLsp(LspMessage.Initialize, request)
134136
return response.responseCode == 200
135137
}
136138

137-
fun index(): Boolean {
139+
suspend fun index(): Boolean {
138140
val projectRoot = project.basePath ?: return false
139141

140142
val indexStartTime = System.currentTimeMillis()
@@ -180,7 +182,7 @@ class ProjectContextProvider(val project: Project, private val encoderServer: En
180182
}.await()
181183
}
182184

183-
fun getUsage(): Usage? {
185+
internal suspend fun getUsage(): Usage? {
184186
val response = sendMsgToLsp(LspMessage.GetUsageMetrics, request = null)
185187
return try {
186188
val parsedResponse = mapper.readValue<Usage>(response.responseBody)
@@ -191,9 +193,10 @@ class ProjectContextProvider(val project: Project, private val encoderServer: En
191193
}
192194
}
193195

196+
@RequiresBackgroundThread
194197
fun updateIndex(filePaths: List<String>, mode: IndexUpdateMode) {
195198
val encrypted = encryptRequest(UpdateIndexRequest(filePaths, mode.command))
196-
sendMsgToLsp(LspMessage.UpdateIndex, encrypted)
199+
runBlocking(IO) { sendMsgToLsp(LspMessage.UpdateIndex, encrypted) }
197200
}
198201

199202
private fun recordIndexWorkspace(
@@ -307,12 +310,13 @@ class ProjectContextProvider(val project: Project, private val encoderServer: En
307310
return encoderServer.encrypt(payloadJson)
308311
}
309312

310-
private fun sendMsgToLsp(msgType: LspMessage, request: String?): LspResponse {
313+
private suspend fun sendMsgToLsp(msgType: LspMessage, request: String?): LspResponse = withContext(IO) {
311314
logger.info { "sending message: ${msgType.endpoint} to lsp on port ${encoderServer.port}" }
312-
val url = URL("http://localhost:${encoderServer.port}/${msgType.endpoint}")
315+
val url = URI("http://127.0.0.1:${encoderServer.port}/${msgType.endpoint}").toURL()
313316
// use 1h as timeout for index, 5 seconds for other APIs
314317
val timeoutMs = if (msgType is LspMessage.Index) 60.minutes.inWholeMilliseconds.toInt() else 5000
315-
return with(url.openConnection() as HttpURLConnection) {
318+
319+
with(url.openConnection() as HttpURLConnection) {
316320
setConnectionProperties(this)
317321
setConnectionTimeout(this, timeoutMs)
318322
request?.let { r ->

0 commit comments

Comments
 (0)