@@ -22,7 +22,6 @@ import com.intellij.util.concurrency.annotations.RequiresEdt
2222import kotlinx.coroutines.CoroutineScope
2323import kotlinx.coroutines.Job
2424import kotlinx.coroutines.future.await
25- import kotlinx.coroutines.isActive
2625import kotlinx.coroutines.launch
2726import kotlinx.coroutines.withContext
2827import org.eclipse.lsp4j.Position
@@ -36,7 +35,6 @@ import software.aws.toolkits.core.utils.getLogger
3635import software.aws.toolkits.core.utils.info
3736import software.aws.toolkits.core.utils.warn
3837import software.aws.toolkits.jetbrains.core.coroutines.EDT
39- import software.aws.toolkits.jetbrains.core.coroutines.disposableCoroutineScope
4038import software.aws.toolkits.jetbrains.core.coroutines.getCoroutineBgContext
4139import software.aws.toolkits.jetbrains.core.credentials.ToolkitConnection
4240import software.aws.toolkits.jetbrains.core.credentials.ToolkitConnectionManager
@@ -181,15 +179,9 @@ class CodeWhispererService(private val cs: CoroutineScope) : Disposable {
181179 }
182180 }
183181
184- // When popup is disposed we will cancel this coroutine. The only places popup can get disposed should be
185- // from CodeWhispererPopupManager.cancelPopup() and CodeWhispererPopupManager.closePopup().
186- // It's possible and ok that coroutine will keep running until the next time we check it's state.
187- // As long as we don't show to the user extra info we are good.
188- val coroutineScope = disposableCoroutineScope(popup)
189-
190182 var states: InvocationContext ? = null
191183
192- val job = coroutineScope .launch {
184+ val job = cs .launch {
193185 try {
194186 var startTime = System .nanoTime()
195187 CodeWhispererInvocationStatus .getInstance().setInvocationStart()
@@ -211,13 +203,11 @@ class CodeWhispererService(private val cs: CoroutineScope) : Disposable {
211203 runInEdt {
212204 states = processCodeWhispererUI(workerContext, states)
213205 }
214- if (! isActive) {
215- // If job is cancelled before we do another request, don't bother making
216- // another API call to save resources
206+ if (! CodeWhispererInvocationStatus .getInstance().isDisplaySessionActive()) {
217207 LOG .debug { " Skipping sending remaining requests on CodeWhisperer session exit" }
218208 return @launch
219209 }
220- } while (nextToken != null )
210+ } while (nextToken != null && nextToken.left.isNotEmpty() )
221211 } catch (e: Exception ) {
222212 // TODO flare: flare doesn't return exceptions
223213 val sessionId = " "
@@ -302,7 +292,7 @@ class CodeWhispererService(private val cs: CoroutineScope) : Disposable {
302292 return null
303293 }
304294
305- if (completions.partialResultToken == null ) {
295+ if (completions.partialResultToken?.left?.isEmpty() == true ) {
306296 CodeWhispererInvocationStatus .getInstance().finishInvocation()
307297 }
308298
@@ -336,7 +326,7 @@ class CodeWhispererService(private val cs: CoroutineScope) : Disposable {
336326 // If there are no recommendations at all in this session, we need to manually send the user decision event here
337327 // since it won't be sent automatically later
338328 if (! hasAtLeastOneValid) {
339- if (completions.partialResultToken == null ) {
329+ if (completions.partialResultToken?.left?.isEmpty() == true ) {
340330 LOG .debug { " None of the recommendations are valid, exiting CodeWhisperer session" }
341331 CodeWhispererPopupManager .getInstance().cancelPopup(popup)
342332 return null
@@ -531,7 +521,7 @@ class CodeWhispererService(private val cs: CoroutineScope) : Disposable {
531521 editor.caretModel.primaryCaret.logicalPosition.column
532522 )
533523 if (nextToken != null ) {
534- workDoneToken = nextToken
524+ partialResultToken = nextToken
535525 }
536526 }
537527 }
0 commit comments