Search in sources :

Example 96 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.

the class GroovyShellLanguageConsoleView method generateClosure.

@NotNull
private GrClosableBlock generateClosure(@NotNull GrMethod method) {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
    StringBuilder buffer = new StringBuilder();
    buffer.append('{');
    GrParameter[] parameters = method.getParameters();
    for (GrParameter parameter : parameters) {
        buffer.append(parameter.getText());
        buffer.append(',');
    }
    if (parameters.length > 0)
        buffer.delete(buffer.length() - 1, buffer.length());
    buffer.append("->}");
    return factory.createClosureFromText(buffer.toString(), getFile());
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) NotNull(org.jetbrains.annotations.NotNull)

Example 97 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.

the class GrPullUpHelper method addClassToSupers.

private void addClassToSupers(MemberInfo info, PsiClass aClass, PsiSubstitutor substitutor, GrTypeDefinition targetSuperClass) {
    final PsiReferenceList sourceReferenceList = info.getSourceReferenceList();
    LOG.assertTrue(sourceReferenceList != null);
    PsiQualifiedReferenceElement ref = mySourceClass.equals(sourceReferenceList.getParent()) ? removeFromReferenceList(sourceReferenceList, aClass) : findReferenceToClass(sourceReferenceList, aClass);
    if (ref != null && !targetSuperClass.isInheritor(aClass, false)) {
        GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(myProject);
        replaceMovedMemberTypeParameters(ref, PsiUtil.typeParametersIterable(mySourceClass), substitutor, elementFactory);
        GrReferenceList referenceList;
        if (targetSuperClass.isInterface()) {
            referenceList = targetSuperClass.getExtendsClause();
            if (referenceList == null) {
                GrExtendsClause newClause = GroovyPsiElementFactory.getInstance(myProject).createExtendsClause();
                PsiElement anchor = targetSuperClass.getTypeParameterList() != null ? targetSuperClass.getTypeParameterList() : targetSuperClass.getNameIdentifierGroovy();
                referenceList = (GrReferenceList) targetSuperClass.addAfter(newClause, anchor);
                addSpacesAround(referenceList);
            }
        } else {
            referenceList = targetSuperClass.getImplementsClause();
            if (referenceList == null) {
                GrImplementsClause newClause = GroovyPsiElementFactory.getInstance(myProject).createImplementsClause();
                PsiElement anchor = targetSuperClass.getExtendsClause() != null ? targetSuperClass.getExtendsClause() : targetSuperClass.getTypeParameterList() != null ? targetSuperClass.getTypeParameterList() : targetSuperClass.getNameIdentifierGroovy();
                referenceList = (GrReferenceList) targetSuperClass.addAfter(newClause, anchor);
                addSpacesAround(referenceList);
            }
        }
        assert referenceList != null;
        referenceList.add(ref);
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrExtendsClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrExtendsClause) GrImplementsClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrImplementsClause) GrReferenceList(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrReferenceList) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 98 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.

the class GrPullUpHelper method fixReferencesToStatic.

