in plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/popup/CodeWhispererPopupManager.kt [119:189]
fun changeStates(
states: InvocationContext,
indexChange: Int,
recommendationAdded: Boolean = false,
) {
val (_, _, recommendationContext, popup) = states
val (details) = recommendationContext
if (recommendationAdded) {
LOG.debug {
"Add recommendations to the existing CodeWhisperer session, current number of recommendations: ${details.size}"
}
ApplicationManager.getApplication().messageBus.syncPublisher(CODEWHISPERER_POPUP_STATE_CHANGED)
.recommendationAdded(states, sessionContext)
return
}
val userInputOriginal = recommendationContext.userInputOriginal
val userInput = recommendationContext.userInputSinceInvocation
val typeaheadOriginal = run {
val startOffset = states.requestContext.caretPosition.offset
val currOffset = states.requestContext.editor.caretModel.offset
if (startOffset > currOffset) {
cancelPopup(popup)
return
}
// userInput + typeahead
val prefix = states.requestContext.editor.document.charsSequence
.substring(startOffset, currOffset)
if (prefix.length < userInputOriginal.length) {
cancelPopup(popup)
return
} else {
prefix.substring(userInputOriginal.length)
}
}
val isReverse = indexChange < 0
val validCount = getValidCount(details, userInput, typeaheadOriginal)
val validSelectedIndex = getValidSelectedIndex(details, userInput, sessionContext.selectedIndex, typeaheadOriginal)
if ((validSelectedIndex == validCount - 1 && indexChange == 1) ||
(validSelectedIndex == 0 && indexChange == -1)
) {
return
}
val selectedIndex = findNewSelectedIndex(
isReverse,
details,
userInput,
sessionContext.selectedIndex + indexChange,
typeaheadOriginal
)
if (selectedIndex == -1 || !isValidRecommendation(details[selectedIndex], userInput, typeaheadOriginal)) {
LOG.debug { "None of the recommendation is valid at this point, cancelling the popup" }
cancelPopup(popup)
return
}
val typeahead = resolveTypeahead(states, selectedIndex, typeaheadOriginal)
sessionContext = SessionContext(
typeahead,
typeaheadOriginal,
selectedIndex,
sessionContext.seen,
sessionContext.toBeRemovedHighlighter,
isPopupShowing = sessionContext.isPopupShowing,
perceivedLatency = sessionContext.perceivedLatency
)
ApplicationManager.getApplication().messageBus.syncPublisher(CODEWHISPERER_POPUP_STATE_CHANGED).stateChanged(
states,
sessionContext
)
}