Search in sources :

Example 16 with GroovyPsiElement

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

the class ArgumentListGenerator method generate.

public void generate(@Nullable GrClosureSignature signature, @NotNull GrExpression[] exprs, @NotNull GrNamedArgument[] namedArgs, @NotNull GrClosableBlock[] clArgs, @NotNull GroovyPsiElement context) {
    GrClosureSignatureUtil.ArgInfo<PsiElement>[] argInfos = signature == null ? null : GrClosureSignatureUtil.mapParametersToArguments(signature, namedArgs, exprs, clArgs, context, false, false);
    if (argInfos == null && signature != null) {
        argInfos = GrClosureSignatureUtil.mapParametersToArguments(signature, namedArgs, exprs, clArgs, context, true, true);
    }
    final PsiSubstitutor substitutor = signature == null ? PsiSubstitutor.EMPTY : signature.getSubstitutor();
    if (argInfos == null || NullUtils.hasNull(argInfos)) {
        generateSimple(exprs, namedArgs, clArgs, context, substitutor);
        return;
    }
    final GrClosureParameter[] params = signature.getParameters();
    final Project project = context.getProject();
    myBuilder.append('(');
    boolean hasCommaAtEnd = false;
    for (int i = 0; i < argInfos.length; i++) {
        GrClosureSignatureUtil.ArgInfo<PsiElement> arg = argInfos[i];
        if (arg == null)
            continue;
        final GrClosureParameter param = params[i];
        boolean generated = arg.isMultiArg ? generateMultiArg(arg, param, substitutor, project, context) : generateSingeArg(arg, param);
        if (generated) {
            hasCommaAtEnd = true;
            myBuilder.append(", ");
        }
    }
    if (hasCommaAtEnd) {
        myBuilder.delete(myBuilder.length() - 2, myBuilder.length());
    //myBuilder.removeFromTheEnd(2);
    }
    myBuilder.append(')');
}
Also used : GrClosureSignatureUtil(org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil) Project(com.intellij.openapi.project.Project) GrClosureParameter(org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 17 with GroovyPsiElement

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

the class ClassItemGeneratorImpl method writeMethod.

@Override
public void writeMethod(StringBuilder builder, PsiMethod method) {
    if (method == null)
        return;
    GenerationUtil.writeDocComment(builder, method, true);
    String name = method.getName();
    boolean isAbstract = method.hasModifierProperty(PsiModifier.ABSTRACT);
    PsiModifierList modifierList = method.getModifierList();
    final PsiClass containingClass = method.getContainingClass();
    if (method.isConstructor() && containingClass != null && containingClass.isEnum()) {
        ModifierListGenerator.writeModifiers(builder, modifierList, ModifierListGenerator.ENUM_CONSTRUCTOR_MODIFIERS);
    } else {
        ModifierListGenerator.writeModifiers(builder, modifierList);
    }
    if (method.hasTypeParameters()) {
        GenerationUtil.writeTypeParameters(builder, method, classNameProvider);
        builder.append(' ');
    }
    //append return type
    if (!method.isConstructor()) {
        PsiType retType = context.typeProvider.getReturnType(method);
        TypeWriter.writeType(builder, retType, method, classNameProvider);
        builder.append(' ');
    }
    builder.append(name);
    if (method instanceof GroovyPsiElement) {
        context.searchForLocalVarsToWrap((GroovyPsiElement) method);
    }
    GenerationUtil.writeParameterList(builder, method.getParameterList().getParameters(), classNameProvider, context);
    if (method instanceof GrAnnotationMethod) {
        GrAnnotationMemberValue defaultValue = ((GrAnnotationMethod) method).getDefaultValue();
        if (defaultValue != null) {
            builder.append("default ");
            defaultValue.accept(new AnnotationGenerator(builder, context));
        }
    }
    GenerationUtil.writeThrowsList(builder, method.getThrowsList(), getMethodExceptions(method), classNameProvider);
    if (!isAbstract) {
        /* ************ body ********* */
        if (method instanceof GrMethod) {
            if (method instanceof GrReflectedMethod && ((GrReflectedMethod) method).getSkippedParameters().length > 0) {
                builder.append("{\n").append(generateDelegateCall((GrReflectedMethod) method)).append("\n}\n");
            } else {
                new CodeBlockGenerator(builder, context.extend()).generateMethodBody((GrMethod) method);
            }
        } else if (method instanceof GrAccessorMethod) {
            writeAccessorBody(builder, method);
        } else if (method instanceof LightMethodBuilder && containingClass instanceof GroovyScriptClass) {
            if ("main".equals(method.getName())) {
                writeMainScriptMethodBody(builder, method);
            } else if ("run".equals(method.getName())) {
                writeRunScriptMethodBody(builder, method);
            }
        } else {
            builder.append("{//todo\n}");
        }
    } else {
        builder.append(';');
    }
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) LightMethodBuilder(com.intellij.psi.impl.light.LightMethodBuilder) GrAnnotationMemberValue(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue) GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass)

