@@ -449,10 +449,12 @@ abstract class AbstractTextInput(
449449
450450 val absoluteStart = LinePosition (0 , 0 , isVisual = true )
451451 val replaceTextOperation = ReplaceTextOperation (
452- absoluteStart,
453- LinePosition (visualLines.lastIndex, visualLines.last().length, isVisual = true ),
454- newText,
455- true
452+ AddTextOperation (newText, absoluteStart),
453+ RemoveTextOperation (
454+ absoluteStart,
455+ LinePosition (visualLines.lastIndex, visualLines.last().length, isVisual = true ),
456+ selectAfterUndo = true
457+ )
456458 )
457459 commitTextOperation(replaceTextOperation)
458460 }
@@ -523,13 +525,16 @@ abstract class AbstractTextInput(
523525 return
524526 }
525527
528+ val addTextOperation = AddTextOperation (text, cursor)
529+
526530 if (hasSelection()) {
527- val replaceTextOperation = ReplaceTextOperation (selectionStart(), selectionEnd(), newText, true )
531+ val removeTextOperation = RemoveTextOperation (selectionStart(), selectionEnd(), selectAfterUndo = true )
532+ val replaceTextOperation = ReplaceTextOperation (addTextOperation, removeTextOperation)
528533 commitTextOperation(replaceTextOperation)
529534 return
530535 }
531536
532- commitTextOperation(AddTextOperation (text, cursor) )
537+ commitTextOperation(addTextOperation )
533538 }
534539
535540 protected fun addText (newText : String , position : LinePosition ) {
@@ -1100,29 +1105,17 @@ abstract class AbstractTextInput(
11001105 }
11011106
11021107 protected inner class ReplaceTextOperation (
1103- private val startPos : LinePosition ,
1104- private val endPos : LinePosition ,
1105- private val newText : String ,
1106- private val selectAfterUndo : Boolean ,
1108+ val addTextOperation : AddTextOperation ,
1109+ val removeTextOperation : RemoveTextOperation
11071110 ) : TextOperation() {
1108- private val textOld = getTextBetween(startPos, endPos)
1109- private var removeStartPos: LinePosition = startPos
1110-
11111111 override fun redo () {
1112- removeText(startPos, endPos)
1113- removeStartPos = minOf(cursor, startPos)
1114- addText(newText, removeStartPos)
1112+ removeTextOperation.redo()
1113+ addTextOperation.redo()
11151114 }
11161115
11171116 override fun undo () {
1118- removeText(removeStartPos, removeStartPos.offsetColumn(newText.length))
1119- addText(textOld, startPos)
1120- setCursorPosition(endPos.toVisualPos())
1121- if (selectAfterUndo) {
1122- cursor = startPos
1123- otherSelectionEnd = endPos
1124- cursorNeedsRefocus = true
1125- }
1117+ addTextOperation.undo()
1118+ removeTextOperation.undo()
11261119 }
11271120 }
11281121
0 commit comments