From e32e78766a6af18552ffdb57415ee5dc26c7084d Mon Sep 17 00:00:00 2001 From: Yaroslav Lepenkin Date: Tue, 19 Jan 2016 16:47:35 +0300 Subject: [PATCH] Add space only before '=' --- .../codeInsight/JavaReformatOnTypingTest.java | 4 ++ .../editorActions/AutoFormatTypedHandler.java | 45 +++++-------------- 2 files changed, 14 insertions(+), 35 deletions(-) diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/JavaReformatOnTypingTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/JavaReformatOnTypingTest.java index 1c0f2258dc5c..ec6b370badbc 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/JavaReformatOnTypingTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/JavaReformatOnTypingTest.java @@ -85,6 +85,10 @@ public class JavaReformatOnTypingTest extends LightPlatformCodeInsightFixtureTes doTest("c ", "^=", "c ^= "); doTest("c ", "|=", "c |= "); } + + public void test_DistinguishAssignmentAndEquality() { + doTest("class T { boolean b = 1 }", "==", "class T { boolean b = 1 == }"); + } private void doTest(String before, String typing, String after) { useSpacesAroundAssignmentOperator(true, myFixture.getProject(), JavaLanguage.INSTANCE); 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 6697def3dd13..e5bd8da3d46b 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/AutoFormatTypedHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/AutoFormatTypedHandler.java @@ -20,7 +20,6 @@ import com.intellij.lang.Language; import com.intellij.openapi.actionSystem.CommonDataKeys; import com.intellij.openapi.actionSystem.DataContext; import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.editor.CaretModel; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.EditorModificationUtil; @@ -39,11 +38,9 @@ import org.jetbrains.annotations.TestOnly; public class AutoFormatTypedHandler extends TypedActionHandlerBase { private static boolean myIsEnabledInTests = false; - private static char[] NO_SPACE_AFTER = { '+', '-', '*', '/', '%', '&', '^', '|', '<', '>', '!', '=', ' ' }; - - private boolean myIgnoreNextSpace = false; - private Document myLastModifiedDocument = null; - private int myLastOffset = -1; + private static char[] NO_SPACE_AFTER = { + '+', '-', '*', '/', '%', '&', '^', '|', '<', '>', '!', '=', ' ' + }; public AutoFormatTypedHandler(@Nullable TypedActionHandler originalHandler) { super(originalHandler); @@ -61,31 +58,18 @@ public class AutoFormatTypedHandler extends TypedActionHandlerBase { @Override public void execute(@NotNull Editor editor, char charTyped, @NotNull DataContext dataContext) { - if (isEnabled() && charTyped == ' ' && shouldIgnoreSpace(editor)) { - myIgnoreNextSpace = false; - return; - } - Document document = editor.getDocument(); - boolean addSpaces = isEnabled() && charTyped == '=' && shouldInsertSpaces(editor, dataContext); - int caretOffset = editor.getCaretModel().getOffset(); CharSequence text = document.getImmutableCharSequence(); - if (addSpaces && shouldInsertBefore(caretOffset, text)) { - EditorModificationUtil.insertStringAtCaret(editor, " "); - } - - if (myOriginalHandler != null) myOriginalHandler.execute(editor, charTyped, dataContext); - if (addSpaces) { + if (isEnabled() && charTyped == '=' + && isSpaceAroundAssignment(editor, dataContext) + && shouldInsertBefore(caretOffset, text)) + { EditorModificationUtil.insertStringAtCaret(editor, " "); - myIgnoreNextSpace = true; - myLastModifiedDocument = document; - myLastOffset = editor.getCaretModel().getOffset(); - } - else { - myIgnoreNextSpace = false; } + + if (myOriginalHandler != null) myOriginalHandler.execute(editor, charTyped, dataContext); } private static boolean shouldInsertBefore(int caretOffset, CharSequence text) { @@ -101,7 +85,7 @@ public class AutoFormatTypedHandler extends TypedActionHandlerBase { return true; } - private static boolean shouldInsertSpaces(Editor editor, DataContext dataContext) { + private static boolean isSpaceAroundAssignment(Editor editor, DataContext dataContext) { final Project project = CommonDataKeys.PROJECT.getData(dataContext); PsiFile file = project == null ? null : PsiUtilBase.getPsiFileInEditor(editor, project); if (file != null) { @@ -113,13 +97,4 @@ public class AutoFormatTypedHandler extends TypedActionHandlerBase { return false; } - private boolean shouldIgnoreSpace(@NotNull Editor editor) { - if (!myIgnoreNextSpace) { - return false; - } - Document document = editor.getDocument(); - CaretModel caretModel = editor.getCaretModel(); - return myLastModifiedDocument == document && myLastOffset == caretModel.getOffset(); - } - }