Search in sources :

Example 6 with FoldingGroup

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

the class ClosureFolding method createDescriptors.

@Nullable
private List<NamedFoldingDescriptor> createDescriptors(PsiElement classRBrace, int rangeStart, int rangeEnd, String header, String footer) {
    if (rangeStart >= rangeEnd)
        return null;
    FoldingGroup group = FoldingGroup.newGroup("lambda");
    List<NamedFoldingDescriptor> foldElements = new ArrayList<>();
    foldElements.add(new NamedFoldingDescriptor(myNewExpression, getClosureStartOffset(), rangeStart, group, header));
    if (rangeEnd + 1 < getClosureEndOffset()) {
        foldElements.add(new NamedFoldingDescriptor(classRBrace, rangeEnd, getClosureEndOffset(), group, footer));
    }
    return foldElements;
}
Also used : FoldingGroup(com.intellij.openapi.editor.FoldingGroup) NamedFoldingDescriptor(com.intellij.lang.folding.NamedFoldingDescriptor) ArrayList(java.util.ArrayList) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with FoldingGroup

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

the class JavaFoldingBuilderBase method addOneLineMethodFolding.

private boolean addOneLineMethodFolding(@NotNull List<FoldingDescriptor> descriptorList, @NotNull PsiMethod method) {
    if (!JavaCodeFoldingSettings.getInstance().isCollapseOneLineMethods()) {
        return false;
    }
    Document document = method.getContainingFile().getViewProvider().getDocument();
    PsiCodeBlock body = method.getBody();
    PsiIdentifier nameIdentifier = method.getNameIdentifier();
    if (body == null || document == null || nameIdentifier == null) {
        return false;
    }
    if (document.getLineNumber(nameIdentifier.getTextRange().getStartOffset()) != document.getLineNumber(method.getParameterList().getTextRange().getEndOffset())) {
        return false;
    }
    PsiJavaToken lBrace = body.getLBrace();
    PsiJavaToken rBrace = body.getRBrace();
    PsiStatement[] statements = body.getStatements();
    if (lBrace == null || rBrace == null || statements.length != 1) {
        return false;
    }
    PsiStatement statement = statements[0];
    if (statement.textContains('\n')) {
        return false;
    }
    if (!areOnAdjacentLines(lBrace, statement, document) || !areOnAdjacentLines(statement, rBrace, document)) {
        //the user might intend to type at an empty line
        return false;
    }
    int leftStart = method.getParameterList().getTextRange().getEndOffset();
    int bodyStart = body.getTextRange().getStartOffset();
    if (bodyStart > leftStart && !StringUtil.isEmptyOrSpaces(document.getCharsSequence().subSequence(leftStart + 1, bodyStart))) {
        return false;
    }
    int leftEnd = statement.getTextRange().getStartOffset();
    int rightStart = statement.getTextRange().getEndOffset();
    int rightEnd = body.getTextRange().getEndOffset();
    if (leftEnd <= leftStart + 1 || rightEnd <= rightStart + 1) {
        return false;
    }
    String leftText = " { ";
    String rightText = " }";
    if (!fitsRightMargin(method, document, leftStart, rightEnd, rightStart - leftEnd + leftText.length() + rightText.length())) {
        return false;
    }
    FoldingGroup group = FoldingGroup.newGroup("one-liner");
    descriptorList.add(new NamedFoldingDescriptor(lBrace, leftStart, leftEnd, group, leftText));
    descriptorList.add(new NamedFoldingDescriptor(rBrace, rightStart, rightEnd, group, rightText));
    return true;
}
Also used : FoldingGroup(com.intellij.openapi.editor.FoldingGroup) NamedFoldingDescriptor(com.intellij.lang.folding.NamedFoldingDescriptor) Document(com.intellij.openapi.editor.Document)

Example 8 with FoldingGroup

use of com.intellij.openapi.editor.FoldingGroup 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)

Example 9 with FoldingGroup

use of com.intellij.openapi.editor.FoldingGroup 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)

Example 10 with FoldingGroup

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

the class UpdateFoldRegionsOperation method addNewRegions.

