Search in sources :

Example 1 with ElixirAccessExpression

use of org.elixir_lang.psi.ElixirAccessExpression in project intellij-elixir by KronicDeth.

the class Exception method defaultValueElementByKeyElement.

/**
     * The default value elements for the struct defined for the exception.
     *
     * @return Maps the element for the key in the struct to the element in the default value.  When the list form of
     *   fields without default values is used, the Map value element is {@code  null}.
     */
@NotNull
public Map<PsiElement, PsiElement> defaultValueElementByKeyElement() {
    PsiElement[] finalArguments = ElixirPsiImplUtil.finalArguments(navigationItem);
    assert finalArguments != null;
    assert finalArguments.length == 1;
    PsiElement finalArgument = finalArguments[0];
    Map<PsiElement, PsiElement> defaultValueElementByKeyElement = new HashMap<PsiElement, PsiElement>(finalArguments.length);
    if (finalArgument instanceof ElixirAccessExpression) {
        PsiElement accessExpressionChild = stripAccessExpression(finalArgument);
        assert accessExpressionChild instanceof ElixirList;
        ElixirList list = (ElixirList) accessExpressionChild;
        PsiElement[] listChildren = list.getChildren();
        if (listChildren.length == 1) {
            PsiElement listChild = listChildren[0];
            if (listChild instanceof QuotableKeywordList) {
                QuotableKeywordList quotableKeywordList = (QuotableKeywordList) listChild;
                putQuotableKeywordList(defaultValueElementByKeyElement, quotableKeywordList);
            } else {
                defaultValueElementByKeyElement.put(listChild, null);
            }
        } else {
            for (PsiElement key : list.getChildren()) {
                defaultValueElementByKeyElement.put(key, null);
            }
        }
    } else if (finalArgument instanceof QuotableKeywordList) {
        QuotableKeywordList quotableKeywordList = (QuotableKeywordList) finalArgument;
        putQuotableKeywordList(defaultValueElementByKeyElement, quotableKeywordList);
    } else {
        assert finalArgument != null;
    }
    return defaultValueElementByKeyElement;
}
Also used : HashMap(java.util.HashMap) ElixirList(org.elixir_lang.psi.ElixirList) ElixirAccessExpression(org.elixir_lang.psi.ElixirAccessExpression) QuotableKeywordList(org.elixir_lang.psi.QuotableKeywordList) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ElixirAccessExpression

use of org.elixir_lang.psi.ElixirAccessExpression in project intellij-elixir by KronicDeth.

the class Delegation method callDefinitionHeadCallList.

/*
     * Static Methods
     */
public static List<Call> callDefinitionHeadCallList(Call defdelegateCall) {
    List<Call> callDefinitionHeadCallList = null;
    PsiElement[] finalArguments = ElixirPsiImplUtil.finalArguments(defdelegateCall);
    assert finalArguments != null;
    assert finalArguments.length > 0;
    PsiElement firstFinalArgument = finalArguments[0];
    if (firstFinalArgument instanceof ElixirAccessExpression) {
        PsiElement accessExpressionChild = stripAccessExpression(firstFinalArgument);
        if (accessExpressionChild instanceof ElixirList) {
            ElixirList list = (ElixirList) accessExpressionChild;
            Call[] listCalls = PsiTreeUtil.getChildrenOfType(list, Call.class);
            callDefinitionHeadCallList = filterCallDefinitionHeadCallList(listCalls);
        }
    } else if (firstFinalArgument instanceof Call) {
        Call call = (Call) firstFinalArgument;
        callDefinitionHeadCallList = filterCallDefinitionHeadCallList(call);
    }
    if (callDefinitionHeadCallList == null) {
        callDefinitionHeadCallList = Collections.emptyList();
    }
    return callDefinitionHeadCallList;
}
Also used : Call(org.elixir_lang.psi.call.Call) ElixirList(org.elixir_lang.psi.ElixirList) ElixirAccessExpression(org.elixir_lang.psi.ElixirAccessExpression) PsiElement(com.intellij.psi.PsiElement)

