Search in sources :

Example 46 with PsiComment

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

the class GroovyStatementMover method getElementToMove.

@Nullable
private static GroovyPsiElement getElementToMove(GroovyFileBase file, int offset) {
    offset = CharArrayUtil.shiftForward(file.getText(), offset, " \t");
    PsiElement element = file.findElementAt(offset);
    final GrDocComment docComment = PsiTreeUtil.getParentOfType(element, GrDocComment.class);
    if (docComment != null) {
        final GrDocCommentOwner owner = docComment.getOwner();
        if (owner != null) {
            element = owner;
        }
    }
    if (element instanceof PsiComment) {
        element = PsiTreeUtil.nextVisibleLeaf(element);
    }
    return (GroovyPsiElement) PsiTreeUtil.findFirstParent(element, element11 -> isMoveable(element11));
}
Also used : GrMembersDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMembersDeclaration) Document(com.intellij.openapi.editor.Document) GrCaseLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel) GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) ArrayList(java.util.ArrayList) PsiRecursiveElementVisitor(com.intellij.psi.PsiRecursiveElementVisitor) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrDocCommentOwner(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocCommentOwner) StringUtil(com.intellij.openapi.util.text.StringUtil) LineRange(com.intellij.codeInsight.editorActions.moveUpDown.LineRange) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) Editor(com.intellij.openapi.editor.Editor) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) StatementUpDownMover(com.intellij.codeInsight.editorActions.moveUpDown.StatementUpDownMover) PsiComment(com.intellij.psi.PsiComment) HandlerUtils(org.jetbrains.plugins.groovy.editor.HandlerUtils) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) NotNull(org.jetbrains.annotations.NotNull) CharArrayUtil(com.intellij.util.text.CharArrayUtil) PsiUtil(org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil) PsiComment(com.intellij.psi.PsiComment) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrDocCommentOwner(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocCommentOwner) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) Nullable(org.jetbrains.annotations.Nullable)

Example 47 with PsiComment

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

the class GrAnnotatorImpl method annotate.

@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    if (FileIndexFacade.getInstance(element.getProject()).isInLibrarySource(element.getContainingFile().getVirtualFile()))
        return;
    if (element instanceof GroovyPsiElement) {
        final GroovyAnnotator annotator = new GroovyAnnotator(holder);
        ((GroovyPsiElement) element).accept(annotator);
        if (PsiUtil.isCompileStatic(element)) {
            final GroovyStaticTypeCheckVisitor typeCheckVisitor = myTypeCheckVisitorThreadLocal.get();
            assert typeCheckVisitor != null;
            typeCheckVisitor.accept((GroovyPsiElement) element, holder);
        }
    } else if (element instanceof PsiComment) {
        String text = element.getText();
        if (text.startsWith("/*") && !(text.endsWith("*/"))) {
            TextRange range = element.getTextRange();
            holder.createErrorAnnotation(TextRange.create(range.getEndOffset() - 1, range.getEndOffset()), GroovyBundle.message("doc.end.expected"));
        }
    } else {
        final PsiElement parent = element.getParent();
        if (parent instanceof GrMethod) {
            if (element.equals(((GrMethod) parent).getNameIdentifierGroovy()) && ((GrMethod) parent).getReturnTypeElementGroovy() == null) {
                GroovyAnnotator.checkMethodReturnType((GrMethod) parent, element, holder);
            }
        } else if (parent instanceof GrField) {
            final GrField field = (GrField) parent;
            if (element.equals(field.getNameIdentifierGroovy())) {
                final GrAccessorMethod[] getters = field.getGetters();
                for (GrAccessorMethod getter : getters) {
                    GroovyAnnotator.checkMethodReturnType(getter, field.getNameIdentifierGroovy(), holder);
                }
                final GrAccessorMethod setter = field.getSetter();
                if (setter != null) {
                    GroovyAnnotator.checkMethodReturnType(setter, field.getNameIdentifierGroovy(), holder);
                }
            }
        }
    }
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) PsiComment(com.intellij.psi.PsiComment) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GroovyStaticTypeCheckVisitor(org.jetbrains.plugins.groovy.codeInspection.type.GroovyStaticTypeCheckVisitor) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 48 with PsiComment

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

the class ChangeToEndOfLineCommentIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final PsiComment comment = (PsiComment) element;
    final JavaPsiFacade manager = JavaPsiFacade.getInstance(comment.getProject());
    final PsiElement parent = comment.getParent();
    assert parent != null;
    final PsiElementFactory factory = manager.getElementFactory();
    final String commentText = comment.getText();
    final PsiElement whitespace = comment.getNextSibling();
    final String text = commentText.substring(2, commentText.length() - 2);
    final String[] lines = text.split("\n");
    for (int i = lines.length - 1; i >= 1; i--) {
        final PsiComment nextComment = factory.createCommentFromText("//" + lines[i].trim() + '\n', parent);
        parent.addAfter(nextComment, comment);
    /* if (whitespace != null) {
      final PsiElement newWhiteSpace =
          factory.createWhiteSpaceFromText(whitespace.getText());
      parent.addAfter(newWhiteSpace, comment);
    }  */
    }
    final PsiComment newComment = factory.createCommentFromText("//" + lines[0], parent);
    comment.replace(newComment);
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) PsiComment(com.intellij.psi.PsiComment) PsiElementFactory(com.intellij.psi.PsiElementFactory) PsiElement(com.intellij.psi.PsiElement)

Example 49 with PsiComment

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

the class EndOfLineCommentPredicate method satisfiedBy.

@Override
public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof PsiComment)) {
        return false;
    }
    if (element instanceof PsiDocComment) {
        return false;
    }
    final PsiComment comment = (PsiComment) element;
    final IElementType type = comment.getTokenType();
    return GroovyTokenTypes.mSL_COMMENT.equals(type);
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) IElementType(com.intellij.psi.tree.IElementType) PsiComment(com.intellij.psi.PsiComment)

Example 50 with PsiComment

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

the class ChangeToCStyleCommentIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final PsiComment selectedComment = (PsiComment) element;
    PsiComment firstComment = selectedComment;
    while (true) {
        final PsiElement prevComment = getPrevNonWhiteSpace(firstComment);
        if (!isEndOfLineComment(prevComment)) {
            break;
        }
        firstComment = (PsiComment) prevComment;
    }
    final JavaPsiFacade manager = JavaPsiFacade.getInstance(selectedComment.getProject());
    final PsiElementFactory factory = manager.getElementFactory();
    String text = getCommentContents(firstComment);
    final List<PsiElement> commentsToDelete = new ArrayList<>();
    PsiElement nextComment = firstComment;
    while (true) {
        nextComment = getNextNonWhiteSpace(nextComment);
        if (!isEndOfLineComment(nextComment)) {
            break;
        }
        text += //to get the whitespace for proper spacing
        nextComment.getPrevSibling().getText() + "  " + getCommentContents((PsiComment) nextComment);
        commentsToDelete.add(nextComment);
    }
    final PsiComment newComment = factory.createCommentFromText("/*" + text + " */", selectedComment.getParent());
    firstComment.replace(newComment);
    for (PsiElement commentToDelete : commentsToDelete) {
        commentToDelete.delete();
    }
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) PsiComment(com.intellij.psi.PsiComment) PsiElementFactory(com.intellij.psi.PsiElementFactory) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement)

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