Search in sources :

Example 6 with IntRange

use of org.apache.commons.lang.math.IntRange in project intellij-elixir by KronicDeth.

the class Issue480Test method assertResolvedNameArityRange.

private void assertResolvedNameArityRange(@NotNull PsiElement element, @NotNull String name, int arity) {
    PsiReference reference = element.getReference();
    assertNotNull(reference);
    PsiElement resolved = reference.resolve();
    assertNotNull(resolved);
    assertInstanceOf(resolved, ElixirIdentifier.class);
    PsiElement maybeDefMaybeCall = resolved.getParent().getParent().getParent();
    assertInstanceOf(maybeDefMaybeCall, Call.class);
    Call maybeDefCall = (Call) maybeDefMaybeCall;
    assertTrue(CallDefinitionClause.is(maybeDefCall));
    assertEquals(pair(name, new IntRange(arity)), nameArityRange(maybeDefCall));
}
Also used : Call(org.elixir_lang.psi.call.Call) IntRange(org.apache.commons.lang.math.IntRange) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement)

Example 7 with IntRange

use of org.apache.commons.lang.math.IntRange in project intellij-elixir by KronicDeth.

the class CallDefinition method getLineMarkerInfo.

@Nullable
private LineMarkerInfo getLineMarkerInfo(@NotNull Call call) {
    LineMarkerInfo lineMarkerInfo = null;
    if (daemonCodeAnalyzerSettings.SHOW_METHOD_SEPARATORS && CallDefinitionClause.is(call)) {
        Call previousCallDefinitionClause = siblingCallDefinitionClause(call, PREVIOUS_SIBLING);
        boolean firstClause;
        if (previousCallDefinitionClause == null) {
            firstClause = true;
        } else {
            Pair<String, IntRange> callNameArityRange = nameArityRange(call);
            if (callNameArityRange != null) {
                Pair<String, IntRange> previousNameArityRange = nameArityRange(previousCallDefinitionClause);
                firstClause = previousNameArityRange == null || !previousNameArityRange.equals(callNameArityRange);
            } else {
                firstClause = true;
            }
        }
        if (firstClause) {
            PsiElement previousExpression = previousSiblingExpression(call);
            if (previousExpression instanceof AtUnqualifiedNoParenthesesCall) {
                AtUnqualifiedNoParenthesesCall previousModuleAttributeDefinition = (AtUnqualifiedNoParenthesesCall) previousExpression;
                String moduleAttributeName = moduleAttributeName(previousModuleAttributeDefinition);
                if (moduleAttributeName.equals("@doc")) {
                    firstClause = false;
                } else if (moduleAttributeName.equals("@spec")) {
                    Pair<String, IntRange> callNameArityRange = nameArityRange(call);
                    if (callNameArityRange != null) {
                        Pair<String, Integer> specNameArity = moduleAttributeNameArity(previousModuleAttributeDefinition);
                        if (specNameArity != null) {
                            Integer specArity = specNameArity.second;
                            IntRange callArityRange = callNameArityRange.second;
                            if (callArityRange.containsInteger(specArity)) {
                                firstClause = false;
                            }
                        }
                    }
                }
            }
        }
        if (firstClause) {
            lineMarkerInfo = callDefinitionSeparator(call);
        }
    }
    return lineMarkerInfo;
}
Also used : LineMarkerInfo(com.intellij.codeInsight.daemon.LineMarkerInfo) Call(org.elixir_lang.psi.call.Call) AtUnqualifiedNoParenthesesCall(org.elixir_lang.psi.AtUnqualifiedNoParenthesesCall) AtUnqualifiedNoParenthesesCall(org.elixir_lang.psi.AtUnqualifiedNoParenthesesCall) IntRange(org.apache.commons.lang.math.IntRange) Pair(com.intellij.openapi.util.Pair) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with IntRange

use of org.apache.commons.lang.math.IntRange in project intellij-elixir by KronicDeth.

the class CallDefinition method getLineMarkerInfo.

