Search in sources :

Example 31 with GroovyPsiElementFactory

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

the class MoveGroovyMemberHandler method createEnumConstant.

private static GrEnumConstant createEnumConstant(String constantName, GrExpression initializerExpr, Project project) throws IncorrectOperationException {
    final GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(project);
    final String enumConstantText = initializerExpr != null ? constantName + "(" + initializerExpr.getText() + ")" : constantName;
    return elementFactory.createEnumConstantFromText(enumConstantText);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)

Example 32 with GroovyPsiElementFactory

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

the class RenameAliasImportedMethodProcessor method renameElement.

@Override
public void renameElement(PsiElement psiElement, String newName, UsageInfo[] usages, @Nullable RefactoringElementListener listener) throws IncorrectOperationException {
    boolean isGetter = GroovyPropertyUtils.isSimplePropertyGetter((PsiMethod) psiElement);
    boolean isSetter = GroovyPropertyUtils.isSimplePropertySetter((PsiMethod) psiElement);
    List<UsageInfo> methodAccess = new ArrayList<>(usages.length);
    List<UsageInfo> propertyAccess = new ArrayList<>(usages.length);
    for (UsageInfo usage : usages) {
        final PsiElement element = usage.getElement();
        if (element instanceof GrReferenceExpression && ((GrReferenceExpression) element).advancedResolve().isInvokedOnProperty()) {
            propertyAccess.add(usage);
        } else {
            methodAccess.add(usage);
        }
    }
    super.renameElement(psiElement, newName, methodAccess.toArray(new UsageInfo[methodAccess.size()]), listener);
    final String propertyName;
    if (isGetter) {
        propertyName = GroovyPropertyUtils.getPropertyNameByGetterName(newName, true);
    } else if (isSetter) {
        propertyName = GroovyPropertyUtils.getPropertyNameBySetterName(newName);
    } else {
        propertyName = null;
    }
    if (propertyName == null) {
        //it means accessor is renamed to not-accessor and we should replace all property-access-refs with method-access-refs
        final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(psiElement.getProject());
        for (UsageInfo info : propertyAccess) {
            final PsiElement element = info.getElement();
            if (element instanceof GrReferenceExpression) {
                final PsiElement qualifier = ((GrReferenceExpression) element).getQualifier();
                String qualifierPrefix = qualifier == null ? "" : qualifier.getText() + ".";
                if (isGetter) {
                    final GrExpression call = factory.createExpressionFromText(qualifierPrefix + newName + "()");
                    ((GrReferenceExpression) element).replaceWithExpression(call, true);
                } else {
                    final PsiElement parent = element.getParent();
                    assert parent instanceof GrAssignmentExpression;
                    final GrExpression rValue = ((GrAssignmentExpression) parent).getRValue();
                    final GrExpression call = factory.createExpressionFromText(qualifierPrefix + newName + "(" + (rValue == null ? "" : rValue.getText()) + ")");
                    ((GrAssignmentExpression) parent).replaceWithExpression(call, true);
                }
            }
        }
    } else {
        for (UsageInfo usage : propertyAccess) {
            final PsiReference ref = usage.getReference();
            if (ref != null) {
                ref.handleElementRename(propertyName);
            }
        }
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) UsageInfo(com.intellij.usageView.UsageInfo) UnresolvableCollisionUsageInfo(com.intellij.refactoring.rename.UnresolvableCollisionUsageInfo) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 33 with GroovyPsiElementFactory

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

the class GroovyIntroduceParameterUtil method processChangedMethodCall.

public static void processChangedMethodCall(PsiElement element, GrIntroduceParameterSettings settings, Project project) {
    if (!(element.getParent() instanceof GrMethodCallExpression)) {
        LOG.error(element.getParent());
        return;
    }
    GrMethodCallExpression methodCall = (GrMethodCallExpression) element.getParent();
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    final String name = settings.getName();
    LOG.assertTrue(name != null);
    GrExpression expression = factory.createExpressionFromText(name, null);
    final GrArgumentList argList = methodCall.getArgumentList();
    final PsiElement[] exprs = argList.getAllArguments();
    if (exprs.length > 0) {
        argList.addAfter(expression, exprs[exprs.length - 1]);
    } else {
        argList.add(expression);
    }
    removeParametersFromCall(methodCall, settings);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 34 with GroovyPsiElementFactory

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

the class GroovyIntroduceParameterUtil method generateDelegate.

public static GrMethod generateDelegate(PsiMethod prototype, IntroduceParameterData.ExpressionWrapper initializer, Project project) {
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    GrMethod result;
    if (prototype instanceof GrMethod) {
        result = (GrMethod) prototype.copy();
    } else {
        StringBuilder builder = new StringBuilder();
        builder.append(prototype.getModifierList().getText()).append(' ');
        if (prototype.getReturnTypeElement() != null) {
            builder.append(prototype.getReturnTypeElement().getText());
        }
        builder.append(' ').append(prototype.getName());
        builder.append(prototype.getParameterList().getText());
        builder.append("{}");
        result = factory.createMethodFromText(builder.toString());
    }
    StringBuilder call = new StringBuilder();
    call.append("def foo(){\n");
    final GrParameter[] parameters = result.getParameters();
    call.append(prototype.getName());
    if (initializer.getExpression() instanceof GrClosableBlock) {
        if (parameters.length > 0) {
            call.append('(');
            for (GrParameter parameter : parameters) {
                call.append(parameter.getName()).append(", ");
            }
            call.replace(call.length() - 2, call.length(), ")");
        }
        call.append(initializer.getText());
    } else {
        call.append('(');
        for (GrParameter parameter : parameters) {
            call.append(parameter.getName()).append(", ");
        }
        call.append(initializer.getText());
        call.append(")");
    }
    call.append("\n}");
    final GrOpenBlock block = factory.createMethodFromText(call.toString()).getBlock();
    result.getBlock().replace(block);
    final PsiElement parent = prototype.getParent();
    final GrMethod method = (GrMethod) parent.addBefore(result, prototype);
    JavaCodeStyleManager.getInstance(method.getProject()).shortenClassReferences(method);
    return method;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 35 with GroovyPsiElementFactory

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

the class GroovyIntroduceParameterMethodUsagesProcessor method addParameter.

@NotNull
public static GrParameter addParameter(@NotNull GrParametersOwner parametersOwner, @Nullable MethodJavaDocHelper javaDocHelper, @NotNull PsiType forcedType, @NotNull String parameterName, boolean isFinal, @NotNull Project project) {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    final String typeText = forcedType.equalsToText(CommonClassNames.JAVA_LANG_OBJECT) || forcedType == PsiType.NULL || PsiType.VOID.equals(forcedType) ? null : forcedType.getCanonicalText();
    GrParameter parameter = factory.createParameter(parameterName, typeText, parametersOwner);
    parameter.getModifierList().setModifierProperty(PsiModifier.FINAL, isFinal);
    final PsiParameter anchorParameter = getAnchorParameter(parametersOwner);
    final GrParameterList parameterList = parametersOwner.getParameterList();
    parameter = (GrParameter) parameterList.addAfter(parameter, anchorParameter);
    JavaCodeStyleManager.getInstance(project).shortenClassReferences(parameter);
    if (javaDocHelper != null) {
        final PsiDocTag tagForAnchorParameter = javaDocHelper.getTagForParameter(anchorParameter);
        javaDocHelper.addParameterAfter(parameterName, tagForAnchorParameter);
    }
    return parameter;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) NotNull(org.jetbrains.annotations.NotNull)

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