Example 18 with GroovyPsiElement

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

the class SetterWriter method write.

public void write() {
    final boolean isStatic = mySetter.hasModifierProperty(PsiModifier.STATIC);
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myContext.project);
    PsiParameter[] parameters = mySetter.getParameterList().getParameters();
    PsiParameter parameter = parameters[parameters.length - 1];
    final PsiType parameterType = myContext.typeProvider.getParameterType(parameter);
    myBuffer.append("private static ");
    processTypeParameters(parameterType);
    myBuffer.append(myName);
    if (!(parameterType instanceof PsiPrimitiveType)) {
        parameter = factory.createParameter(parameter.getName(), "Value", null);
    }
    PsiParameter[] actual = inferActualParameters(isStatic, parameters, parameter);
    final GroovyPsiElement place = createStubMethod(actual);
    GenerationUtil.writeParameterList(myBuffer, actual, myClassNameProvider, myContext);
    writeBody(isStatic, parameters, parameter, place);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 19 with GroovyPsiElement

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

the class ClosureGenerator method getOwner.

@NonNls
@NotNull
private CharSequence getOwner(@NotNull GrClosableBlock closure) {
    final GroovyPsiElement context = PsiTreeUtil.getParentOfType(closure, GrMember.class, GroovyFile.class);
    LOG.assertTrue(context != null);
    final PsiClass contextClass;
    if (context instanceof GroovyFile) {
        contextClass = ((GroovyFile) context).getScriptClass();
    } else if (context instanceof PsiClass) {
        contextClass = (PsiClass) context;
    } else if (context instanceof GrMember) {
        if (((GrMember) context).hasModifierProperty(PsiModifier.STATIC)) {
            //no context class
            contextClass = null;
        } else {
            contextClass = ((GrMember) context).getContainingClass();
        }
    } else {
        contextClass = null;
    }
    if (contextClass == null)
        return "null";
    final PsiElement implicitClass = GenerationUtil.getWrappingImplicitClass(closure);
    if (implicitClass == null) {
        return "this";
    } else {
        final StringBuilder buffer = new StringBuilder();
        GenerationUtil.writeThisReference(contextClass, buffer, this.context);
        return buffer;
    }
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) NonNls(org.jetbrains.annotations.NonNls) NotNull(org.jetbrains.annotations.NotNull)

Example 20 with GroovyPsiElement

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

the class GenerationUtil method invokeMethodByResolveResult.

public static void invokeMethodByResolveResult(@Nullable GrExpression caller, @NotNull GroovyResolveResult resolveResult, @NotNull String methodName, @NotNull GrExpression[] exprs, @NotNull GrNamedArgument[] namedArgs, @NotNull GrClosableBlock[] closureArgs, @NotNull ExpressionGenerator expressionGenerator, @NotNull GroovyPsiElement psiContext) {
    final PsiElement resolved = resolveResult.getElement();
    if (resolved instanceof PsiMethod) {
        final PsiSubstitutor substitutor = resolveResult.getSubstitutor();
        expressionGenerator.invokeMethodOn(((PsiMethod) resolved), caller, exprs, namedArgs, closureArgs, substitutor, psiContext);
        return;
    }
    //other case
    final StringBuilder builder = expressionGenerator.getBuilder();
    final ExpressionContext expressionContext = expressionGenerator.getContext();
    if (caller != null) {
        caller.accept(expressionGenerator);
        builder.append('.');
    }
    builder.append(methodName);
    final ArgumentListGenerator argumentListGenerator = new ArgumentListGenerator(builder, expressionContext);
    argumentListGenerator.generate(null, exprs, namedArgs, closureArgs, psiContext);
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Aggregations

GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)98 PsiElement (com.intellij.psi.PsiElement)34 Nullable (org.jetbrains.annotations.Nullable)17 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)17 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)16 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)13 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)12 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)11 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)9 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)8 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)8 GrApplicationStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement)8 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)8 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)8 Project (com.intellij.openapi.project.Project)7 TextRange (com.intellij.openapi.util.TextRange)7 NotNull (org.jetbrains.annotations.NotNull)7 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)7 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)6