Search in sources :

Example 1 with PerlDelegatingLightNamedElement

use of com.perl5.lang.perl.psi.light.PerlDelegatingLightNamedElement in project Perl5-IDEA by Camelcade.

the class PerlConstantsWrapper method calcLightElementsFromPsi.

@NotNull
@Override
public List<PerlDelegatingLightNamedElement> calcLightElementsFromPsi() {
    PsiElement firstChild = getFirstChild();
    boolean multipleDefinition = firstChild instanceof PsiPerlAnonHash;
    List<PerlDelegatingLightNamedElement> result = new ArrayList<>();
    PerlHashUtil.processHashElements(firstChild, (keyElement, valElement) -> {
        if (isAcceptableIdentifierElement(keyElement)) {
            result.add(new PerlLightConstantDefinitionElement(this, ElementManipulators.getValueText(keyElement), LIGHT_SUB_DEFINITION, keyElement, PerlPackageUtil.getContextPackageName(this), Collections.emptyList(), PerlSubAnnotations.tryToFindAnnotations(keyElement, getParent())));
        }
        return multipleDefinition;
    });
    return result;
}
Also used : PerlLightConstantDefinitionElement(com.perl5.lang.perl.parser.constant.psi.light.PerlLightConstantDefinitionElement) PerlDelegatingLightNamedElement(com.perl5.lang.perl.psi.light.PerlDelegatingLightNamedElement) ArrayList(java.util.ArrayList) PsiPerlAnonHash(com.perl5.lang.perl.psi.PsiPerlAnonHash) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PerlDelegatingLightNamedElement

use of com.perl5.lang.perl.psi.light.PerlDelegatingLightNamedElement 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;
}
Also used : PerlLightMethodDefinitionElement(com.perl5.lang.perl.psi.light.PerlLightMethodDefinitionElement) PerlDelegatingLightNamedElement(com.perl5.lang.perl.psi.light.PerlDelegatingLightNamedElement) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PerlDelegatingLightNamedElement

use of com.perl5.lang.perl.psi.light.PerlDelegatingLightNamedElement 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;
}
Also used : PerlHashEntry(com.perl5.lang.perl.util.PerlHashEntry) PerlLightMethodDefinitionElement(com.perl5.lang.perl.psi.light.PerlLightMethodDefinitionElement) PsiPerlAnonArray(com.perl5.lang.perl.psi.PsiPerlAnonArray) PerlDelegatingLightNamedElement(com.perl5.lang.perl.psi.light.PerlDelegatingLightNamedElement) PsiPerlAnonHash(com.perl5.lang.perl.psi.PsiPerlAnonHash) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with PerlDelegatingLightNamedElement

use of com.perl5.lang.perl.psi.light.PerlDelegatingLightNamedElement in project Perl5-IDEA by Camelcade.

the class MojoHelperWrapper method calcLightElementsFromPsi.

@NotNull
@Override
public List<PerlDelegatingLightNamedElement> calcLightElementsFromPsi() {
    List<PsiElement> listElements = getCallArgumentsList();
    if (listElements.size() != 2) {
        return Collections.emptyList();
    }
    PsiElement identifierElement = listElements.get(0);
    if (!isAcceptableIdentifierElement(identifierElement)) {
        return Collections.emptyList();
    }
    PsiElement bodyElement = listElements.get(1);
    // fixme we could handle reference here
    if (!(bodyElement instanceof PerlSubExpr)) {
        return Collections.emptyList();
    }
    String subName = ElementManipulators.getValueText(identifierElement);
    if (StringUtil.isEmpty(subName)) {
        return Collections.emptyList();
    }
    PsiPerlBlock subDefinitionBody = ((PerlSubExpr) bodyElement).getBlock();
    return Collections.singletonList(new MojoHelperDefinition(this, subName, LIGHT_METHOD_DEFINITION, identifierElement, MOJO_CONTROLLER_NS, PerlSubDefinitionElement.getPerlSubArgumentsFromBody(subDefinitionBody), computeSubAnnotations(this, identifierElement), subDefinitionBody));
}
Also used : PsiPerlBlock(com.perl5.lang.perl.psi.PsiPerlBlock) PerlSubExpr(com.perl5.lang.perl.psi.PerlSubExpr) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with PerlDelegatingLightNamedElement

use of com.perl5.lang.perl.psi.light.PerlDelegatingLightNamedElement 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)8 PerlDelegatingLightNamedElement (com.perl5.lang.perl.psi.light.PerlDelegatingLightNamedElement)6 NotNull (org.jetbrains.annotations.NotNull)6 PsiPerlAnonArray (com.perl5.lang.perl.psi.PsiPerlAnonArray)2 PsiPerlAnonHash (com.perl5.lang.perl.psi.PsiPerlAnonHash)2 PerlLightMethodDefinitionElement (com.perl5.lang.perl.psi.light.PerlLightMethodDefinitionElement)2 ArrayList (java.util.ArrayList)2 TextAttributesKey (com.intellij.openapi.editor.colors.TextAttributesKey)1 PsiReference (com.intellij.psi.PsiReference)1 IElementType (com.intellij.psi.tree.IElementType)1 PerlLightExceptionClassDefinition (com.perl5.lang.perl.parser.Exception.Class.psi.light.PerlLightExceptionClassDefinition)1 PerlLightConstantDefinitionElement (com.perl5.lang.perl.parser.constant.psi.light.PerlLightConstantDefinitionElement)1 PerlSubExpr (com.perl5.lang.perl.psi.PerlSubExpr)1 PsiPerlBlock (com.perl5.lang.perl.psi.PsiPerlBlock)1 PerlSubReference (com.perl5.lang.perl.psi.references.PerlSubReference)1 PerlSubAnnotations (com.perl5.lang.perl.psi.utils.PerlSubAnnotations)1 PerlSubArgument (com.perl5.lang.perl.psi.utils.PerlSubArgument)1 PerlHashEntry (com.perl5.lang.perl.util.PerlHashEntry)1