Search in sources :

Example 6 with NamedFoldingDescriptor

use of com.intellij.lang.folding.NamedFoldingDescriptor 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 NamedFoldingDescriptor

use of com.intellij.lang.folding.NamedFoldingDescriptor 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)

Aggregations

NamedFoldingDescriptor (com.intellij.lang.folding.NamedFoldingDescriptor)7 FoldingGroup (com.intellij.openapi.editor.FoldingGroup)5 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)3 TextRange (com.intellij.openapi.util.TextRange)3 PsiElement (com.intellij.psi.PsiElement)3 ASTNode (com.intellij.lang.ASTNode)2 IElementType (com.intellij.psi.tree.IElementType)2 Document (com.intellij.openapi.editor.Document)1 PsiReference (com.intellij.psi.PsiReference)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)1 PsiElementProcessor (com.intellij.psi.search.PsiElementProcessor)1 ArrayList (java.util.ArrayList)1 Call (org.elixir_lang.psi.call.Call)1 Type (org.elixir_lang.psi.operation.Type)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1 GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)1 GrString (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)1 GrStringInjection (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection)1