Skip to content

Commit

Permalink
bug fix(amazonq): fix an issue where user action is accept but teleme…
Browse files Browse the repository at this point in the history
…try is reject (aws#5319)

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

* detekt

* fix increment logic when IntelliSense is showing up

* Change to use a semaphore
  • Loading branch information
andrewyuq authored and KevinDing1 committed Feb 6, 2025
1 parent 39f7f7b commit 6ef5cf3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ import com.intellij.ui.popup.AbstractPopup
import com.intellij.ui.popup.PopupFactoryImpl
import com.intellij.util.messages.Topic
import com.intellij.util.ui.UIUtil
import kotlinx.coroutines.sync.Semaphore
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 +87,8 @@ import javax.swing.JLabel
class CodeWhispererPopupManager {
val popupComponents = CodeWhispererPopupComponents()

var shouldListenerCancelPopup: Boolean = true
// Act like a semaphore: one increment only corresponds to one decrement
var allowEditsDuringSuggestionPreview = Semaphore(MAX_EDIT_SOURCE_DURING_SUGGESTION_PREVIEW)
var sessionContext = SessionContext()
private set

Expand Down Expand Up @@ -245,12 +248,20 @@ class CodeWhispererPopupManager {
}
}

// Don't want to block or throw any kinds of exceptions here if it can continue to provide suggestions
fun dontClosePopupAndRun(runnable: () -> Unit) {
try {
shouldListenerCancelPopup = false
runnable()
} finally {
shouldListenerCancelPopup = true
if (allowEditsDuringSuggestionPreview.tryAcquire()) {
try {
runnable()
} finally {
try {
allowEditsDuringSuggestionPreview.release()
} catch (e: Exception) {
LOG.error(e) { "Failed to release allowEditsDuringSuggestionPreview semaphore" }
}
}
} else {
LOG.error { "Failed to acquire allowEditsDuringSuggestionPreview semaphore" }
}
}

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

val codewhispererCaretListener: CaretListener = object : CaretListener {
override fun caretPositionChanged(event: CaretEvent) {
if (shouldListenerCancelPopup) {
if (allowEditsDuringSuggestionPreview.availablePermits == MAX_EDIT_SOURCE_DURING_SUGGESTION_PREVIEW) {
cancelPopup(states.popup)
}
super.caretPositionChanged(event)
Expand Down Expand Up @@ -702,6 +713,7 @@ class CodeWhispererPopupManager {
"CodeWhisperer user action performed",
CodeWhispererUserActionListener::class.java
)
const val MAX_EDIT_SOURCE_DURING_SUGGESTION_PREVIEW = 2
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ 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.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 +21,17 @@ class CodeWhispererPopupIntelliSenseAcceptListener(private val states: Invocatio

addIntelliSenseAcceptListener(newLookup, states)
}

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

fun addIntelliSenseAcceptListener(lookup: Lookup, states: InvocationContext) {
if (!CodeWhispererPopupManager.getInstance().allowEditsDuringSuggestionPreview.tryAcquire()) {
LOG.error { "Failed to acquire allowEditsDuringSuggestionPreview semaphore" }
}
lookup.addLookupListener(object : LookupListener {
override fun beforeItemSelected(event: LookupEvent): Boolean {
CodeWhispererPopupManager.getInstance().shouldListenerCancelPopup = false
return super.beforeItemSelected(event)
}
override fun itemSelected(event: LookupEvent) {
if (!CodeWhispererInvocationStatus.getInstance().isDisplaySessionActive() ||
!(event.lookup as LookupImpl).isShown
Expand All @@ -46,7 +52,11 @@ fun addIntelliSenseAcceptListener(lookup: Lookup, states: InvocationContext) {

private fun cleanup() {
lookup.removeLookupListener(this)
CodeWhispererPopupManager.getInstance().shouldListenerCancelPopup = true
try {
CodeWhispererPopupManager.getInstance().allowEditsDuringSuggestionPreview.release()
} catch (e: Exception) {
LOG.error(e) { "Failed to release allowEditsDuringSuggestionPreview semaphore" }
}
}
})
}

0 comments on commit 6ef5cf3

Please sign in to comment.