Search in sources :

Example 1 with PerlLightExceptionClassDefinition

use of com.perl5.lang.perl.parser.Exception.Class.psi.light.PerlLightExceptionClassDefinition in project Perl5-IDEA by Camelcade.

the class PerlExceptionClassWrapper method processExceptionElement.

private void processExceptionElement(@NotNull List<PsiElement> listElements, int currentIndex, @NotNull List<PerlDelegatingLightNamedElement> result) {
    PsiElement listElement = listElements.get(currentIndex);
    if (!isAcceptableIdentifierElement(listElement)) {
        return;
    }
    String namespaceName = ElementManipulators.getValueText(listElement);
    if (StringUtil.isEmpty(namespaceName)) {
        return;
    }
    Map<String, PerlHashEntry> exceptionSettings = listElements.size() > currentIndex + 1 ? PerlHashUtil.collectHashMap(listElements.get(currentIndex + 1)) : Collections.emptyMap();
    // Building fields
    Set<PerlSubArgument> throwArguments = Collections.emptySet();
    PerlHashEntry fieldsEntry = exceptionSettings.get("fields");
    if (fieldsEntry != null && fieldsEntry.isComplete()) {
        PsiElement fieldsContainer = fieldsEntry.getNonNullValueElement();
        if (fieldsContainer instanceof PsiPerlAnonArray) {
            fieldsContainer = ((PsiPerlAnonArray) fieldsContainer).getExpr();
        }
        List<PsiElement> elements = PerlArrayUtil.collectListElements(fieldsContainer);
        if (!elements.isEmpty()) {
            // Fields method
            result.add(new PerlLightMethodDefinitionElement<>(this, FIELDS_METHOD_NAME, LIGHT_METHOD_DEFINITION, fieldsEntry.keyElement, namespaceName, Collections.emptyList(), null));
            // fields themselves
            throwArguments = new LinkedHashSet<>();
            for (PsiElement fieldElement : elements) {
                if (isAcceptableIdentifierElement(fieldElement)) {
                    String fieldName = PerlScalarUtil.getStringContent(fieldElement);
                    if (StringUtil.isNotEmpty(fieldName)) {
                        throwArguments.add(PerlSubArgument.mandatoryScalar(fieldName));
                        result.add(new PerlLightMethodDefinitionElement<>(this, fieldName, LIGHT_METHOD_DEFINITION, fieldElement, namespaceName, Collections.emptyList(), null));
                    }
                }
            }
        }
    }
    // making exception class
    PerlHashEntry isaEntry = exceptionSettings.get("isa");
    String parentClass = "Exception::Class::Base";
    if (isaEntry != null && isaEntry.isComplete()) {
        String manualIsa = isaEntry.getValueString();
        if (manualIsa != null) {
            parentClass = manualIsa;
        }
    }
    result.add(new PerlLightExceptionClassDefinition(this, namespaceName, LIGHT_NAMESPACE_DEFINITION, listElement, PerlMroType.DFS, Collections.singletonList(parentClass), PerlNamespaceAnnotations.tryToFindAnnotations(listElement, getParent()), Collections.emptyList(), Collections.emptyList(), Collections.emptyMap()));
    // making alias
    PerlHashEntry aliasEntry = exceptionSettings.get("alias");
    if (aliasEntry != null && aliasEntry.isComplete()) {
        if (isAcceptableIdentifierElement(aliasEntry.valueElement)) {
            String aliasName = aliasEntry.getValueString();
            if (StringUtils.isNotEmpty(aliasName)) {
                result.add(new PerlLightSubDefinitionElement<>(this, aliasName, LIGHT_SUB_DEFINITION, aliasEntry.getNonNullValueElement(), PerlPackageUtil.getContextPackageName(this), new ArrayList<>(throwArguments), PerlSubAnnotations.tryToFindAnnotations(aliasEntry.keyElement, aliasEntry.valueElement)));
            }
        }
    }
}
Also used : PerlLightExceptionClassDefinition(com.perl5.lang.perl.parser.Exception.Class.psi.light.PerlLightExceptionClassDefinition) PerlSubArgument(com.perl5.lang.perl.psi.utils.PerlSubArgument) PsiPerlAnonArray(com.perl5.lang.perl.psi.PsiPerlAnonArray) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiElement (com.intellij.psi.PsiElement)1 PerlLightExceptionClassDefinition (com.perl5.lang.perl.parser.Exception.Class.psi.light.PerlLightExceptionClassDefinition)1 PsiPerlAnonArray (com.perl5.lang.perl.psi.PsiPerlAnonArray)1 PerlSubArgument (com.perl5.lang.perl.psi.utils.PerlSubArgument)1