Search in sources :

Example 26 with PsiComment

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

the class PsiCommentManipulator method handleContentChange.

@Override
public PsiComment handleContentChange(@NotNull PsiComment psiComment, @NotNull TextRange range, String newContent) throws IncorrectOperationException {
    String oldText = psiComment.getText();
    String newText = oldText.substring(0, range.getStartOffset()) + newContent + oldText.substring(range.getEndOffset());
    FileType type = psiComment.getContainingFile().getFileType();
    PsiFile fromText = PsiFileFactory.getInstance(psiComment.getProject()).createFileFromText("__." + type.getDefaultExtension(), type, newText);
    PsiComment newElement = PsiTreeUtil.getParentOfType(fromText.findElementAt(0), psiComment.getClass(), false);
    assert newElement != null : type + " " + type.getDefaultExtension() + " " + newText;
    return (PsiComment) psiComment.replace(newElement);
}
Also used : PsiComment(com.intellij.psi.PsiComment) FileType(com.intellij.openapi.fileTypes.FileType) PsiFile(com.intellij.psi.PsiFile)

Example 27 with PsiComment

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

the class DartServerFindUsagesHandler method getUsageInfo.

@Nullable
public static UsageInfo getUsageInfo(@NotNull final PsiElement usageElement, @NotNull final TextRange range, final boolean potentialUsage) {
    final int offset = range.getStartOffset() - usageElement.getTextRange().getStartOffset();
    boolean nonCodeUsage = usageElement instanceof PsiComment || usageElement.getParent() instanceof PsiComment;
    final UsageInfo usageInfo = new UsageInfo(usageElement, offset, offset + range.getLength(), nonCodeUsage);
    usageInfo.setDynamicUsage(potentialUsage);
    return usageInfo;
}
Also used : PsiComment(com.intellij.psi.PsiComment) UsageInfo(com.intellij.usageView.UsageInfo) Nullable(org.jetbrains.annotations.Nullable)

Example 28 with PsiComment

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

the class DartFoldingBuilder method foldComments.

private static void foldComments(@NotNull final List<FoldingDescriptor> descriptors, @NotNull final Collection<PsiElement> psiElements, @Nullable final TextRange fileHeaderRange) {
    PsiElement psiElement;
    for (Iterator<PsiElement> iter = psiElements.iterator(); iter.hasNext(); ) {
        psiElement = iter.next();
        if (!(psiElement instanceof PsiComment)) {
            continue;
        }
        if (fileHeaderRange != null && fileHeaderRange.intersects(psiElement.getTextRange())) {
            continue;
        }
        final IElementType elementType = psiElement.getNode().getElementType();
        if ((elementType == DartTokenTypesSets.MULTI_LINE_DOC_COMMENT || elementType == DartTokenTypesSets.MULTI_LINE_COMMENT) && !isCustomRegionElement(psiElement)) {
            descriptors.add(new FoldingDescriptor(psiElement, psiElement.getTextRange()));
        } else if (elementType == DartTokenTypesSets.SINGLE_LINE_DOC_COMMENT || elementType == DartTokenTypesSets.SINGLE_LINE_COMMENT) {
            final PsiElement firstCommentInSequence = psiElement;
            PsiElement lastCommentInSequence = firstCommentInSequence;
            PsiElement nextElement = firstCommentInSequence;
            boolean containsCustomRegionMarker = isCustomRegionElement(nextElement);
            while (iter.hasNext() && (nextElement = nextElement.getNextSibling()) != null && (nextElement instanceof PsiWhiteSpace || nextElement.getNode().getElementType() == elementType)) {
                if (nextElement.getNode().getElementType() == elementType) {
                    // advance iterator to skip processed comments sequence
                    iter.next();
                    lastCommentInSequence = nextElement;
                    containsCustomRegionMarker |= isCustomRegionElement(nextElement);
                }
            }
            if (lastCommentInSequence != firstCommentInSequence && !containsCustomRegionMarker) {
                final TextRange range = TextRange.create(firstCommentInSequence.getTextRange().getStartOffset(), lastCommentInSequence.getTextRange().getEndOffset());
                descriptors.add(new FoldingDescriptor(firstCommentInSequence, range));
            }
        }
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiComment(com.intellij.psi.PsiComment) FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) TextRange(com.intellij.openapi.util.TextRange) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 29 with PsiComment

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

the class GherkinSuppressionUtil method getSuppressionComment.

@Nullable
private static PsiComment getSuppressionComment(@NotNull String toolId, @NotNull PsiElement element) {
    final PsiElement comment = PsiTreeUtil.skipSiblingsBackward(element, PsiWhiteSpace.class);
    if (comment instanceof PsiComment) {
        String text = comment.getText();
        Matcher matcher = SUPPRESS_IN_LINE_COMMENT_PATTERN.matcher(text);
        if (matcher.matches() && SuppressionUtil.isInspectionToolIdMentioned(matcher.group(1), toolId)) {
            return (PsiComment) comment;
        }
    }
    return null;
}
Also used : PsiComment(com.intellij.psi.PsiComment) Matcher(java.util.regex.Matcher) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 30 with PsiComment

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

the class GherkinSuppressionUtil method getSuppressedIn.

@Nullable
private static PsiComment getSuppressedIn(@NotNull PsiElement place, @NotNull String toolId) {
    // find suppression holder with suppression comment about given inspection tool
    PsiElement suppressionHolder = PsiTreeUtil.getNonStrictParentOfType(place, GherkinSuppressionHolder.class);
    while (suppressionHolder != null) {
        final PsiComment suppressionHolderElement = getSuppressionComment(toolId, suppressionHolder);
        if (suppressionHolderElement != null) {
            return suppressionHolderElement;
        }
        suppressionHolder = PsiTreeUtil.getParentOfType(suppressionHolder, GherkinSuppressionHolder.class);
    }
    return null;
}
Also used : PsiComment(com.intellij.psi.PsiComment) GherkinSuppressionHolder(org.jetbrains.plugins.cucumber.psi.GherkinSuppressionHolder) 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