diff --git a/java/java-impl/src/com/intellij/codeInsight/folding/impl/CollapseBlockHandler.java b/java/java-impl/src/com/intellij/codeInsight/folding/impl/CollapseBlockHandler.java index 890268037df3..9b60f1e49d01 100644 --- a/java/java-impl/src/com/intellij/codeInsight/folding/impl/CollapseBlockHandler.java +++ b/java/java-impl/src/com/intellij/codeInsight/folding/impl/CollapseBlockHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2015 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. @@ -52,6 +52,11 @@ public class CollapseBlockHandler implements CodeInsightActionHandler { int end = block.getTextRange().getEndOffset(); FoldRegion existing = FoldingUtil.findFoldRegion(editor, start, end); if (existing != null) { + if (existing.isExpanded()) { + existing.setExpanded(false); + editor.getCaretModel().moveToOffset(existing.getEndOffset()); + return; + } previous = existing; if (info.getPsiElement(existing) == null) myPrevious = existing; block = PsiTreeUtil.getParentOfType(block, PsiCodeBlock.class); diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/folding/JavaFoldingTest.groovy b/java/java-tests/testSrc/com/intellij/codeInsight/folding/JavaFoldingTest.groovy index 7347fe3b190a..78c6299ffb1c 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/folding/JavaFoldingTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/folding/JavaFoldingTest.groovy @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2015 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. @@ -1116,6 +1116,31 @@ class Foo { \t} }"""); } + + public void testCollapseExistingButExpandedBlock() { + String text = '''class Foo { + void m { + if (true) { + System.out.println(); + } + } +} +''' + configure text + + myFixture.editor.caretModel.moveToOffset(text.indexOf("System")) + myFixture.performEditorAction("CollapseBlock") + + myFixture.performEditorAction("ExpandAllRegions") + + myFixture.editor.caretModel.moveToOffset(text.indexOf("System")) + myFixture.performEditorAction("CollapseBlock") + + def topLevelRegions = ((FoldingModelEx)myFixture.editor.foldingModel).fetchTopLevel() + assert topLevelRegions.length == 1 + assert topLevelRegions[0].startOffset == text.indexOf('{', text.indexOf("if")) + assert topLevelRegions[0].endOffset == text.indexOf('}', text.indexOf("if")) + 1 + } private int getFoldRegionsCount() { return myFixture.editor.foldingModel.allFoldRegions.length