fix: resolve crashes in DiffStreamHandler — unsafe cast and negative line numbers (#11893)

* fix: resolve crashes in DiffStreamHandler — unsafe cast and negative line numbers

- Use safe cast (as?) for TextEditor in undoChanges() to prevent ClassCastException
- Add early return for empty documents and clamp line numbers to prevent IndexOutOfBoundsException in initUnfinishedRangeHighlights()
- Remove redundant .toList() call in rejectAll()

Fixes #4267

* refactor: inline lineNum variable in initUnfinishedRangeHighlights
This commit is contained in:
Dallin Romney 2026-03-26 14:28:11 -07:00 • committed by GitHub
parent e8d2c4e139
commit 5a8d10fff4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -82,7 +82,7 @@ class DiffStreamHandler(
if (hasAcceptedOrRejectedBlock) {
ApplicationManager.getApplication().invokeLater {
val blocksToReject = diffBlocks.toList()
blocksToReject.toList().forEach { it.handleReject() }
blocksToReject.forEach { it.handleReject() }
}
} else {
undoChanges()
@ -265,14 +265,14 @@ class DiffStreamHandler(
}
private fun initUnfinishedRangeHighlights() {
if (editor.document.lineCount == 0) return
val editorUtils = EditorUtils(editor)
val unfinishedKey = editorUtils.createTextAttributesKey("CONTINUE_DIFF_UNFINISHED_LINE", 0x20888888)
for (i in startLine..endLine) {
val highlighter = editor.markupModel.addLineHighlighter(
unfinishedKey, min(
i, editor.document.lineCount - 1
), HighlighterLayer.LAST
unfinishedKey, min(i, editor.document.lineCount - 1).coerceAtLeast(0), HighlighterLayer.LAST
)
unfinishedHighlighters.add(highlighter)
}
@ -413,7 +413,8 @@ class DiffStreamHandler(
WriteCommandAction.runWriteCommandAction(project) {
val undoManager = UndoManager.getInstance(project)
val fileEditor = FileEditorManager.getInstance(project).getSelectedEditor(virtualFile) as TextEditor
val fileEditor = FileEditorManager.getInstance(project).getSelectedEditor(virtualFile) as? TextEditor
?: return@runWriteCommandAction
if (undoManager.isUndoAvailable(fileEditor)) {
val numChanges = diffBlocks.sumOf { it.deletedLines.size + it.addedLines.size }