Example 3 with ElixirAccessExpression

use of org.elixir_lang.psi.ElixirAccessExpression in project intellij-elixir by KronicDeth.

the class Used method provideNodesFromChild.

public static Collection<TreeElement> provideNodesFromChild(@NotNull TreeElement child) {
    Collection<TreeElement> nodes = null;
    if (child instanceof Use) {
        Use use = (Use) child;
        PsiElement[] finalArguments = ElixirPsiImplUtil.finalArguments(use.call());
        assert finalArguments != null;
        if (finalArguments.length > 0) {
            PsiElement firstFinalArgument = finalArguments[0];
            if (firstFinalArgument instanceof ElixirAccessExpression) {
                PsiElement accessExpressionChild = stripAccessExpression(firstFinalArgument);
                if (accessExpressionChild instanceof QualifiableAlias) {
                    PsiReference reference = accessExpressionChild.getReference();
                    if (reference != null) {
                        PsiElement ancestor = reference.resolve();
                        while (ancestor != null && !(ancestor instanceof PsiFile)) {
                            if (ancestor instanceof Call) {
                                Call call = (Call) ancestor;
                                if (Module.is(call)) {
                                    Module module = new Module(call);
                                    Call[] childCalls = ElixirPsiImplUtil.macroChildCalls(call);
                                    if (childCalls != null) {
                                        Map<Pair<String, Integer>, CallDefinition> macroByNameArity = new HashMap<Pair<String, Integer>, CallDefinition>(childCalls.length);
                                        for (Call childCall : childCalls) {
                                            /* portion of {@link org.elixir_lang.structure_view.element.enclosingModular.Module#childCallTreeElements}
                                                       dealing with macros, restricted to __using__/1 */
                                            if (CallDefinitionClause.isMacro(childCall)) {
                                                Pair<String, IntRange> nameArityRange = CallDefinitionClause.nameArityRange(childCall);
                                                if (nameArityRange != null) {
                                                    String name = nameArityRange.first;
                                                    IntRange arityRange = nameArityRange.second;
                                                    if (name.equals(USING) && arityRange.containsInteger(1)) {
                                                        addClausesToCallDefinition(childCall, name, arityRange, macroByNameArity, module, Timed.Time.COMPILE, new Inserter<CallDefinition>() {

                                                            @Override
                                                            public void insert(CallDefinition element) {
                                                            }
                                                        });
                                                    }
                                                }
                                            }
                                        }
                                        if (macroByNameArity.size() > 0) {
                                            PsiElement[] usingArguments;
                                            CallDefinition macro;
                                            CallDefinitionClause matchingClause = null;
                                            if (finalArguments.length > 1) {
                                                usingArguments = Arrays.copyOfRange(finalArguments, 1, finalArguments.length);
                                                Pair<String, Integer> nameArity = pair(USING, usingArguments.length);
                                                macro = macroByNameArity.get(nameArity);
                                                if (macro != null) {
                                                    matchingClause = macro.matchingClause(usingArguments);
                                                }
                                            } else {
                                                /* `use <ALIAS>` will calls `__using__/1` even though there is
                                                           no additional argument, but it obviously can't select a clause. */
                                                Pair<String, Integer> nameArity = pair(USING, 1);
                                                macro = macroByNameArity.get(nameArity);
                                                List<CallDefinitionClause> macroClauseList = macro.clauseList();
                                                if (macroClauseList.size() == 1) {
                                                    matchingClause = macroClauseList.get(0);
                                                } else {
                                                // TODO match default argument clause/head to non-default argument clause that would be executed.
                                                }
                                            }
                                            if (matchingClause != null) {
                                                TreeElement[] callDefinitionClauseChildren = matchingClause.getChildren();
                                                int length = callDefinitionClauseChildren.length;
                                                if (length > 0) {
                                                    TreeElement lastCallDefinitionClauseChild = callDefinitionClauseChildren[length - 1];
                                                    if (lastCallDefinitionClauseChild instanceof Quote) {
                                                        Quote quote = (Quote) lastCallDefinitionClauseChild;
                                                        Quote injectedQuote = quote.used(use);
                                                        TreeElement[] injectedQuoteChildren = injectedQuote.getChildren();
                                                        nodes = new ArrayList<TreeElement>(injectedQuoteChildren.length);
                                                        for (TreeElement injectedQuoteChild : injectedQuoteChildren) {
                                                            if (!(injectedQuoteChild instanceof Overridable)) {
                                                                nodes.add(injectedQuoteChild);
                                                            }
                                                        }
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                } else {
                                    break;
                                }
                            }
                            ancestor = ancestor.getParent();
                        }
                    }
                }
            }
        }
    }
    if (nodes == null) {
        nodes = Collections.emptyList();
    }
    return nodes;
}
Also used : Module.addClausesToCallDefinition(org.elixir_lang.structure_view.element.modular.Module.addClausesToCallDefinition) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair) Call(org.elixir_lang.psi.call.Call) QualifiableAlias(org.elixir_lang.psi.QualifiableAlias) ElixirAccessExpression(org.elixir_lang.psi.ElixirAccessExpression) IntRange(org.apache.commons.lang.math.IntRange) PsiReference(com.intellij.psi.PsiReference) TreeElement(com.intellij.ide.util.treeView.smartTree.TreeElement) Module(org.elixir_lang.structure_view.element.modular.Module)

Example 4 with ElixirAccessExpression

use of org.elixir_lang.psi.ElixirAccessExpression in project intellij-elixir by KronicDeth.

the class Field method is.

/*
     * Static Methods
     */
public static boolean is(ElixirAtom atom) {
    boolean field = false;
    PsiElement parent = atom.getParent();
    if (parent instanceof ElixirAccessExpression && parent.getChildren().length == 1) {
        PsiElement grandParent = parent.getParent();
        if (grandParent instanceof ElixirList) {
            PsiElement greatGrandParent = grandParent.getParent();
            if (greatGrandParent instanceof ElixirAccessExpression && greatGrandParent.getChildren().length == 1) {
                PsiElement greatGreatGrandParent = greatGrandParent.getParent();
                if (greatGreatGrandParent instanceof ElixirNoParenthesesOneArgument) {
                    PsiElement greatGreatGreatGrandParent = greatGreatGrandParent.getParent();
                    if (greatGreatGreatGrandParent instanceof Call) {
                        Call greatGreatGreatGrandParentCall = (Call) greatGreatGreatGrandParent;
                        field = Structure.is(greatGreatGreatGrandParentCall);
                    }
                }
            }
        }
    }
    return field;
}
Also used : Call(org.elixir_lang.psi.call.Call) ElixirNoParenthesesOneArgument(org.elixir_lang.psi.ElixirNoParenthesesOneArgument) ElixirList(org.elixir_lang.psi.ElixirList) ElixirAccessExpression(org.elixir_lang.psi.ElixirAccessExpression) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiElement (com.intellij.psi.PsiElement)4 ElixirAccessExpression (org.elixir_lang.psi.ElixirAccessExpression)4 ElixirList (org.elixir_lang.psi.ElixirList)3 Call (org.elixir_lang.psi.call.Call)3 TreeElement (com.intellij.ide.util.treeView.smartTree.TreeElement)1 Pair (com.intellij.openapi.util.Pair)1 PsiFile (com.intellij.psi.PsiFile)1 PsiReference (com.intellij.psi.PsiReference)1 HashMap (java.util.HashMap)1 IntRange (org.apache.commons.lang.math.IntRange)1 ElixirNoParenthesesOneArgument (org.elixir_lang.psi.ElixirNoParenthesesOneArgument)1 QualifiableAlias (org.elixir_lang.psi.QualifiableAlias)1 QuotableKeywordList (org.elixir_lang.psi.QuotableKeywordList)1 Module (org.elixir_lang.structure_view.element.modular.Module)1 Module.addClausesToCallDefinition (org.elixir_lang.structure_view.element.modular.Module.addClausesToCallDefinition)1 NotNull (org.jetbrains.annotations.NotNull)1