Search in sources :

Example 41 with FoldRegion

use of com.intellij.openapi.editor.FoldRegion in project intellij-community by JetBrains.

the class FoldingUtilTest method testFoldTreeIterator.

public void testFoldTreeIterator() {
    configureFromFileText(getTestName(false) + ".txt", "abcdefghijklmnopqrstuvwxyz");
    EditorTestUtil.addFoldRegion(myEditor, 0, 10, ".", true);
    EditorTestUtil.addFoldRegion(myEditor, 0, 5, ".", false);
    EditorTestUtil.addFoldRegion(myEditor, 1, 2, ".", true);
    EditorTestUtil.addFoldRegion(myEditor, 7, 10, ".", false);
    EditorTestUtil.addFoldRegion(myEditor, 10, 11, ".", true);
    StringBuilder b = new StringBuilder();
    Iterator<FoldRegion> it = FoldingUtil.createFoldTreeIterator(myEditor);
    while (it.hasNext()) {
        FoldRegion region = it.next();
        if (b.length() > 0) {
            b.append('|');
        }
        b.append(region.getStartOffset()).append(',').append(region.getEndOffset());
    }
    assertEquals("0,10|0,5|1,2|7,10|10,11", b.toString());
}
Also used : FoldRegion(com.intellij.openapi.editor.FoldRegion)

Example 42 with FoldRegion

use of com.intellij.openapi.editor.FoldRegion in project intellij-community by JetBrains.

the class BlockSelectionEditingTest method testBlockRemovalAndCollapsedFoldRegionsBefore.

public void testBlockRemovalAndCollapsedFoldRegionsBefore() throws IOException {
    // Inspired by IDEA-69371
    String initialText = "fold line #1\n" + "fold line #2\n" + "initialText    line 1\n" + "initialText    line 2\n" + "initialText    line 3";
    init(initialText, TestFileType.TEXT);
    final int foldEndOffset = initialText.indexOf("initialText");
    addCollapsedFoldRegion(0, foldEndOffset, "...");
    int column = "initialText".length();
    final LogicalPosition blockStart = new LogicalPosition(3, column);
    final LogicalPosition blockEnd = new LogicalPosition(4, column);
    final SelectionModel selectionModel = myEditor.getSelectionModel();
    selectionModel.setBlockSelection(blockStart, blockEnd);
    delete();
    delete();
    String expectedText = "fold line #1\n" + "fold line #2\n" + "initialText    line 1\n" + "initialText  line 2\n" + "initialText  line 3";
    assertEquals(expectedText, myEditor.getDocument().getText());
    assertSelectionRanges(new int[][] { { 59, 59 }, { 79, 79 } });
    final FoldRegion foldRegion = getFoldRegion(0);
    assertNotNull(foldRegion);
    assertEquals(0, foldRegion.getStartOffset());
    assertEquals(foldEndOffset, foldRegion.getEndOffset());
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) SelectionModel(com.intellij.openapi.editor.SelectionModel) FoldRegion(com.intellij.openapi.editor.FoldRegion)

Example 43 with FoldRegion

use of com.intellij.openapi.editor.FoldRegion in project intellij-community by JetBrains.

the class FoldingTransformation method findFoldRegion.

private FoldRegion findFoldRegion(int line) {
    int index = Arrays.binarySearch(myFoldBeginings, line);
    FoldRegion region;
    if (index >= 0)
        region = myCollapsed.get(index);
    else {
        index = -index - 1;
        if (index == 0)
            return null;
        region = myCollapsed.get(index - 1);
    }
    if (getEndLine(region) < line)
        return null;
    return region;
}
Also used : FoldRegion(com.intellij.openapi.editor.FoldRegion)

Example 44 with FoldRegion

use of com.intellij.openapi.editor.FoldRegion in project intellij-community by JetBrains.

the class FoldingTransformation method transform.

