Search in sources :

Example 1 with Contract

use of org.jetbrains.annotations.Contract in project Railcraft by Railcraft.

the class InvTools method acceptsItemStack.

/**
     * Checks if inventory will accept the ItemStack.
     *
     * @param stack The ItemStack
     * @param dest  The IInventory
     * @return true if room for stack
     */
@Contract("null,_ -> false;")
public static boolean acceptsItemStack(@Nullable ItemStack stack, IInventoryComposite dest) {
    if (isEmpty(stack))
        return false;
    ItemStack newStack = stack.copy();
    newStack.stackSize = 1;
    for (IInventoryObject inv : dest) {
        for (IInvSlot slot : InventoryIterator.getRailcraft(inv)) {
            if (slot.canPutStackInSlot(stack))
                return true;
        }
    }
    return false;
}
Also used : IInvSlot(mods.railcraft.common.util.inventory.iterators.IInvSlot) IInventoryObject(mods.railcraft.common.util.inventory.wrappers.IInventoryObject) ItemStack(net.minecraft.item.ItemStack) Contract(org.jetbrains.annotations.Contract)

Example 2 with Contract

use of org.jetbrains.annotations.Contract in project kotlin by JetBrains.

the class KtPsiUtil method getTopmostParentOfTypes.

@Nullable
@Contract("null, _ -> null")
public static PsiElement getTopmostParentOfTypes(@Nullable PsiElement element, @NotNull Class<? extends PsiElement>... parentTypes) {
    if (element instanceof PsiFile)
        return null;
    PsiElement answer = PsiTreeUtil.getParentOfType(element, parentTypes);
    if (answer instanceof PsiFile)
        return answer;
    do {
        PsiElement next = PsiTreeUtil.getParentOfType(answer, parentTypes);
        if (next == null)
            break;
        answer = next;
    } while (true);
    return answer;
}
Also used : PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) Contract(org.jetbrains.annotations.Contract) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with Contract

use of org.jetbrains.annotations.Contract in project kotlin by JetBrains.

the class KotlinParsing method parseFunction.

/*
     * function
     *   : modifiers "fun" typeParameters?
     *       (type ".")?
     *       SimpleName
     *       typeParameters? functionParameters (":" type)?
     *       typeConstraints
     *       functionBody?
     *   ;
     */
@Contract("false -> !null")
IElementType parseFunction(boolean failIfIdentifierExists) {
    assert _at(FUN_KEYWORD);
    // FUN_KEYWORD
    advance();
    // Recovery for the case of class A { fun| }
    if (at(RBRACE)) {
        error("Function body expected");
        return FUN;
    }
    boolean typeParameterListOccurred = false;
    if (at(LT)) {
        parseTypeParameterList(TokenSet.create(LBRACKET, LBRACE, RBRACE, LPAR));
        typeParameterListOccurred = true;
    }
    myBuilder.disableJoiningComplexTokens();
    TokenSet functionNameFollow = TokenSet.create(LT, LPAR, RPAR, COLON, EQ);
    boolean receiverFound = parseReceiverType("function", functionNameFollow);
    if (at(IDENTIFIER) && failIfIdentifierExists) {
        myBuilder.restoreJoiningComplexTokensState();
        return null;
    }
    // function as expression has no name
    parseFunctionOrPropertyName(receiverFound, "function", functionNameFollow, /*nameRequired = */
    false);
    myBuilder.restoreJoiningComplexTokensState();
    TokenSet valueParametersFollow = TokenSet.create(EQ, LBRACE, RBRACE, SEMICOLON, RPAR);
    if (at(LT)) {
        PsiBuilder.Marker error = mark();
        parseTypeParameterList(TokenSet.orSet(TokenSet.create(LPAR), valueParametersFollow));
        if (typeParameterListOccurred) {
            int offset = myBuilder.getCurrentOffset();
            error.rollbackTo();
            error = mark();
            advance(offset - myBuilder.getCurrentOffset());
            error.error("Only one type parameter list is allowed for a function");
        } else {
            error.drop();
        }
        typeParameterListOccurred = true;
    }
    if (at(LPAR)) {
        parseValueParameterList(false, /* typeRequired  = */
        false, valueParametersFollow);
    } else {
        error("Expecting '('");
    }
    if (at(COLON)) {
        // COLON
        advance();
        parseTypeRef();
    }
    parseTypeConstraintsGuarded(typeParameterListOccurred);
    if (at(SEMICOLON)) {
        // SEMICOLON
        advance();
    } else if (at(EQ) || at(LBRACE)) {
        parseFunctionBody();
    }
    return FUN;
}
Also used : TokenSet(com.intellij.psi.tree.TokenSet) PsiBuilder(com.intellij.lang.PsiBuilder) Contract(org.jetbrains.annotations.Contract)

Example 4 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 ElixirMapArguments mapArguments) {
    ASTNode node = mapArguments.getNode();
    ASTNode[] openingCurlies = node.getChildren(TokenSet.create(ElixirTypes.OPENING_CURLY));
    assert openingCurlies.length == 1;
    ASTNode openingCurly = openingCurlies[0];
    OtpErlangObject[] quotedArguments;
    ElixirMapUpdateArguments mapUpdateArguments = mapArguments.getMapUpdateArguments();
    if (mapUpdateArguments != null) {
        quotedArguments = new OtpErlangObject[] { mapUpdateArguments.quote() };
    } else {
        ElixirMapConstructionArguments mapConstructionArguments = mapArguments.getMapConstructionArguments();
        if (mapConstructionArguments != null) {
            quotedArguments = mapConstructionArguments.quoteArguments();
        } else {
            quotedArguments = new OtpErlangObject[0];
        }
    }
    return quotedFunctionCall("%{}", metadata(openingCurly), quotedArguments);
}
Also used : ASTNode(com.intellij.lang.ASTNode) Contract(org.jetbrains.annotations.Contract) NotNull(org.jetbrains.annotations.NotNull)

Example 5 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 AtUnqualifiedBracketOperation atUnqualifiedBracketOperation) {
    Quotable operator = atUnqualifiedBracketOperation.getAtPrefixOperator();
    OtpErlangObject quotedOperator = operator.quote();
    ASTNode node = atUnqualifiedBracketOperation.getNode();
    ASTNode[] identifierNodes = node.getChildren(IDENTIFIER_TOKEN_SET);
    assert identifierNodes.length == 1;
    ASTNode identifierNode = identifierNodes[0];
    String identifier = identifierNode.getText();
    OtpErlangList metadata = metadata(atUnqualifiedBracketOperation);
    OtpErlangObject quotedOperand = quotedVariable(identifier, metadata);
    OtpErlangTuple quotedContainer = quotedFunctionCall(quotedOperator, metadata, quotedOperand);
    Quotable bracketArguments = atUnqualifiedBracketOperation.getBracketArguments();
    OtpErlangObject quotedBracketArguments = bracketArguments.quote();
    return quotedFunctionCall("Elixir.Access", "get", metadata(bracketArguments), quotedContainer, quotedBracketArguments);
}
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