Search in sources :

Example 31 with PsiComment

use of com.intellij.psi.PsiComment in project kotlin by JetBrains.

the class UpdateKotlinCopyright method scanFile.

@Override
protected void scanFile() {
    PsiElement first = getFile().getFirstChild();
    PsiElement last = first;
    PsiElement next = first;
    while (next != null) {
        if (next instanceof PsiComment || next instanceof PsiWhiteSpace) {
            next = getNextSibling(next);
        } else {
            break;
        }
        last = next;
    }
    if (first != null) {
        checkComments(first, last, true);
    }
}
Also used : PsiComment(com.intellij.psi.PsiComment) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 32 with PsiComment

use of com.intellij.psi.PsiComment in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoMethodSeparatorProvider method findAnchorElement.

@NotNull
private static PsiElement findAnchorElement(@NotNull GoTopLevelDeclaration o) {
    PsiElement result = o;
    PsiElement p = o;
    while ((p = p.getPrevSibling()) != null) {
        if (p instanceof PsiComment) {
            result = p;
        } else if (p instanceof PsiWhiteSpace) {
            if (p.getText().contains("\n\n"))
                return result;
        } else {
            break;
        }
    }
    return result;
}
Also used : PsiComment(com.intellij.psi.PsiComment) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) NotNull(org.jetbrains.annotations.NotNull)

Example 33 with PsiComment

use of com.intellij.psi.PsiComment in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoInspectionSuppressor method isSuppressedInStatement.

private static boolean isSuppressedInStatement(@NotNull String toolId, @Nullable PsiElement statement) {
    if (statement != null) {
        PsiElement prev = PsiTreeUtil.skipSiblingsBackward(statement, PsiWhiteSpace.class);
        if (prev instanceof PsiComment) {
            String text = prev.getText();
            Matcher matcher = SuppressionUtil.SUPPRESS_IN_LINE_COMMENT_PATTERN.matcher(text);
            return matcher.matches() && SuppressionUtil.isInspectionToolIdMentioned(matcher.group(1), toolId);
        }
    }
    return false;
}
Also used : PsiComment(com.intellij.psi.PsiComment) Matcher(java.util.regex.Matcher) PsiElement(com.intellij.psi.PsiElement)

Example 34 with PsiComment

use of com.intellij.psi.PsiComment in project ideavim by JetBrains.

the class SearchHelper method findMatchingBlockCommentPair.

private static int findMatchingBlockCommentPair(@NotNull PsiElement element, int pos) {
    final Language language = element.getLanguage();
    final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(language);
    final PsiComment comment = PsiTreeUtil.getParentOfType(element, PsiComment.class, false);
    if (comment != null) {
        final int ret = findMatchingBlockCommentPair(comment, pos, commenter.getBlockCommentPrefix(), commenter.getBlockCommentSuffix());
        if (ret >= 0) {
            return ret;
        }
        if (commenter instanceof CodeDocumentationAwareCommenter) {
            final CodeDocumentationAwareCommenter docCommenter = (CodeDocumentationAwareCommenter) commenter;
            return findMatchingBlockCommentPair(comment, pos, docCommenter.getDocumentationCommentPrefix(), docCommenter.getDocumentationCommentSuffix());
        }
    }
    return -1;
}
Also used : CodeDocumentationAwareCommenter(com.intellij.lang.CodeDocumentationAwareCommenter) PsiComment(com.intellij.psi.PsiComment) Language(com.intellij.lang.Language) CodeDocumentationAwareCommenter(com.intellij.lang.CodeDocumentationAwareCommenter) Commenter(com.intellij.lang.Commenter)

Example 35 with PsiComment

use of com.intellij.psi.PsiComment in project android by JetBrains.

the class AndroidBaseXmlRefactoringAction method getExtractableRange.

@Nullable
private static Pair<PsiElement, PsiElement> getExtractableRange(PsiFile file, int start, int end) {
    PsiElement startElement = file.findElementAt(start);
    PsiElement parent = startElement != null ? startElement.getParent() : null;
    while (parent != null && !(parent instanceof PsiFile) && parent.getTextRange().getStartOffset() == startElement.getTextRange().getStartOffset()) {
        startElement = parent;
        parent = parent.getParent();
    }
    PsiElement endElement = file.findElementAt(end - 1);
    parent = endElement != null ? endElement.getParent() : null;
    while (parent != null && !(parent instanceof PsiFile) && parent.getTextRange().getEndOffset() == endElement.getTextRange().getEndOffset()) {
        endElement = parent;
        parent = parent.getParent();
    }
    if (startElement == null || endElement == null) {
        return null;
    }
    final PsiElement commonParent = startElement.getParent();
    if (commonParent == null || !(commonParent instanceof XmlTag) || commonParent != endElement.getParent()) {
        return null;
    }
    PsiElement e = startElement;
    boolean containTag = false;
    while (e != null) {
        if (!(e instanceof XmlText) && !(e instanceof XmlTag) && !(e instanceof PsiWhiteSpace) && !(e instanceof PsiComment)) {
            return null;
        }
        if (e instanceof XmlTag) {
            containTag = true;
        }
        if (e == endElement) {
            break;
        }
        e = e.getNextSibling();
    }
    return e != null && containTag ? Pair.create(startElement, endElement) : null;
}
Also used : PsiComment(com.intellij.psi.PsiComment) PsiFile(com.intellij.psi.PsiFile) XmlText(com.intellij.psi.xml.XmlText) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) 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