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 class Test {\n" + + "}"; + init(text); + + CaretModel caretModel = myEditor.getCaretModel(); + int caretOffset = caretModel.getOffset(); + + addCollapsedFoldRegion(0, text.indexOf("public") - 1, "/***/"); + assertEquals(caretOffset, caretModel.getOffset()); + + type('a'); + + assertEquals(caretOffset + 1, caretModel.getOffset()); + assertEquals(1, myEditor.getFoldingModel().getAllFoldRegions().length); + FoldRegion foldRegion = getFoldRegion(0); + assertFalse(foldRegion.isExpanded()); + } +} diff --git a/platform/platform-impl/testSrc/com/intellij/openapi/editor/impl/softwrap/mapping/SoftWrapApplianceOnDocumentModificationTest.java b/platform/platform-impl/testSrc/com/intellij/openapi/editor/impl/softwrap/mapping/SoftWrapApplianceOnDocumentModificationTest.java index 04ae39db4c6d..0d0e24a5e8d4 100644 --- a/platform/platform-impl/testSrc/com/intellij/openapi/editor/impl/softwrap/mapping/SoftWrapApplianceOnDocumentModificationTest.java +++ b/platform/platform-impl/testSrc/com/intellij/openapi/editor/impl/softwrap/mapping/SoftWrapApplianceOnDocumentModificationTest.java @@ -17,22 +17,19 @@ package com.intellij.openapi.editor.impl.softwrap.mapping; import com.intellij.openapi.editor.*; import com.intellij.openapi.editor.ex.SoftWrapModelEx; +import com.intellij.openapi.editor.impl.AbstractEditorProcessingOnDocumentModificationTest; import com.intellij.openapi.editor.impl.SoftWrapModelImpl; -import com.intellij.testFramework.LightPlatformCodeInsightTestCase; import gnu.trove.TIntHashSet; import gnu.trove.TIntProcedure; import java.awt.*; import java.io.IOException; -import java.util.Arrays; /** * @author Denis Zhdanov * @since 09/16/2010 */ -public class SoftWrapApplianceOnDocumentModificationTest extends LightPlatformCodeInsightTestCase { - - //private static final String PATH = "/codeInsight/softwrap/"; +public class SoftWrapApplianceOnDocumentModificationTest extends AbstractEditorProcessingOnDocumentModificationTest { @Override protected void tearDown() throws Exception { @@ -243,11 +240,6 @@ public class SoftWrapApplianceOnDocumentModificationTest extends LightPlatformCo assertEquals(offset, caretModel.getOffset()); } - //private void init(final int visibleWidth) throws Exception { - // configureByFile(PATH + getFileName()); - // initCommon(visibleWidth); - //} - private static TIntHashSet collectSoftWrapStartOffsets(int documentLine) { TIntHashSet result = new TIntHashSet(); for (SoftWrap softWrap : myEditor.getSoftWrapModel().getSoftWrapsForLine(documentLine)) { @@ -256,52 +248,8 @@ public class SoftWrapApplianceOnDocumentModificationTest extends LightPlatformCo return result; } - private void init(int visibleWidth, String fileText) throws IOException { - configureFromFileText(getFileName(), fileText); - initCommon(visibleWidth); - } - - private String getFileName() { - return getTestName(false) + ".txt"; - } - - private 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); - } - }); - } - - private static void addCollapsedFoldRegion(final int startOffset, final int endOffset, final String placeholder) { - addFoldRegion(startOffset, endOffset, placeholder); - toggleFoldRegionState(getFoldRegion(startOffset), false); - } - - private static void toggleFoldRegionState(final FoldRegion foldRegion, final boolean expanded) { - myEditor.getFoldingModel().runBatchFoldingOperation(new Runnable() { - @Override - public void run() { - foldRegion.setExpanded(expanded); - } - }); - } - - private 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() - )); - } - - private static void initCommon(final int visibleWidth) { + private void init(final int visibleWidth, String fileText) throws IOException { + init(fileText); myEditor.getSettings().setUseSoftWraps(true); SoftWrapModelImpl model = (SoftWrapModelImpl)myEditor.getSoftWrapModel(); model.refreshSettings();