Search in sources :

Example 6 with GroovyScriptClass

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

the class GrIntroduceFieldHandlerBase method findPossibleScopes.

@NotNull
@Override
protected PsiClass[] findPossibleScopes(GrExpression expression, GrVariable variable, StringPartInfo partInfo, Editor editor) {
    PsiElement place = getCurrentPlace(expression, variable, partInfo);
    PsiClass aClass = PsiUtil.getContextClass(place);
    if (aClass instanceof GroovyScriptClass) {
        return new PsiClass[] { aClass };
    } else {
        List<PsiClass> result = ContainerUtil.newArrayList(aClass);
        while (aClass != null) {
            aClass = PsiTreeUtil.getParentOfType(aClass, PsiClass.class);
            ContainerUtil.addIfNotNull(result, aClass);
        }
        return result.toArray(new PsiClass[result.size()]);
    }
}
Also used : GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) PsiClass(com.intellij.psi.PsiClass) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with GroovyScriptClass

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

the class GrIntroduceFieldProcessor method initializeInMethod.

void initializeInMethod(@NotNull GrVariable field, @NotNull List<PsiElement> replaced) {
    final PsiElement _scope = myContext.getScope();
    final PsiElement scope = _scope instanceof GroovyScriptClass ? ((GroovyScriptClass) _scope).getContainingFile() : _scope;
    final PsiElement place = replaced.get(0);
    final GrMember member = GrIntroduceFieldHandler.getContainer(place, scope);
    GrStatementOwner container = member instanceof GrMethod ? ((GrMethod) member).getBlock() : member instanceof GrClassInitializer ? ((GrClassInitializer) member).getBlock() : place.getContainingFile() instanceof GroovyFile ? ((GroovyFile) place.getContainingFile()) : null;
    assert container != null;
    final PsiElement anchor;
    if (mySettings.removeLocalVar()) {
        GrVariable variable = myLocalVariable;
        anchor = PsiTreeUtil.getParentOfType(variable, GrStatement.class);
    } else {
        anchor = GrIntroduceHandlerBase.findAnchor(replaced.toArray(new PsiElement[replaced.size()]), container);
        GrIntroduceHandlerBase.assertStatement(anchor, myContext.getScope());
    }
    initializeInMethodInner(field, container, (GrStatement) anchor, replaced.get(0));
}
Also used : GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 8 with GroovyScriptClass

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

the class GrIntroduceFieldProcessor method insertField.

@NotNull
protected GrVariableDeclaration insertField(@NotNull PsiClass targetClass) {
    GrVariableDeclaration declaration = createField(targetClass);
    if (targetClass instanceof GrEnumTypeDefinition) {
        final GrEnumConstantList enumConstants = ((GrEnumTypeDefinition) targetClass).getEnumConstantList();
        return (GrVariableDeclaration) targetClass.addAfter(declaration, enumConstants);
    }
    if (targetClass instanceof GrTypeDefinition) {
        PsiElement anchor = getAnchorForDeclaration((GrTypeDefinition) targetClass);
        return (GrVariableDeclaration) targetClass.addAfter(declaration, anchor);
    } else {
        assert targetClass instanceof GroovyScriptClass;
        final GroovyFile file = ((GroovyScriptClass) targetClass).getContainingFile();
        PsiElement[] elements = file.getMethods();
        if (elements.length == 0)
            elements = file.getStatements();
        final PsiElement anchor = ArrayUtil.getFirstElement(elements);
        return (GrVariableDeclaration) file.addBefore(declaration, anchor);
    }
}
Also used : GrEnumConstantList(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstantList) GrEnumTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrEnumTypeDefinition) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with GroovyScriptClass

use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass 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 10 with GroovyScriptClass

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

the class ClassItemGeneratorImpl method collectMethods.

@Override
public Collection<PsiMethod> collectMethods(PsiClass typeDefinition) {
    List<PsiMethod> result = ContainerUtil.filter(typeDefinition.getMethods(), m -> !GroovyObjectTransformationSupport.isGroovyObjectSupportMethod(m));
    if (typeDefinition instanceof GroovyScriptClass) {
        final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(context.project);
        final String name = typeDefinition.getName();
        GrTypeDefinition tempClass = factory.createTypeDefinition("class " + name + " extends groovy.lang.Script {\n" + "  def " + name + "(groovy.lang.Binding binding){\n" + "    super(binding);\n" + "  }\n" + "  def " + name + "(){\n" + "    super();\n" + "  }\n" + "}");
        ContainerUtil.addAll(result, tempClass.getCodeConstructors());
    }
    return result;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)

Aggregations

GroovyScriptClass (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass)26 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)8 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)6 PsiElement (com.intellij.psi.PsiElement)4 NotNull (org.jetbrains.annotations.NotNull)4 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)3 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)3 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)3 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)3 GrMember (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember)3 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)3 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)3 PsiClass (com.intellij.psi.PsiClass)2 LightMethodBuilder (com.intellij.psi.impl.light.LightMethodBuilder)2 GrDocComment (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)2 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)2 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)2 GrPackageDefinition (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition)2 Template (com.intellij.codeInsight.template.Template)1 TemplateManager (com.intellij.codeInsight.template.TemplateManager)1