mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
minor: do nothing if disabled
This commit is contained in:
@@ -47,7 +47,7 @@ public class JavaReformatOnTypingTest extends LightPlatformCodeInsightFixtureTes
|
||||
}
|
||||
|
||||
public void test_AddSpacesAroundAssignmentOperator() throws Exception {
|
||||
doTest("class T { int<caret> }", "=", "class T { int = <caret> }");
|
||||
doTest("class T { int<caret> }", "=", "class T { int =<caret> }");
|
||||
}
|
||||
|
||||
public void test_IgnoreSpacePressedAfterAssignmentOperator() {
|
||||
@@ -59,9 +59,7 @@ public class JavaReformatOnTypingTest extends LightPlatformCodeInsightFixtureTes
|
||||
}
|
||||
|
||||
public void test_DoNotInsertDoubleSpaceBeforeAssignment() {
|
||||
doTest("class T { int <caret> }",
|
||||
"=",
|
||||
"class T { int = <caret> }");
|
||||
doTest("class T { int <caret> }", "=", "class T { int =<caret> }");
|
||||
}
|
||||
|
||||
public void test_DoNotInsertDoubleSpaceAnywhere() {
|
||||
@@ -73,21 +71,21 @@ public class JavaReformatOnTypingTest extends LightPlatformCodeInsightFixtureTes
|
||||
}
|
||||
|
||||
public void test_DoNotInsertSpaceIfNotAssignment() {
|
||||
doTest("1 <caret>", "!=", "1 != <caret>");
|
||||
doTest("c <caret>", ">=", "c >= <caret>");
|
||||
doTest("c <caret>", "<=", "c <= <caret>");
|
||||
doTest("c <caret>", "+=", "c += <caret>");
|
||||
doTest("c <caret>", "-=", "c -= <caret>");
|
||||
doTest("c <caret>", "*=", "c *= <caret>");
|
||||
doTest("c <caret>", "/=", "c /= <caret>");
|
||||
doTest("c <caret>", "&=", "c &= <caret>");
|
||||
doTest("c <caret>", "%=", "c %= <caret>");
|
||||
doTest("c <caret>", "^=", "c ^= <caret>");
|
||||
doTest("c <caret>", "|=", "c |= <caret>");
|
||||
doTest("1 <caret>", "!=a", "1 != a<caret>");
|
||||
doTest("c <caret>", ">=a", "c >= a<caret>");
|
||||
doTest("c <caret>", "<=a", "c <= a<caret>");
|
||||
doTest("c <caret>", "+=a", "c += a<caret>");
|
||||
doTest("c <caret>", "-=a", "c -= a<caret>");
|
||||
doTest("c <caret>", "*=a", "c *= a<caret>");
|
||||
doTest("c <caret>", "/=a", "c /= a<caret>");
|
||||
doTest("c <caret>", "&=a", "c &= a<caret>");
|
||||
doTest("c <caret>", "%=a", "c %= a<caret>");
|
||||
doTest("c <caret>", "^=a", "c ^= a<caret>");
|
||||
doTest("c <caret>", "|=a", "c |= a<caret>");
|
||||
}
|
||||
|
||||
public void test_DistinguishAssignmentAndEquality() {
|
||||
doTest("class T { boolean b = 1 <caret> }", "==", "class T { boolean b = 1 == <caret> }");
|
||||
doTest("class T { boolean b = 1 <caret> }", "==", "class T { boolean b = 1 ==<caret> }");
|
||||
}
|
||||
|
||||
private void doTest(String before, String typing, String after) {
|
||||
|
||||
+25
-4
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user