Search in sources :

Example 41 with FoldingDescriptor

use of com.intellij.lang.folding.FoldingDescriptor in project intellij by bazelbuild.

the class BuildFileFoldingBuilder method foldSequentialComments.

private static void foldSequentialComments(List<FoldingDescriptor> descriptors, ASTNode node) {
    // need to skip previous comments in sequence
    ASTNode curNode = node.getTreePrev();
    while (curNode != null) {
        if (curNode.getElementType() == BuildToken.COMMENT) {
            return;
        }
        curNode = isWhitespaceOrNewline(curNode) ? curNode.getTreePrev() : null;
    }
    // fold sequence comments in one block
    curNode = node.getTreeNext();
    ASTNode lastCommentNode = node;
    while (curNode != null) {
        if (curNode.getElementType() == BuildToken.COMMENT) {
            lastCommentNode = curNode;
            curNode = curNode.getTreeNext();
            continue;
        }
        curNode = isWhitespaceOrNewline(curNode) ? curNode.getTreeNext() : null;
    }
    if (lastCommentNode != node) {
        descriptors.add(new FoldingDescriptor(node, TextRange.create(node.getStartOffset(), lastCommentNode.getTextRange().getEndOffset())));
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) ASTNode(com.intellij.lang.ASTNode)

Example 42 with FoldingDescriptor

use of com.intellij.lang.folding.FoldingDescriptor in project intellij by bazelbuild.

the class BuildFileFoldingBuilderTest method testMultilineCommentFolded.

@Test
public void testMultilineCommentFolded() throws Throwable {
    BuildFile file = createBuildFile(new WorkspacePath("java/com/google/BUILD"), "# multi-line comment, folded", "# second line of comment", "def function(arg1, arg2):", "    stmt1", "    stmt2", "", "variable = 1");
    FoldingDescriptor[] foldingRegions = getFoldingRegions(file);
    assertThat(foldingRegions).hasLength(2);
    assertThat(foldingRegions[0].getElement().getPsi()).isEqualTo(file.firstChildOfClass(PsiComment.class));
}
Also used : BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) PsiComment(com.intellij.psi.PsiComment) Test(org.junit.Test)

Example 43 with FoldingDescriptor

use of com.intellij.lang.folding.FoldingDescriptor in project intellij by bazelbuild.

the class BuildFileFoldingBuilderTest method testMultilineStringFoldedToFirstLine.

@Test
public void testMultilineStringFoldedToFirstLine() throws Throwable {
    BuildFile file = createBuildFile(new WorkspacePath("java/com/google/BUILD"), "\"\"\"First line of string", "Second line of string\"\"\"");
    FoldingDescriptor[] foldingRegions = getFoldingRegions(file);
    assertThat(foldingRegions).hasLength(1);
    assertThat(foldingRegions[0].getPlaceholderText()).isEqualTo("\"\"\"First line of string...\"\"\"");
}
Also used : BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) Test(org.junit.Test)

Example 44 with FoldingDescriptor

use of com.intellij.lang.folding.FoldingDescriptor in project idea-php-typo3-plugin by cedricziel.

the class RouteFoldingBuilder method buildFoldRegions.

@NotNull
@Override
public FoldingDescriptor @NotNull [] buildFoldRegions(@NotNull PsiElement root, @NotNull Document document, boolean quick) {
    FoldingGroup group = FoldingGroup.newGroup("TYPO3Route");
    List<FoldingDescriptor> descriptors = new ArrayList<>();
    Collection<StringLiteralExpression> literalExpressions = PsiTreeUtil.findChildrenOfType(root, StringLiteralExpression.class);
    for (final StringLiteralExpression literalExpression : literalExpressions) {
        for (PsiReference reference : literalExpression.getReferences()) {
            if (reference instanceof RouteReference) {
                String value = literalExpression.getContents();
                FoldingDescriptor descriptor = foldRouteReferenceString(reference, value, group);
                if (descriptor != null) {
                    descriptors.add(descriptor);
                }
            }
        }
    }
    return descriptors.toArray(FoldingDescriptor.EMPTY);
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) FoldingGroup(com.intellij.openapi.editor.FoldingGroup) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) NotNull(org.jetbrains.annotations.NotNull)

Example 45 with FoldingDescriptor

use of com.intellij.lang.folding.FoldingDescriptor in project idea-php-typo3-plugin by cedricziel.

the class RouteFoldingBuilder method foldRouteReferenceString.

@Nullable
private FoldingDescriptor foldRouteReferenceString(PsiReference reference, String value, FoldingGroup group) {
    PsiElement element = reference.getElement();
    TextRange foldingRange = new TextRange(element.getTextRange().getStartOffset() + 1, element.getTextRange().getEndOffset() - 1);
    if (!RouteIndex.hasRoute(element.getProject(), value)) {
        return null;
    }
    Collection<RouteStub> route = RouteIndex.getRoute(element.getProject(), value);
    if (route.size() == 0) {
        return null;
    }
    RouteStub routeDef = route.iterator().next();
    return new FoldingDescriptor(element.getNode(), foldingRange, group) {

        @Nullable
        @Override
        public String getPlaceholderText() {
            if (routeDef.getPath() == null) {
                return routeDef.getController() + "::" + routeDef.getMethod();
            }
            return routeDef.getPath();
        }
    };
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)88 TextRange (com.intellij.openapi.util.TextRange)53 ASTNode (com.intellij.lang.ASTNode)33 PsiElement (com.intellij.psi.PsiElement)25 IElementType (com.intellij.psi.tree.IElementType)17 NamedFoldingDescriptor (com.intellij.lang.folding.NamedFoldingDescriptor)11 UnfairTextRange (com.intellij.openapi.util.UnfairTextRange)11 ArrayList (java.util.ArrayList)11 FoldingGroup (com.intellij.openapi.editor.FoldingGroup)9 PsiComment (com.intellij.psi.PsiComment)9 NotNull (org.jetbrains.annotations.NotNull)9 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)7 BuildFile (com.google.idea.blaze.base.lang.buildfile.psi.BuildFile)6 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)6 Nullable (org.jetbrains.annotations.Nullable)6 Test (org.junit.Test)6 FoldRegion (com.intellij.openapi.editor.FoldRegion)5 LinkedList (java.util.LinkedList)5 FoldingBuilder (com.intellij.lang.folding.FoldingBuilder)4 Project (com.intellij.openapi.project.Project)4