Search in sources :

Example 6 with FoldRegion

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

the class UpdateFoldRegionsOperation method applyExpandStatus.

private static void applyExpandStatus(@NotNull List<FoldRegion> newRegions, @NotNull Map<FoldRegion, Boolean> shouldExpand, @NotNull Map<FoldingGroup, Boolean> groupExpand) {
    for (final FoldRegion region : newRegions) {
        final FoldingGroup group = region.getGroup();
        final Boolean expanded = group == null ? shouldExpand.get(region) : groupExpand.get(group);
        if (expanded != null) {
            region.setExpanded(expanded.booleanValue());
        }
    }
}
Also used : FoldingGroup(com.intellij.openapi.editor.FoldingGroup) FoldRegion(com.intellij.openapi.editor.FoldRegion)

Example 7 with FoldRegion

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

the class UpdateFoldRegionsOperation method removeInvalidRegions.

private void removeInvalidRegions(@NotNull EditorFoldingInfo info, @NotNull FoldingModelEx foldingModel, FoldingUpdate.FoldingMap elementsToFold, @NotNull Map<TextRange, Boolean> rangeToExpandStatusMap) {
    List<FoldRegion> toRemove = new ArrayList<>();
    InjectedLanguageManager injectedManager = InjectedLanguageManager.getInstance(myProject);
    for (FoldRegion region : foldingModel.getAllFoldRegions()) {
        if (myKeepCollapsedRegions && !region.isExpanded() && !regionOrGroupCanBeRemovedWhenCollapsed(region))
            continue;
        PsiElement element = info.getPsiElement(region);
        if (element != null) {
            PsiFile containingFile = element.getContainingFile();
            boolean isInjected = injectedManager.isInjectedFragment(containingFile);
            if (isInjected != myForInjected)
                continue;
        }
        final Collection<FoldingDescriptor> descriptors;
        if (element != null && !(descriptors = elementsToFold.get(element)).isEmpty()) {
            boolean matchingDescriptorFound = false;
            FoldingDescriptor[] array = descriptors.toArray(new FoldingDescriptor[descriptors.size()]);
            for (FoldingDescriptor descriptor : array) {
                TextRange range = descriptor.getRange();
                if (TextRange.areSegmentsEqual(region, range)) {
                    matchingDescriptorFound = true;
                    if (!region.isValid() || region.getGroup() != null || descriptor.getGroup() != null || !region.getPlaceholderText().equals(descriptor.getPlaceholderText()) || range.getLength() < 2) {
                        rangeToExpandStatusMap.put(range, region.isExpanded());
                        toRemove.add(region);
                        break;
                    } else {
                        elementsToFold.remove(element, descriptor);
                    }
                }
            }
            if (!matchingDescriptorFound) {
                if (Registry.is("editor.durable.folding.state")) {
                    for (FoldingDescriptor descriptor : descriptors) {
                        rangeToExpandStatusMap.put(descriptor.getRange(), region.isExpanded());
                    }
                }
                toRemove.add(region);
            }
        } else if (region.isValid() && info.isLightRegion(region)) {
            boolean isExpanded = region.isExpanded();
            rangeToExpandStatusMap.put(TextRange.create(region), isExpanded);
        } else {
            toRemove.add(region);
        }
    }
    for (final FoldRegion region : toRemove) {
        foldingModel.removeFoldRegion(region);
        info.removeRegion(region);
    }
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) FoldRegion(com.intellij.openapi.editor.FoldRegion) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement)

Example 8 with FoldRegion

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

the class BaseFoldingHandler method getFoldRegionsForCaret.

/**
   * Returns a region corresponding to current caret position, and all regions contained in it.
   */