private void fixReferencesToStatic(GroovyPsiElement classMember) throws IncorrectOperationException {
    final StaticReferencesCollector collector = new StaticReferencesCollector(myMembersToMove);
    classMember.accept(collector);
    ArrayList<GrReferenceElement> refs = collector.getReferences();
    ArrayList<PsiElement> members = collector.getReferees();
    ArrayList<PsiClass> classes = collector.getRefereeClasses();
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myProject);
    for (int i = 0; i < refs.size(); i++) {
        GrReferenceElement ref = refs.get(i);
        PsiElement namedElement = members.get(i);
        PsiClass aClass = classes.get(i);
        if (namedElement instanceof PsiNamedElement) {
            GrReferenceExpression newRef = (GrReferenceExpression) factory.createExpressionFromText("a." + ((PsiNamedElement) namedElement).getName(), null);
            GrExpression qualifier = newRef.getQualifierExpression();
            assert qualifier != null;
            qualifier = (GrExpression) qualifier.replace(factory.createReferenceExpressionFromText(aClass.getQualifiedName()));
            qualifier.putCopyableUserData(PRESERVE_QUALIFIER, ref.isQualified());
            PsiElement replaced = ref.replace(newRef);
            JavaCodeStyleManager.getInstance(myProject).shortenClassReferences(replaced);
        }
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 99 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.

the class GrPullUpHelper method doMoveMethod.

private void doMoveMethod(PsiSubstitutor substitutor, MemberInfo info) {
    GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(myProject);
    GrMethod method = (GrMethod) info.getMember();
    PsiMethod sibling = method;
    PsiMethod anchor = null;
    while (sibling != null) {
        sibling = PsiTreeUtil.getNextSiblingOfType(sibling, PsiMethod.class);
        if (sibling != null) {
            anchor = MethodSignatureUtil.findMethodInSuperClassBySignatureInDerived(method.getContainingClass(), myTargetSuperClass, sibling.getSignature(PsiSubstitutor.EMPTY), false);
            if (anchor != null) {
                break;
            }
        }
    }
    GrMethod methodCopy = (GrMethod) method.copy();
    if (method.findSuperMethods(myTargetSuperClass).length == 0) {
        deleteOverrideAnnotationIfFound(methodCopy);
    }
    final boolean isOriginalMethodAbstract = method.hasModifierProperty(PsiModifier.ABSTRACT) || method.hasModifierProperty(PsiModifier.DEFAULT);
    if (myTargetSuperClass.isInterface() || info.isToAbstract()) {
        GroovyChangeContextUtil.clearContextInfo(method);
        RefactoringUtil.makeMethodAbstract(myTargetSuperClass, methodCopy);
        if (myTargetSuperClass.isInterface()) {
            PsiUtil.setModifierProperty(methodCopy, PsiModifier.ABSTRACT, false);
        }
        replaceMovedMemberTypeParameters(methodCopy, PsiUtil.typeParametersIterable(mySourceClass), substitutor, elementFactory);
        final GrMethod movedElement = anchor != null ? (GrMethod) myTargetSuperClass.addBefore(methodCopy, anchor) : (GrMethod) myTargetSuperClass.add(methodCopy);
        CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(method.getProject());
        if (styleSettings.INSERT_OVERRIDE_ANNOTATION) {
            if (PsiUtil.isLanguageLevel5OrHigher(mySourceClass) && !myTargetSuperClass.isInterface() || PsiUtil.isLanguageLevel6OrHigher(mySourceClass)) {
                new AddAnnotationFix(CommonClassNames.JAVA_LANG_OVERRIDE, method).invoke(method.getProject(), null, mySourceClass.getContainingFile());
            }
        }
        GrDocComment oldDoc = method.getDocComment();
        if (oldDoc != null) {
            GrDocCommentUtil.setDocComment(movedElement, oldDoc);
        }
        myDocCommentPolicy.processCopiedJavaDoc(methodCopy.getDocComment(), oldDoc, isOriginalMethodAbstract);
        myMembersAfterMove.add(movedElement);
        if (isOriginalMethodAbstract) {
            deleteMemberWithDocComment(method);
        }
    } else {
        if (isOriginalMethodAbstract) {
            PsiUtil.setModifierProperty(myTargetSuperClass, PsiModifier.ABSTRACT, true);
        }
        fixReferencesToStatic(methodCopy);
        replaceMovedMemberTypeParameters(methodCopy, PsiUtil.typeParametersIterable(mySourceClass), substitutor, elementFactory);
        final PsiMethod superClassMethod = myTargetSuperClass.findMethodBySignature(methodCopy, false);
        Language language = myTargetSuperClass.getLanguage();
        final PsiMethod movedElement;
        if (superClassMethod != null && superClassMethod.hasModifierProperty(PsiModifier.ABSTRACT)) {
            movedElement = (PsiMethod) superClassMethod.replace(convertMethodToLanguage(methodCopy, language));
        } else {
            movedElement = anchor != null ? (PsiMethod) myTargetSuperClass.addBefore(convertMethodToLanguage(methodCopy, language), anchor) : (PsiMethod) myTargetSuperClass.add(convertMethodToLanguage(methodCopy, language));
            myMembersAfterMove.add(movedElement);
        }
        if (movedElement instanceof GrMethod) {
            GrDocCommentUtil.setDocComment((GrDocCommentOwner) movedElement, method.getDocComment());
        }
        deleteMemberWithDocComment(method);
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) Language(com.intellij.lang.Language) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) AddAnnotationFix(com.intellij.codeInsight.intention.AddAnnotationFix) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)

Example 100 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.

the class GroovyMoveClassToInnerHandler method moveClass.

@Override
public PsiClass moveClass(@NotNull PsiClass aClass, @NotNull PsiClass targetClass) {
    if (!(aClass instanceof GrTypeDefinition))
        return null;
    GroovyChangeContextUtil.encodeContextInfo(aClass);
    PsiDocComment doc = aClass.getDocComment();
    PsiElement brace = targetClass.getRBrace();
    PsiClass newClass = (PsiClass) targetClass.addBefore(aClass, brace);
    PsiElement sibling = newClass.getPrevSibling();
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(targetClass.getProject());
    if (!org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.isNewLine(sibling)) {
        targetClass.addBefore(factory.createLineTerminator("\n "), newClass);
    } else if (doc != null) {
        LOG.assertTrue(sibling != null);
        sibling.replace(factory.createLineTerminator(sibling.getText() + " "));
    }
    if (doc != null) {
        targetClass.addBefore(doc, newClass);
        targetClass.addBefore(factory.createLineTerminator("\n"), newClass);
    }
    if (targetClass.isInterface()) {
        PsiUtil.setModifierProperty(newClass, PsiModifier.PUBLIC, true);
    } else {
        PsiUtil.setModifierProperty(newClass, PsiModifier.STATIC, true);
    }
    GroovyChangeContextUtil.decodeContextInfo(newClass, null, null);
    return newClass;
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)

Aggregations

GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)159 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)42 PsiElement (com.intellij.psi.PsiElement)30 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)23 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)22 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)21 IncorrectOperationException (com.intellij.util.IncorrectOperationException)20 Nullable (org.jetbrains.annotations.Nullable)20 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)18 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)18 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)17 NotNull (org.jetbrains.annotations.NotNull)16 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)15 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)14 Project (com.intellij.openapi.project.Project)8 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)8 ASTNode (com.intellij.lang.ASTNode)7 ArrayList (java.util.ArrayList)7 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)6