Search in sources :

Example 1 with PerlSubCallElement

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

the class PerlMooseAttributeHandler method createMojoAttributes.

@NotNull
private List<PerlDelegatingLightNamedElement<?>> createMojoAttributes(@NotNull PerlSubCallElement subCallElement, @NotNull Pair<List<PsiElement>, List<PsiElement>> lists) {
    List<PsiElement> arguments = lists.second.subList(1, lists.second.size());
    PsiElement argument = ContainerUtil.getFirstItem(arguments);
    PerlSubExpr subExpr = ObjectUtils.tryCast(argument, PerlSubExpr.class);
    List<PerlDelegatingLightNamedElement<?>> result = new ArrayList<>();
    String namespaceName = PerlPackageUtil.getContextNamespaceName(subCallElement);
    for (PsiElement identifier : lists.first) {
        AtomicNotNullLazyValue<PerlValue> valueProvider = AtomicNotNullLazyValue.createValue(() -> PerlSmartGetterValue.create(subExpr != null ? PerlResolveUtil.computeReturnValueFromControlFlow(subExpr) : argument != null ? PerlValuesManager.from(argument) : PerlScalarValue.create(namespaceName)));
        PerlLightMethodDefinitionElement<PerlSubCallElement> newMethod = new PerlLightMethodDefinitionElement<>(subCallElement, ElementManipulators.getValueText(identifier), LIGHT_METHOD_DEFINITION, identifier, namespaceName, Arrays.asList(PerlSubArgument.self(), PerlSubArgument.optionalScalar(PerlSubArgument.NEW_VALUE_VALUE)), PerlSubAnnotations.tryToFindAnnotations(identifier, subCallElement.getParent()), valueProvider, subExpr == null ? null : subExpr.getBlock());
        result.add(newMethod);
    }
    return result;
}
Also used : PerlLightMethodDefinitionElement(com.perl5.lang.perl.psi.light.PerlLightMethodDefinitionElement) PerlDelegatingLightNamedElement(com.perl5.lang.perl.psi.light.PerlDelegatingLightNamedElement) PerlValue(com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValue) PerlSubCallElement(com.perl5.lang.perl.psi.impl.PerlSubCallElement) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PerlSubCallElement

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

the class PerlHandleCompletionProvider method isApplicable.

private boolean isApplicable(@NotNull PsiElement position) {
    PerlSubCallElement subCallElement = PsiTreeUtil.getParentOfType(position, PerlSubCallElement.class);
    if (subCallElement == null) {
        return true;
    }
    PsiPerlMethod method = subCallElement.getMethod();
    if (method == null || method.hasExplicitNamespace()) {
        return true;
    }
    PerlSubNameElement subNameElement = method.getSubNameElement();
    if (subNameElement == null) {
        return true;
    }
    return !AUTO_VIVIFICATION_SUBS.contains(subNameElement.getName());
}
Also used : PerlSubNameElement(com.perl5.lang.perl.psi.PerlSubNameElement) PsiPerlMethod(com.perl5.lang.perl.psi.PsiPerlMethod) PerlSubCallElement(com.perl5.lang.perl.psi.impl.PerlSubCallElement)

Example 3 with PerlSubCallElement

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

the class PerlClassAccessorHandler method computeLightElementsFromPsi.

