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;
}
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();
}
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);
}
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);
}
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);
}
Aggregations