diff --git a/platform/lang-impl/src/com/intellij/codeInsight/generation/CommentByLineCommentHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/generation/CommentByLineCommentHandler.java index 82905466722e..e10fd362f758 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/generation/CommentByLineCommentHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/generation/CommentByLineCommentHandler.java @@ -151,8 +151,11 @@ public class CommentByLineCommentHandler implements CodeInsightActionHandler { else { if (!hasSelection) { // Don't tweak caret position if we're already located on the last document line. - if (myEditor.getCaretModel().getLogicalPosition().line < myDocument.getLineCount() - 1) { - myEditor.getCaretModel().moveCaretRelatively(0, 1, false, false, true); + LogicalPosition position = myEditor.getCaretModel().getLogicalPosition(); + if (position.line < myDocument.getLineCount() - 1) { + int verticalShift = 1 + myEditor.getSoftWrapModel().getSoftWrapsForLine(position.line).size() + - position.softWrapLinesOnCurrentLogicalLine; + myEditor.getCaretModel().moveCaretRelatively(0, verticalShift, false, false, true); } } else { diff --git a/platform/platform-impl/testSrc/com/intellij/openapi/editor/impl/softwrap/mapping/SoftWrapApplianceOnDocumentModificationTest.java b/platform/platform-impl/testSrc/com/intellij/openapi/editor/impl/softwrap/mapping/SoftWrapApplianceOnDocumentModificationTest.java index d7d990ba6573..b77e9a1f13ac 100644 --- a/platform/platform-impl/testSrc/com/intellij/openapi/editor/impl/softwrap/mapping/SoftWrapApplianceOnDocumentModificationTest.java +++ b/platform/platform-impl/testSrc/com/intellij/openapi/editor/impl/softwrap/mapping/SoftWrapApplianceOnDocumentModificationTest.java @@ -629,6 +629,21 @@ public class SoftWrapApplianceOnDocumentModificationTest extends AbstractEditorP } } + public void testVerticalCaretShiftOnLineComment() throws IOException { + String text = + "1. just a line that is long enough to be soft wrapped\n" + + "2. just a line that is long enough to be soft wrapped\n" + + "3. just a line that is long enough to be soft wrapped\n" + + "4. just a line that is long enough to be soft wrapped"; + init(100, text); + + CaretModel caretModel = myEditor.getCaretModel(); + caretModel.moveToOffset(text.indexOf("2.") + 2); + lineComment(); + + assertEquals(myEditor.offsetToLogicalPosition(text.indexOf("3.") + 2), caretModel.getLogicalPosition()); + } + private static TIntHashSet collectSoftWrapStartOffsets(int documentLine) { TIntHashSet result = new TIntHashSet(); for (SoftWrap softWrap : myEditor.getSoftWrapModel().getSoftWrapsForLine(documentLine)) { diff --git a/platform/testFramework/src/com/intellij/testFramework/LightPlatformCodeInsightTestCase.java b/platform/testFramework/src/com/intellij/testFramework/LightPlatformCodeInsightTestCase.java index f10be1e141b6..298b52ea9311 100644 --- a/platform/testFramework/src/com/intellij/testFramework/LightPlatformCodeInsightTestCase.java +++ b/platform/testFramework/src/com/intellij/testFramework/LightPlatformCodeInsightTestCase.java @@ -15,6 +15,7 @@ */ package com.intellij.testFramework; +import com.intellij.codeInsight.generation.CommentByLineCommentHandler; import com.intellij.ide.DataManager; import com.intellij.injected.editor.DocumentWindow; import com.intellij.injected.editor.EditorWindow; @@ -491,6 +492,10 @@ public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTest doAction(IdeActions.ACTION_EDITOR_PASTE); } + protected static void lineComment() { + new CommentByLineCommentHandler().invoke(getProject(), getEditor(), getFile()); + } + private static void doAction(@NotNull String actionId) { EditorActionManager actionManager = EditorActionManager.getInstance(); EditorActionHandler actionHandler = actionManager.getActionHandler(actionId);