@@ -15,11 +15,13 @@ import com.intellij.openapi.vfs.VfsUtilCore
1515import com.intellij.openapi.vfs.VirtualFile
1616import com.intellij.openapi.vfs.VirtualFileVisitor
1717import com.intellij.openapi.vfs.isFile
18+ import com.intellij.util.concurrency.annotations.RequiresBackgroundThread
1819import kotlinx.coroutines.CoroutineScope
1920import kotlinx.coroutines.async
2021import kotlinx.coroutines.delay
2122import kotlinx.coroutines.launch
2223import kotlinx.coroutines.runBlocking
24+ import kotlinx.coroutines.withContext
2325import kotlinx.coroutines.withTimeout
2426import software.aws.toolkits.core.utils.debug
2527import software.aws.toolkits.core.utils.getLogger
@@ -34,7 +36,7 @@ import software.aws.toolkits.jetbrains.settings.CodeWhispererSettings
3436import software.aws.toolkits.telemetry.AmazonqTelemetry
3537import java.io.OutputStreamWriter
3638import java.net.HttpURLConnection
37- import java.net.URL
39+ import java.net.URI
3840import java.util.concurrent.atomic.AtomicBoolean
3941import java.util.concurrent.atomic.AtomicInteger
4042import 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