use of com.perl5.lang.perl.parser.moose.psi.impl.PerlMooseAttributeWrapper in project Perl5-IDEA by Camelcade.
the class PerlMooseAttributeWrapper method createMojoAttributes.
@NotNull
private List<PerlDelegatingLightNamedElement> createMojoAttributes(@NotNull List<PsiElement> identifiers) {
List<PerlDelegatingLightNamedElement> result = new ArrayList<>();
String packageName = PerlPackageUtil.getContextPackageName(this);
for (PsiElement identifier : identifiers) {
PerlLightMethodDefinitionElement<PerlMooseAttributeWrapper> newMethod = new PerlLightMethodDefinitionElement<>(this, ElementManipulators.getValueText(identifier), LIGHT_METHOD_DEFINITION, identifier, packageName, Arrays.asList(PerlSubArgument.self(), PerlSubArgument.optionalScalar("new_value")), PerlSubAnnotations.tryToFindAnnotations(identifier, getParent()));
result.add(setMojoReturnsComputation(newMethod));
}
return result;
}
use of com.perl5.lang.perl.parser.moose.psi.impl.PerlMooseAttributeWrapper in project Perl5-IDEA by Camelcade.
the class PerlMooseAttributeWrapper method createMooseAttributes.
@NotNull
private List<PerlDelegatingLightNamedElement> createMooseAttributes(@NotNull List<PsiElement> identifiers, @NotNull List<PsiElement> listElements) {
List<PerlDelegatingLightNamedElement> result = new ArrayList<>();
String packageName = PerlPackageUtil.getContextPackageName(this);
Map<String, PerlHashEntry> parameters = PerlHashUtil.packToHash(listElements.subList(1, listElements.size()));
// handling is
PerlHashEntry isParameter = parameters.get("is");
boolean isWritable = isParameter != null && StringUtil.equals("rw", isParameter.getValueString());
PsiElement forcedIdentifier = null;
// handling isa and does
PerlHashEntry isaEntry = parameters.get("isa");
String valueClass = null;
if (isaEntry == null) {
isaEntry = parameters.get("does");
}
if (isaEntry != null && isaEntry.valueElement != null && isAcceptableIdentifierElement(isaEntry.valueElement)) {
valueClass = isaEntry.getValueString();
if (StringUtil.isEmpty(valueClass)) {
valueClass = null;
}
}
// handling accessor, reader, etc.
List<PerlLightMethodDefinitionElement> secondaryResult = new ArrayList<>();
for (String key : MOOSE_SUB_NAMES_KEYS) {
PerlHashEntry entry = parameters.get(key);
if (entry == null) {
continue;
}
String methodName = entry.getValueString();
if (StringUtil.isEmpty(methodName)) {
continue;
}
if (!isWritable && key.equals(MUTATOR_KEY)) {
continue;
}
if (!isWritable && key.equals(READER_KEY) || key.equals(ACCESSOR_KEY)) {
forcedIdentifier = entry.valueElement;
continue;
}
if (!isAcceptableIdentifierElement(entry.valueElement)) {
continue;
}
// fixme we could optimize new_value with subclassing and hardcoding of signature
PsiElement identifier = entry.getNonNullValueElement();
PerlLightMethodDefinitionElement<PerlMooseAttributeWrapper> secondaryElement = new PerlLightMethodDefinitionElement<>(this, ElementManipulators.getValueText(identifier), LIGHT_METHOD_DEFINITION, identifier, packageName, key.equals(MUTATOR_KEY) ? Arrays.asList(PerlSubArgument.self(), PerlSubArgument.optionalScalar("new_value", valueClass)) : Collections.emptyList(), PerlSubAnnotations.tryToFindAnnotations(identifier, entry.keyElement, getParent()));
if (key.equals(READER_KEY) && valueClass != null) {
String finalClass = valueClass;
secondaryElement.setReturnsComputation((a, b) -> finalClass);
}
secondaryResult.add(secondaryElement);
}
// handle handles
PerlHashEntry handlesEntry = parameters.get("handles");
if (handlesEntry != null) {
// to show proper signatures, we need an access to delegates, what requires indexes; we should do this in runtime, not indexing, but store delegation target
if (handlesEntry.valueElement instanceof PsiPerlAnonHash) {
// handle handles HASH
Map<String, PerlHashEntry> delegatesMap = PerlHashUtil.collectHashMap(handlesEntry.valueElement);
for (PerlHashEntry delegateEntry : delegatesMap.values()) {
if (!isAcceptableIdentifierElement(delegateEntry.keyElement)) {
continue;
}
secondaryResult.add(new PerlLightMethodDefinitionElement<>(this, ElementManipulators.getValueText(delegateEntry.keyElement), LIGHT_METHOD_DEFINITION, delegateEntry.keyElement, packageName, Collections.emptyList(), PerlSubAnnotations.tryToFindAnnotations(delegateEntry.keyElement, handlesEntry.keyElement, getParent())));
}
} else if (handlesEntry.valueElement instanceof PsiPerlAnonArray) {
// handle handles ARRAY
List<PsiElement> delegatesIdentifiers = PerlArrayUtil.collectListElements(((PsiPerlAnonArray) handlesEntry.valueElement).getExpr());
for (PsiElement identifier : delegatesIdentifiers) {
if (!isAcceptableIdentifierElement(identifier)) {
continue;
}
secondaryResult.add(new PerlLightMethodDefinitionElement<>(this, ElementManipulators.getValueText(identifier), LIGHT_METHOD_DEFINITION, identifier, packageName, Collections.emptyList(), PerlSubAnnotations.tryToFindAnnotations(identifier, handlesEntry.keyElement, getParent())));
}
}
}
for (PsiElement identifier : identifiers) {
if (forcedIdentifier != null) {
identifier = forcedIdentifier;
}
if (!isAcceptableIdentifierElement(identifier)) {
continue;
}
PerlAttributeDefinition newElement = new PerlAttributeDefinition(this, PerlAttributeDefinition.DEFAULT_NAME_COMPUTATION.fun(ElementManipulators.getValueText(identifier)), LIGHT_ATTRIBUTE_DEFINITION, identifier, packageName, isWritable ? Arrays.asList(PerlSubArgument.self(), PerlSubArgument.optionalScalar("new_value", valueClass)) : Collections.emptyList(), PerlSubAnnotations.tryToFindAnnotations(identifier, getParent()));
if (valueClass != null) {
String finalClass = valueClass;
newElement.setReturnsComputation((a, b) -> finalClass);
}
result.add(newElement);
result.addAll(secondaryResult);
}
return result;
}
use of com.perl5.lang.perl.parser.moose.psi.impl.PerlMooseAttributeWrapper in project Perl5-IDEA by Camelcade.
the class PerlAttributeGrouper method group.
@NotNull
@Override
public Collection<Group> group(@NotNull AbstractTreeNode parent, @NotNull Collection<TreeElement> children) {
if (children.isEmpty() || parent instanceof GroupWrapper && ((GroupWrapper) parent).getValue() instanceof AttributeGroup) {
return Collections.emptyList();
}
Map<PerlMooseAttributeWrapper, AttributeGroup> groupMap = FactoryMap.create(AttributeGroup::new);
for (TreeElement childTreeElement : children) {
if (!(childTreeElement instanceof PerlStructureViewElement)) {
continue;
}
Object value = ((PerlStructureViewElement) childTreeElement).getValue();
if (!(value instanceof PerlLightMethodDefinitionElement)) {
continue;
}
PsiElement delegate = ((PerlLightMethodDefinitionElement) value).getDelegate();
if (!(delegate instanceof PerlMooseAttributeWrapper)) {
continue;
}
groupMap.get(delegate).addChild(childTreeElement);
}
return new ArrayList<>(groupMap.values());
}
Aggregations