private List<FoldRegion> addNewRegions(@NotNull EditorFoldingInfo info, @NotNull FoldingModelEx foldingModel, FoldingUpdate.FoldingMap elementsToFold, @NotNull Map<TextRange, Boolean> rangeToExpandStatusMap, @NotNull Map<FoldRegion, Boolean> shouldExpand, @NotNull Map<FoldingGroup, Boolean> groupExpand) {
    List<FoldRegion> newRegions = new ArrayList<>();
    SmartPointerManager smartPointerManager = SmartPointerManager.getInstance(myProject);
    for (PsiElement element : elementsToFold.keySet()) {
        ProgressManager.checkCanceled();
        final Collection<FoldingDescriptor> descriptors = elementsToFold.get(element);
        for (FoldingDescriptor descriptor : descriptors) {
            FoldingGroup group = descriptor.getGroup();
            TextRange range = descriptor.getRange();
            String placeholder = null;
            try {
                placeholder = descriptor.getPlaceholderText();
            } catch (IndexNotReadyException ignore) {
            }
            if (range.getEndOffset() > myEditor.getDocument().getTextLength()) {
                LOG.error(String.format("Invalid folding descriptor detected (%s). It ends beyond the document range (%d)", descriptor, myEditor.getDocument().getTextLength()));
                continue;
            }
            FoldRegion region = foldingModel.createFoldRegion(range.getStartOffset(), range.getEndOffset(), placeholder == null ? "..." : placeholder, group, descriptor.isNonExpandable());
            if (region == null)
                continue;
            PsiElement psi = descriptor.getElement().getPsi();
            if (psi == null || !psi.isValid() || !foldingModel.addFoldRegion(region) || !myFile.isValid()) {
                region.dispose();
                continue;
            }
            if (descriptor.canBeRemovedWhenCollapsed())
                region.putUserData(CAN_BE_REMOVED_WHEN_COLLAPSED, Boolean.TRUE);
            info.addRegion(region, smartPointerManager.createSmartPsiElementPointer(psi));
            newRegions.add(region);
            boolean expandStatus = !descriptor.isNonExpandable() && shouldExpandNewRegion(element, range, rangeToExpandStatusMap);
            if (group == null) {
                shouldExpand.put(region, expandStatus);
            } else {
                final Boolean alreadyExpanded = groupExpand.get(group);
                groupExpand.put(group, alreadyExpanded == null ? expandStatus : alreadyExpanded.booleanValue() || expandStatus);
            }
        }
    }
    return newRegions;
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) FoldingGroup(com.intellij.openapi.editor.FoldingGroup) FoldRegion(com.intellij.openapi.editor.FoldRegion) TextRange(com.intellij.openapi.util.TextRange) SmartPointerManager(com.intellij.psi.SmartPointerManager) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) PsiElement(com.intellij.psi.PsiElement)

Aggregations

FoldingGroup (com.intellij.openapi.editor.FoldingGroup)12 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)6 NamedFoldingDescriptor (com.intellij.lang.folding.NamedFoldingDescriptor)5 FoldRegion (com.intellij.openapi.editor.FoldRegion)5 TextRange (com.intellij.openapi.util.TextRange)5 NotNull (org.jetbrains.annotations.NotNull)4 PsiElement (com.intellij.psi.PsiElement)3 ArrayList (java.util.ArrayList)3 PsiReference (com.intellij.psi.PsiReference)2 StringLiteralExpression (com.jetbrains.php.lang.psi.elements.StringLiteralExpression)2 Nullable (org.jetbrains.annotations.Nullable)2 Document (com.intellij.openapi.editor.Document)1 EditorEx (com.intellij.openapi.editor.ex.EditorEx)1 FoldingModelEx (com.intellij.openapi.editor.ex.FoldingModelEx)1 IndexNotReadyException (com.intellij.openapi.project.IndexNotReadyException)1 Project (com.intellij.openapi.project.Project)1 SmartPointerManager (com.intellij.psi.SmartPointerManager)1 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)1 PsiElementProcessor (com.intellij.psi.search.PsiElementProcessor)1 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)1