Search in sources :

Example 21 with Contract

use of org.jetbrains.annotations.Contract in project intellij-elixir by KronicDeth.

the class ElixirPsiImplUtil method quote.

/* Replaces `nil` argument in variables with the quoted ElixirMatchdNotParenthesesArguments.
     *
     */
@Contract(pure = true)
@NotNull
public static OtpErlangObject quote(@NotNull final UnqualifiedNoParenthesesCall unqualifiedNoParenthesesCall) {
    String identifier = unqualifiedNoParenthesesCall.functionName();
    OtpErlangObject quotedIdentifer = new OtpErlangAtom(identifier);
    ElixirNoParenthesesOneArgument noParenthesesOneArgument = unqualifiedNoParenthesesCall.getNoParenthesesOneArgument();
    OtpErlangObject[] quotedArguments = noParenthesesOneArgument.quoteArguments();
    OtpErlangList blockCallMetadata = metadata(unqualifiedNoParenthesesCall);
    // see https://github.com/elixir-lang/elixir/blob/de39bbaca277002797e52ffbde617ace06233a2b//lib/elixir/src/elixir_parser.yrl#L627-L628
    if (quotedArguments.length == 1) {
        OtpErlangObject quotedArgument = quotedArguments[0];
        if (Macro.isExpression(quotedArgument)) {
            OtpErlangTuple expression = (OtpErlangTuple) quotedArgument;
            OtpErlangObject receiver = expression.elementAt(0);
            if (receiver.equals(MINUS) || receiver.equals(PLUS)) {
                OtpErlangList dualCallArguments = Macro.callArguments(expression);
                // [Arg]
                if (dualCallArguments.arity() == 1) {
                    /* @note getChildren[0] is NOT the same as getFirstChild().  getFirstChild() will get the
                             leaf node for identifier instead of the first compound, rule node for the argument. */
                    PsiElement argument = unqualifiedNoParenthesesCall.getChildren()[1].getFirstChild();
                    if (!(argument instanceof ElixirAccessExpression && argument.getFirstChild() instanceof ElixirParentheticalStab)) {
                        blockCallMetadata = new OtpErlangList(new OtpErlangObject[] { AMBIGUOUS_OP_KEYWORD_PAIR, blockCallMetadata.elementAt(0) });
                    }
                }
            }
        }
    }
    ElixirDoBlock doBlock = unqualifiedNoParenthesesCall.getDoBlock();
    return quotedBlockCall(quotedIdentifer, blockCallMetadata, quotedArguments, doBlock);
}
Also used : LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Contract(org.jetbrains.annotations.Contract) NotNull(org.jetbrains.annotations.NotNull)

Example 22 with Contract

use of org.jetbrains.annotations.Contract in project intellij-elixir by KronicDeth.

the class ElixirPsiImplUtil method aliasToModular.

@Contract(pure = true)
@Nullable
private static Call aliasToModular(@NotNull QualifiableAlias alias, @NotNull PsiReference startingReference) {
    PsiElement fullyResolvedAlias = fullyResolveAlias(alias, startingReference);
    Call modular = null;
    if (fullyResolvedAlias instanceof Call) {
        Call fullyResolvedAliasCall = (Call) fullyResolvedAlias;
        if (isModular(fullyResolvedAliasCall)) {
            modular = fullyResolvedAliasCall;
        }
    }
    return modular;
}
Also used : Call(org.elixir_lang.psi.call.Call) CallDefinitionClause.enclosingModularMacroCall(org.elixir_lang.structure_view.element.CallDefinitionClause.enclosingModularMacroCall) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Contract(org.jetbrains.annotations.Contract) Nullable(org.jetbrains.annotations.Nullable)

Example 23 with Contract

use of org.jetbrains.annotations.Contract in project intellij-elixir by KronicDeth.

the class ElixirPsiImplUtil method quote.

@Contract(pure = true)
@NotNull
public static OtpErlangObject quote(ElixirRelativeIdentifier relativeIdentifier) {
    PsiElement[] children = relativeIdentifier.getChildren();
    OtpErlangObject quotedRelativeIdentifier;
    // Only tokens
    if (children.length == 0) {
        ASTNode relativeIdentifierNode = relativeIdentifier.getNode();
        // take first node to avoid SIGNIFICANT_WHITE_SPACE after DUAL_OPERATOR
        ASTNode operatorNode = relativeIdentifierNode.getFirstChildNode();
        quotedRelativeIdentifier = new OtpErlangAtom(operatorNode.getText());
    } else {
        assert children.length == 1;
        PsiElement child = children[0];
        if (child instanceof Atomable) {
            Atomable atomable = (Atomable) child;
            quotedRelativeIdentifier = atomable.quoteAsAtom();
        } else {
            Quotable quotable = (Quotable) child;
            quotedRelativeIdentifier = quotable.quote();
        }
    }
    return quotedRelativeIdentifier;
}
Also used : ASTNode(com.intellij.lang.ASTNode) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Contract(org.jetbrains.annotations.Contract) NotNull(org.jetbrains.annotations.NotNull)

