Search in sources :

Example 41 with Contract

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;
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GrTupleType(org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType) GrRangeType(org.jetbrains.plugins.groovy.lang.psi.impl.GrRangeType) Contract(org.jetbrains.annotations.Contract) Nullable(org.jetbrains.annotations.Nullable)

Example 42 with Contract

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());
    }
}
Also used : DocumentAdapter(com.intellij.ui.DocumentAdapter) JBTextField(com.intellij.ui.components.JBTextField) DialogBuilder(com.intellij.openapi.ui.DialogBuilder) DocumentEvent(javax.swing.event.DocumentEvent) Contract(org.jetbrains.annotations.Contract)

Example 43 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 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;
}
Also used : NotImplementedException(org.apache.commons.lang.NotImplementedException) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Contract(org.jetbrains.annotations.Contract) NotNull(org.jetbrains.annotations.NotNull)

Example 44 with Contract

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;
}
Also used : ElixirSdkRelease(org.elixir_lang.sdk.ElixirSdkRelease) Contract(org.jetbrains.annotations.Contract) NotNull(org.jetbrains.annotations.NotNull)

Example 45 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 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);
}
Also used : LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) 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