IDEA-86397 Delete line (Ctrl+Y) moves caret to the 1st column position

This commit is contained in:
Denis.Zhdanov
2012-05-25 09:15:44 +04:00
parent 3c37e19fb1
commit 6549a62893
2 changed files with 20 additions and 2 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -44,9 +44,12 @@ public class EditorModificationUtil {
int selectionStart = selectionModel.getSelectionStart();
int selectionEnd = selectionModel.getSelectionEnd();
editor.getCaretModel().moveToOffset(selectionStart);
final CaretModel caretModel = editor.getCaretModel();
final VisualPosition position = caretModel.getVisualPosition();
caretModel.moveToOffset(selectionStart);
selectionModel.removeSelection();
editor.getDocument().deleteString(selectionStart, selectionEnd);
caretModel.moveToVisualPosition(position); // Try to preserve visual caret position
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
}
@@ -980,6 +980,21 @@ public class SoftWrapApplianceOnDocumentModificationTest extends AbstractEditorP
assertEquals(text, myEditor.getSelectionModel().getSelectedText());
}
public void testCaretPositionAfterLineRemoval() throws IOException {
final String text =
"123456\n" +
"123456\n" +
"line towrapbecauseitislong\n" +
"923456";
init(8, text);
final CaretModel caretModel = myEditor.getCaretModel();
caretModel.moveToOffset(text.indexOf("e"));
final VisualPosition position = caretModel.getVisualPosition();
deleteLine();
assertEquals(text.substring(0, text.indexOf("line")) + text.substring(text.indexOf('9')), myEditor.getDocument().getText());
assertEquals(position, caretModel.getVisualPosition());
}
private void init(final int visibleWidthInColumns, @NotNull String fileText) throws IOException {
int symbolWidthInPixels = 7;
init(visibleWidthInColumns * symbolWidthInPixels, fileText, symbolWidthInPixels);