@Override
@NotNull
public List<? extends PerlDelegatingLightNamedElement<?>> computeLightElementsFromPsi(@NotNull PerlSubCallElement psiElement) {
    String packageName = PerlPackageUtil.getContextNamespaceName(psiElement);
    if (StringUtil.isEmpty(packageName)) {
        return Collections.emptyList();
    }
    List<PsiElement> listElements = psiElement.getCallArgumentsList();
    if (listElements.isEmpty()) {
        return Collections.emptyList();
    }
    List<PerlDelegatingLightNamedElement<?>> result = new ArrayList<>();
    for (PsiElement listElement : listElements) {
        if (!psiElement.isAcceptableIdentifierElement(listElement)) {
            continue;
        }
        String baseName = ElementManipulators.getValueText(listElement);
        PerlSubAnnotations subAnnotations = PerlSubAnnotations.computeForLightElement(psiElement, listElement);
        for (Function<String, String> computation : getNamesComputations(psiElement)) {
            result.add(new PerlClassAccessorMethod(psiElement, baseName, computation, CLASS_ACCESSOR_METHOD, listElement, packageName, subAnnotations));
        }
    }
    return result;
}
Also used : PerlSubAnnotations(com.perl5.lang.perl.psi.utils.PerlSubAnnotations) PerlDelegatingLightNamedElement(com.perl5.lang.perl.psi.light.PerlDelegatingLightNamedElement) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with PerlSubCallElement

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

the class PerlAttributeGrouper method group.

@Override
@NotNull
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<PerlSubCallElement, 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 (!PerlMooseAttributeHandler.isMooseAttributeWrapper(delegate)) {
            continue;
        }
        assert delegate instanceof PerlSubCallElement;
        groupMap.get(delegate).addChild(childTreeElement);
    }
    return new ArrayList<>(groupMap.values());
}
Also used : PerlLightMethodDefinitionElement(com.perl5.lang.perl.psi.light.PerlLightMethodDefinitionElement) PerlStructureViewElement(com.perl5.lang.perl.idea.structureView.elements.PerlStructureViewElement) PerlSubCallElement(com.perl5.lang.perl.psi.impl.PerlSubCallElement) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with PerlSubCallElement

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

the class PerlIndentProcessor method getNodeIndent.

