Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug fix(amazonq): fix an issue where user action is accept but telemetry is reject #5319

Merged
merged 8 commits into from
Feb 4, 2025
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import com.intellij.util.ui.UIUtil
import software.amazon.awssdk.services.codewhispererruntime.model.Import
import software.amazon.awssdk.services.codewhispererruntime.model.Reference
import software.aws.toolkits.core.utils.debug
import software.aws.toolkits.core.utils.error
import software.aws.toolkits.core.utils.getLogger
import software.aws.toolkits.jetbrains.services.codewhisperer.editor.CodeWhispererEditorManager
import software.aws.toolkits.jetbrains.services.codewhisperer.layout.CodeWhispererLayoutConfig.addHorizontalGlue
Expand Down Expand Up @@ -85,7 +86,8 @@ import javax.swing.JLabel
class CodeWhispererPopupManager {
andrewyuq marked this conversation as resolved.
Show resolved Hide resolved
val popupComponents = CodeWhispererPopupComponents()

var shouldListenerCancelPopup: Boolean = true
// Act like a semaphore: one increment only corresponds to one decrement
andrewyuq marked this conversation as resolved.
Show resolved Hide resolved
var shouldEditorChangeCancelPopup: Int = 0
var sessionContext = SessionContext()
private set

Expand Down Expand Up @@ -247,10 +249,16 @@ class CodeWhispererPopupManager {

fun dontClosePopupAndRun(runnable: () -> Unit) {
try {
shouldListenerCancelPopup = false
shouldEditorChangeCancelPopup++
LOG.debug { "Incrementing shouldListenerCancelPopup semaphore value" }
runnable()
} finally {
shouldListenerCancelPopup = true
if (shouldEditorChangeCancelPopup <= 0) {
LOG.error { "shouldListenerCancelPopup semaphore is not updated correctly" }
} else {
shouldEditorChangeCancelPopup--
LOG.debug { "Decrementing shouldListenerCancelPopup semaphore value" }
}
}
}

Expand Down Expand Up @@ -496,7 +504,7 @@ class CodeWhispererPopupManager {
val editor = states.requestContext.editor
val codewhispererSelectionListener: SelectionListener = object : SelectionListener {
override fun selectionChanged(event: SelectionEvent) {
if (shouldListenerCancelPopup) {
if (shouldEditorChangeCancelPopup == 0) {
cancelPopup(states.popup)
}
super.selectionChanged(event)
Expand All @@ -512,7 +520,7 @@ class CodeWhispererPopupManager {
if (!delete) return
if (editor.caretModel.offset == event.offset) {
changeStates(states, 0)
} else if (shouldListenerCancelPopup) {
} else if (shouldEditorChangeCancelPopup == 0) {
cancelPopup(states.popup)
}
}
Expand All @@ -521,7 +529,7 @@ class CodeWhispererPopupManager {

val codewhispererCaretListener: CaretListener = object : CaretListener {
override fun caretPositionChanged(event: CaretEvent) {
if (shouldListenerCancelPopup) {
if (shouldEditorChangeCancelPopup == 0) {
cancelPopup(states.popup)
}
super.caretPositionChanged(event)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ import com.intellij.codeInsight.lookup.LookupEvent
import com.intellij.codeInsight.lookup.LookupListener
import com.intellij.codeInsight.lookup.LookupManagerListener
import com.intellij.codeInsight.lookup.impl.LookupImpl
import software.aws.toolkits.core.utils.debug
import software.aws.toolkits.core.utils.error
import software.aws.toolkits.core.utils.getLogger
import software.aws.toolkits.jetbrains.services.codewhisperer.model.InvocationContext
import software.aws.toolkits.jetbrains.services.codewhisperer.popup.CodeWhispererPopupManager
import software.aws.toolkits.jetbrains.services.codewhisperer.popup.listeners.CodeWhispererPopupIntelliSenseAcceptListener.Companion.LOG
import software.aws.toolkits.jetbrains.services.codewhisperer.service.CodeWhispererInvocationStatus

class CodeWhispererPopupIntelliSenseAcceptListener(private val states: InvocationContext) : LookupManagerListener {
Expand All @@ -18,14 +22,20 @@ class CodeWhispererPopupIntelliSenseAcceptListener(private val states: Invocatio

addIntelliSenseAcceptListener(newLookup, states)
}

companion object {
val LOG = getLogger<CodeWhispererPopupIntelliSenseAcceptListener>()
}
}

fun addIntelliSenseAcceptListener(lookup: Lookup, states: InvocationContext) {
lookup.addLookupListener(object : LookupListener {
override fun beforeItemSelected(event: LookupEvent): Boolean {
CodeWhispererPopupManager.getInstance().shouldListenerCancelPopup = false
return super.beforeItemSelected(event)
override fun lookupShown(event: LookupEvent) {
CodeWhispererPopupManager.getInstance().shouldEditorChangeCancelPopup++
LOG.debug { "Incrementing shouldListenerCancelPopup semaphore value" }
super.lookupShown(event)
}

override fun itemSelected(event: LookupEvent) {
if (!CodeWhispererInvocationStatus.getInstance().isDisplaySessionActive() ||
!(event.lookup as LookupImpl).isShown
Expand All @@ -46,7 +56,12 @@ fun addIntelliSenseAcceptListener(lookup: Lookup, states: InvocationContext) {

private fun cleanup() {
lookup.removeLookupListener(this)
CodeWhispererPopupManager.getInstance().shouldListenerCancelPopup = true
if (CodeWhispererPopupManager.getInstance().shouldEditorChangeCancelPopup <= 0) {
LOG.error { "shouldListenerCancelPopup semaphore is not updated correctly" }
} else {
CodeWhispererPopupManager.getInstance().shouldEditorChangeCancelPopup--
LOG.debug { "Decrementing shouldListenerCancelPopup semaphore value" }
}
}
})
}
Loading