Search in sources :

Example 36 with GroovyPsiElementFactory

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

the class GroovyIntroduceParameterMethodUsagesProcessor method processChangeMethodUsage.

@Override
public boolean processChangeMethodUsage(IntroduceParameterData data, UsageInfo usage, UsageInfo[] usages) throws IncorrectOperationException {
    GrCall callExpression = GroovyRefactoringUtil.getCallExpressionByMethodReference(usage.getElement());
    if (callExpression == null)
        return true;
    GrArgumentList argList = callExpression.getArgumentList();
    GrExpression[] oldArgs = argList.getExpressionArguments();
    final GrExpression anchor;
    if (!data.getMethodToSearchFor().isVarArgs()) {
        anchor = getLast(oldArgs);
    } else {
        final PsiParameter[] parameters = data.getMethodToSearchFor().getParameterList().getParameters();
        if (parameters.length > oldArgs.length) {
            anchor = getLast(oldArgs);
        } else {
            final int lastNonVararg = parameters.length - 2;
            anchor = lastNonVararg >= 0 ? oldArgs[lastNonVararg] : null;
        }
    }
    PsiMethod method = PsiTreeUtil.getParentOfType(argList, PsiMethod.class);
    GrClosureSignature signature = GrClosureSignatureUtil.createSignature(callExpression);
    if (signature == null)
        signature = GrClosureSignatureUtil.createSignature(data.getMethodToSearchFor(), PsiSubstitutor.EMPTY);
    final GrClosureSignatureUtil.ArgInfo<PsiElement>[] actualArgs = GrClosureSignatureUtil.mapParametersToArguments(signature, callExpression.getNamedArguments(), callExpression.getExpressionArguments(), callExpression.getClosureArguments(), callExpression, false, true);
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(data.getProject());
    if (method != null && IntroduceParameterUtil.isMethodInUsages(data, method, usages)) {
        argList.addAfter(factory.createExpressionFromText(data.getParameterName()), anchor);
    } else {
        final PsiElement _expr = data.getParameterInitializer().getExpression();
        PsiElement initializer = ExpressionConverter.getExpression(_expr, GroovyLanguage.INSTANCE, data.getProject());
        LOG.assertTrue(initializer instanceof GrExpression);
        GrExpression newArg = GroovyIntroduceParameterUtil.addClosureToCall(initializer, argList);
        if (newArg == null) {
            final PsiElement dummy = argList.addAfter(factory.createExpressionFromText("1"), anchor);
            newArg = ((GrExpression) dummy).replaceWithExpression((GrExpression) initializer, true);
        }
        final PsiMethod methodToReplaceIn = data.getMethodToReplaceIn();
        new OldReferencesResolver(callExpression, newArg, methodToReplaceIn, data.getReplaceFieldsWithGetters(), initializer, signature, actualArgs, methodToReplaceIn.getParameterList().getParameters()).resolve();
        ChangeContextUtil.clearContextInfo(initializer);
        //newArg can be replaced by OldReferenceResolver
        if (newArg.isValid()) {
            JavaCodeStyleManager.getInstance(newArg.getProject()).shortenClassReferences(newArg);
            CodeStyleManager.getInstance(data.getProject()).reformat(newArg);
        }
    }
    if (actualArgs == null) {
        removeParamsFromUnresolvedCall(callExpression, data);
    } else {
        removeParametersFromCall(actualArgs, data.getParametersToRemove());
    }
    if (argList.getAllArguments().length == 0 && PsiImplUtil.hasClosureArguments(callExpression)) {
        final GrArgumentList emptyArgList = ((GrMethodCallExpression) factory.createExpressionFromText("foo{}")).getArgumentList();
        argList.replace(emptyArgList);
    }
    return false;
}
Also used : GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)

Example 37 with GroovyPsiElementFactory

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

the class GrIntroduceLocalVariableProcessor method resolveLocalConflicts.

private static void resolveLocalConflicts(@NotNull PsiElement tempContainer, @NotNull String varName) {
    for (PsiElement child : tempContainer.getChildren()) {
        if (child instanceof GrReferenceExpression && !child.getText().contains(".")) {
            PsiReference psiReference = child.getReference();
            if (psiReference != null) {
                final PsiElement resolved = psiReference.resolve();
                if (resolved != null) {
                    String fieldName = getFieldName(resolved);
                    if (fieldName != null && varName.equals(fieldName)) {
                        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(tempContainer.getProject());
                        ((GrReferenceExpression) child).replaceWithExpression(factory.createExpressionFromText("this." + child.getText()), true);
                    }
                }
            }
        } else {
            resolveLocalConflicts(child, varName);
        }
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 38 with GroovyPsiElementFactory

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

the class GroovyOverrideImplementUtil method setupAnnotations.

private static void setupAnnotations(@NotNull GrTypeDefinition aClass, @NotNull PsiMethod method, @NotNull GrMethod result) {
    if (OverrideImplementUtil.isInsertOverride(method, aClass)) {
        result.getModifierList().addAnnotation(CommonClassNames.JAVA_LANG_OVERRIDE);
    }
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(method.getProject());
    final PsiParameter[] originalParams = method.getParameterList().getParameters();
    GrParameter[] parameters = result.getParameters();
    for (int i = 0; i < parameters.length; i++) {
        GrParameter parameter = parameters[i];
        PsiParameter original = originalParams[i];
        for (PsiAnnotation annotation : original.getModifierList().getAnnotations()) {
            final GrModifierList modifierList = parameter.getModifierList();
            String qname = annotation.getQualifiedName();
            if (qname != null && modifierList.findAnnotation(qname) == null) {
                if (annotation instanceof GrAnnotation) {
                    modifierList.add(annotation);
                } else {
                    modifierList.add(factory.createAnnotationFromText(annotation.getText()));
                }
            }
        }
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Example 39 with GroovyPsiElementFactory

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

the class TryCatchFinallySurrounder method doSurroundElements.

@Override
protected GroovyPsiElement doSurroundElements(PsiElement[] elements, PsiElement context) throws IncorrectOperationException {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(elements[0].getProject());
    GrTryCatchStatement tryStatement = (GrTryCatchStatement) factory.createStatementFromText("try {\n} catch(exception e){\n} finally{\n}", context);
    addStatements(tryStatement.getTryBlock(), elements);
    return tryStatement;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrTryCatchStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrTryCatchStatement)

Example 40 with GroovyPsiElementFactory

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

the class WhileSurrounder method doSurroundElements.

@Override
protected GroovyPsiElement doSurroundElements(PsiElement[] elements, PsiElement context) throws IncorrectOperationException {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(elements[0].getProject());
    GrWhileStatement whileStatement = (GrWhileStatement) factory.createStatementFromText("while(a){\n}", context);
    addStatements(((GrBlockStatement) whileStatement.getBody()).getBlock(), elements);
    return whileStatement;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrWhileStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrWhileStatement)

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