@NotNull
public Indent getNodeIndent(@NotNull ASTNode node) {
    IElementType nodeType = node.getElementType();
    ASTNode parent = node.getTreeParent();
    ASTNode grandParent = parent != null ? parent.getTreeParent() : null;
    IElementType parentNodeType = parent != null ? parent.getElementType() : null;
    IElementType grandParentNodeType = grandParent != null ? grandParent.getElementType() : null;
    ASTNode prevSibling = FormatterUtil.getPreviousNonWhitespaceSibling(node);
    IElementType prevSiblingElementType = prevSibling != null ? prevSibling.getElementType() : null;
    ASTNode nextSibling = FormatterUtil.getNextNonWhitespaceSibling(node);
    IElementType nextSiblingElementType = nextSibling != null ? nextSibling.getElementType() : null;
    boolean isFirst = prevSibling == null;
    boolean isLast = nextSibling == null;
    if (parentNodeType == ATTRIBUTES) {
        return Indent.getContinuationIndent();
    }
    if (PerlFormattingTokenSets.FOR_ELEMENTS_TOKENSET.contains(nodeType)) {
        return Indent.getContinuationIndent();
    }
    if (isFirst && BLOCK_OPENERS.contains(nodeType) || isLast && BLOCK_CLOSERS.contains(nodeType)) {
        return Indent.getNoneIndent();
    }
    if (VARIABLE_DECLARATIONS.contains(parentNodeType) && (nodeType == LEFT_PAREN || nodeType == RIGHT_PAREN)) {
        return Indent.getNoneIndent();
    }
    boolean forceFirstIndent = false;
    if (HEREDOC_BODIES_TOKENSET.contains(nodeType)) {
        PsiElement psi = node.getPsi();
        assert psi instanceof PerlHeredocElementImpl;
        if (!((PerlHeredocElementImpl) psi).isIndentable()) {
            return Indent.getAbsoluteNoneIndent();
        }
        forceFirstIndent = true;
    }
    if (getAbsoluteUnindentableTokens().contains(nodeType)) {
        return Indent.getAbsoluteNoneIndent();
    }
    if ((BLOCK_LIKE_CONTAINERS.contains(nodeType) || nodeType == SUB_EXPR) && PerlFormattingTokenSets.MULTI_PARAM_BLOCK_CONTAINERS.contains(parentNodeType)) {
        return Indent.getNoneIndent();
    }
    if (parent == null || grandParent == null && nodeType != HEREDOC_END_INDENTABLE && !HEREDOC_BODIES_TOKENSET.contains(nodeType)) {
        return Indent.getNoneIndent();
    }
    if (getUnindentableTokens().contains(nodeType) || (nodeType instanceof PerlPolyNamedElementType && !(node.getPsi() instanceof PerlSubCallElement) && nodeType != USE_STATEMENT && nodeType != NO_STATEMENT)) {
        return Indent.getNoneIndent();
    }
    if (parentNodeType == STRING_LIST && (nodeType == QUOTE_SINGLE_OPEN || nodeType == QUOTE_SINGLE_CLOSE)) {
        return Indent.getNoneIndent();
    }
    if (nodeType == STRING_BARE && parentNodeType == STRING_LIST) {
        return Indent.getContinuationIndent();
    }
    // defined by parent
    if (getUnindentableContainers().contains(parentNodeType)) {
        // a little magic for sub attributes
        if (parentNodeType == SUB_DEFINITION) {
            if (nodeType == COLON && nextSiblingElementType == ATTRIBUTE || nodeType == ATTRIBUTE && prevSiblingElementType != COLON) {
                return Indent.getContinuationIndent();
            }
        }
        return Indent.getNoneIndent();
    }
    if (PerlFormattingTokenSets.COMMA_LIKE_SEQUENCES.contains(parentNodeType)) {
        return grandParentNodeType == STATEMENT ? Indent.getContinuationWithoutFirstIndent() : Indent.getContinuationIndent();
    }
    if (parentNodeType == CALL_ARGUMENTS) {
        return Indent.getContinuationIndent();
    }
    if (getBlockLikeContainers().contains(parentNodeType)) {
        return Indent.getNormalIndent();
    }
    return forceFirstIndent ? Indent.getContinuationIndent() : Indent.getContinuationWithoutFirstIndent();
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PerlPolyNamedElementType(com.perl5.lang.perl.psi.stubs.PerlPolyNamedElementType) ASTNode(com.intellij.lang.ASTNode) PerlSubCallElement(com.perl5.lang.perl.psi.impl.PerlSubCallElement) PsiElement(com.intellij.psi.PsiElement) PerlHeredocElementImpl(com.perl5.lang.perl.psi.impl.PerlHeredocElementImpl) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiElement (com.intellij.psi.PsiElement)10 PerlSubCallElement (com.perl5.lang.perl.psi.impl.PerlSubCallElement)9 NotNull (org.jetbrains.annotations.NotNull)8 PerlDelegatingLightNamedElement (com.perl5.lang.perl.psi.light.PerlDelegatingLightNamedElement)4 PerlValue (com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValue)3 PerlLightMethodDefinitionElement (com.perl5.lang.perl.psi.light.PerlLightMethodDefinitionElement)3 IElementType (com.intellij.psi.tree.IElementType)2 PerlSubNameElement (com.perl5.lang.perl.psi.PerlSubNameElement)2 PerlSubAnnotations (com.perl5.lang.perl.psi.utils.PerlSubAnnotations)2 ArrayList (java.util.ArrayList)2 ASTNode (com.intellij.lang.ASTNode)1 Logger (com.intellij.openapi.diagnostic.Logger)1 Ref (com.intellij.openapi.util.Ref)1 StringUtil (com.intellij.openapi.util.text.StringUtil)1 ElementManipulators (com.intellij.psi.ElementManipulators)1 PsiReference (com.intellij.psi.PsiReference)1 StubInputStream (com.intellij.psi.stubs.StubInputStream)1 StubOutputStream (com.intellij.psi.stubs.StubOutputStream)1 CachedValueProvider (com.intellij.psi.util.CachedValueProvider)1 CachedValuesManager (com.intellij.psi.util.CachedValuesManager)1