Example 24 with Contract

use of org.jetbrains.annotations.Contract in project intellij-elixir by KronicDeth.

the class ElixirPsiImplUtil method fullyQualifiedName.

@Contract(pure = true)
@Nullable
public static String fullyQualifiedName(@NotNull final QualifiableAlias qualifiableAlias) {
    String fullyQualifiedName = null;
    PsiElement[] children = qualifiableAlias.getChildren();
    int operatorIndex = Normalized.operatorIndex(children);
    Quotable qualifier = org.elixir_lang.psi.operation.infix.Normalized.leftOperand(children, operatorIndex);
    String qualifierName = null;
    if (qualifier instanceof Call) {
        Call qualifierCall = (Call) qualifier;
        if (qualifierCall.isCalling(KERNEL, __MODULE__, 0)) {
            Call enclosingCall = enclosingModularMacroCall(qualifierCall);
            if (enclosingCall != null && enclosingCall instanceof StubBased) {
                StubBased enclosingStubBasedCall = (StubBased) enclosingCall;
                qualifierName = enclosingStubBasedCall.canonicalName();
            }
        }
    } else if (qualifier instanceof QualifiableAlias) {
        QualifiableAlias qualifiableQualifier = (QualifiableAlias) qualifier;
        qualifierName = qualifiableQualifier.fullyQualifiedName();
    } else if (qualifier instanceof ElixirAccessExpression) {
        PsiElement qualifierChild = stripAccessExpression(qualifier);
        if (qualifierChild instanceof ElixirAlias) {
            ElixirAlias childAlias = (ElixirAlias) qualifierChild;
            qualifierName = childAlias.getName();
        }
    }
    if (qualifierName != null) {
        Quotable rightOperand = org.elixir_lang.psi.operation.infix.Normalized.rightOperand(children, operatorIndex);
        if (rightOperand instanceof ElixirAlias) {
            ElixirAlias relativeAlias = (ElixirAlias) rightOperand;
            fullyQualifiedName = qualifierName + "." + relativeAlias.getName();
        }
    }
    return fullyQualifiedName;
}
Also used : Call(org.elixir_lang.psi.call.Call) CallDefinitionClause.enclosingModularMacroCall(org.elixir_lang.structure_view.element.CallDefinitionClause.enclosingModularMacroCall) StubBased(org.elixir_lang.psi.call.StubBased) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Contract(org.jetbrains.annotations.Contract) Nullable(org.jetbrains.annotations.Nullable)

Example 25 with Contract

use of org.jetbrains.annotations.Contract in project intellij-elixir by KronicDeth.

the class ElixirPsiImplUtil method quote.

@Contract(pure = true)
@NotNull
public static OtpErlangObject quote(@NotNull final HeredocLine heredocLine, @NotNull final Heredoc heredoc, int prefixLength) {
    ElixirHeredocLinePrefix heredocLinePrefix = heredocLine.getHeredocLinePrefix();
    ASTNode excessWhitespace = heredocLinePrefix.excessWhitespace(heredoc.getFragmentType(), prefixLength);
    Body body = heredocLine.getBody();
    ASTNode[] directChildNodes = childNodes(body);
    ASTNode[] accumulatedChildNodes;
    if (excessWhitespace != null) {
        accumulatedChildNodes = new ASTNode[directChildNodes.length + 1];
        accumulatedChildNodes[0] = excessWhitespace;
        for (int i = 0; i < directChildNodes.length; i++) {
            accumulatedChildNodes[i + 1] = directChildNodes[i];
        }
    } else {
        accumulatedChildNodes = directChildNodes;
    }
    return quotedChildNodes(heredoc, metadata(heredocLine), accumulatedChildNodes);
}
Also used : ASTNode(com.intellij.lang.ASTNode) Contract(org.jetbrains.annotations.Contract) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Contract (org.jetbrains.annotations.Contract)111 NotNull (org.jetbrains.annotations.NotNull)48 Nullable (org.jetbrains.annotations.Nullable)40 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)29 PsiElement (com.intellij.psi.PsiElement)19 Call (org.elixir_lang.psi.call.Call)17 ASTNode (com.intellij.lang.ASTNode)14 CallDefinitionClause.enclosingModularMacroCall (org.elixir_lang.structure_view.element.CallDefinitionClause.enclosingModularMacroCall)9 PsiFile (com.intellij.psi.PsiFile)6 IElementType (com.intellij.psi.tree.IElementType)6 BigInteger (java.math.BigInteger)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 Quotable (org.elixir_lang.psi.Quotable)5 Project (com.intellij.openapi.project.Project)4 NavigatablePsiElement (com.intellij.psi.NavigatablePsiElement)3 Matcher (java.util.regex.Matcher)3 TreeElement (com.intellij.ide.util.treeView.smartTree.TreeElement)2 SdkModificator (com.intellij.openapi.projectRoots.SdkModificator)2 TextRange (com.intellij.openapi.util.TextRange)2 File (java.io.File)2