Search in sources :

Example 1 with GroovyGenerationInfo

use of org.jetbrains.plugins.groovy.actions.generate.GroovyGenerationInfo in project intellij-community by JetBrains.

the class GroovyGenerateMethodMissingHandler method generateMemberPrototypes.

@NotNull
@Override
protected List<? extends GenerationInfo> generateMemberPrototypes(PsiClass aClass, ClassMember[] members) throws IncorrectOperationException {
    final String templName = JavaTemplateUtil.TEMPLATE_FROM_USAGE_METHOD_BODY;
    final FileTemplate template = FileTemplateManager.getInstance(aClass.getProject()).getCodeTemplate(templName);
    final GrMethod method = genMethod(aClass, template);
    return method != null ? Collections.singletonList(new GroovyGenerationInfo<>(method, true)) : Collections.<GenerationInfo>emptyList();
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) GroovyGenerationInfo(org.jetbrains.plugins.groovy.actions.generate.GroovyGenerationInfo) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with GroovyGenerationInfo

use of org.jetbrains.plugins.groovy.actions.generate.GroovyGenerationInfo in project intellij-community by JetBrains.

the class GroovyGeneratePropertyMissingHandler method generateMemberPrototypes.

@NotNull
@Override
protected List<? extends GenerationInfo> generateMemberPrototypes(PsiClass aClass, ClassMember[] members) throws IncorrectOperationException {
    final String templName = JavaTemplateUtil.TEMPLATE_FROM_USAGE_METHOD_BODY;
    final FileTemplate template = FileTemplateManager.getInstance(aClass.getProject()).getCodeTemplate(templName);
    final GrMethod getter = genGetter(aClass, template);
    final GrMethod setter = genSetter(aClass, template);
    final ArrayList<GroovyGenerationInfo<GrMethod>> result = new ArrayList<>();
    if (getter != null)
        result.add(new GroovyGenerationInfo<>(getter, true));
    if (setter != null)
        result.add(new GroovyGenerationInfo<>(setter, true));
    return result;
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) GroovyGenerationInfo(org.jetbrains.plugins.groovy.actions.generate.GroovyGenerationInfo) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with GroovyGenerationInfo

use of org.jetbrains.plugins.groovy.actions.generate.GroovyGenerationInfo in project intellij-community by JetBrains.

the class GroovyGenerateConstructorHandler method generateMemberPrototypes.

@Override
@NotNull
protected List<? extends GenerationInfo> generateMemberPrototypes(PsiClass aClass, ClassMember[] members) throws IncorrectOperationException {
    final List<? extends GenerationInfo> list = super.generateMemberPrototypes(aClass, members);
    List<PsiGenerationInfo<GrMethod>> grConstructors = new ArrayList<>();
    final Project project = aClass.getProject();
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
    for (GenerationInfo generationInfo : list) {
        final PsiMember constructorMember = generationInfo.getPsiMember();
        assert constructorMember instanceof PsiMethod;
        final PsiMethod constructor = (PsiMethod) constructorMember;
        final PsiCodeBlock block = constructor.getBody();
        assert block != null;
        final String constructorName = aClass.getName();
        final String body = StringUtil.replace(StringUtil.replace(block.getText(), DEF_PSEUDO_ANNO, ""), ";", "");
        final PsiParameterList list1 = constructor.getParameterList();
        List<String> parametersNames = new ArrayList<>();
        List<String> parametersTypes = new ArrayList<>();
        for (PsiParameter parameter : list1.getParameters()) {
            final String fullName = parameter.getName();
            parametersNames.add(StringUtil.trimStart(fullName, DEF_PSEUDO_ANNO));
            parametersTypes.add(fullName.startsWith(DEF_PSEUDO_ANNO) ? null : parameter.getType().getCanonicalText());
        }
        final String[] paramNames = ArrayUtil.toStringArray(parametersNames);
        final String[] paramTypes = ArrayUtil.toStringArray(parametersTypes);
        assert constructorName != null;
        GrMethod grConstructor = factory.createConstructorFromText(constructorName, paramTypes, paramNames, body);
        PsiReferenceList throwsList = grConstructor.getThrowsList();
        for (PsiJavaCodeReferenceElement element : constructor.getThrowsList().getReferenceElements()) {
            throwsList.add(element);
        }
        codeStyleManager.shortenClassReferences(grConstructor);
        grConstructors.add(new GroovyGenerationInfo<>(grConstructor));
    }
    return grConstructors;
}
Also used : ArrayList(java.util.ArrayList) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) Project(com.intellij.openapi.project.Project) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) GroovyGenerationInfo(org.jetbrains.plugins.groovy.actions.generate.GroovyGenerationInfo) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with GroovyGenerationInfo

