diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/JavaReformatOnTypingTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/JavaReformatOnTypingTest.java index ec6b370badbc..9eeb7255de0d 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/JavaReformatOnTypingTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/JavaReformatOnTypingTest.java @@ -47,7 +47,7 @@ public class JavaReformatOnTypingTest extends LightPlatformCodeInsightFixtureTes } public void test_AddSpacesAroundAssignmentOperator() throws Exception { - doTest("class T { int }", "=", "class T { int = }"); + doTest("class T { int }", "=", "class T { int = }"); } public void test_IgnoreSpacePressedAfterAssignmentOperator() { @@ -59,9 +59,7 @@ public class JavaReformatOnTypingTest extends LightPlatformCodeInsightFixtureTes } public void test_DoNotInsertDoubleSpaceBeforeAssignment() { - doTest("class T { int }", - "=", - "class T { int = }"); + doTest("class T { int }", "=", "class T { int = }"); } public void test_DoNotInsertDoubleSpaceAnywhere() { @@ -73,21 +71,21 @@ public class JavaReformatOnTypingTest extends LightPlatformCodeInsightFixtureTes } public void test_DoNotInsertSpaceIfNotAssignment() { - doTest("1 ", "!=", "1 != "); - doTest("c ", ">=", "c >= "); - doTest("c ", "<=", "c <= "); - doTest("c ", "+=", "c += "); - doTest("c ", "-=", "c -= "); - doTest("c ", "*=", "c *= "); - doTest("c ", "/=", "c /= "); - doTest("c ", "&=", "c &= "); - doTest("c ", "%=", "c %= "); - doTest("c ", "^=", "c ^= "); - doTest("c ", "|=", "c |= "); + doTest("1 ", "!=a", "1 != a"); + doTest("c ", ">=a", "c >= a"); + doTest("c ", "<=a", "c <= a"); + doTest("c ", "+=a", "c += a"); + doTest("c ", "-=a", "c -= a"); + doTest("c ", "*=a", "c *= a"); + doTest("c ", "/=a", "c /= a"); + doTest("c ", "&=a", "c &= a"); + doTest("c ", "%=a", "c %= a"); + doTest("c ", "^=a", "c ^= a"); + doTest("c ", "|=a", "c |= a"); } public void test_DistinguishAssignmentAndEquality() { - doTest("class T { boolean b = 1 }", "==", "class T { boolean b = 1 == }"); + doTest("class T { boolean b = 1 }", "==", "class T { boolean b = 1 == }"); } private void doTest(String before, String typing, String after) { diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/AutoFormatTypedHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/AutoFormatTypedHandler.java index e5bd8da3d46b..ee2f12e0e5df 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/AutoFormatTypedHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/AutoFormatTypedHandler.java @@ -42,6 +42,10 @@ public class AutoFormatTypedHandler extends TypedActionHandlerBase { '+', '-', '*', '/', '%', '&', '^', '|', '<', '>', '!', '=', ' ' }; + private Document myLastEditedDocument; + private char myLastTypedChar; + private int myLastTypedOffset; + public AutoFormatTypedHandler(@Nullable TypedActionHandler originalHandler) { super(originalHandler); } @@ -58,20 +62,37 @@ public class AutoFormatTypedHandler extends TypedActionHandlerBase { @Override public void execute(@NotNull Editor editor, char charTyped, @NotNull DataContext dataContext) { + if (!isEnabled()) { + executeOriginalHandler(editor, charTyped, dataContext); + return; + } + Document document = editor.getDocument(); int caretOffset = editor.getCaretModel().getOffset(); CharSequence text = document.getImmutableCharSequence(); - if (isEnabled() && charTyped == '=' - && isSpaceAroundAssignment(editor, dataContext) - && shouldInsertBefore(caretOffset, text)) - { + if (charTyped == '=' && isSpaceAroundAssignment(editor, dataContext) && shouldInsertBefore(caretOffset, text)) { + EditorModificationUtil.insertStringAtCaret(editor, " "); + } + if (isSameDocumentAsPrevious(editor) && myLastTypedChar == '=' && charTyped != '=' && charTyped != ' ') { EditorModificationUtil.insertStringAtCaret(editor, " "); } + executeOriginalHandler(editor, charTyped, dataContext); + + myLastTypedChar = charTyped; + myLastTypedOffset = editor.getCaretModel().getOffset(); + myLastEditedDocument = editor.getDocument(); + } + + private void executeOriginalHandler(@NotNull Editor editor, char charTyped, @NotNull DataContext dataContext) { if (myOriginalHandler != null) myOriginalHandler.execute(editor, charTyped, dataContext); } + private boolean isSameDocumentAsPrevious(Editor editor) { + return editor.getDocument() == myLastEditedDocument && editor.getCaretModel().getOffset() == myLastTypedOffset; + } + private static boolean shouldInsertBefore(int caretOffset, CharSequence text) { if (caretOffset == 0) return false; char charBefore = text.charAt(caretOffset - 1);