feat: register an active handler to detect intentional cursor movements

This commit is contained in:
Jacob Kim 2025-08-14 19:15:50 -07:00
parent 6f2c029ceb
commit 209de229c0
2 changed files with 129 additions and 76 deletions

View file

@ -1,15 +1,12 @@
package com.github.continuedev.continueintellijextension.nextEdit
import com.github.continuedev.continueintellijextension.listeners.HandlerPriority
import com.github.continuedev.continueintellijextension.listeners.SelectionChangeManager
import com.github.continuedev.continueintellijextension.listeners.StateSnapshot
import com.github.continuedev.continueintellijextension.listeners.ActiveHandlerManager
import com.github.continuedev.continueintellijextension.utils.InlineCompletionUtils
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.Service
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.LogicalPosition
import com.intellij.openapi.editor.ScrollType
import com.intellij.openapi.editor.event.SelectionEvent
import com.intellij.openapi.editor.markup.HighlighterLayer
import com.intellij.openapi.editor.markup.HighlighterTargetArea
import com.intellij.openapi.editor.markup.RangeHighlighter
@ -48,6 +45,9 @@ class NextEditJumpManager(private val project: Project) {
// State management
private var jumpState = JumpState()
// Handler management
private var jumpHandler: NextEditJumpHandler? = null
// UI components
private var jumpPopup: JBPopup? = null
private var editorHighlighter: RangeHighlighter? = null
@ -67,7 +67,7 @@ class NextEditJumpManager(private val project: Project) {
)
init {
registerSelectionChangeHandler()
// registerSelectionChangeHandler()
}
// Public API
@ -97,6 +97,15 @@ class NextEditJumpManager(private val project: Project) {
fun getSavedCompletionAfterJump(): CompletionDataForAfterJump? = jumpState.completionAfterJump
fun getJumpPosition(): LogicalPosition? = jumpState.jumpPosition
fun getOriginalPosition(): LogicalPosition? = jumpState.oldCursorPosition
fun abortJump() {
// Public method that delegates to the existing private rejectJump logic
rejectJump()
}
// Main entry point
suspend fun suggestJump(
editor: Editor,
@ -122,6 +131,11 @@ class NextEditJumpManager(private val project: Project) {
oldCursorPosition = currentPosition
)
// Register active handler to track cursor movements during jump
jumpHandler = NextEditJumpHandler(project, this)
val activeHandlerManager = project.getService(ActiveHandlerManager::class.java)
activeHandlerManager.setActiveHandler(jumpHandler!!)
// Show UI - ensure this runs on EDT
return try {
var result = false
@ -333,6 +347,9 @@ class NextEditJumpManager(private val project: Project) {
kotlinx.coroutines.delay(100)
jumpState = jumpState.copy(justAccepted = false)
// Clear the active handler after jump is complete
clearActiveHandler()
// Trigger inline suggestion
triggerInlineSuggestCallback?.invoke()
}
@ -352,6 +369,9 @@ class NextEditJumpManager(private val project: Project) {
// Clear state
clearJumpState()
// Clear handler when rejecting
clearActiveHandler()
}
private fun clearJumpDecoration() {
@ -413,53 +433,62 @@ class NextEditJumpManager(private val project: Project) {
}
}
private fun registerSelectionChangeHandler() {
val selectionManager = project.getService(SelectionChangeManager::class.java)
selectionManager.registerListener(
"nextEditJumpManager",
{ event, state -> handleSelectionChange(event, state) },
HandlerPriority.HIGH
)
}
private suspend fun handleSelectionChange(
event: SelectionEvent,
state: StateSnapshot
): Boolean {
// Preserve chain during jump operations
when {
state.jumpInProgress -> {
println("Jump in progress, preserving chain")
// Check if cursor moved away from expected position
val currentPos = event.editor.caretModel.logicalPosition
val oldPos = jumpState.oldCursorPosition
val jumpPos = jumpState.jumpPosition
// If cursor moved but not to jump position, reject the jump
if (oldPos != null && jumpPos != null &&
!currentPos.equals(oldPos) && !currentPos.equals(jumpPos)
) {
println("DEBUG: Cursor moved unexpectedly, rejecting jump")
rejectJump()
}
return true
}
state.jumpJustAccepted -> {
println("Jump just accepted, preserving chain")
return true
}
else -> return false
}
}
// private fun registerSelectionChangeHandler() {
// val selectionManager = project.getService(SelectionChangeManager::class.java)
//
// selectionManager.registerListener(
// "nextEditJumpManager",
// { event, state -> handleSelectionChange(event, state) },
// HandlerPriority.HIGH
// )
// }
//
// private suspend fun handleSelectionChange(
// event: SelectionEvent,
// state: StateSnapshot
// ): Boolean {
// // Preserve chain during jump operations
// when {
// state.jumpInProgress -> {
// println("Jump in progress, preserving chain")
//
// // Check if cursor moved away from expected position
// val currentPos = event.editor.caretModel.logicalPosition
// val oldPos = jumpState.oldCursorPosition
// val jumpPos = jumpState.jumpPosition
//
// // If cursor moved but not to jump position, reject the jump
// if (oldPos != null && jumpPos != null &&
// !currentPos.equals(oldPos) && !currentPos.equals(jumpPos)
// ) {
// println("DEBUG: Cursor moved unexpectedly, rejecting jump")
// rejectJump()
// }
//
// return true
// }
//
// state.jumpJustAccepted -> {
// println("Jump just accepted, preserving chain")
// return true
// }
//
// else -> return false
// }
// }
fun cleanup() {
clearJumpState()
triggerInlineSuggestCallback = null
deleteChainCallback = null
}
private fun clearActiveHandler() {
jumpHandler?.let { handler ->
val activeHandlerManager = project.getService(ActiveHandlerManager::class.java)
activeHandlerManager.clearActiveHandler()
handler.dispose()
}
jumpHandler = null
}
}

View file

@ -1,17 +1,15 @@
package com.github.continuedev.continueintellijextension.nextEdit
import com.github.continuedev.continueintellijextension.Position
import com.github.continuedev.continueintellijextension.listeners.HandlerPriority
import com.github.continuedev.continueintellijextension.listeners.SelectionChangeManager
import com.github.continuedev.continueintellijextension.listeners.StateSnapshot
import com.github.continuedev.continueintellijextension.listeners.ActiveHandlerManager
import com.github.continuedev.continueintellijextension.utils.InlineCompletionUtils
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.components.Service
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.LogicalPosition
import com.intellij.openapi.editor.colors.EditorColorsManager
import com.intellij.openapi.editor.colors.EditorFontType
import com.intellij.openapi.editor.event.SelectionEvent
import com.intellij.openapi.editor.markup.*
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.popup.JBPopup
@ -33,6 +31,9 @@ class NextEditWindowManager(private val project: Project) {
private var isAccepted = false
private var currentCompletionId: String? = null
// Handler management
private var windowHandler: NextEditWindowHandler? = null
suspend fun showNextEditWindow(
editor: Editor,
currCursorPos: Position,
@ -54,6 +55,12 @@ class NextEditWindowManager(private val project: Project) {
// Clear existing decorations first
hideAllNextEditWindows()
// Register active handler for this window
val cursorPosition = LogicalPosition(currCursorPos.line, currCursorPos.character)
windowHandler = NextEditWindowHandler(project, this, cursorPosition)
val activeHandlerManager = project.getService(ActiveHandlerManager::class.java)
activeHandlerManager.setActiveHandler(windowHandler!!)
// Determine if this is a line deletion case
val isLineDelete = determineIfLineDelete(
editor,
@ -600,6 +607,10 @@ class NextEditWindowManager(private val project: Project) {
println(" isLineDelete: $isLineDelete")
isAccepted = true
// Clear handler before making document changes that move cursor
clearActiveHandler()
hideAllNextEditWindows()
try {
@ -695,6 +706,8 @@ class NextEditWindowManager(private val project: Project) {
}
private fun rejectEdit() {
clearActiveHandler()
hideAllNextEditWindows()
// Log rejection and delete chain (placeholder)
@ -719,6 +732,8 @@ class NextEditWindowManager(private val project: Project) {
// Clear all deletion decorations without tracking editors
clearAllDeletionDecorations()
deletionHighlighters = emptyList()
clearActiveHandler() // TODO: might be redundant
}
private fun clearAllDeletionDecorations() {
@ -732,6 +747,15 @@ class NextEditWindowManager(private val project: Project) {
}
}
private fun clearActiveHandler() {
windowHandler?.let { handler ->
val activeHandlerManager = project.getService(ActiveHandlerManager::class.java)
activeHandlerManager.clearActiveHandler()
handler.dispose()
}
windowHandler = null
}
fun hasAccepted(): Boolean = isAccepted
fun setAccepted(accepted: Boolean) {
@ -747,30 +771,30 @@ class NextEditWindowManager(private val project: Project) {
currentCompletionId = null
}
fun registerSelectionChangeHandler() {
val selectionManager = project.getService(SelectionChangeManager::class.java)
selectionManager.registerListener(
"nextEditWindowManager",
{ event, state ->
handleSelectionChange(event, state)
},
HandlerPriority.HIGH
)
}
private suspend fun handleSelectionChange(
event: SelectionEvent,
state: StateSnapshot
): Boolean {
// If window was just accepted, preserve the chain
if (state.nextEditWindowAccepted) {
println("Next edit window accepted, preserving chain")
return true
}
return false
}
// fun registerSelectionChangeHandler() {
// val selectionManager = project.getService(SelectionChangeManager::class.java)
//
// selectionManager.registerListener(
// "nextEditWindowManager",
// { event, state ->
// handleSelectionChange(event, state)
// },
// HandlerPriority.HIGH
// )
// }
//
// private suspend fun handleSelectionChange(
// event: SelectionEvent,
// state: StateSnapshot
// ): Boolean {
// // If window was just accepted, preserve the chain
// if (state.nextEditWindowAccepted) {
// println("Next edit window accepted, preserving chain")
// return true
// }
//
// return false
// }
}
enum class PopupAction {