diff --git a/platform/platform-impl/src/com/intellij/openapi/editor/ex/util/EditorUtil.java b/platform/platform-impl/src/com/intellij/openapi/editor/ex/util/EditorUtil.java index 514d63e580f3..f25430648a30 100644 --- a/platform/platform-impl/src/com/intellij/openapi/editor/ex/util/EditorUtil.java +++ b/platform/platform-impl/src/com/intellij/openapi/editor/ex/util/EditorUtil.java @@ -782,15 +782,11 @@ public final class EditorUtil { /** * Finds the start offset of visual line at which given offset is located, not taking soft wraps into account. - * - * @see #calcSurroundingRange(com.intellij.openapi.editor.Editor, com.intellij.openapi.editor.VisualPosition, com.intellij.openapi.editor.VisualPosition) */ public static int getNotFoldedLineStartOffset(@NotNull Editor editor, int offset) { while(true) { offset = getLineStartOffset(offset, editor.getDocument()); - // this assumes that there cannot be two adjacent collapsed fold regions - // (such case is not properly handled currently by FoldingModelImpl/FoldRegionsTree anyway) - FoldRegion foldRegion = editor.getFoldingModel().getCollapsedRegionAtOffset(offset); + FoldRegion foldRegion = editor.getFoldingModel().getCollapsedRegionAtOffset(offset - 1); if (foldRegion == null || foldRegion.getStartOffset() >= offset) { break; } @@ -801,14 +797,10 @@ public final class EditorUtil { /** * Finds the end offset of visual line at which given offset is located, not taking soft wraps into account. - * - * @see #calcSurroundingRange(com.intellij.openapi.editor.Editor, com.intellij.openapi.editor.VisualPosition, com.intellij.openapi.editor.VisualPosition) */ public static int getNotFoldedLineEndOffset(@NotNull Editor editor, int offset) { while(true) { offset = getLineEndOffset(offset, editor.getDocument()); - // this assumes that there cannot be two adjacent collapsed fold regions - // (such case is not properly handled currently by FoldingModelImpl/FoldRegionsTree anyway) FoldRegion foldRegion = editor.getFoldingModel().getCollapsedRegionAtOffset(offset); if (foldRegion == null || foldRegion.getEndOffset() <= offset) { break; diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/editor/ex/util/EditorUtilTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/editor/ex/util/EditorUtilTest.java new file mode 100644 index 000000000000..0b2392452f92 --- /dev/null +++ b/platform/platform-tests/testSrc/com/intellij/openapi/editor/ex/util/EditorUtilTest.java @@ -0,0 +1,39 @@ +/* + * Copyright 2000-2014 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.ex.util; + +import com.intellij.testFramework.EditorTestUtil; +import com.intellij.testFramework.LightPlatformCodeInsightTestCase; + +public class EditorUtilTest extends LightPlatformCodeInsightTestCase { + public void testGetNotFoldedLineStartEndOffsets() throws Exception { + configureFromFileText(getTestName(false) + ".txt", + "aaa\nbbb\nccc\nddd"); + EditorTestUtil.addFoldRegion(myEditor, 4, 8, "...", true); + + assertVisualLineRange(2, 0, 3); + assertVisualLineRange(4, 4, 11); + assertVisualLineRange(7, 4, 11); + assertVisualLineRange(8, 4, 11); + assertVisualLineRange(9, 4, 11); + assertVisualLineRange(13, 12, 15); + } + + private static void assertVisualLineRange(int offset, int lineStartOffset, int lineEndOffset) { + assertEquals(lineStartOffset, EditorUtil.getNotFoldedLineStartOffset(myEditor, offset)); + assertEquals(lineEndOffset, EditorUtil.getNotFoldedLineEndOffset(myEditor, offset)); + } +} \ No newline at end of file diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/editor/impl/softwrap/mapping/SoftWrapApplianceOnDocumentModificationTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/editor/impl/softwrap/mapping/SoftWrapApplianceOnDocumentModificationTest.java index d8d0f5dc6a48..101e30e0651a 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/editor/impl/softwrap/mapping/SoftWrapApplianceOnDocumentModificationTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/editor/impl/softwrap/mapping/SoftWrapApplianceOnDocumentModificationTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -1040,6 +1040,17 @@ public class SoftWrapApplianceOnDocumentModificationTest extends AbstractEditorT assertFalse(foldRegion.isExpanded()); assertEquals(foldStart, myEditor.getCaretModel().getOffset()); } + + public void testFoldRegionEndingAtLineStart() throws IOException { + init(100, "aaa\nbbb\nccc\nddd"); + addCollapsedFoldRegion(4, 8, "..."); + addCollapsedFoldRegion(13, 15, "..."); + + myEditor.getDocument().insertString(10, "C"); + + // verify that cached layout data is intact after document change and position recalculation is done correctly + assertEquals(new LogicalPosition(0, 0), myEditor.visualToLogicalPosition(new VisualPosition(0, 0))); + } private void init(final int visibleWidthInColumns, @NotNull String fileText) throws IOException { init(visibleWidthInColumns, 7, fileText); diff --git a/platform/testFramework/src/com/intellij/testFramework/EditorTestUtil.java b/platform/testFramework/src/com/intellij/testFramework/EditorTestUtil.java index bbd5734afd79..6c4b05933add 100644 --- a/platform/testFramework/src/com/intellij/testFramework/EditorTestUtil.java +++ b/platform/testFramework/src/com/intellij/testFramework/EditorTestUtil.java @@ -30,20 +30,20 @@ import com.intellij.openapi.editor.highlighter.HighlighterIterator; import com.intellij.openapi.editor.impl.DefaultEditorTextRepresentationHelper; import com.intellij.openapi.editor.impl.SoftWrapModelImpl; import com.intellij.openapi.editor.impl.softwrap.mapping.SoftWrapApplianceManager; +import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.registry.Registry; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.tree.IElementType; -import org.junit.Assert; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.junit.Assert; import java.awt.*; import java.util.ArrayList; import java.util.List; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; +import static org.junit.Assert.*; /** * @author Maxim.Mossienko @@ -391,6 +391,21 @@ public class EditorTestUtil { Registry.get("editor.allow.multiple.carets").setValue(false); } + public static FoldRegion addFoldRegion(@NotNull Editor editor, final int startOffset, final int endOffset, final String placeholder, final boolean collapse) { + final FoldingModel foldingModel = editor.getFoldingModel(); + final Ref ref = new Ref(); + foldingModel.runBatchFoldingOperation(new Runnable() { + @Override + public void run() { + FoldRegion region = foldingModel.addFoldRegion(startOffset, endOffset, placeholder); + assertNotNull(region); + region.setExpanded(!collapse); + ref.set(region); + } + }); + return ref.get(); + } + public static class CaretAndSelectionState { public final List carets = new ArrayList(); @Nullable