Search in sources :

Example 1 with PsiDocComment

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

the class CStyleCommentPredicate 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();
    if (!GroovyTokenTypes.mML_COMMENT.equals(type)) {
        return false;
    }
    final PsiElement sibling = PsiTreeUtil.nextLeaf(comment);
    if (sibling == null) {
        return true;
    }
    if (!(isWhitespace(sibling))) {
        return false;
    }
    final String whitespaceText = sibling.getText();
    return whitespaceText.indexOf((int) '\n') >= 0 || whitespaceText.indexOf((int) '\r') >= 0;
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) IElementType(com.intellij.psi.tree.IElementType) PsiComment(com.intellij.psi.PsiComment) PsiElement(com.intellij.psi.PsiElement)

Example 2 with PsiDocComment

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

the class GroovyGenerationInfo method adjustDocCommentIfExists.

private static void adjustDocCommentIfExists(PsiMember member) {
    final PsiElement child = member.getFirstChild();
    if (child instanceof PsiDocComment) {
        final Project project = member.getProject();
        final GrDocComment groovyDoc = GroovyPsiElementFactory.getInstance(project).createDocCommentFromText(child.getText());
        child.delete();
        CodeStyleManager.getInstance(project).reformat(member);
        member.getParent().addBefore(groovyDoc, member);
    }
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) Project(com.intellij.openapi.project.Project) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)

Example 3 with PsiDocComment

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

the class MoveGroovyClassHandler method doMoveClass.

@Override
public PsiClass doMoveClass(@NotNull PsiClass aClass, @NotNull PsiDirectory moveDestination) throws IncorrectOperationException {
    if (!aClass.getLanguage().equals(GroovyLanguage.INSTANCE))
        return null;
    PsiFile file = aClass.getContainingFile();
    if (!(file instanceof GroovyFile))
        return null;
    final PsiPackage newPackage = JavaDirectoryService.getInstance().getPackage(moveDestination);
    LOG.assertTrue(newPackage != null);
    PsiClass newClass = null;
    final String newPackageName = newPackage.getQualifiedName();
    if (aClass instanceof GroovyScriptClass) {
        final PsiClass[] classes = ((GroovyFile) file).getClasses();
        if (classes.length == 1) {
            if (!moveDestination.equals(file.getContainingDirectory())) {
                Project project = file.getProject();
                MoveFilesOrDirectoriesUtil.doMoveFile(file, moveDestination);
                DumbService.getInstance(project).completeJustSubmittedTasks();
                file = moveDestination.findFile(file.getName());
                assert file != null;
                ((PsiClassOwner) file).setPackageName(newPackageName);
            }
            return ((GroovyFile) file).getScriptClass();
        }
        //script class is moved the first from the file due to MoveClassOrPackageProcessor:88 (element sort)
        correctSelfReferences(aClass, newPackage);
        final GroovyFile newFile = generateNewScript((GroovyFile) file, newPackage);
        for (PsiElement child : file.getChildren()) {
            if (!(child instanceof GrTopStatement || child instanceof PsiComment))
                continue;
            if (child instanceof PsiClass || child instanceof GrImportStatement || child instanceof GrPackageDefinition)
                continue;
            if (child instanceof GrDocComment) {
                final GrDocCommentOwner owner = GrDocCommentUtil.findDocOwner((GrDocComment) child);
                if (owner instanceof PsiClass)
                    continue;
            }
            child.delete();
        }
        if (!moveDestination.equals(file.getContainingDirectory())) {
            moveDestination.add(newFile);
        //aClass.getManager().moveFile(newFile, moveDestination);
        }
        newClass = newFile.getClasses()[0];
        correctOldClassReferences(newClass, aClass);
    } else {
        if (!moveDestination.equals(file.getContainingDirectory()) && moveDestination.findFile(file.getName()) != null) {
            // moving second of two classes which were in the same file to a different directory (IDEADEV-3089)
            correctSelfReferences(aClass, newPackage);
            PsiFile newFile = moveDestination.findFile(file.getName());
            final FileASTNode fileNode = newFile.getNode();
            fileNode.addChild(Factory.createSingleLeafElement(GroovyTokenTypes.mNLS, "\n\n", 0, 2, null, aClass.getManager()));
            final PsiDocComment docComment = aClass.getDocComment();
            if (docComment != null) {
                newFile.add(docComment);
                fileNode.addChild(Factory.createSingleLeafElement(GroovyTokenTypes.mNLS, "\n", 0, 1, null, aClass.getManager()));
            }
            newClass = (GrTypeDefinition) newFile.add(aClass);
            correctOldClassReferences(newClass, aClass);
            aClass.delete();
        } else if (((GroovyFile) file).getClasses().length > 1) {
            correctSelfReferences(aClass, newPackage);
            Project project = aClass.getProject();
            PsiFileFactory fileFactory = PsiFileFactory.getInstance(project);
            GroovyFile newFile = (GroovyFile) moveDestination.add(fileFactory.createFileFromText(aClass.getName() + "." + GroovyFileType.DEFAULT_EXTENSION, GroovyLanguage.INSTANCE, "class XXX {}"));
            final PsiClass created = newFile.getClasses()[0];
            PsiDocComment docComment = aClass.getDocComment();
            if (docComment != null) {
                newFile.addBefore(docComment, created);
                docComment.delete();
            }
            newClass = (PsiClass) created.replace(aClass);
            setPackageDefinition((GroovyFile) file, newFile, newPackageName);
            correctOldClassReferences(newClass, aClass);
            aClass.delete();
        }
    }
    return newClass;
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) GrDocCommentOwner(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocCommentOwner) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) Project(com.intellij.openapi.project.Project) FileASTNode(com.intellij.lang.FileASTNode) GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GrTopStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement)

