IDEA-63617 Soft wraps: Correct caret positioning after commenting soft-wrapped line

1. Caret is moved to the necessary number of visual lines after line comment action;
2. Corresponding test is added;
This commit is contained in:
Denis Zhdanov
2010-12-29 08:11:12 +03:00
parent 2d5dafc984
commit 1082882cb4
3 changed files with 25 additions and 2 deletions
@@ -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 {
@@ -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)) {
@@ -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);