IDEA-115756 Caret is moved on the start of line after formatting, if positioned not on the end of line [CR-IC-2978]

This commit is contained in:
Yaroslav Lepenkin
2013-10-31 20:22:13 +04:00
parent 97c9424063
commit df823d606f
2 changed files with 62 additions and 13 deletions
@@ -23,13 +23,13 @@ import org.jetbrains.annotations.NotNull;
import java.io.IOException;
/**
* Is intended to test formatting in editor behavior, i.e. check how formatting affects things like caret position, selection etc.
*
* Is intended to test formatting in editor behavior, i.e. check how formatting affects things like caret position, selection etc.
*
* @author Denis Zhdanov
* @since 6/1/11 6:17 PM
*/
public class JavaFormatterInEditorTest extends LightPlatformCodeInsightTestCase {
public void testCaretPositionOnLongLineWrapping() throws IOException {
// Inspired by IDEA-70242
getCurrentCodeStyleSettings().getCommonSettings(JavaLanguage.INSTANCE).WRAP_LONG_LINES = true;
@@ -39,7 +39,7 @@ public class JavaFormatterInEditorTest extends LightPlatformCodeInsightTestCase
"\n" +
"class <caret>Test {\n" +
"}",
"import static java.util.concurrent\n" +
" .atomic.AtomicInteger.*;\n" +
"\n" +
@@ -47,10 +47,59 @@ public class JavaFormatterInEditorTest extends LightPlatformCodeInsightTestCase
"}"
);
}
public void testCaretPositionPreserved_WhenOnSameLineWithWhiteSpacesOnly() throws IOException {
String text = "class Test {\n" +
" void test() {\n" +
" <caret>\n" +
" }\n" +
"}";
doTest(text, text);
String after = "class Test {\n" +
" void test() {\n" +
" <caret> \n" +
" }\n" +
"}";
doTest(text, after);
}
public void testCaretPositionPreserved_WhenSomeFormattingNeeded() throws IOException {
String before = "public class Test {\n" +
" int a;\n" +
" \n" +
" public static void main(String[] args) {\n" +
" <caret>\n" +
" }\n" +
"\n" +
" static final long j = 2;\n" +
"}";
String after = "public class Test {\n" +
" int a;\n" +
"\n" +
" public static void main(String[] args) {\n" +
" <caret>\n" +
" }\n" +
"\n" +
" static final long j = 2;\n" +
"}";
doTest(before, after);
before = "public class Test {\n" +
" int a;\n" +
" \n" +
" public static void main(String[] args) {\n" +
" <caret> \n" +
" }\n" +
"\n" +
" static final long j = 2;\n" +
"}";
doTest(before, after);
}
public void doTest(@NotNull String before, @NotNull String after) throws IOException {
configureFromFileText(getTestName(false) + ".java", before);
CodeStyleManager.getInstance(getProject()).reformatText(getFile(), 0, getEditor().getDocument().getTextLength());
checkResultByText(after);
}
}
}
@@ -181,7 +181,7 @@ public class CodeStyleManagerImpl extends CodeStyleManager {
// So, if 'virtual space in editor' is enabled, we save target visual column. Caret indent is ensured otherwise
int visualColumnToRestore = -1;
String caretIndentToRestore = null;
RangeMarker caretRangeMarker = null;
RangeMarker beforeCaretRangeMarker = null;
if (editor != null) {
Document document = editor.getDocument();
@@ -202,7 +202,7 @@ public class CodeStyleManagerImpl extends CodeStyleManager {
if (fixCaretPosition) {
visualColumnToRestore = editor.getCaretModel().getVisualPosition().column;
caretIndentToRestore = document.getText(TextRange.create(lineStartOffset, caretOffset));
caretRangeMarker = document.createRangeMarker(lineStartOffset, caretOffset);
beforeCaretRangeMarker = document.createRangeMarker(0, lineStartOffset);
}
}
@@ -261,11 +261,11 @@ public class CodeStyleManagerImpl extends CodeStyleManager {
}
}
else {
if (caretRangeMarker == null || !caretRangeMarker.isValid() || caretIndentToRestore == null) {
if (beforeCaretRangeMarker == null || !beforeCaretRangeMarker.isValid() || caretIndentToRestore == null) {
return;
}
int offset = caretRangeMarker.getStartOffset();
caretRangeMarker.dispose();
int offset = beforeCaretRangeMarker.getEndOffset();
beforeCaretRangeMarker.dispose();
if (editor.getCaretModel().getVisualPosition().column == visualColumnToRestore) {
return;
}
@@ -484,10 +484,10 @@ public class CodeStyleManagerImpl extends CodeStyleManager {
* </ol>
* </pre>
* <p/>
* This method inserts that dummy comment (fallback to identifier <code>xxx</code>, see {@link CodeStyleManagerImpl#createDummy(PsiFile)})
* This method inserts that dummy comment (fallback to identifier <code>xxx</code>, see {@link CodeStyleManagerImpl#createDummy(PsiFile)})
* if necessary (if target line contains white space symbols only).
* <p/>
* <b>Note:</b> it's expected that the whole white space region that contains given offset is processed in a way that all
* {@link RangeMarker range markers} registered for the given offset are expanded to the whole white space region.
* E.g. there is a possible case that particular range marker serves for defining formatting range, hence, its start/end offsets