From df823d606fc631ecd87871c6aefbf2947d738b8d Mon Sep 17 00:00:00 2001 From: Yaroslav Lepenkin Date: Thu, 31 Oct 2013 20:21:42 +0400 Subject: [PATCH] IDEA-115756 Caret is moved on the start of line after formatting, if positioned not on the end of line [CR-IC-2978] --- .../java/JavaFormatterInEditorTest.java | 61 +++++++++++++++++-- .../codeStyle/CodeStyleManagerImpl.java | 14 ++--- 2 files changed, 62 insertions(+), 13 deletions(-) diff --git a/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterInEditorTest.java b/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterInEditorTest.java index de3e88e4e6c1..169e7657a329 100644 --- a/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterInEditorTest.java +++ b/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterInEditorTest.java @@ -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 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" + + " \n" + + " }\n" + + "}"; + doTest(text, text); + + String after = "class Test {\n" + + " void test() {\n" + + " \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" + + " \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" + + " \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" + + " \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); } -} +} \ No newline at end of file diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerImpl.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerImpl.java index 87a489b2e1df..92eaec73b035 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerImpl.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerImpl.java @@ -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 { * * *

- * This method inserts that dummy comment (fallback to identifier xxx, see {@link CodeStyleManagerImpl#createDummy(PsiFile)}) + * This method inserts that dummy comment (fallback to identifier xxx, see {@link CodeStyleManagerImpl#createDummy(PsiFile)}) * if necessary (if target line contains white space symbols only). *

- + * Note: 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