From 8f1514a2abcecc708028837bc898d7e311d7f0ff Mon Sep 17 00:00:00 2001 From: Dmitry Batrak Date: Mon, 13 Oct 2014 19:12:36 +0400 Subject: [PATCH] IDEA-93998 In overwrite mode it is impossible to add new line to the file --- .../intellij/openapi/actionSystem/IdeActions.java | 1 + .../openapi/editor/actions/EnterAction.java | 13 +++++++++---- .../openapi/editor/actions/EditorActionTest.java | 7 +++++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/platform/platform-api/src/com/intellij/openapi/actionSystem/IdeActions.java b/platform/platform-api/src/com/intellij/openapi/actionSystem/IdeActions.java index 5043ba3792fb..66486d8da4be 100644 --- a/platform/platform-api/src/com/intellij/openapi/actionSystem/IdeActions.java +++ b/platform/platform-api/src/com/intellij/openapi/actionSystem/IdeActions.java @@ -56,6 +56,7 @@ public interface IdeActions { @NonNls String ACTION_EDITOR_CLONE_CARET_BELOW= "EditorCloneCaretBelow"; @NonNls String ACTION_EDITOR_CLONE_CARET_ABOVE= "EditorCloneCaretAbove"; @NonNls String ACTION_EDITOR_TOGGLE_STICKY_SELECTION= "EditorToggleStickySelection"; + @NonNls String ACTION_EDITOR_TOGGLE_OVERWRITE_MODE= "EditorToggleInsertState"; @NonNls String ACTION_EDITOR_NEXT_TEMPLATE_VARIABLE = "NextTemplateVariable"; @NonNls String ACTION_EDITOR_PREVIOUS_TEMPLATE_VARIABLE = "PreviousTemplateVariable"; diff --git a/platform/platform-impl/src/com/intellij/openapi/editor/actions/EnterAction.java b/platform/platform-impl/src/com/intellij/openapi/editor/actions/EnterAction.java index ea2107632122..5669f5c19950 100644 --- a/platform/platform-impl/src/com/intellij/openapi/editor/actions/EnterAction.java +++ b/platform/platform-impl/src/com/intellij/openapi/editor/actions/EnterAction.java @@ -58,9 +58,15 @@ public class EnterAction extends EditorAction { public static void insertNewLineAtCaret(Editor editor) { MacUIUtil.hideCursor(); + Document document = editor.getDocument(); + int caretLine = editor.getCaretModel().getLogicalPosition().line; if(!editor.isInsertMode()) { - if(editor.getCaretModel().getLogicalPosition().line < editor.getDocument().getLineCount()-1) { - LogicalPosition pos = new LogicalPosition(editor.getCaretModel().getLogicalPosition().line+1, 0); + int lineCount = document.getLineCount(); + if(caretLine < lineCount) { + if (caretLine == lineCount - 1) { + document.insertString(document.getTextLength(), "\n"); + } + LogicalPosition pos = new LogicalPosition(caretLine + 1, 0); editor.getCaretModel().moveToLogicalPosition(pos); editor.getSelectionModel().removeSelection(); editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); @@ -69,10 +75,9 @@ public class EnterAction extends EditorAction { } EditorModificationUtil.deleteSelectedText(editor); // Smart indenting here: - Document document = editor.getDocument(); CharSequence text = document.getCharsSequence(); - int indentLineNum = editor.getCaretModel().getLogicalPosition().line; + int indentLineNum = caretLine; int lineLength = 0; if (document.getLineCount() > 0) { for(;indentLineNum >= 0; indentLineNum--) { diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/editor/actions/EditorActionTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/editor/actions/EditorActionTest.java index b3dee9baedb5..a23766200251 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/editor/actions/EditorActionTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/editor/actions/EditorActionTest.java @@ -138,4 +138,11 @@ public class EditorActionTest extends AbstractEditorTest { executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_RIGHT); assertEquals(new VisualPosition(0, 6), myEditor.getCaretModel().getVisualPosition()); } + + public void testEnterOnLastLineInOverwriteMode() throws Exception { + init("text", TestFileType.TEXT); + executeAction(IdeActions.ACTION_EDITOR_TOGGLE_OVERWRITE_MODE); + executeAction(IdeActions.ACTION_EDITOR_ENTER); + checkResultByText("text\n"); + } } \ No newline at end of file