Search in sources :

Example 61 with PsiComment

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

the class XmlDeclareIdInCommentAction method applyFix.

@Override
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
    final PsiElement psiElement = descriptor.getPsiElement();
    final PsiFile psiFile = psiElement.getContainingFile();
    new WriteCommandAction(project, psiFile) {

        @Override
        protected void run(@NotNull final Result result) throws Throwable {
            final XmlTag tag = PsiTreeUtil.getParentOfType(psiElement, XmlTag.class);
            if (tag == null)
                return;
            final Language language = psiFile.getViewProvider().getBaseLanguage();
            final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(language);
            if (commenter == null)
                return;
            final PsiFile tempFile = PsiFileFactory.getInstance(project).createFileFromText("dummy", language.getAssociatedFileType(), commenter.getBlockCommentPrefix() + "@declare id=\"" + myId + "\"" + commenter.getBlockCommentSuffix() + "\n");
            final XmlTag parent = tag.getParentTag();
            if (parent != null && parent.isValid()) {
                final XmlTag[] tags = parent.getSubTags();
                if (tags.length > 0) {
                    final PsiFile psi = tempFile.getViewProvider().getPsi(language);
                    if (psi != null) {
                        final PsiElement element = psi.findElementAt(1);
                        if (element instanceof PsiComment) {
                            parent.getNode().addChild(element.getNode(), tags[0].getNode());
                        }
                    }
                }
            }
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) PsiComment(com.intellij.psi.PsiComment) Language(com.intellij.lang.Language) Commenter(com.intellij.lang.Commenter) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) Result(com.intellij.openapi.application.Result) XmlTag(com.intellij.psi.xml.XmlTag)

Example 62 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 63 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 64 with PsiComment

use of com.intellij.psi.PsiComment in project ballerina by ballerina-lang.

the class BallerinaFoldingBuilder method addFoldingDescriptor.

private void addFoldingDescriptor(@NotNull List<FoldingDescriptor> descriptors, PsiElement node, PsiElement bodyNode) {
    // Sometimes the body node might start with a comment node.
    PsiElement prevSibling = bodyNode.getPrevSibling();
    while (prevSibling != null && (prevSibling instanceof PsiComment || prevSibling instanceof PsiWhiteSpace)) {
        prevSibling = prevSibling.getPrevSibling();
    }
    if (prevSibling != null) {
        // Calculate the start and end offsets.
        int startOffset = prevSibling.getTextRange().getStartOffset();
        int endOffset = node.getTextRange().getEndOffset();
        // Add the new folding descriptor.
        descriptors.add(new NamedFoldingDescriptor(node, startOffset, endOffset, null, "{...}"));
    }
}
Also used : PsiComment(com.intellij.psi.PsiComment) NamedFoldingDescriptor(com.intellij.lang.folding.NamedFoldingDescriptor) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 65 with PsiComment

use of com.intellij.psi.PsiComment in project Perl5-IDEA by Camelcade.

the class PerlTemplateContextType method isInContext.

@Override
public boolean isInContext(@NotNull PsiFile psiFile, int fileOffset) {
    if (!PsiUtilCore.getLanguageAtOffset(psiFile, fileOffset).isKindOf(PerlLanguage.INSTANCE)) {
        return false;
    }
    PsiElement element = psiFile.findElementAt(fileOffset);
    if (element == null) {
        element = psiFile.findElementAt(fileOffset - 1);
    }
    if (element == null) {
        return false;
    }
    IElementType tokenType = PsiUtilCore.getElementType(element);
    return !(element instanceof PsiWhiteSpace || element instanceof PerlStringContentElementImpl || element instanceof PsiComment || tokenType == PerlElementTypes.REGEX_TOKEN) && isInContext(element);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiComment(com.intellij.psi.PsiComment) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) PerlStringContentElementImpl(com.perl5.lang.perl.psi.impl.PerlStringContentElementImpl)

Aggregations

PsiComment (com.intellij.psi.PsiComment)68 PsiElement (com.intellij.psi.PsiElement)47 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)21 Nullable (org.jetbrains.annotations.Nullable)19 IElementType (com.intellij.psi.tree.IElementType)11 ArrayList (java.util.ArrayList)11 TextRange (com.intellij.openapi.util.TextRange)9 PsiFile (com.intellij.psi.PsiFile)8 NotNull (org.jetbrains.annotations.NotNull)8 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)7 ASTNode (com.intellij.lang.ASTNode)6 Matcher (java.util.regex.Matcher)6 Document (com.intellij.openapi.editor.Document)4 LineRange (com.intellij.codeInsight.editorActions.moveUpDown.LineRange)3 PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)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