Search in sources :

Example 21 with PsiComment

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

the class GroovySuppressableInspectionTool method getElementToolSuppressedIn.

@Nullable
public static PsiElement getElementToolSuppressedIn(final PsiElement place, @NotNull String toolId) {
    if (place == null)
        return null;
    AccessToken accessToken = ApplicationManager.getApplication().acquireReadActionLock();
    try {
        final PsiElement statement = PsiUtil.findEnclosingStatement(place);
        if (statement != null) {
            PsiElement prev = statement.getPrevSibling();
            while (prev != null && StringUtil.isEmpty(prev.getText().trim())) {
                prev = prev.getPrevSibling();
            }
            if (prev instanceof PsiComment) {
                String text = prev.getText();
                Matcher matcher = SuppressionUtil.SUPPRESS_IN_LINE_COMMENT_PATTERN.matcher(text);
                if (matcher.matches() && SuppressionUtil.isInspectionToolIdMentioned(matcher.group(1), toolId)) {
                    return prev;
                }
            }
        }
        GrMember member = null;
        GrDocComment docComment = PsiTreeUtil.getParentOfType(place, GrDocComment.class);
        if (docComment != null) {
            GrDocCommentOwner owner = docComment.getOwner();
            if (owner instanceof GrMember) {
                member = (GrMember) owner;
            }
        }
        if (member == null) {
            member = PsiTreeUtil.getNonStrictParentOfType(place, GrMember.class);
        }
        while (member != null) {
            GrModifierList modifierList = member.getModifierList();
            for (String ids : getInspectionIdsSuppressedInAnnotation(modifierList)) {
                if (SuppressionUtil.isInspectionToolIdMentioned(ids, toolId)) {
                    return modifierList;
                }
            }
            member = PsiTreeUtil.getParentOfType(member, GrMember.class);
        }
        return null;
    } finally {
        accessToken.finish();
    }
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) PsiComment(com.intellij.psi.PsiComment) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) Matcher(java.util.regex.Matcher) AccessToken(com.intellij.openapi.application.AccessToken) GrDocCommentOwner(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocCommentOwner) PsiElement(com.intellij.psi.PsiElement) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) Nullable(org.jetbrains.annotations.Nullable)

Example 22 with PsiComment

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

the class AbstractSuppressByNoInspectionCommentFix method invoke.

@Override
public void invoke(@NotNull final Project project, @Nullable Editor editor, @NotNull final PsiElement element) throws IncorrectOperationException {
    PsiElement container = getContainer(element);
    if (container == null)
        return;
    final List<? extends PsiElement> comments = getCommentsFor(container);
    if (comments != null) {
        for (PsiElement comment : comments) {
            if (comment instanceof PsiComment && SuppressionUtil.isSuppressionComment(comment)) {
                replaceSuppressionComment(comment);
                return;
            }
        }
    }
    boolean caretWasBeforeStatement = editor != null && editor.getCaretModel().getOffset() == container.getTextRange().getStartOffset();
    try {
        createSuppression(project, element, container);
    } catch (IncorrectOperationException e) {
        if (!ApplicationManager.getApplication().isUnitTestMode() && editor != null) {
            Messages.showErrorDialog(editor.getComponent(), InspectionsBundle.message("suppress.inspection.annotation.syntax.error", e.getMessage()));
        }
    }
    if (caretWasBeforeStatement) {
        editor.getCaretModel().moveToOffset(container.getTextRange().getStartOffset());
    }
    UndoUtil.markPsiFileForUndo(element.getContainingFile());
}
Also used : PsiComment(com.intellij.psi.PsiComment) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiElement(com.intellij.psi.PsiElement)

Example 23 with PsiComment

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

the class TodoCommentInspection method checkFile.

@Nullable
@Override
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) {
    final TodoItem[] todoItems = PsiTodoSearchHelper.SERVICE.getInstance(file.getProject()).findTodoItems(file);
    final List<ProblemDescriptor> result = new ArrayList<>();
    final THashSet<PsiComment> comments = new THashSet<>();
    for (TodoItem todoItem : todoItems) {
        final PsiComment comment = PsiTreeUtil.getParentOfType(file.findElementAt(todoItem.getTextRange().getStartOffset()), PsiComment.class, false);
        if (comment != null && comments.add(comment)) {
            result.add(manager.createProblemDescriptor(comment, InspectionsBundle.message("todo.comment.problem.descriptor"), isOnTheFly, null, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
        }
    }
    return result.toArray(new ProblemDescriptor[result.size()]);
}
Also used : PsiComment(com.intellij.psi.PsiComment) TodoItem(com.intellij.psi.search.TodoItem) ArrayList(java.util.ArrayList) THashSet(gnu.trove.THashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 24 with PsiComment

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

the class DuplocatorUtil method isIgnoredNode.

public static boolean isIgnoredNode(PsiElement element) {
    if (element instanceof PsiWhiteSpace || element instanceof PsiErrorElement || element instanceof PsiComment) {
        return true;
    }
    if (!(element instanceof LeafElement)) {
        return false;
    }
    if (CharArrayUtil.containsOnlyWhiteSpaces(element.getText())) {
        return true;
    }
    EquivalenceDescriptorProvider descriptorProvider = EquivalenceDescriptorProvider.getInstance(element);
    if (descriptorProvider == null) {
        return false;
    }
    final IElementType elementType = ((LeafElement) element).getElementType();
    return descriptorProvider.getIgnoredTokens().contains(elementType);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiErrorElement(com.intellij.psi.PsiErrorElement) PsiComment(com.intellij.psi.PsiComment) EquivalenceDescriptorProvider(com.intellij.dupLocator.equivalence.EquivalenceDescriptorProvider) LeafElement(com.intellij.psi.impl.source.tree.LeafElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 25 with PsiComment

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

the class CommentBreakerEnterProcessor method doEnter.

@Override
public boolean doEnter(Editor editor, PsiElement psiElement, boolean isModified) {
    if (isModified)
        return false;
    final PsiElement atCaret = psiElement.getContainingFile().findElementAt(editor.getCaretModel().getOffset());
    final PsiComment comment = PsiTreeUtil.getParentOfType(atCaret, PsiComment.class, false);
    if (comment != null) {
        plainEnter(editor);
        if (comment.getTokenType() == JavaTokenType.END_OF_LINE_COMMENT) {
            EditorModificationUtil.insertStringAtCaret(editor, "// ");
        }
        return true;
    }
    return false;
}
Also used : PsiComment(com.intellij.psi.PsiComment) PsiElement(com.intellij.psi.PsiElement)

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