diff --git a/java/java-impl/src/com/intellij/codeInsight/folding/impl/JavaFoldingBuilder.java b/java/java-impl/src/com/intellij/codeInsight/folding/impl/JavaFoldingBuilder.java index 54f184930b18..85467a36efe5 100644 --- a/java/java-impl/src/com/intellij/codeInsight/folding/impl/JavaFoldingBuilder.java +++ b/java/java-impl/src/com/intellij/codeInsight/folding/impl/JavaFoldingBuilder.java @@ -77,7 +77,12 @@ public class JavaFoldingBuilder extends FoldingBuilderEx implements DumbAware { TextRange range = getFileHeader(file); if (range != null && range.getLength() > 1 && document.getLineNumber(range.getEndOffset()) > document.getLineNumber(range.getStartOffset())) { - result.add(new FoldingDescriptor(file, range)); + PsiElement anchorElementToUse = file; + PsiElement candidate = file.getFirstChild(); + if (candidate != null && candidate.getTextRange().equals(range)) { + anchorElementToUse = candidate; + } + result.add(new FoldingDescriptor(anchorElementToUse, range)); } return result.toArray(new FoldingDescriptor[result.size()]); diff --git a/platform/platform-impl/testSrc/com/intellij/openapi/editor/impl/AbstractEditorProcessingOnDocumentModificationTest.java b/platform/platform-impl/testSrc/com/intellij/openapi/editor/impl/AbstractEditorProcessingOnDocumentModificationTest.java new file mode 100644 index 000000000000..63294058b27c --- /dev/null +++ b/platform/platform-impl/testSrc/com/intellij/openapi/editor/impl/AbstractEditorProcessingOnDocumentModificationTest.java @@ -0,0 +1,77 @@ +/* + * Copyright 2000-2010 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.openapi.editor.impl; + +import com.intellij.openapi.editor.FoldRegion; +import com.intellij.testFramework.LightPlatformCodeInsightTestCase; + +import java.io.IOException; +import java.util.Arrays; + +/** + * Base super class for tests that check various IJ editor functionality on managed document modification. + *
+ * It's main purpose is to provide utility methods like fold regions addition and setup; typing etc. + * + * @author Denis Zhdanov + * @since 11/18/10 7:43 PM + */ +public abstract class AbstractEditorProcessingOnDocumentModificationTest extends LightPlatformCodeInsightTestCase { + + protected void init(String fileText) throws IOException { + configureFromFileText(getFileName(), fileText); + } + + private String getFileName() { + return getTestName(false) + ".txt"; + } + + protected static void addFoldRegion(final int startOffset, final int endOffset, final String placeholder) { + myEditor.getFoldingModel().runBatchFoldingOperation(new Runnable() { + @Override + public void run() { + myEditor.getFoldingModel().addFoldRegion(startOffset, endOffset, placeholder); + } + }); + } + + protected static void addCollapsedFoldRegion(final int startOffset, final int endOffset, final String placeholder) { + addFoldRegion(startOffset, endOffset, placeholder); + toggleFoldRegionState(getFoldRegion(startOffset), false); + } + + protected static void toggleFoldRegionState(final FoldRegion foldRegion, final boolean expanded) { + myEditor.getFoldingModel().runBatchFoldingOperation(new Runnable() { + @Override + public void run() { + foldRegion.setExpanded(expanded); + } + }); + } + + protected static FoldRegion getFoldRegion(int startOffset) { + FoldRegion[] foldRegions = myEditor.getFoldingModel().getAllFoldRegions(); + for (FoldRegion foldRegion : foldRegions) { + if (foldRegion.getStartOffset() == startOffset) { + return foldRegion; + } + } + throw new IllegalArgumentException(String.format( + "Can't find fold region with start offset %d. Registered fold regions: %s. Document text: '%s'", + startOffset, Arrays.toString(foldRegions), myEditor.getDocument().getCharsSequence() + )); + } +} diff --git a/platform/platform-impl/testSrc/com/intellij/openapi/editor/impl/FoldingProcessingOnDocumentModificationTest.java b/platform/platform-impl/testSrc/com/intellij/openapi/editor/impl/FoldingProcessingOnDocumentModificationTest.java new file mode 100644 index 000000000000..f248b22eaf92 --- /dev/null +++ b/platform/platform-impl/testSrc/com/intellij/openapi/editor/impl/FoldingProcessingOnDocumentModificationTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2000-2010 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.openapi.editor.impl; + +import com.intellij.openapi.editor.CaretModel; +import com.intellij.openapi.editor.FoldRegion; + +import java.io.IOException; + +/** + * @author Denis Zhdanov + * @since 11/18/10 7:42 PM + */ +public class FoldingProcessingOnDocumentModificationTest extends AbstractEditorProcessingOnDocumentModificationTest { + + public void testUnexpectedClassLevelJavadocExpandingOnClassSignatureChange() throws IOException { + // Inspired by IDEA-61275 + + String text = + "/**\n" + + " * This is a test comment\n" + + " */\n" + + "public