Search in sources :

Example 46 with FoldingDescriptor

use of com.intellij.lang.folding.FoldingDescriptor in project oxy-template-support-plugin by mutant-industries.

the class MatchingTagsAndJsBlock method buildBraceMatcherBasedFolding.

private static void buildBraceMatcherBasedFolding(@NotNull List<FoldingDescriptor> descriptors, @NotNull PsiElement root, @NotNull Document document, @NotNull SyntaxHighlighter highlighter) {
    LexerEditorHighlighter editorHighlighter = new LexerEditorHighlighter(highlighter, EditorColorsManager.getInstance().getGlobalScheme());
    editorHighlighter.setText(document.getText());
    FileType fileType = root.getContainingFile().getFileType();
    BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, root.getLanguage());
    TextRange totalRange = root.getTextRange();
    final HighlighterIterator iterator = editorHighlighter.createIterator(totalRange.getStartOffset());
    final LinkedList<Trinity<Integer, Integer, IElementType>> stack = new LinkedList<>();
    String editorText = document.getText();
    TextRange range;
    while (!iterator.atEnd() && iterator.getStart() < totalRange.getEndOffset()) {
        final Trinity<Integer, Integer, IElementType> last;
        if (OxyTemplateParserDefinition.PARAMETER_QUOTES.contains(iterator.getTokenType())) {
            iterator.advance();
            continue;
        }
        if (braceMatcher.isLBraceToken(iterator, editorText, fileType)) {
            if (iterator.getTokenType() == OxyTemplateTypes.T_XML_TAG_START) {
                IElementType tokenType = iterator.getTokenType();
                do {
                    iterator.advance();
                } while (!iterator.atEnd() && iterator.getTokenType() != OxyTemplateTypes.T_XML_OPEN_TAG_END && iterator.getTokenType() != OxyTemplateTypes.T_XML_CLOSE_TAG_END && iterator.getTokenType() != OxyTemplateTypes.T_XML_EMPTY_TAG_END);
                if (iterator.atEnd()) {
                    return;
                }
                stack.addLast(Trinity.create(iterator.getStart(), iterator.getEnd(), tokenType));
                iterator.retreat();
            } else {
                stack.addLast(Trinity.create(iterator.getStart() + 2, iterator.getEnd(), iterator.getTokenType()));
            }
        } else if (braceMatcher.isRBraceToken(iterator, editorText, fileType) && !stack.isEmpty() && braceMatcher.isPairBraces((last = stack.getLast()).third, iterator.getTokenType())) {
            stack.removeLast();
            range = new TextRange(last.first, iterator.getEnd() - (iterator.getTokenType() == OxyTemplateTypes.T_XML_CLOSE_TAG_END ? 1 : 2));
            if (last.third != OxyTemplateTypes.T_XML_EMPTY_TAG_END && StringUtil.countChars(document.getText(range), '\n') >= 2) {
                descriptors.add(new FoldingDescriptor(root, range));
            }
        }
        iterator.advance();
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) BraceMatcher(com.intellij.codeInsight.highlighting.BraceMatcher) Trinity(com.intellij.openapi.util.Trinity) TextRange(com.intellij.openapi.util.TextRange) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) LinkedList(java.util.LinkedList) IElementType(com.intellij.psi.tree.IElementType) OxyTemplateFileType(ool.intellij.plugin.file.type.OxyTemplateFileType) FileType(com.intellij.openapi.fileTypes.FileType) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 47 with FoldingDescriptor

use of com.intellij.lang.folding.FoldingDescriptor in project Perl5-IDEA by Camelcade.

the class PerlFoldingBuilder method getCommentsDescriptors.

/**
 * Searching for sequential comments (starting from newline or subblock beginning) and making folding descriptors for such blocks of size > 1
 *
 * @param comments list of collected comments
 * @param document document to search in
 * @return list of FoldingDescriptros
 */
