From 75f04d0383e9a5ef56f17e42358aaed7328e43b9 Mon Sep 17 00:00:00 2001 From: peter Date: Thu, 9 Jul 2015 18:06:37 +0200 Subject: [PATCH] IDEA-41929 Ctrl + Shift + Enter doesn't work for "for (; ;)" --- .../smartEnter/JavaSmartEnterProcessor.java | 3 ++- .../editorActions/smartEnter/MissingForBodyFixer.java | 9 ++++++--- .../testData/codeInsight/completeStatement/EmptyFor.java | 5 +++++ .../codeInsight/completeStatement/EmptyFor_after.java | 7 +++++++ .../com/intellij/codeInsight/CompleteStatementTest.java | 2 ++ 5 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/completeStatement/EmptyFor.java create mode 100644 java/java-tests/testData/codeInsight/completeStatement/EmptyFor_after.java diff --git a/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/JavaSmartEnterProcessor.java b/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/JavaSmartEnterProcessor.java index f416c4769f73..c760212a843b 100644 --- a/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/JavaSmartEnterProcessor.java +++ b/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/JavaSmartEnterProcessor.java @@ -248,7 +248,8 @@ public class JavaSmartEnterProcessor extends SmartEnterProcessor { final PsiElement[] children = atCaret.getChildren(); for (PsiElement child : children) { - if (atCaret instanceof PsiStatement && child instanceof PsiStatement) continue; + if (atCaret instanceof PsiStatement && child instanceof PsiStatement && + !(atCaret instanceof PsiForStatement && child == ((PsiForStatement)atCaret).getInitialization())) continue; collectAllElements(child, res, recurse); } } diff --git a/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/MissingForBodyFixer.java b/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/MissingForBodyFixer.java index a742a5e5d1b0..e11fa67e9e3a 100644 --- a/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/MissingForBodyFixer.java +++ b/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/MissingForBodyFixer.java @@ -42,12 +42,15 @@ public class MissingForBodyFixer implements Fixer { if (body != null && startLine(doc, body) == startLine(doc, forStatement)) return; PsiElement eltToInsertAfter = forStatement.getRParenth(); - String text = "{}"; + String braces = "{\n}"; + String text = braces; if (eltToInsertAfter == null) { eltToInsertAfter = forStatement; - text = "){}"; + text = ")" + text; } - doc.insertString(eltToInsertAfter.getTextRange().getEndOffset(), text); + int offset = eltToInsertAfter.getTextRange().getEndOffset(); + doc.insertString(offset, text); + editor.getCaretModel().moveToOffset(offset + text.length() - braces.length()); } @Nullable diff --git a/java/java-tests/testData/codeInsight/completeStatement/EmptyFor.java b/java/java-tests/testData/codeInsight/completeStatement/EmptyFor.java new file mode 100644 index 000000000000..570d9397f9bf --- /dev/null +++ b/java/java-tests/testData/codeInsight/completeStatement/EmptyFor.java @@ -0,0 +1,5 @@ +class Foo { + { + for (;;) + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completeStatement/EmptyFor_after.java b/java/java-tests/testData/codeInsight/completeStatement/EmptyFor_after.java new file mode 100644 index 000000000000..a03cfa6a5fa7 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completeStatement/EmptyFor_after.java @@ -0,0 +1,7 @@ +class Foo { + { + for (;;) { + + } + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/CompleteStatementTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/CompleteStatementTest.java index ef21fe30c104..8b1485bd4a7f 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/CompleteStatementTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/CompleteStatementTest.java @@ -85,6 +85,8 @@ public class CompleteStatementTest extends EditorActionTestCase { public void testFor() throws Exception { doTest(); } + public void testEmptyFor() { doTest(); } + public void testForEach() throws Exception { doTest(); } public void testForBlock() throws Exception { doTest(); }