Search in sources :

Example 11 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 12 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 13 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 14 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 15 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)

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