@Nullable
private LineMarkerInfo getLineMarkerInfo(@NotNull AtUnqualifiedNoParenthesesCall atUnqualifiedNoParenthesesCall) {
    LineMarkerInfo lineMarkerInfo = null;
    String moduleAttributeName = moduleAttributeName(atUnqualifiedNoParenthesesCall);
    if (moduleAttributeName.equals("@doc")) {
        PsiElement previousExpression = siblingExpression(atUnqualifiedNoParenthesesCall, PREVIOUS_SIBLING);
        boolean firstInGroup = true;
        if (previousExpression instanceof AtUnqualifiedNoParenthesesCall) {
            AtUnqualifiedNoParenthesesCall previousModuleAttribute = (AtUnqualifiedNoParenthesesCall) previousExpression;
            String previousModuleAttributeName = moduleAttributeName(previousModuleAttribute);
            if (previousModuleAttributeName.equals("@spec")) {
                Pair<String, Integer> moduleAttributeNameArity = moduleAttributeNameArity(previousModuleAttribute);
                if (moduleAttributeNameArity != null) {
                    Call nextSiblingCallDefinitionClause = siblingCallDefinitionClause(atUnqualifiedNoParenthesesCall, NEXT_SIBLING);
                    if (nextSiblingCallDefinitionClause != null) {
                        Pair<String, IntRange> nameArityRange = nameArityRange(nextSiblingCallDefinitionClause);
                        if (nameArityRange != null) {
                            IntRange arityRange = nameArityRange.second;
                            if (arityRange.containsInteger(moduleAttributeNameArity.second)) {
                                // the previous spec is part of the group
                                firstInGroup = false;
                            }
                        }
                    }
                }
            }
        }
        if (firstInGroup) {
            lineMarkerInfo = callDefinitionSeparator(atUnqualifiedNoParenthesesCall);
        }
    } else if (moduleAttributeName.equals("@spec")) {
        PsiElement previousExpression = siblingExpression(atUnqualifiedNoParenthesesCall, PREVIOUS_SIBLING);
        boolean firstInGroup = true;
        if (previousExpression instanceof AtUnqualifiedNoParenthesesCall) {
            AtUnqualifiedNoParenthesesCall previousModuleAttribute = (AtUnqualifiedNoParenthesesCall) previousExpression;
            String previousModuleAttributeName = moduleAttributeName(previousModuleAttribute);
            if (previousModuleAttributeName.equals("@doc")) {
                firstInGroup = false;
            } else if (previousModuleAttributeName.equals("@spec")) {
                Pair<String, Integer> moduleAttributeNameArity = moduleAttributeNameArity(atUnqualifiedNoParenthesesCall);
                if (moduleAttributeNameArity != null) {
                    Pair<String, Integer> previousModuleAttributeNameArity = moduleAttributeNameArity(previousModuleAttribute);
                    if (previousModuleAttributeNameArity != null) {
                        // name match, now check if the arities match.
                        if (moduleAttributeNameArity.first.equals(previousModuleAttributeNameArity.first)) {
                            Integer moduleAttributeArity = moduleAttributeNameArity.second;
                            Integer previousModuleAttributeArity = previousModuleAttributeNameArity.second;
                            if (moduleAttributeArity.equals(previousModuleAttributeArity)) {
                                /* same arity with different pattern is same function, so the previous @spec should
                                       check if it is first because this one isn't */
                                firstInGroup = false;
                            } else {
                                /* same name, but different arity needs to determine if the call definition has an
                                       arity range. */
                                Call specification = specification(atUnqualifiedNoParenthesesCall);
                                if (specification != null) {
                                    Call type = specificationType(specification);
                                    if (type != null) {
                                        PsiReference reference = type.getReference();
                                        if (reference != null) {
                                            List<PsiElement> resolvedList = null;
                                            if (reference instanceof PsiPolyVariantReference) {
                                                PsiPolyVariantReference polyVariantReference = (PsiPolyVariantReference) reference;
                                                ResolveResult[] resolveResults = polyVariantReference.multiResolve(false);
                                                if (resolveResults.length > 0) {
                                                    resolvedList = new ArrayList<PsiElement>();
                                                    for (ResolveResult resolveResult : resolveResults) {
                                                        resolvedList.add(resolveResult.getElement());
                                                    }
                                                }
                                            } else {
                                                PsiElement resolved = reference.resolve();
                                                if (resolved != null) {
                                                    resolvedList = Collections.singletonList(resolved);
                                                }
                                            }
                                            if (resolvedList != null && resolvedList.size() > 0) {
                                                for (PsiElement resolved : resolvedList) {
                                                    if (resolved instanceof Call) {
                                                        Pair<String, IntRange> resolvedNameArityRange = nameArityRange((Call) resolved);
                                                        if (resolvedNameArityRange != null) {
                                                            IntRange resolvedArityRange = resolvedNameArityRange.second;
                                                            if (resolvedArityRange.containsInteger(moduleAttributeArity) && resolvedArityRange.containsInteger(previousModuleAttributeArity)) {
                                                                // the current @spec and the previous @spec apply to the same call definition clause
                                                                firstInGroup = false;
                                                                break;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } else {
            Call specification = specification(atUnqualifiedNoParenthesesCall);
            if (specification != null) {
                Call type = specificationType(specification);
                if (type != null) {
                    PsiReference reference = type.getReference();
                    if (reference != null) {
                        if (reference instanceof PsiPolyVariantReference) {
                            PsiPolyVariantReference polyVariantReference = (PsiPolyVariantReference) reference;
                            ResolveResult[] resolveResults = polyVariantReference.multiResolve(false);
                            PsiFile containingFile = type.getContainingFile();
                            for (ResolveResult resolveResult : resolveResults) {
                                PsiElement element = resolveResult.getElement();
                                if (element != null) {
                                    if (element.getContainingFile().equals(containingFile) && element.getTextOffset() < type.getTextOffset()) {
                                        firstInGroup = false;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        if (firstInGroup) {
            lineMarkerInfo = callDefinitionSeparator(atUnqualifiedNoParenthesesCall);
        }
    }
    return lineMarkerInfo;
}
Also used : Call(org.elixir_lang.psi.call.Call) AtUnqualifiedNoParenthesesCall(org.elixir_lang.psi.AtUnqualifiedNoParenthesesCall) AtUnqualifiedNoParenthesesCall(org.elixir_lang.psi.AtUnqualifiedNoParenthesesCall) IntRange(org.apache.commons.lang.math.IntRange) LineMarkerInfo(com.intellij.codeInsight.daemon.LineMarkerInfo) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with IntRange

use of org.apache.commons.lang.math.IntRange in project intellij-elixir by KronicDeth.

the class CallDefinitionClause method callDefinitionClauseLookupElements.

/*
     * Private Instance Methods
     */
@NotNull
private static Iterable<LookupElement> callDefinitionClauseLookupElements(@NotNull Call scope) {
    Call[] childCalls = macroChildCalls(scope);
    List<LookupElement> lookupElementList = null;
    if (childCalls != null && childCalls.length > 0) {
        for (Call childCall : childCalls) {
            if (org.elixir_lang.structure_view.element.CallDefinitionClause.is(childCall)) {
                Pair<String, IntRange> nameArityRange = nameArityRange(childCall);
                if (nameArityRange != null) {
                    String name = nameArityRange.first;
                    if (name != null) {
                        if (lookupElementList == null) {
                            lookupElementList = new ArrayList<LookupElement>();
                        }
                        lookupElementList.add(org.elixir_lang.code_insight.lookup.element.CallDefinitionClause.createWithSmartPointer(nameArityRange.first, childCall));
                    }
                }
            }
        }
    }
    if (lookupElementList == null) {
        lookupElementList = Collections.emptyList();
    }
    return lookupElementList;
}
Also used : Call(org.elixir_lang.psi.call.Call) IntRange(org.apache.commons.lang.math.IntRange) LookupElement(com.intellij.codeInsight.lookup.LookupElement) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with IntRange

use of org.apache.commons.lang.math.IntRange in project intellij-elixir by KronicDeth.

the class CallDefinition method fromCall.

/*
     * Constructors
     */
/**
     * @param call a def(macro)?p? call
     */
@Nullable
public static CallDefinition fromCall(@NotNull Call call) {
    Modular modular = CallDefinitionClause.enclosingModular(call);
    CallDefinition callDefinition = null;
    if (modular != null) {
        Pair<String, IntRange> nameArityRange = CallDefinitionClause.nameArityRange(call);
        if (nameArityRange != null) {
            String name = nameArityRange.first;
            /* arity is assumed to be max arity in the range because that's how {@code h} and ExDoc treat functions
                   with defaults. */
            int arity = nameArityRange.second.getMaximumInteger();
            Time time = CallDefinitionClause.time(call);
            callDefinition = new CallDefinition(modular, time, name, arity);
        }
    }
    return callDefinition;
}
Also used : IntRange(org.apache.commons.lang.math.IntRange) Modular(org.elixir_lang.structure_view.element.modular.Modular) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

IntRange (org.apache.commons.lang.math.IntRange)31 Call (org.elixir_lang.psi.call.Call)10 PsiElement (com.intellij.psi.PsiElement)6 File (java.io.File)6 Nullable (org.jetbrains.annotations.Nullable)6 ArrayList (java.util.ArrayList)5 NotNull (org.jetbrains.annotations.NotNull)4 Test (org.testng.annotations.Test)4 Pair (com.intellij.openapi.util.Pair)3 DataType (com.linkedin.pinot.common.data.FieldSpec.DataType)3 DataGeneratorSpec (com.linkedin.pinot.tools.data.generator.DataGeneratorSpec)3 List (java.util.List)3 AtUnqualifiedNoParenthesesCall (org.elixir_lang.psi.AtUnqualifiedNoParenthesesCall)3 LineMarkerInfo (com.intellij.codeInsight.daemon.LineMarkerInfo)2 PsiReference (com.intellij.psi.PsiReference)2 FieldSpec (com.linkedin.pinot.common.data.FieldSpec)2 FieldType (com.linkedin.pinot.common.data.FieldSpec.FieldType)2 DataGenerator (com.linkedin.pinot.tools.data.generator.DataGenerator)2 HashMap (java.util.HashMap)2 TimeUnit (java.util.concurrent.TimeUnit)2