Search in sources :

Example 16 with PsiComment

use of com.intellij.psi.PsiComment in project intellij-community by JetBrains.

the class PsiEquivalenceUtil method getFilteredChildren.

@NotNull
public static PsiElement[] getFilteredChildren(@NotNull final PsiElement element, @Nullable Condition<PsiElement> isElementSignificantCondition, boolean areCommentsSignificant) {
    ASTNode[] children1 = element.getNode().getChildren(null);
    ArrayList<PsiElement> array = new ArrayList<>();
    for (ASTNode node : children1) {
        final PsiElement child = node.getPsi();
        if (!(child instanceof PsiWhiteSpace) && (areCommentsSignificant || !(child instanceof PsiComment)) && (isElementSignificantCondition == null || isElementSignificantCondition.value(child))) {
            array.add(child);
        }
    }
    return PsiUtilCore.toPsiElementArray(array);
}
Also used : PsiComment(com.intellij.psi.PsiComment) ASTNode(com.intellij.lang.ASTNode) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with PsiComment

use of com.intellij.psi.PsiComment in project intellij-plugins by JetBrains.

the class CfmlFoldingBuilder method addFoldingDescriptors.

private static void addFoldingDescriptors(final List<FoldingDescriptor> descriptors, final PsiElement tag, @NotNull Document document) {
    TextRange elementRange = tag.getTextRange();
    final int start = elementRange.getStartOffset();
    final int end = elementRange.getEndOffset();
    if (start + 1 < end) {
        TextRange range = null;
        ASTNode astNode = tag.getNode();
        IElementType astType = astNode.getElementType();
        if (tag instanceof CfmlTag) {
            //if (tag instanceof CfmlTagFunctionImpl || tag instanceof CfmlTagComponentImpl || tag instanceof CfmlTagScriptImpl) {
            range = buildRangeForBraces(range, astNode, CfmlTokenTypes.R_ANGLEBRACKET, CfmlTokenTypes.LSLASH_ANGLEBRACKET);
        //}
        } else if (astType == CfmlElementTypes.FUNCTIONBODY || astType == CfmlElementTypes.BLOCK_OF_STATEMENTS) {
            range = buildRange(range, start, end);
        } else if (astType == CfmlElementTypes.SWITCHEXPRESSION) {
            ASTNode lparen = astNode.findChildByType(CfscriptTokenTypes.L_CURLYBRACKET);
            ASTNode rparen = astNode.findChildByType(CfscriptTokenTypes.R_CURLYBRACKET);
            if (lparen != null && rparen != null) {
                range = buildRange(range, lparen.getStartOffset(), rparen.getTextRange().getEndOffset());
            }
        } else if (tag instanceof PsiComment) {
            boolean isColdFusionComment = astNode.getElementType() == CfmlTokenTypes.COMMENT;
            int endIndex = astNode.getText().lastIndexOf(isColdFusionComment ? "--->" : "*/");
            if (endIndex != -1) {
                String commentText = astNode.getText().substring(0, endIndex);
                if (commentText.contains("\n")) {
                    int startOffset = tag.getTextRange().getStartOffset();
                    range = buildRange(range, startOffset + (isColdFusionComment ? "<!---" : "/*").length(), startOffset + commentText.length());
                }
            }
        }
        if (range != null) {
            descriptors.add(new FoldingDescriptor(astNode, range));
        }
        // TODO: insert condition
        addFoldingDescriptorsFromChildren(descriptors, tag, document);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiComment(com.intellij.psi.PsiComment) FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) CfmlTag(com.intellij.coldFusion.model.psi.CfmlTag)

Example 18 with PsiComment

use of com.intellij.psi.PsiComment in project intellij-plugins by JetBrains.

the class CfmlFoldingBuilder method isCollapsedByDefault.

public boolean isCollapsedByDefault(@NotNull ASTNode node) {
    CodeFoldingSettings settings = CodeFoldingSettings.getInstance();
    final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(node);
    if (element instanceof PsiComment) {
        // find out if file header
        final ASTNode parent = node.getTreeParent();
        ASTNode treePrev = node.getTreePrev();
        if (parent.getElementType() == CfmlElementTypes.CFML_FILE && treePrev == null) {
            return CodeFoldingSettings.getInstance().COLLAPSE_FILE_HEADER;
        } else {
            return CodeFoldingSettings.getInstance().COLLAPSE_DOC_COMMENTS;
        }
    } else if (element instanceof CfmlFunction || node.getElementType() == CfmlElementTypes.FUNCTIONBODY) {
        return settings.COLLAPSE_METHODS;
    }
    return false;
}
Also used : CodeFoldingSettings(com.intellij.codeInsight.folding.CodeFoldingSettings) CfmlFunction(com.intellij.coldFusion.model.psi.CfmlFunction) PsiComment(com.intellij.psi.PsiComment) ASTNode(com.intellij.lang.ASTNode) PsiElement(com.intellij.psi.PsiElement)

Example 19 with PsiComment

use of com.intellij.psi.PsiComment in project intellij-plugins by JetBrains.

the class DartMethodLineMarkerProvider method getLineMarkerInfo.

@Nullable
@Override
public LineMarkerInfo getLineMarkerInfo(@NotNull final PsiElement element) {
    if (!myDaemonSettings.SHOW_METHOD_SEPARATORS) {
        return null;
    }
    // only continue if element is one of the markable elements (such as methods)
    if (isMarkableElement(element)) {
        // the method line markers are not nestable, aka, methods inside of methods, are not marked
        if (PsiTreeUtil.findFirstParent(element, true, DartMethodLineMarkerProvider::isMarkableElement) != null) {
            return null;
        }
        // move the marker to previous siblings until comments have been included
        PsiElement markerLocation = element;
        while (markerLocation.getPrevSibling() != null && (markerLocation.getPrevSibling() instanceof PsiComment || (markerLocation.getPrevSibling() instanceof PsiWhiteSpace && markerLocation.getPrevSibling().getPrevSibling() != null && markerLocation.getPrevSibling().getPrevSibling() instanceof PsiComment))) {
            markerLocation = markerLocation.getPrevSibling();
        }
        // if the markerLocation element doesn't have a previous sibling (not whitespace), do not mark
        PsiElement prevElement = markerLocation;
        while (prevElement.getPrevSibling() != null && prevElement.getPrevSibling() instanceof PsiWhiteSpace) {
            prevElement = prevElement.getPrevSibling();
        }
        if (prevElement.getPrevSibling() == null) {
            return null;
        }
        // finally, create the marker
        LineMarkerInfo info = new LineMarkerInfo<>(markerLocation, markerLocation.getTextRange(), null, Pass.LINE_MARKERS, FunctionUtil.<Object, String>nullConstant(), null, GutterIconRenderer.Alignment.RIGHT);
        EditorColorsScheme scheme = myColorsManager.getGlobalScheme();
        info.separatorColor = scheme.getColor(CodeInsightColors.METHOD_SEPARATORS_COLOR);
        info.separatorPlacement = SeparatorPlacement.TOP;
        return info;
    }
    return null;
}
Also used : LineMarkerInfo(com.intellij.codeInsight.daemon.LineMarkerInfo) PsiComment(com.intellij.psi.PsiComment) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with PsiComment

use of com.intellij.psi.PsiComment in project intellij-plugins by JetBrains.

the class DartDocUtil method getDocumentationText.

@Nullable
private static String getDocumentationText(final DartComponent dartComponent) {
    // PSI is not perfect currently, doc comment may be not part of the corresponding DartComponent element, so docs are searched for in several places:
    // - direct child of this DartComponent
    // - previous sibling (or previous sibling of parent element if this element is first child of its parent DartClassMembers)
    // Consequent line doc comments (///) are joined
    // 1. Look for multiline doc comment as direct child
    final DartDocComment multilineComment = PsiTreeUtil.getChildOfType(dartComponent, DartDocComment.class);
    if (multilineComment != null)
        return getMultilineDocCommentText(multilineComment);
    // 2. Look for single line doc comments as direct children
    final PsiComment[] childComments = PsiTreeUtil.getChildrenOfType(dartComponent, PsiComment.class);
    if (childComments != null) {
        //
        final String docText = getSingleLineDocCommentsText(childComments);
        if (docText != null)
            return docText;
    }
    PsiElement anchorElement = dartComponent;
    final PsiElement parent = dartComponent.getParent();
    if (parent instanceof DartClassMembers && parent.getFirstChild() == dartComponent || dartComponent instanceof DartVarAccessDeclaration) {
        anchorElement = parent;
    }
    // 3. Look for multiline doc comment or line doc comments as previous siblings
    final List<PsiComment> siblingComments = new ArrayList<>();
    PsiElement previous = anchorElement;
    while ((previous = UsefulPsiTreeUtil.getPrevSiblingSkipWhiteSpaces(previous, true)) instanceof PsiComment) {
        if (previous instanceof DartDocComment) {
            return getMultilineDocCommentText((DartDocComment) previous);
        }
        siblingComments.add(0, (PsiComment) previous);
    }
    if (!siblingComments.isEmpty()) {
        return getSingleLineDocCommentsText(siblingComments.toArray(new PsiComment[siblingComments.size()]));
    }
    return null;
}
Also used : PsiComment(com.intellij.psi.PsiComment) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiComment (com.intellij.psi.PsiComment)60 PsiElement (com.intellij.psi.PsiElement)39 Nullable (org.jetbrains.annotations.Nullable)18 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)14 ArrayList (java.util.ArrayList)10 IElementType (com.intellij.psi.tree.IElementType)8 TextRange (com.intellij.openapi.util.TextRange)7 NotNull (org.jetbrains.annotations.NotNull)7 Matcher (java.util.regex.Matcher)6 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)5 PsiFile (com.intellij.psi.PsiFile)5 ASTNode (com.intellij.lang.ASTNode)4 Document (com.intellij.openapi.editor.Document)4 PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)4 LineRange (com.intellij.codeInsight.editorActions.moveUpDown.LineRange)3 XmlTag (com.intellij.psi.xml.XmlTag)3 Commenter (com.intellij.lang.Commenter)2 Language (com.intellij.lang.Language)2 Editor (com.intellij.openapi.editor.Editor)2 UnfairTextRange (com.intellij.openapi.util.UnfairTextRange)2