diff --git a/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/LeaveCodeBlockEnterProcessor.java b/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/LeaveCodeBlockEnterProcessor.java index 44f6f7a90991..7284dd0aa2a2 100644 --- a/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/LeaveCodeBlockEnterProcessor.java +++ b/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/LeaveCodeBlockEnterProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2011 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -56,8 +56,8 @@ public class LeaveCodeBlockEnterProcessor implements EnterProcessor { if (node != null && CONTROL_FLOW_ELEMENT_TYPES.contains(node.getElementType())) { return false; } - - boolean leaveCodeBlock = isControlFlowBreak(psiElement) || isValidStatementInsideControlFlowOperator(psiElement, isModified); + + boolean leaveCodeBlock = isControlFlowBreak(psiElement); if (!leaveCodeBlock) { return false; } @@ -110,26 +110,4 @@ public class LeaveCodeBlockEnterProcessor implements EnterProcessor { private static boolean isControlFlowBreak(@Nullable PsiElement element) { return element instanceof PsiReturnStatement || element instanceof PsiThrowStatement; } - - private static boolean isValidStatementInsideControlFlowOperator(final @Nullable PsiElement element, boolean modified) { - if (modified || element == null || PsiTreeUtil.hasErrorElements(element)) { - return false; - } - - for (PsiElement e = element; e != null; e = e.getParent()) { - final ASTNode node = e.getNode(); - if (node == null) { - return false; - } - - if (node.getElementType() == JavaElementType.METHOD) { - return false; - } - - if (CONTROL_FLOW_ELEMENT_TYPES.contains(node.getElementType())) { - return true; - } - } - return false; - } } diff --git a/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/PlainEnterProcessor.java b/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/PlainEnterProcessor.java index 8701b9872595..74eee25b5f02 100644 --- a/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/PlainEnterProcessor.java +++ b/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/PlainEnterProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2011 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,7 +42,7 @@ import org.jetbrains.annotations.Nullable; public class PlainEnterProcessor implements EnterProcessor { public boolean doEnter(Editor editor, PsiElement psiElement, boolean isModified) { PsiCodeBlock block = getControlStatementBlock(editor.getCaretModel().getOffset(), psiElement); - if (processExistingBlankLine(editor, block)) { + if (processExistingBlankLine(editor, block, psiElement)) { return true; } EditorActionHandler enterHandler = getEnterHandler(IdeActions.ACTION_EDITOR_START_NEW_LINE); @@ -114,30 +114,42 @@ public class PlainEnterProcessor implements EnterProcessor { * * @param editor target editor * @param codeBlock target code block to which new empty line is going to be inserted + * @param element target element under caret * @return true if it was found out that the given code block starts with the empty line and caret * is pointed to correct position there, i.e. no additional processing is required; * false otherwise */ - private static boolean processExistingBlankLine(@NotNull Editor editor, @Nullable PsiCodeBlock codeBlock) { + private static boolean processExistingBlankLine(@NotNull Editor editor, @Nullable PsiCodeBlock codeBlock, @Nullable PsiElement element) { + PsiWhiteSpace whiteSpace = null; if (codeBlock == null) { - return false; + if (element != null) { + final PsiElement next = PsiTreeUtil.nextLeaf(element); + if (next instanceof PsiWhiteSpace) { + whiteSpace = (PsiWhiteSpace)next; + } + } + } + else { + whiteSpace = PsiTreeUtil.findChildOfType(codeBlock, PsiWhiteSpace.class); + if (whiteSpace == null) { + return false; + } + + PsiElement lbraceCandidate = whiteSpace.getPrevSibling(); + if (lbraceCandidate == null) { + return false; + } + + ASTNode node = lbraceCandidate.getNode(); + if (node == null || node.getElementType() != JavaTokenType.LBRACE) { + return false; + } } - PsiWhiteSpace whiteSpace = PsiTreeUtil.findChildOfType(codeBlock, PsiWhiteSpace.class); if (whiteSpace == null) { return false; } - PsiElement lbraceCandidate = whiteSpace.getPrevSibling(); - if (lbraceCandidate == null) { - return false; - } - - ASTNode node = lbraceCandidate.getNode(); - if (node == null || node.getElementType() != JavaTokenType.LBRACE) { - return false; - } - final TextRange textRange = whiteSpace.getTextRange(); final Document document = editor.getDocument(); final CharSequence whiteSpaceText = document.getCharsSequence().subSequence(textRange.getStartOffset(), textRange.getEndOffset()); @@ -147,7 +159,9 @@ public class PlainEnterProcessor implements EnterProcessor { int i = CharArrayUtil.shiftForward(whiteSpaceText, 0, " \t"); if (i >= whiteSpaceText.length() - 1) { - assert false : String.format("code block: %s, white space: %s", codeBlock.getTextRange(), whiteSpace.getTextRange()); + assert false : String.format("code block: %s, white space: %s", + codeBlock == null ? "undefined" : codeBlock.getTextRange(), + whiteSpace.getTextRange()); return false; } diff --git a/java/java-tests/testData/codeInsight/completeStatement/LeaveValidCodeBlock.java b/java/java-tests/testData/codeInsight/completeStatement/LeaveValidCodeBlock.java deleted file mode 100644 index daecd42542be..000000000000 --- a/java/java-tests/testData/codeInsight/completeStatement/LeaveValidCodeBlock.java +++ /dev/null @@ -1,7 +0,0 @@ -public class Foo { - void test(int i) { - if (i > 1) { - i = 1; - } - } -} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completeStatement/LeaveValidCodeBlockWithEmptyLineAfterIt.java b/java/java-tests/testData/codeInsight/completeStatement/LeaveValidCodeBlockWithEmptyLineAfterIt.java deleted file mode 100644 index 5a15ff8ea567..000000000000 --- a/java/java-tests/testData/codeInsight/completeStatement/LeaveValidCodeBlockWithEmptyLineAfterIt.java +++ /dev/null @@ -1,8 +0,0 @@ -public class Foo { - void test(int i) { - while (i-- > 1) { - i = 1; - } - - } -} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completeStatement/LeaveValidCodeBlockWithEmptyLineAfterIt_after.java b/java/java-tests/testData/codeInsight/completeStatement/LeaveValidCodeBlockWithEmptyLineAfterIt_after.java deleted file mode 100644 index a17b86eea7c6..000000000000 --- a/java/java-tests/testData/codeInsight/completeStatement/LeaveValidCodeBlockWithEmptyLineAfterIt_after.java +++ /dev/null @@ -1,8 +0,0 @@ -public class Foo { - void test(int i) { - while (i-- > 1) { - i = 1; - } - - } -} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completeStatement/LeaveValidCodeBlock_after.java b/java/java-tests/testData/codeInsight/completeStatement/LeaveValidCodeBlock_after.java deleted file mode 100644 index 4a9a65e26169..000000000000 --- a/java/java-tests/testData/codeInsight/completeStatement/LeaveValidCodeBlock_after.java +++ /dev/null @@ -1,8 +0,0 @@ -public class Foo { - void test(int i) { - if (i > 1) { - i = 1; - } - - } -} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completeStatement/ValidCodeBlock.java b/java/java-tests/testData/codeInsight/completeStatement/ValidCodeBlock.java new file mode 100644 index 000000000000..7d147ef8080f --- /dev/null +++ b/java/java-tests/testData/codeInsight/completeStatement/ValidCodeBlock.java @@ -0,0 +1,22 @@ +/* + * Copyright 2000-2011 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +public class Foo { + void test(int i) { + if (i > 1) { + i = 1; + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completeStatement/ValidCodeBlockWithEmptyLineAfterIt.java b/java/java-tests/testData/codeInsight/completeStatement/ValidCodeBlockWithEmptyLineAfterIt.java new file mode 100644 index 000000000000..a697680a64d5 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completeStatement/ValidCodeBlockWithEmptyLineAfterIt.java @@ -0,0 +1,23 @@ +/* + * Copyright 2000-2011 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +public class Foo { + void test(int i) { + while (i-- > 1) { + i = 1; + + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completeStatement/ValidCodeBlockWithEmptyLineAfterIt_after.java b/java/java-tests/testData/codeInsight/completeStatement/ValidCodeBlockWithEmptyLineAfterIt_after.java new file mode 100644 index 000000000000..2dd0b98b8e9b --- /dev/null +++ b/java/java-tests/testData/codeInsight/completeStatement/ValidCodeBlockWithEmptyLineAfterIt_after.java @@ -0,0 +1,23 @@ +/* + * Copyright 2000-2011 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +public class Foo { + void test(int i) { + while (i-- > 1) { + i = 1; + + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completeStatement/ValidCodeBlock_after.java b/java/java-tests/testData/codeInsight/completeStatement/ValidCodeBlock_after.java new file mode 100644 index 000000000000..91b35b59280b --- /dev/null +++ b/java/java-tests/testData/codeInsight/completeStatement/ValidCodeBlock_after.java @@ -0,0 +1,23 @@ +/* + * Copyright 2000-2011 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +public class Foo { + void test(int i) { + if (i > 1) { + i = 1; + + } + } +} \ 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 8d9fac724365..72fc0d7dabce 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/CompleteStatementTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/CompleteStatementTest.java @@ -1,3 +1,18 @@ +/* + * Copyright 2000-2011 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.intellij.codeInsight; import com.intellij.JavaTestUtil; @@ -216,9 +231,9 @@ public class CompleteStatementTest extends EditorActionTestCase { public void testForUpdateGeneration() throws Exception { doTest(); } - public void testLeaveValidCodeBlock() throws Exception { doTest(); } + public void testValidCodeBlock() throws Exception { doTest(); } - public void testLeaveValidCodeBlockWithEmptyLineAfterIt() throws Exception { doTest(); } + public void testValidCodeBlockWithEmptyLineAfterIt() throws Exception { doTest(); } public void testFromJavadocParameterDescriptionEndToNextParameter() throws Exception { doTest(); }