Search in sources :

Example 1 with PsiPerlAnonHash

use of com.perl5.lang.perl.psi.PsiPerlAnonHash 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 PsiPerlAnonHash

use of com.perl5.lang.perl.psi.PsiPerlAnonHash 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 3 with PsiPerlAnonHash

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

the class PerlStringCompletionUtil method fillWithHashIndexes.

public static void fillWithHashIndexes(@NotNull final PsiElement element, @NotNull final CompletionResultSet result) {
    for (String text : HASH_INDEXES_CACHE) {
        addElement(text, result);
    }
    PsiFile file = element.getContainingFile();
    file.accept(new PerlRecursiveVisitor() {

        @Override
        public void visitStringContentElement(@NotNull PerlStringContentElementImpl o) {
            if (o != element && SIMPLE_HASH_INDEX.accepts(o)) {
                processStringElement(o);
            }
            super.visitStringContentElement(o);
        }

        @Override
        public void visitCommaSequenceExpr(@NotNull PsiPerlCommaSequenceExpr o) {
            if (o.getParent() instanceof PsiPerlAnonHash) {
                PsiElement sequenceElement = o.getFirstChild();
                boolean isKey = true;
                while (sequenceElement != null) {
                    ProgressManager.checkCanceled();
                    IElementType elementType = sequenceElement.getNode().getElementType();
                    if (isKey && sequenceElement instanceof PerlString) {
                        for (PerlStringContentElement stringElement : PerlPsiUtil.collectStringElements(sequenceElement)) {
                            processStringElement(stringElement);
                        }
                    } else if (elementType == COMMA || elementType == FAT_COMMA) {
                        isKey = !isKey;
                    }
                    sequenceElement = PerlPsiUtil.getNextSignificantSibling(sequenceElement);
                }
            }
            super.visitCommaSequenceExpr(o);
        }

        protected void processStringElement(PerlStringContentElement stringContentElement) {
            String text = stringContentElement.getText();
            if (StringUtil.isNotEmpty(text) && !HASH_INDEXES_CACHE.contains(text) && PerlLexer.IDENTIFIER_PATTERN.matcher(text).matches()) {
                HASH_INDEXES_CACHE.add(text);
                addElement(text, result);
            }
        }
    });
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) PerlStringContentElementImpl(com.perl5.lang.perl.psi.impl.PerlStringContentElementImpl)

Aggregations

PsiElement (com.intellij.psi.PsiElement)3 PsiPerlAnonHash (com.perl5.lang.perl.psi.PsiPerlAnonHash)2 PerlDelegatingLightNamedElement (com.perl5.lang.perl.psi.light.PerlDelegatingLightNamedElement)2 NotNull (org.jetbrains.annotations.NotNull)2 PsiFile (com.intellij.psi.PsiFile)1 IElementType (com.intellij.psi.tree.IElementType)1 PerlLightConstantDefinitionElement (com.perl5.lang.perl.parser.constant.psi.light.PerlLightConstantDefinitionElement)1 PsiPerlAnonArray (com.perl5.lang.perl.psi.PsiPerlAnonArray)1 PerlStringContentElementImpl (com.perl5.lang.perl.psi.impl.PerlStringContentElementImpl)1 PerlLightMethodDefinitionElement (com.perl5.lang.perl.psi.light.PerlLightMethodDefinitionElement)1 PerlHashEntry (com.perl5.lang.perl.util.PerlHashEntry)1 ArrayList (java.util.ArrayList)1