Example 4 with PsiDocComment

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

the class MoveGroovyMemberHandler method doMove.

@Override
@NotNull
public PsiMember doMove(@NotNull MoveMembersOptions options, @NotNull PsiMember member, PsiElement anchor, @NotNull PsiClass targetClass) {
    GroovyChangeContextUtil.encodeContextInfo(member);
    final PsiDocComment docComment;
    if (member instanceof PsiDocCommentOwner) {
        docComment = ((PsiDocCommentOwner) member).getDocComment();
    } else {
        docComment = null;
    }
    PsiMember moved;
    if (options.makeEnumConstant() && member instanceof GrVariable && EnumConstantsUtil.isSuitableForEnumConstant(((PsiVariable) member).getType(), targetClass)) {
        final GrEnumConstant prototype = createEnumConstant(member.getName(), ((GrVariable) member).getInitializerGroovy(), member.getProject());
        moved = (PsiMember) addEnumConstant(targetClass, prototype, anchor);
        member.delete();
    } else if (member instanceof GrEnumConstant) {
        moved = (PsiMember) addEnumConstant(targetClass, (GrEnumConstant) member, null);
    } else if (member instanceof GrField) {
        if (anchor != null)
            anchor = anchor.getParent();
        final GrVariableDeclaration parent = (GrVariableDeclaration) member.getParent();
        GrVariableDeclaration movedDeclaration = (GrVariableDeclaration) targetClass.addAfter(parent, anchor);
        int number = ArrayUtil.find(parent.getMembers(), member);
        final GrMember[] members = movedDeclaration.getMembers();
        for (int i = 0; i < number; i++) {
            members[i].delete();
        }
        for (int i = number + 1; i < members.length; i++) {
            members[i].delete();
        }
        if (member.getContainingClass().isInterface() && !targetClass.isInterface()) {
            //might need to make modifiers explicit, see IDEADEV-11416
            final PsiModifierList list = movedDeclaration.getModifierList();
            VisibilityUtil.setVisibility(list, VisibilityUtil.getVisibilityModifier(member.getModifierList()));
            list.setModifierProperty(PsiModifier.STATIC, member.hasModifierProperty(PsiModifier.STATIC));
            list.setModifierProperty(PsiModifier.FINAL, member.hasModifierProperty(PsiModifier.FINAL));
        }
        moved = movedDeclaration.getMembers()[0];
    } else if (member instanceof GrMethod) {
        moved = (PsiMember) targetClass.addAfter(member, anchor);
        if (member.getContainingClass().isInterface() && !targetClass.isInterface()) {
            //might need to make modifiers explicit, see IDEADEV-11416
            final PsiModifierList list = moved.getModifierList();
            assert list != null;
            list.setModifierProperty(PsiModifier.STATIC, member.hasModifierProperty(PsiModifier.STATIC));
            list.setModifierProperty(PsiModifier.FINAL, member.hasModifierProperty(PsiModifier.FINAL));
            VisibilityUtil.setVisibility(list, VisibilityUtil.getVisibilityModifier(member.getModifierList()));
        }
    } else {
        moved = (PsiMember) targetClass.addAfter(member, anchor);
    }
    if (docComment != null) {
        PsiElement insertedDocComment = targetClass.addBefore(docComment, moved);
        PsiElement prevSibling = insertedDocComment.getPrevSibling();
        addLineFeedIfNeeded(prevSibling);
        docComment.delete();
    }
    member.delete();
    return moved;
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GrEnumConstant(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with PsiDocComment

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

the class JavaPushDownDelegate method pushDownToClass.

@Override
public void pushDownToClass(PsiElement targetElement, PushDownData<MemberInfo, PsiMember> pushDownData) {
    final PsiElementFactory factory = JavaPsiFacade.getInstance(pushDownData.getSourceClass().getProject()).getElementFactory();
    final PsiClass targetClass = targetElement instanceof PsiClass ? (PsiClass) targetElement : null;
    if (targetClass == null) {
        return;
    }
    final PsiClass sourceClass = (PsiClass) pushDownData.getSourceClass();
    final PsiSubstitutor substitutor = TypeConversionUtil.getSuperClassSubstitutor(sourceClass, targetClass, PsiSubstitutor.EMPTY);
    for (MemberInfoBase<? extends PsiElement> memberInfo : pushDownData.getMembersToMove()) {
        PsiMember member = (PsiMember) memberInfo.getMember();
        final List<PsiReference> refsToRebind = new ArrayList<>();
        final PsiModifierList list = member.getModifierList();
        LOG.assertTrue(list != null);
        if (list.hasModifierProperty(PsiModifier.STATIC)) {
            for (final PsiReference reference : ReferencesSearch.search(member)) {
                final PsiElement element = reference.getElement();
                if (element instanceof PsiReferenceExpression) {
                    final PsiExpression qualifierExpression = ((PsiReferenceExpression) element).getQualifierExpression();
                    if (qualifierExpression instanceof PsiReferenceExpression && !(((PsiReferenceExpression) qualifierExpression).resolve() instanceof PsiClass)) {
                        continue;
                    }
                }
                refsToRebind.add(reference);
            }
        }
        member = (PsiMember) member.copy();
        RefactoringUtil.replaceMovedMemberTypeParameters(member, PsiUtil.typeParametersIterable(sourceClass), substitutor, factory);
        PsiMember newMember = null;
        if (member instanceof PsiField) {
            ((PsiField) member).normalizeDeclaration();
            if (sourceClass.isInterface() && !targetClass.isInterface()) {
                PsiUtil.setModifierProperty(member, PsiModifier.PUBLIC, true);
                PsiUtil.setModifierProperty(member, PsiModifier.STATIC, true);
                PsiUtil.setModifierProperty(member, PsiModifier.FINAL, true);
            }
            newMember = (PsiMember) targetClass.add(member);
        } else if (member instanceof PsiMethod) {
            PsiMethod method = (PsiMethod) member;
            PsiMethod methodBySignature = MethodSignatureUtil.findMethodBySuperSignature(targetClass, method.getSignature(substitutor), false);
            if (methodBySignature == null) {
                newMember = (PsiMethod) targetClass.add(method);
                final PsiMethod oldMethod = (PsiMethod) memberInfo.getMember();
                if (sourceClass.isInterface() && !targetClass.isInterface()) {
                    PsiUtil.setModifierProperty(newMember, PsiModifier.PUBLIC, true);
                    if (oldMethod.hasModifierProperty(PsiModifier.ABSTRACT)) {
                        RefactoringUtil.makeMethodAbstract(targetClass, (PsiMethod) newMember);
                    } else {
                        PsiUtil.setModifierProperty(newMember, PsiModifier.DEFAULT, false);
                    }
                }
                if (memberInfo.isToAbstract()) {
                    if (newMember.hasModifierProperty(PsiModifier.PRIVATE)) {
                        PsiUtil.setModifierProperty(newMember, PsiModifier.PROTECTED, true);
                    }
                    pushDownData.getCommentPolicy().processNewJavaDoc(((PsiMethod) newMember).getDocComment());
                    OverrideImplementUtil.annotateOnOverrideImplement((PsiMethod) newMember, targetClass, (PsiMethod) memberInfo.getMember());
                }
            } else {
                //abstract method: remove @Override
                if (!memberInfo.isToAbstract()) {
                    final PsiAnnotation annotation = AnnotationUtil.findAnnotation(methodBySignature, "java.lang.Override");
                    if (annotation != null && !leaveOverrideAnnotation(sourceClass, substitutor, method)) {
                        annotation.delete();
                    }
                }
                final PsiDocComment oldDocComment = method.getDocComment();
                if (oldDocComment != null) {
                    final PsiDocComment docComment = methodBySignature.getDocComment();
                    final int policy = pushDownData.getCommentPolicy().getJavaDocPolicy();
                    if (policy == DocCommentPolicy.COPY || policy == DocCommentPolicy.MOVE) {
                        if (docComment != null) {
                            docComment.replace(oldDocComment);
                        } else {
                            methodBySignature.getParent().addBefore(oldDocComment, methodBySignature);
                        }
                    }
                }
            }
        } else if (member instanceof PsiClass) {
            if (sourceClass.isInterface() && !targetClass.isInterface()) {
                PsiUtil.setModifierProperty(member, PsiModifier.PUBLIC, true);
            }
            if (Boolean.FALSE.equals(memberInfo.getOverrides())) {
                final PsiClass psiClass = (PsiClass) memberInfo.getMember();
                PsiClassType classType = null;
                if (!targetClass.isInheritor(psiClass, false)) {
                    final PsiClassType[] types = ((MemberInfo) memberInfo).getSourceReferenceList().getReferencedTypes();
                    for (PsiClassType type : types) {
                        if (type.resolve() == psiClass) {
                            classType = (PsiClassType) substitutor.substitute(type);
                        }
                    }
                    PsiJavaCodeReferenceElement classRef = classType != null ? factory.createReferenceElementByType(classType) : factory.createClassReferenceElement(psiClass);
                    if (psiClass.isInterface()) {
                        targetClass.getImplementsList().add(classRef);
                    } else {
                        targetClass.getExtendsList().add(classRef);
                    }
                }
            } else {
                newMember = (PsiMember) targetClass.add(member);
            }
        }
        if (newMember != null) {
            decodeRefs(sourceClass, newMember, targetClass);
            //rebind imports first
            Collections.sort(refsToRebind, Comparator.comparing(PsiReference::getElement, PsiUtil.BY_POSITION));
            for (PsiReference psiReference : refsToRebind) {
                JavaCodeStyleManager.getInstance(sourceClass.getProject()).shortenClassReferences(psiReference.bindToElement(newMember));
            }
            final JavaRefactoringListenerManager listenerManager = JavaRefactoringListenerManager.getInstance(newMember.getProject());
            ((JavaRefactoringListenerManagerImpl) listenerManager).fireMemberMoved(sourceClass, newMember);
        }
    }
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) JavaRefactoringListenerManager(com.intellij.refactoring.listeners.JavaRefactoringListenerManager) JavaRefactoringListenerManagerImpl(com.intellij.refactoring.listeners.impl.JavaRefactoringListenerManagerImpl)

Aggregations

PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)56 PsiDocTag (com.intellij.psi.javadoc.PsiDocTag)15 Project (com.intellij.openapi.project.Project)9 PsiElement (com.intellij.psi.PsiElement)8 IElementType (com.intellij.psi.tree.IElementType)7 TextRange (com.intellij.openapi.util.TextRange)6 ArrayList (java.util.ArrayList)5 NotNull (org.jetbrains.annotations.NotNull)5 ASTNode (com.intellij.lang.ASTNode)4 CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 PsiComment (com.intellij.psi.PsiComment)3 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)3 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)2 Module (com.intellij.openapi.module.Module)2 PsiDocCommentOwner (com.intellij.psi.PsiDocCommentOwner)2 PsiDocParamRef (com.intellij.psi.impl.source.javadoc.PsiDocParamRef)2 HashMap (com.intellij.util.containers.HashMap)2