Search in sources :

Example 1 with GrMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.

the class GroovyGenerateEqualsHelper method createEquals.

private PsiMethod createEquals() throws IncorrectOperationException {
    final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(myProject);
    String[] nameSuggestions = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, null, null, PsiType.getJavaLangObject(myClass.getManager(), myClass.getResolveScope())).names;
    final String objectBaseName = nameSuggestions.length > 0 ? nameSuggestions[0] : BASE_OBJECT_PARAMETER_NAME;
    myParameterName = getUniqueLocalVarName(objectBaseName, myEqualsFields);
    //todo isApplicable it
    final PsiType classType = TypesUtil.createType(myClass.getQualifiedName(), myClass.getContext());
    nameSuggestions = codeStyleManager.suggestVariableName(VariableKind.LOCAL_VARIABLE, null, null, classType).names;
    String instanceBaseName = nameSuggestions.length > 0 && nameSuggestions[0].length() < 10 ? nameSuggestions[0] : BASE_OBJECT_LOCAL_NAME;
    myClassInstanceName = getUniqueLocalVarName(instanceBaseName, myEqualsFields);
    @NonNls StringBuffer buffer = new StringBuffer();
    buffer.append("boolean equals(").append(myParameterName).append(") {\n");
    addEqualsPrologue(buffer);
    if (myEqualsFields.length > 0) {
        addClassInstance(buffer);
        ArrayList<PsiField> equalsFields = new ArrayList<>();
        ContainerUtil.addAll(equalsFields, myEqualsFields);
        Collections.sort(equalsFields, EqualsFieldsComparator.INSTANCE);
        for (PsiField field : equalsFields) {
            if (!field.hasModifierProperty(PsiModifier.STATIC)) {
                final PsiType type = field.getType();
                if (type instanceof PsiArrayType) {
                    addArrayEquals(buffer, field);
                } else if (type instanceof PsiPrimitiveType) {
                    if (PsiType.DOUBLE.equals(type) || PsiType.FLOAT.equals(type)) {
                        addDoubleFieldComparison(buffer, field);
                    } else {
                        addFieldComparison(buffer, field);
                    }
                } else {
                    if (type instanceof PsiClassType) {
                        final PsiClass aClass = ((PsiClassType) type).resolve();
                        if (aClass != null && aClass.isEnum()) {
                            addFieldComparison(buffer, field);
                            continue;
                        }
                    }
                    addFieldComparison(buffer, field);
                }
            }
        }
    }
    buffer.append("\nreturn true\n}");
    GrMethod result = myFactory.createMethodFromText(buffer.toString());
    final PsiParameter parameter = result.getParameterList().getParameters()[0];
    PsiUtil.setModifierProperty(parameter, PsiModifier.FINAL, CodeStyleSettingsManager.getSettings(myProject).GENERATE_FINAL_PARAMETERS);
    try {
        result = ((GrMethod) CodeStyleManager.getInstance(myProject).reformat(result));
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
    return result;
}
Also used : NonNls(org.jetbrains.annotations.NonNls) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 2 with GrMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod 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 3 with GrMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.

the class CreateParameterForFieldIntention method performForConstructor.

private static void performForConstructor(PsiElement element, final Project project, Editor editor, List<GrField> candidates) {
    final GrMethod constructor = PsiTreeUtil.getParentOfType(element, GrMethod.class);
    if (candidates.isEmpty())
        return;
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        for (GrField candidate : candidates) {
            addParameter(candidate, constructor, project);
        }
        return;
    }
    final JList list = new JBList(candidates.toArray(new GrField[candidates.size()]));
    list.setCellRenderer(new DefaultPsiElementCellRenderer());
    new PopupChooserBuilder(list).setTitle(GroovyIntentionsBundle.message("create.parameter.for.field.intention.name")).setMovable(true).setItemChoosenCallback(() -> {
        final Object[] selectedValues = list.getSelectedValues();
        CommandProcessor.getInstance().executeCommand(project, () -> {
            for (Object selectedValue : selectedValues) {
                LOG.assertTrue(((GrField) selectedValue).isValid());
                addParameter(((GrField) selectedValue), constructor, project);
            }
        }, GroovyIntentionsBundle.message("create.parameter.for.field.intention.name"), null);
    }).createPopup().showInBestPositionFor(editor);
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) DefaultPsiElementCellRenderer(com.intellij.ide.util.DefaultPsiElementCellRenderer) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) JBList(com.intellij.ui.components.JBList) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder)

Example 4 with GrMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.

the class CreateParameterForFieldIntention method findFieldCandidates.

@Nullable
private static List<GrField> findFieldCandidates(PsiElement element) {
    final GrMethod constructor = PsiTreeUtil.getParentOfType(element, GrMethod.class);
    if (constructor == null || !constructor.isConstructor())
        return null;
    if (constructor.getBlock() == null)
        return null;
    if (PsiTreeUtil.isAncestor(constructor.getBlock(), element, false)) {
        return null;
    }
    final PsiClass clazz = constructor.getContainingClass();
    if (!(clazz instanceof GrTypeDefinition))
        return null;
    return findCandidatesCached(constructor, (GrTypeDefinition) clazz);
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with GrMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.

the class CreateParameterForFieldIntention method performForField.

private static void performForField(PsiElement element, final Project project, Editor editor, List<GrMethod> constructors) {
    final GrField field = PsiTreeUtil.getParentOfType(element, GrField.class);
    if (constructors.isEmpty())
        return;
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        for (GrMethod constructor : constructors) {
            addParameter(field, constructor, project);
        }
        return;
    }
    final JList list = new JBList(constructors.toArray(new GrMethod[constructors.size()]));
    list.setCellRenderer(new MethodCellRenderer(true));
    new PopupChooserBuilder(list).setTitle(GroovyIntentionsBundle.message("create.parameter.for.field.intention.name")).setMovable(true).setItemChoosenCallback(() -> {
        final Object[] selectedValues = list.getSelectedValues();
        Arrays.sort(selectedValues, (o1, o2) -> ((GrMethod) o2).getParameterList().getParametersCount() - ((GrMethod) o1).getParameterList().getParametersCount());
        CommandProcessor.getInstance().executeCommand(project, () -> {
            for (Object selectedValue : selectedValues) {
                LOG.assertTrue(((GrMethod) selectedValue).isValid());
                addParameter(field, ((GrMethod) selectedValue), project);
            }
        }, GroovyIntentionsBundle.message("create.parameter.for.field.intention.name"), null);
    }).createPopup().showInBestPositionFor(editor);
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) JBList(com.intellij.ui.components.JBList) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder) MethodCellRenderer(com.intellij.ide.util.MethodCellRenderer)

Aggregations

GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)134 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)24 PsiElement (com.intellij.psi.PsiElement)22 NotNull (org.jetbrains.annotations.NotNull)21 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)19 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)18 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)17 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)16 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)16 ArrayList (java.util.ArrayList)15 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)15 Nullable (org.jetbrains.annotations.Nullable)12 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)12 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)12 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)12 IncorrectOperationException (com.intellij.util.IncorrectOperationException)10 GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)10 Project (com.intellij.openapi.project.Project)9 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)8 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)8