From d8e7c96fb6ab4a2617c334339ffeb6ae64eaafcc Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Fri, 6 Jul 2018 17:23:38 +0200 Subject: [PATCH] Retype file: use Enter handler correctly (IDEA-CR-34593) --- .../com/intellij/internal/RetypeFileAction.kt | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/internal/RetypeFileAction.kt b/platform/lang-impl/src/com/intellij/internal/RetypeFileAction.kt index c711c4a8e614..ca0e4a049fc5 100644 --- a/platform/lang-impl/src/com/intellij/internal/RetypeFileAction.kt +++ b/platform/lang-impl/src/com/intellij/internal/RetypeFileAction.kt @@ -88,7 +88,21 @@ class RetypeSession(private val project: Project, private val editor: Editor, pr } } if (!lookupSelected) { - (editor as EditorImpl).type(text[pos++].toString()) + val c = text[pos] + if (c == '\n') { + // catch up with previously triggered typed handlers + if (syncPositionWithEditor()) { + queueNext() + return + } + pos++ + executeEditorAction(IdeActions.ACTION_EDITOR_ENTER) + syncPositionWithEditor() + } + else { + pos++ + (editor as EditorImpl).type(c.toString()) + } } if (pos < text.length) { queueNext() @@ -104,8 +118,10 @@ class RetypeSession(private val project: Project, private val editor: Editor, pr } } - private fun syncPositionWithEditor() { - while (pos < editor.document.text.length && editor.document.charsSequence[pos] == text[pos]) { + private fun syncPositionWithEditor(): Boolean { + var result = false + while (pos < editor.document.text.length && pos < text.length && editor.document.charsSequence[pos] == text[pos]) { + result = true completedChars++ pos++ } @@ -114,12 +130,14 @@ class RetypeSession(private val project: Project, private val editor: Editor, pr } else { // unwanted completion, backtrack + println("Text has diverged, backtracking. Editor text:\n${editor.document.text}\nBuffer text:\n${text.substring(0, pos)}") WriteCommandAction.runWriteCommandAction(project) { while (editor.caretModel.offset > pos) { executeEditorAction(IdeActions.ACTION_EDITOR_BACKSPACE) } } } + return result } private fun executeEditorAction(actionId: String) {