use of org.jetbrains.plugins.groovy.actions.generate.GroovyGenerationInfo in project intellij-community by JetBrains.

the class GroovyGenerateEqualsHandler method generateMemberPrototypes.

@Override
@NotNull
protected List<? extends GenerationInfo> generateMemberPrototypes(PsiClass aClass, ClassMember[] originalMembers) throws IncorrectOperationException {
    Project project = aClass.getProject();
    final boolean useInstanceofToCheckParameterType = CodeInsightSettings.getInstance().USE_INSTANCEOF_ON_EQUALS_PARAMETER;
    GroovyGenerateEqualsHelper helper = new GroovyGenerateEqualsHelper(project, aClass, myEqualsFields, myHashCodeFields, myNonNullFields, useInstanceofToCheckParameterType);
    Collection<PsiMethod> methods = helper.generateMembers();
    return ContainerUtil.map2List(methods, (Function<PsiMethod, PsiGenerationInfo<PsiMethod>>) s -> new GroovyGenerationInfo<>(s));
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException) GroovyCodeInsightBundle(org.jetbrains.plugins.groovy.actions.generate.GroovyCodeInsightBundle) GenerateEqualsWizard(com.intellij.codeInsight.generation.ui.GenerateEqualsWizard) PsiMethod(com.intellij.psi.PsiMethod) Collection(java.util.Collection) CodeInsightSettings(com.intellij.codeInsight.CodeInsightSettings) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Computable(com.intellij.openapi.util.Computable) ContainerUtil(com.intellij.util.containers.ContainerUtil) Nullable(org.jetbrains.annotations.Nullable) PsiClass(com.intellij.psi.PsiClass) List(java.util.List) com.intellij.codeInsight.generation(com.intellij.codeInsight.generation) Function(com.intellij.util.Function) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Project(com.intellij.openapi.project.Project) PsiField(com.intellij.psi.PsiField) Messages(com.intellij.openapi.ui.Messages) Logger(com.intellij.openapi.diagnostic.Logger) NotNull(org.jetbrains.annotations.NotNull) GroovyGenerationInfo(org.jetbrains.plugins.groovy.actions.generate.GroovyGenerationInfo) PsiAnonymousClass(com.intellij.psi.PsiAnonymousClass) Project(com.intellij.openapi.project.Project) PsiMethod(com.intellij.psi.PsiMethod) GroovyGenerationInfo(org.jetbrains.plugins.groovy.actions.generate.GroovyGenerationInfo) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

NotNull (org.jetbrains.annotations.NotNull)4 GroovyGenerationInfo (org.jetbrains.plugins.groovy.actions.generate.GroovyGenerationInfo)4 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)3 FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)2 Project (com.intellij.openapi.project.Project)2 ArrayList (java.util.ArrayList)2 CodeInsightSettings (com.intellij.codeInsight.CodeInsightSettings)1 com.intellij.codeInsight.generation (com.intellij.codeInsight.generation)1 GenerateEqualsWizard (com.intellij.codeInsight.generation.ui.GenerateEqualsWizard)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 Logger (com.intellij.openapi.diagnostic.Logger)1 Messages (com.intellij.openapi.ui.Messages)1 Computable (com.intellij.openapi.util.Computable)1 PsiAnonymousClass (com.intellij.psi.PsiAnonymousClass)1 PsiClass (com.intellij.psi.PsiClass)1 PsiField (com.intellij.psi.PsiField)1 PsiMethod (com.intellij.psi.PsiMethod)1 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 Function (com.intellij.util.Function)1