Search in sources :

Example 1 with CodeFoldingManager

use of com.intellij.codeInsight.folding.CodeFoldingManager in project intellij-community by JetBrains.

the class RestoreFoldArrangementCallback method afterArrangement.

@Override
public void afterArrangement(@NotNull final List<ArrangementMoveInfo> moveInfos) {
    // Restore state for the PSI elements not affected by arrangement.
    Project project = myEditor.getProject();
    if (project != null) {
        final FoldRegion[] regions = myEditor.getFoldingModel().getAllFoldRegions();
        final List<FoldRegionInfo> foldRegionsInfo = new ArrayList<>();
        for (FoldRegion region : regions) {
            final FoldRegionInfo info = new FoldRegionInfo(region.getStartOffset(), region.getEndOffset(), region.isExpanded());
            foldRegionsInfo.add(info);
        }
        final CodeFoldingManager foldingManager = CodeFoldingManager.getInstance(project);
        foldingManager.updateFoldRegions(myEditor);
        myEditor.getFoldingModel().runBatchFoldingOperation(() -> {
            for (FoldRegionInfo info : foldRegionsInfo) {
                final FoldRegion foldRegion = foldingManager.findFoldRegion(myEditor, info.myStart, info.myEnd);
                if (foldRegion != null) {
                    foldRegion.setExpanded(info.myIsExpanded);
                }
            }
        });
    }
}
Also used : Project(com.intellij.openapi.project.Project) CodeFoldingManager(com.intellij.codeInsight.folding.CodeFoldingManager) ArrayList(java.util.ArrayList) FoldRegion(com.intellij.openapi.editor.FoldRegion)

Example 2 with CodeFoldingManager

use of com.intellij.codeInsight.folding.CodeFoldingManager in project intellij-community by JetBrains.

the class CodeFoldingConfigurable method apply.

@Override
public void apply() throws ConfigurationException {
    EditorSettingsExternalizable.getInstance().setFoldingOutlineShown(myCbFolding.isSelected());
    super.apply();
    final List<Pair<Editor, Project>> toUpdate = new ArrayList<>();
    for (final Editor editor : EditorFactory.getInstance().getAllEditors()) {
        final Project project = editor.getProject();
        if (project != null && !project.isDefault()) {
            toUpdate.add(Pair.create(editor, project));
        }
    }
    ApplicationManager.getApplication().invokeLater(() -> {
        for (Pair<Editor, Project> each : toUpdate) {
            if (each.second == null || each.second.isDisposed()) {
                continue;
            }
            final CodeFoldingManager foldingManager = CodeFoldingManager.getInstance(each.second);
            if (foldingManager != null) {
                foldingManager.buildInitialFoldings(each.first);
            }
        }
        EditorOptionsPanel.reinitAllEditors();
    }, ModalityState.NON_MODAL);
}
Also used : Project(com.intellij.openapi.project.Project) CodeFoldingManager(com.intellij.codeInsight.folding.CodeFoldingManager) ArrayList(java.util.ArrayList) Editor(com.intellij.openapi.editor.Editor) Pair(com.intellij.openapi.util.Pair)

Example 3 with CodeFoldingManager

use of com.intellij.codeInsight.folding.CodeFoldingManager in project intellij-community by JetBrains.

the class CollapseExpandDocCommentsHandler method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull final Editor editor, @NotNull PsiFile file) {
    CodeFoldingManager foldingManager = CodeFoldingManager.getInstance(project);
    foldingManager.updateFoldRegions(editor);
    final FoldRegion[] allFoldRegions = editor.getFoldingModel().getAllFoldRegions();
    Runnable processor = () -> {
        for (FoldRegion region : allFoldRegions) {
            PsiElement element = EditorFoldingInfo.get(editor).getPsiElement(region);
            if (element instanceof PsiDocCommentBase) {
                region.setExpanded(myExpand);
            }
        }
    };
    editor.getFoldingModel().runBatchFoldingOperation(processor);
}
Also used : CodeFoldingManager(com.intellij.codeInsight.folding.CodeFoldingManager) PsiDocCommentBase(com.intellij.psi.PsiDocCommentBase) FoldRegion(com.intellij.openapi.editor.FoldRegion) PsiElement(com.intellij.psi.PsiElement)

Example 4 with CodeFoldingManager

use of com.intellij.codeInsight.folding.CodeFoldingManager 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)

Aggregations

CodeFoldingManager (com.intellij.codeInsight.folding.CodeFoldingManager)4 FoldRegion (com.intellij.openapi.editor.FoldRegion)3 Project (com.intellij.openapi.project.Project)2 ArrayList (java.util.ArrayList)2 Editor (com.intellij.openapi.editor.Editor)1 Pair (com.intellij.openapi.util.Pair)1 PsiDocCommentBase (com.intellij.psi.PsiDocCommentBase)1 PsiElement (com.intellij.psi.PsiElement)1