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);
}
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;
}
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;
}
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;
}
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);
}
Aggregations