private List<FoldingDescriptor> getCommentsDescriptors(@NotNull List<PsiComment> comments, @NotNull Document document) {
    List<FoldingDescriptor> descriptors = new ArrayList<>();
    TokenSet commentExcludedTokens = getCommentExcludedTokens();
    int currentOffset = 0;
    for (PsiComment comment : comments) {
        ASTNode commentNode = comment.getNode();
        IElementType commentElementType = commentNode.getElementType();
        if (currentOffset <= comment.getTextOffset() && // skips already collapsed blocks
        !commentExcludedTokens.contains(commentElementType)) {
            if (commentElementType == POD) {
                TextRange commentRange = comment.getTextRange();
                int startOffset = commentRange.getStartOffset();
                int endOffset = commentRange.getEndOffset();
                if (comment.getText().endsWith("\n")) {
                    endOffset--;
                }
                currentOffset = endOffset;
                descriptors.add(new FoldingDescriptor(commentNode, new TextRange(startOffset, endOffset)));
                continue;
            }
            boolean isCollapsable = false;
            PsiElement lastComment = comment;
            if (commentElementType == COMMENT_BLOCK || // template blocks are always collapsable
            commentElementType == getTemplateBlockElementType()) {
                isCollapsable = true;
            } else {
                // checking if this is a first element of block or starts from newline
                while (true) {
                    lastComment = lastComment.getPrevSibling();
                    if (lastComment == null || lastComment instanceof PsiComment) {
                        isCollapsable = true;
                        break;
                    } else if (lastComment instanceof PsiWhiteSpace) {
                        // whitespace with newline
                        if (StringUtil.containsLineBreak(lastComment.getNode().getChars())) {
                            isCollapsable = true;
                            break;
                        }
                    } else // non-whitespace block
                    {
                        break;
                    }
                }
            }
            if (isCollapsable) {
                // looking for an end
                int startOffset = comment.getTextOffset();
                if (comment.getText().startsWith("\n") && startOffset > 0 && document.getCharsSequence().charAt(startOffset - 1) != '\n') {
                    startOffset++;
                }
                int endOffset = comment.getTextRange().getEndOffset();
                PsiElement currentComment = comment;
                while (currentComment != null) {
                    if (currentComment instanceof PsiComment && !commentExcludedTokens.contains(currentComment.getNode().getElementType()) && !currentComment.getText().contains("todo") && !currentComment.getText().contains("fixme")) {
                        endOffset = currentComment.getTextOffset() + currentComment.getTextLength();
                        if (currentComment.getText().endsWith("\n")) {
                            endOffset--;
                        }
                    } else if (!(currentComment instanceof PsiWhiteSpace)) {
                        break;
                    }
                    currentComment = currentComment.getNextSibling();
                }
                if (endOffset > startOffset) {
                    int startLine = document.getLineNumber(startOffset);
                    int endLine = document.getLineNumber(endOffset);
                    if (endLine > startLine) {
                        currentOffset = endOffset;
                        descriptors.add(new FoldingDescriptor(commentNode, new TextRange(startOffset, endOffset)));
                    }
                }
            }
        }
    }
    return descriptors;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) PsiComment(com.intellij.psi.PsiComment) TokenSet(com.intellij.psi.tree.TokenSet) ArrayList(java.util.ArrayList) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 48 with FoldingDescriptor

use of com.intellij.lang.folding.FoldingDescriptor in project Perl5-IDEA by Camelcade.

the class PerlFoldingBuilderBase method addDescriptorFor.

/**
 * Finding psi elements of specific types and add Folding descriptor for them if they are more than certain lines lenght
 *
 * @param element     root element for searching
 * @param startMargin beginning margin for collapsable block
 * @param endMargin   end margin for collapsable block
 * @return list of folding descriptors
 */
protected static void addDescriptorFor(@NotNull List<FoldingDescriptor> result, @NotNull Document document, @NotNull PsiElement element, int startMargin, int endMargin, int minLines) {
    if (!(element.getParent() instanceof PerlNamespaceDefinitionWithIdentifier)) {
        TextRange range = element.getTextRange();
        int startOffset = range.getStartOffset() + startMargin;
        int endOffset = range.getEndOffset() - endMargin;
        int startLine = document.getLineNumber(startOffset);
        int endLine = document.getLineNumber(endOffset);
        if (endLine - startLine > minLines) {
            result.add(new FoldingDescriptor(element.getNode(), new TextRange(startOffset, endOffset)));
        }
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) TextRange(com.intellij.openapi.util.TextRange) PerlNamespaceDefinitionWithIdentifier(com.perl5.lang.perl.psi.PerlNamespaceDefinitionWithIdentifier)

Example 49 with FoldingDescriptor

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

the class BuildFileFoldingBuilderTest method testRulesFolded.

@Test
public void testRulesFolded() {
    BuildFile file = createBuildFile(new WorkspacePath("java/com/google/BUILD"), "java_library(", "    name = 'lib',", "    srcs = glob(['*.java']),", ")");
    FoldingDescriptor[] foldingRegions = getFoldingRegions(file);
    assertThat(foldingRegions).hasLength(1);
    assertThat(foldingRegions[0].getElement().getPsi()).isEqualTo(file.findRule("lib"));
}
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 50 with FoldingDescriptor

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

the class BuildFileFoldingBuilderTest method testLoadStatementFolded.

@Test
public void testLoadStatementFolded() {
    BuildFile file = createBuildFile(new WorkspacePath("java/com/google/BUILD"), "load(", "   '//java/com/foo/build_defs.bzl',", "   'function1',", "   'function2',", ")");
    FoldingDescriptor[] foldingRegions = getFoldingRegions(file);
    assertThat(foldingRegions).hasLength(1);
    assertThat(foldingRegions[0].getElement().getPsi()).isEqualTo(file.findChildByClass(LoadStatement.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) LoadStatement(com.google.idea.blaze.base.lang.buildfile.psi.LoadStatement) Test(org.junit.Test)

Aggregations

FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)51 TextRange (com.intellij.openapi.util.TextRange)31 ASTNode (com.intellij.lang.ASTNode)20 PsiElement (com.intellij.psi.PsiElement)16 IElementType (com.intellij.psi.tree.IElementType)12 NamedFoldingDescriptor (com.intellij.lang.folding.NamedFoldingDescriptor)8 PsiComment (com.intellij.psi.PsiComment)7 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)7 FoldingGroup (com.intellij.openapi.editor.FoldingGroup)6 UnfairTextRange (com.intellij.openapi.util.UnfairTextRange)6 NotNull (org.jetbrains.annotations.NotNull)6 ArrayList (java.util.ArrayList)5 LinkedList (java.util.LinkedList)4 Nullable (org.jetbrains.annotations.Nullable)4 BuildFile (com.google.idea.blaze.base.lang.buildfile.psi.BuildFile)3 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)3 FoldRegion (com.intellij.openapi.editor.FoldRegion)3 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)3 Test (org.junit.Test)3 HbBlockWrapper (com.dmarcotte.handlebars.psi.HbBlockWrapper)2