Skip to content

Commit 41bc885

Browse files
committed
bug fix(amazonq): fix an issue where user action is accept but telemetry is reject
1 parent a545699 commit 41bc885

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/popup/CodeWhispererPopupManager.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ import javax.swing.JLabel
8585
class CodeWhispererPopupManager {
8686
val popupComponents = CodeWhispererPopupComponents()
8787

88-
var shouldListenerCancelPopup: Boolean = true
88+
// Act like a semaphore: one increment only corresponds to one decrement
89+
var shouldEditorChangeCancelPopup: Int = 0
8990
var sessionContext = SessionContext()
9091
private set
9192

@@ -247,10 +248,16 @@ class CodeWhispererPopupManager {
247248

248249
fun dontClosePopupAndRun(runnable: () -> Unit) {
249250
try {
250-
shouldListenerCancelPopup = false
251+
shouldEditorChangeCancelPopup++
252+
LOG.debug { "Incrementing shouldListenerCancelPopup semaphore value" }
251253
runnable()
252254
} finally {
253-
shouldListenerCancelPopup = true
255+
if (shouldEditorChangeCancelPopup <= 0) {
256+
LOG.error("shouldListenerCancelPopup semaphore is not updated correctly")
257+
} else {
258+
shouldEditorChangeCancelPopup--
259+
LOG.debug { "Decrementing shouldListenerCancelPopup semaphore value" }
260+
}
254261
}
255262
}
256263

@@ -496,7 +503,7 @@ class CodeWhispererPopupManager {
496503
val editor = states.requestContext.editor
497504
val codewhispererSelectionListener: SelectionListener = object : SelectionListener {
498505
override fun selectionChanged(event: SelectionEvent) {
499-
if (shouldListenerCancelPopup) {
506+
if (shouldEditorChangeCancelPopup == 0) {
500507
cancelPopup(states.popup)
501508
}
502509
super.selectionChanged(event)
@@ -512,7 +519,7 @@ class CodeWhispererPopupManager {
512519
if (!delete) return
513520
if (editor.caretModel.offset == event.offset) {
514521
changeStates(states, 0)
515-
} else if (shouldListenerCancelPopup) {
522+
} else if (shouldEditorChangeCancelPopup == 0) {
516523
cancelPopup(states.popup)
517524
}
518525
}
@@ -521,7 +528,7 @@ class CodeWhispererPopupManager {
521528

522529
val codewhispererCaretListener: CaretListener = object : CaretListener {
523530
override fun caretPositionChanged(event: CaretEvent) {
524-
if (shouldListenerCancelPopup) {
531+
if (shouldEditorChangeCancelPopup == 0) {
525532
cancelPopup(states.popup)
526533
}
527534
super.caretPositionChanged(event)

plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/popup/listeners/CodeWhispererPopupIntelliSenseAcceptListener.kt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ import com.intellij.codeInsight.lookup.LookupEvent
88
import com.intellij.codeInsight.lookup.LookupListener
99
import com.intellij.codeInsight.lookup.LookupManagerListener
1010
import com.intellij.codeInsight.lookup.impl.LookupImpl
11+
import software.aws.toolkits.core.utils.debug
12+
import software.aws.toolkits.core.utils.getLogger
1113
import software.aws.toolkits.jetbrains.services.codewhisperer.model.InvocationContext
1214
import software.aws.toolkits.jetbrains.services.codewhisperer.popup.CodeWhispererPopupManager
15+
import software.aws.toolkits.jetbrains.services.codewhisperer.popup.listeners.CodeWhispererPopupIntelliSenseAcceptListener.Companion.LOG
1316
import software.aws.toolkits.jetbrains.services.codewhisperer.service.CodeWhispererInvocationStatus
1417

1518
class CodeWhispererPopupIntelliSenseAcceptListener(private val states: InvocationContext) : LookupManagerListener {
@@ -18,14 +21,20 @@ class CodeWhispererPopupIntelliSenseAcceptListener(private val states: Invocatio
1821

1922
addIntelliSenseAcceptListener(newLookup, states)
2023
}
24+
25+
companion object {
26+
val LOG = getLogger<CodeWhispererPopupIntelliSenseAcceptListener>()
27+
}
2128
}
2229

2330
fun addIntelliSenseAcceptListener(lookup: Lookup, states: InvocationContext) {
2431
lookup.addLookupListener(object : LookupListener {
25-
override fun beforeItemSelected(event: LookupEvent): Boolean {
26-
CodeWhispererPopupManager.getInstance().shouldListenerCancelPopup = false
27-
return super.beforeItemSelected(event)
32+
override fun lookupShown(event: LookupEvent) {
33+
CodeWhispererPopupManager.getInstance().shouldEditorChangeCancelPopup++
34+
LOG.debug { "Incrementing shouldListenerCancelPopup semaphore value" }
35+
super.lookupShown(event)
2836
}
37+
2938
override fun itemSelected(event: LookupEvent) {
3039
if (!CodeWhispererInvocationStatus.getInstance().isDisplaySessionActive() ||
3140
!(event.lookup as LookupImpl).isShown
@@ -46,7 +55,12 @@ fun addIntelliSenseAcceptListener(lookup: Lookup, states: InvocationContext) {
4655

4756
private fun cleanup() {
4857
lookup.removeLookupListener(this)
49-
CodeWhispererPopupManager.getInstance().shouldListenerCancelPopup = true
58+
if (CodeWhispererPopupManager.getInstance().shouldEditorChangeCancelPopup <= 0) {
59+
LOG.error("shouldListenerCancelPopup semaphore is not updated correctly")
60+
} else {
61+
CodeWhispererPopupManager.getInstance().shouldEditorChangeCancelPopup--
62+
LOG.debug { "Decrementing shouldListenerCancelPopup semaphore value" }
63+
}
5064
}
5165
})
5266
}

0 commit comments

Comments
 (0)