public int transform(int line) {
    FoldRegion foldRegion = findFoldRegion(line);
    int yOffset = 0;
    if (foldRegion != null) {
        int startLine = getStartLine(foldRegion);
        yOffset = (int) ((double) (line - startLine) / getLineLength(foldRegion) * myEditor.getLineHeight());
        line = startLine;
    }
    yOffset += myEditor.logicalPositionToXY(new LogicalPosition(line, 0)).y;
    final JComponent header = myEditor.getHeaderComponent();
    int headerOffset = header == null ? 0 : header.getHeight();
    return yOffset - myEditor.getScrollingModel().getVerticalScrollOffset() + headerOffset;
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) FoldRegion(com.intellij.openapi.editor.FoldRegion)

Example 45 with FoldRegion

use of com.intellij.openapi.editor.FoldRegion in project intellij-community by JetBrains.

the class FoldingAnchorsOverlayStrategy method getAnchorsToDisplay.

@NotNull
Collection<DisplayedFoldingAnchor> getAnchorsToDisplay(int firstVisibleOffset, int lastVisibleOffset, FoldRegion activeFoldRegion) {
    Map<Integer, DisplayedFoldingAnchor> result = new HashMap<>();
    FoldRegion[] visibleFoldRegions = myEditor.getFoldingModel().fetchVisible();
    for (FoldRegion region : visibleFoldRegions) {
        if (!region.isValid())
            continue;
        final int startOffset = region.getStartOffset();
        if (startOffset > lastVisibleOffset)
            continue;
        final int endOffset = getEndOffset(region);
        if (endOffset < firstVisibleOffset)
            continue;
        if (!isFoldingPossible(startOffset, endOffset))
            continue;
        final FoldingGroup group = region.getGroup();
        if (group != null && myEditor.getFoldingModel().getFirstRegion(group, region) != region)
            continue;
        //offset = Math.min(myEditor.getDocument().getTextLength() - 1, offset);
        int foldStart = myEditor.offsetToVisualLine(startOffset);
        if (!region.isExpanded()) {
            tryAdding(result, region, foldStart, 0, DisplayedFoldingAnchor.Type.COLLAPSED, activeFoldRegion);
        } else {
            //offset = Math.min(myEditor.getDocument().getTextLength() - 1, offset);
            int foldEnd = myEditor.offsetToVisualLine(endOffset);
            tryAdding(result, region, foldStart, foldEnd - foldStart, DisplayedFoldingAnchor.Type.EXPANDED_TOP, activeFoldRegion);
            tryAdding(result, region, foldEnd, foldEnd - foldStart, DisplayedFoldingAnchor.Type.EXPANDED_BOTTOM, activeFoldRegion);
        }
    }
    return result.values();
}
Also used : FoldingGroup(com.intellij.openapi.editor.FoldingGroup) HashMap(com.intellij.util.containers.hash.HashMap) FoldRegion(com.intellij.openapi.editor.FoldRegion) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

FoldRegion (com.intellij.openapi.editor.FoldRegion)47 TextRange (com.intellij.openapi.util.TextRange)7 ArrayList (java.util.ArrayList)7 FoldingModel (com.intellij.openapi.editor.FoldingModel)6 NotNull (org.jetbrains.annotations.NotNull)6 FoldingGroup (com.intellij.openapi.editor.FoldingGroup)5 FoldingModelEx (com.intellij.openapi.editor.ex.FoldingModelEx)5 PsiElement (com.intellij.psi.PsiElement)5 CodeFoldingManager (com.intellij.codeInsight.folding.CodeFoldingManager)3 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)3 Document (com.intellij.openapi.editor.Document)3 Editor (com.intellij.openapi.editor.Editor)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 Nullable (org.jetbrains.annotations.Nullable)3 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)2 RangeMarker (com.intellij.openapi.editor.RangeMarker)2 SoftWrap (com.intellij.openapi.editor.SoftWrap)2 DocumentEx (com.intellij.openapi.editor.ex.DocumentEx)2 Project (com.intellij.openapi.project.Project)2 PsiFile (com.intellij.psi.PsiFile)2