protected List<FoldRegion> getFoldRegionsForCaret(@NotNull Editor editor, @Nullable Caret caret, boolean toCollapse) {
    if (caret == null) {
        caret = editor.getCaretModel().getPrimaryCaret();
    }
    int offset = caret.getOffset();
    FoldRegion rootRegion = FoldingUtil.findFoldRegionStartingAtLine(editor, editor.getDocument().getLineNumber(offset));
    if (rootRegion == null || toCollapse && !rootRegion.isExpanded()) {
        rootRegion = null;
        FoldRegion[] regions = FoldingUtil.getFoldRegionsAtOffset(editor, offset);
        for (FoldRegion region : regions) {
            if (region.isExpanded() == toCollapse) {
                rootRegion = region;
                break;
            }
        }
    }
    List<FoldRegion> result = new ArrayList<>();
    if (rootRegion != null) {
        FoldRegion[] allRegions = editor.getFoldingModel().getAllFoldRegions();
        for (FoldRegion region : allRegions) {
            if (region.getStartOffset() >= rootRegion.getStartOffset() && region.getEndOffset() <= rootRegion.getEndOffset()) {
                result.add(region);
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) FoldRegion(com.intellij.openapi.editor.FoldRegion)

Example 9 with FoldRegion

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

the class CollapseSelectionHandler method isEnabled.

public static boolean isEnabled(@NotNull Editor editor) {
    if (editor.getSelectionModel().hasSelection()) {
        int start = editor.getSelectionModel().getSelectionStart();
        int end = editor.getSelectionModel().getSelectionEnd();
        if (start + 1 >= end) {
            return false;
        }
        if (start < end && editor.getDocument().getCharsSequence().charAt(end - 1) == '\n')
            end--;
        FoldRegion region = FoldingUtil.findFoldRegion(editor, start, end);
        if (region == null) {
            return !((FoldingModelEx) editor.getFoldingModel()).intersectsRegion(start, end);
        } else {
            return EditorFoldingInfo.get(editor).getPsiElement(region) == null;
        }
    } else {
        return FoldingUtil.getFoldRegionsAtOffset(editor, editor.getCaretModel().getOffset()).length > 0;
    }
}
Also used : FoldRegion(com.intellij.openapi.editor.FoldRegion)

Example 10 with FoldRegion

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

the class DocumentFoldingInfo method setToEditor.

@Override
public void setToEditor(@NotNull final Editor editor) {
    assertDispatchThread();
    final PsiManager psiManager = PsiManager.getInstance(myProject);
    if (psiManager.isDisposed())
        return;
    if (!myFile.isValid())
        return;
    final PsiFile psiFile = psiManager.findFile(myFile);
    if (psiFile == null)
        return;
    if (!mySerializedElements.isEmpty()) {
        // Restore postponed state
        assert myPsiElements.isEmpty() : "Sequential deserialization";
        for (SerializedPsiElement entry : mySerializedElements) {
            PsiElement restoredElement = FoldingPolicy.restoreBySignature(psiFile, entry.mySerializedElement);
            if (restoredElement != null && restoredElement.isValid()) {
                myPsiElements.add(SmartPointerManager.getInstance(myProject).createSmartPsiElementPointer(restoredElement));
                restoredElement.putUserData(FOLDING_INFO_KEY, entry.myFoldingInfo);
            }
        }
        mySerializedElements.clear();
    }
    Map<PsiElement, FoldingDescriptor> ranges = null;
    for (SmartPsiElementPointer<PsiElement> ptr : myPsiElements) {
        PsiElement element = ptr.getElement();
        if (element == null || !element.isValid()) {
            continue;
        }
        if (ranges == null) {
            ranges = buildRanges(editor, psiFile);
        }
        FoldingDescriptor descriptor = ranges.get(element);
        if (descriptor == null) {
            continue;
        }
        TextRange range = descriptor.getRange();
        FoldRegion region = FoldingUtil.findFoldRegion(editor, range.getStartOffset(), range.getEndOffset());
        if (region != null) {
            FoldingInfo fi = element.getUserData(FOLDING_INFO_KEY);
            boolean state = fi != null && fi.expanded;
            region.setExpanded(state);
        }
    }
    for (RangeMarker marker : myRangeMarkers) {
        if (!marker.isValid() || marker.getStartOffset() == marker.getEndOffset()) {
            continue;
        }
        FoldRegion region = FoldingUtil.findFoldRegion(editor, marker.getStartOffset(), marker.getEndOffset());
        FoldingInfo info = marker.getUserData(FOLDING_INFO_KEY);
        if (region == null) {
            if (info != null) {
                region = editor.getFoldingModel().addFoldRegion(marker.getStartOffset(), marker.getEndOffset(), info.placeHolder);
            }
            if (region == null) {
                return;
            }
        }
        boolean state = info != null && info.expanded;
        region.setExpanded(state);
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) FoldRegion(com.intellij.openapi.editor.FoldRegion) RangeMarker(com.intellij.openapi.editor.RangeMarker)

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