Search in sources :

Example 1 with FoldRegion

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

the class I18nMessageGotoDeclarationHandler method getGotoDeclarationTarget.

@Override
public PsiElement getGotoDeclarationTarget(@Nullable PsiElement element, Editor editor) {
    if (!(element instanceof PsiJavaToken))
        return null;
    //some street magic
    int i = 4;
    while (element != null && i > 0) {
        final ASTNode node = element.getNode();
        if (node != null && node.getUserData(KEY) instanceof PropertyFoldingBuilder) {
            break;
        } else {
            i--;
            element = element.getParent();
        }
    }
    //case: "literalAnnotatedWithPropertyKey"
    if (element instanceof PsiLiteralExpression) {
        return resolve(element);
    }
    //case: MyBundle.message("literalAnnotatedWithPropertyKey", param1, param2)
    if (element instanceof PsiMethodCallExpression) {
        final PsiMethodCallExpression methodCall = (PsiMethodCallExpression) element;
        FoldRegion foldRegion = null;
        for (FoldRegion region : editor.getFoldingModel().getAllFoldRegions()) {
            final PsiElement psiElement = EditorFoldingInfo.get(editor).getPsiElement(region);
            if (methodCall.equals(psiElement)) {
                foldRegion = region;
            }
        }
        if (foldRegion == null || foldRegion.isExpanded())
            return null;
        for (PsiExpression expression : methodCall.getArgumentList().getExpressions()) {
            if (expression instanceof PsiLiteralExpression && PropertyFoldingBuilder.isI18nProperty((PsiLiteralExpression) expression)) {
                return resolve(expression);
            }
        }
    }
    return null;
}
Also used : ASTNode(com.intellij.lang.ASTNode) FoldRegion(com.intellij.openapi.editor.FoldRegion)

Example 2 with FoldRegion

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

the class ProblemPreviewEditorPresentation method updateFoldings.

private void updateFoldings() {
    myEditor.getFoldingModel().runBatchFoldingOperation(() -> {
        myEditor.getFoldingModel().clearFoldRegions();
        myEditor.getMarkupModel().removeAllHighlighters();
        for (PreviewEditorFoldingRegion region : myFoldedRegions) {
            if (region.endLine - region.startLine > 1) {
                FoldRegion currentRegion = FoldingModelSupport.addFolding(myEditor, region.startLine, region.endLine, false);
                if (currentRegion != null) {
                    DiffDrawUtil.createLineSeparatorHighlighter(myEditor, myDocument.getLineStartOffset(region.startLine), myDocument.getLineEndOffset(region.endLine - 1), () -> currentRegion.isValid() && !currentRegion.isExpanded());
                }
            }
        }
    });
}
Also used : FoldRegion(com.intellij.openapi.editor.FoldRegion)

Example 3 with FoldRegion

use of com.intellij.openapi.editor.FoldRegion 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 4 with FoldRegion

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

the class ExternalProjectPathField method collapse.

public static void collapse(@NotNull final Editor editor, @NotNull final String placeholder) {
    final FoldingModel foldingModel = editor.getFoldingModel();
    foldingModel.runBatchFoldingOperation(() -> {
        for (FoldRegion region : foldingModel.getAllFoldRegions()) {
            foldingModel.removeFoldRegion(region);
        }
        FoldRegion region = foldingModel.addFoldRegion(0, editor.getDocument().getTextLength(), placeholder);
        if (region != null) {
            region.setExpanded(false);
        }
    });
}
Also used : FoldingModel(com.intellij.openapi.editor.FoldingModel) FoldRegion(com.intellij.openapi.editor.FoldRegion)

Example 5 with FoldRegion

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

the class FoldingUtil method findFoldRegionStartingAtLine.

@Nullable
public static FoldRegion findFoldRegionStartingAtLine(@NotNull Editor editor, int line) {
    if (line < 0 || line >= editor.getDocument().getLineCount()) {
        return null;
    }
    FoldRegion result = null;
    FoldRegion[] regions = editor.getFoldingModel().getAllFoldRegions();
    for (FoldRegion region : regions) {
        if (!region.isValid()) {
            continue;
        }
        if (region.getDocument().getLineNumber(region.getStartOffset()) == line) {
            if (result != null)
                return null;
            result = region;
        }
    }
    return result;
}
Also used : FoldRegion(com.intellij.openapi.editor.FoldRegion) Nullable(org.jetbrains.annotations.Nullable)

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