Search in sources :

Example 21 with FoldRegion

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

the class CollapseBlockHandler method invoke.

@Override
public void invoke(@NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile file) {
    int[] targetCaretOffset = { -1 };
    editor.getFoldingModel().runBatchFoldingOperation(() -> {
        final EditorFoldingInfo info = EditorFoldingInfo.get(editor);
        FoldingModelEx model = (FoldingModelEx) editor.getFoldingModel();
        PsiElement element = file.findElementAt(editor.getCaretModel().getOffset() - 1);
        if (!(element instanceof PsiJavaToken) || ((PsiJavaToken) element).getTokenType() != JavaTokenType.RBRACE) {
            element = file.findElementAt(editor.getCaretModel().getOffset());
        }
        if (element == null)
            return;
        PsiCodeBlock block = PsiTreeUtil.getParentOfType(element, PsiCodeBlock.class);
        FoldRegion previous = null;
        FoldRegion myPrevious = null;
        while (block != null) {
            int start = block.getTextRange().getStartOffset();
            int end = block.getTextRange().getEndOffset();
            FoldRegion existing = FoldingUtil.findFoldRegion(editor, start, end);
            if (existing != null) {
                if (existing.isExpanded()) {
                    existing.setExpanded(false);
                    targetCaretOffset[0] = existing.getEndOffset();
                    return;
                }
                previous = existing;
                if (info.getPsiElement(existing) == null)
                    myPrevious = existing;
                block = PsiTreeUtil.getParentOfType(block, PsiCodeBlock.class);
                continue;
            }
            if (!model.intersectsRegion(start, end)) {
                FoldRegion region = model.addFoldRegion(start, end, ourPlaceHolderText);
                LOG.assertTrue(region != null);
                region.setExpanded(false);
                if (myPrevious != null && info.getPsiElement(region) == null) {
                    info.removeRegion(myPrevious);
                    model.removeFoldRegion(myPrevious);
                }
                targetCaretOffset[0] = block.getTextRange().getEndOffset() < editor.getCaretModel().getOffset() ? start : end;
                return;
            } else
                break;
        }
        if (previous != null) {
            previous.setExpanded(false);
            if (myPrevious != null) {
                info.removeRegion(myPrevious);
                model.removeFoldRegion(myPrevious);
            }
            targetCaretOffset[0] = previous.getEndOffset();
        }
    });
    if (targetCaretOffset[0] >= 0)
        editor.getCaretModel().moveToOffset(targetCaretOffset[0]);
}
Also used : FoldRegion(com.intellij.openapi.editor.FoldRegion) FoldingModelEx(com.intellij.openapi.editor.ex.FoldingModelEx)

Example 22 with FoldRegion

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

the class DuplicatesImpl method expandAllRegionsCoveringRange.

private static void expandAllRegionsCoveringRange(final Project project, Editor editor, final TextRange textRange) {
    final FoldRegion[] foldRegions = CodeFoldingManager.getInstance(project).getFoldRegionsAtOffset(editor, textRange.getStartOffset());
    boolean anyCollapsed = false;
    for (final FoldRegion foldRegion : foldRegions) {
        if (!foldRegion.isExpanded()) {
            anyCollapsed = true;
            break;
        }
    }
    if (anyCollapsed) {
        editor.getFoldingModel().runBatchFoldingOperation(() -> {
            for (final FoldRegion foldRegion : foldRegions) {
                if (!foldRegion.isExpanded()) {
                    foldRegion.setExpanded(true);
                }
            }
        });
    }
}
Also used : FoldRegion(com.intellij.openapi.editor.FoldRegion)

Example 23 with FoldRegion

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

the class ExpandRegionAction method expandRegionAtOffset.

public static void expandRegionAtOffset(@NotNull Project project, @NotNull final Editor editor, final int offset) {
    CodeFoldingManager foldingManager = CodeFoldingManager.getInstance(project);
    foldingManager.updateFoldRegions(editor);
    final int line = editor.getDocument().getLineNumber(offset);
    Runnable processor = () -> {
        FoldRegion region = FoldingUtil.findFoldRegionStartingAtLine(editor, line);
        if (region != null && !region.isExpanded()) {
            region.setExpanded(true);
        } else {
            FoldRegion[] regions = FoldingUtil.getFoldRegionsAtOffset(editor, offset);
            for (int i = regions.length - 1; i >= 0; i--) {
                region = regions[i];
                if (!region.isExpanded()) {
                    region.setExpanded(true);
                    break;
                }
            }
        }
    };
    editor.getFoldingModel().runBatchFoldingOperation(processor);
}
Also used : CodeFoldingManager(com.intellij.codeInsight.folding.CodeFoldingManager) FoldRegion(com.intellij.openapi.editor.FoldRegion)

Example 24 with FoldRegion

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

the class FoldingUtil method getFoldRegionsAtOffset.

public static FoldRegion[] getFoldRegionsAtOffset(Editor editor, int offset) {
    List<FoldRegion> list = new ArrayList<>();
    FoldRegion[] allRegions = editor.getFoldingModel().getAllFoldRegions();
    for (FoldRegion region : allRegions) {
        if (region.getStartOffset() <= offset && offset <= region.getEndOffset()) {
            list.add(region);
        }
    }
    FoldRegion[] regions = list.toArray(new FoldRegion[list.size()]);
    Arrays.sort(regions, Collections.reverseOrder(RangeMarker.BY_START_OFFSET));
    return regions;
}
Also used : FoldRegion(com.intellij.openapi.editor.FoldRegion)

Example 25 with FoldRegion

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

the class UpdateFoldRegionsOperation method regionOrGroupCanBeRemovedWhenCollapsed.

private boolean regionOrGroupCanBeRemovedWhenCollapsed(FoldRegion region) {
    FoldingGroup group = region.getGroup();
    List<FoldRegion> affectedRegions = group != null && myEditor instanceof EditorEx ? ((EditorEx) myEditor).getFoldingModel().getGroupedRegions(group) : Collections.singletonList(region);
    for (FoldRegion affectedRegion : affectedRegions) {
        if (regionCanBeRemovedWhenCollapsed(affectedRegion))
            return true;
    }
    return false;
}
Also used : FoldingGroup(com.intellij.openapi.editor.FoldingGroup) EditorEx(com.intellij.openapi.editor.ex.EditorEx) FoldRegion(com.intellij.openapi.editor.FoldRegion)

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