use of org.jetbrains.annotations.Contract in project intellij-community by JetBrains.
the class ClosureParameterEnhancer method findTypeForIteration.
@Contract("null,_ -> null")
@Nullable
public static PsiType findTypeForIteration(@Nullable PsiType type, @NotNull PsiElement context) {
final PsiManager manager = context.getManager();
final GlobalSearchScope resolveScope = context.getResolveScope();
if (type instanceof PsiArrayType) {
return TypesUtil.boxPrimitiveType(((PsiArrayType) type).getComponentType(), manager, resolveScope);
}
if (type instanceof GrTupleType) {
PsiType[] types = ((GrTupleType) type).getParameters();
return types.length == 1 ? types[0] : null;
}
if (type instanceof GrRangeType) {
return ((GrRangeType) type).getIterationType();
}
PsiType fromIterator = findTypeFromIteratorMethod(type, context);
if (fromIterator != null) {
return fromIterator;
}
PsiType extracted = PsiUtil.extractIterableTypeParameter(type, true);
if (extracted != null) {
return extracted;
}
if (TypesUtil.isClassType(type, JAVA_LANG_STRING) || TypesUtil.isClassType(type, JAVA_IO_FILE)) {
return PsiType.getJavaLangString(manager, resolveScope);
}
if (InheritanceUtil.isInheritor(type, JAVA_UTIL_MAP)) {
return getEntryForMap(type, manager.getProject(), resolveScope);
}
return type;
}
use of org.jetbrains.annotations.Contract in project intellij-community by JetBrains.
the class EditContractIntention method invoke.
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
final PsiMethod method = getTargetMethod(project, editor, file);
assert method != null;
Contract existingAnno = AnnotationUtil.findAnnotationInHierarchy(method, Contract.class);
String oldContract = existingAnno == null ? null : existingAnno.value();
boolean oldPure = existingAnno != null && existingAnno.pure();
JBTextField contractText = new JBTextField(oldContract);
JCheckBox pureCB = createPureCheckBox(oldPure);
DialogBuilder builder = createDialog(project, contractText, pureCB);
contractText.getDocument().addDocumentListener(new DocumentAdapter() {
@Override
protected void textChanged(DocumentEvent e) {
String error = getErrorMessage(contractText.getText(), method);
builder.setOkActionEnabled(error == null);
builder.setErrorText(error, contractText);
}
});
if (builder.showAndGet()) {
updateContract(method, contractText.getText(), pureCB.isSelected());
}
}
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 In in) {
PsiElement[] children = in.getChildren();
if (children.length != 3) {
throw new NotImplementedException("BinaryOperation expected to have 3 children (left operand, operator, right operand");
}
Quotable leftOperand = (Quotable) children[0];
OtpErlangObject quotedLeftOperand = leftOperand.quote();
Quotable operator = (Quotable) children[1];
OtpErlangObject quotedOperator = operator.quote();
Quotable rightOperand = (Quotable) children[2];
OtpErlangObject quotedRightOperand = rightOperand.quote();
OtpErlangObject quoted = null;
// @see https://github.com/elixir-lang/elixir/blob/6c288be7300509ff7b809002a3563c6a02dc13fa/lib/elixir/src/elixir_parser.yrl#L583
if (Macro.isExpression(quotedLeftOperand)) {
OtpErlangTuple leftExpression = (OtpErlangTuple) quotedLeftOperand;
OtpErlangObject leftOperator = leftExpression.elementAt(0);
for (OtpErlangAtom rearrangedUnaryOperator : REARRANGED_UNARY_OPERATORS) {
/* build_op({_Kind, Line, 'in'}, {UOp, _, [Left]}, Right) when ?rearrange_uop(UOp) ->
{UOp, meta(Line), [{'in', meta(Line), [Left, Right]}]}; */
if (leftOperator.equals(rearrangedUnaryOperator)) {
OtpErlangObject unaryOperatorArguments = leftExpression.elementAt(2);
OtpErlangObject originalUnaryOperand;
if (unaryOperatorArguments instanceof OtpErlangString) {
OtpErlangString unaryOperatorPrintableArguments = (OtpErlangString) unaryOperatorArguments;
int codePoint = unaryOperatorPrintableArguments.stringValue().codePointAt(0);
originalUnaryOperand = new OtpErlangLong(codePoint);
} else if (unaryOperatorArguments instanceof OtpErlangList) {
OtpErlangList unaryOperatorArgumentList = (OtpErlangList) unaryOperatorArguments;
originalUnaryOperand = unaryOperatorArgumentList.elementAt(0);
} else {
throw new NotImplementedException("Expected REARRANGED_UNARY_OPERATORS operand to be quoted as an OtpErlangString or OtpErlangList");
}
OtpErlangList operatorMetadata = metadata(operator);
quoted = quotedFunctionCall(leftOperator, operatorMetadata, quotedFunctionCall(quotedOperator, operatorMetadata, originalUnaryOperand, quotedRightOperand));
}
}
}
if (quoted == null) {
quoted = quotedFunctionCall(quotedOperator, metadata(operator), quotedLeftOperand, quotedRightOperand);
}
return quoted;
}
use of org.jetbrains.annotations.Contract in project intellij-elixir by KronicDeth.
the class ElixirPsiImplUtil method quoteBinaryFunctionIdentifier.
/**
* Elixir 1.3 changed from `to_char_list` to `to_charlist`
* (https://github.com/elixir-lang/elixir/blob/v1.3/CHANGELOG.md)
*
* @return {@code "to_charlist} by default; {@code "to_char_list"}
*/
@Contract(pure = true)
@NotNull
private static String quoteBinaryFunctionIdentifier(@NotNull final InterpolatedCharList interpolatedCharList) {
ElixirSdkRelease release = getNonNullRelease(interpolatedCharList);
String functionIdentifier = "to_charlist";
if (release.compareTo(ElixirSdkRelease.V_1_3) < 0) {
functionIdentifier = "to_char_list";
}
return functionIdentifier;
}
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 ElixirContainerAssociationOperation containerAssociationOperation) {
PsiElement[] children = containerAssociationOperation.getChildren();
// associationInfixOperator is private so not a PsiElement
assert children.length == 2;
OtpErlangObject[] quotedChildren = new OtpErlangObject[children.length];
int i = 0;
for (PsiElement child : children) {
Quotable quotableChild = (Quotable) child;
quotedChildren[i++] = quotableChild.quote();
}
return new OtpErlangTuple(quotedChildren);
}
Aggregations