Search in sources :

Example 36 with Contract

use of org.jetbrains.annotations.Contract in project intellij-community by JetBrains.

the class UnclearBinaryExpressionInspection method isUnclearExpression.

@Contract("null, _ -> false")
public static boolean isUnclearExpression(PsiExpression expression, PsiElement parent) {
    if (expression instanceof PsiAssignmentExpression) {
        final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression) expression;
        final IElementType tokenType = assignmentExpression.getOperationTokenType();
        if (parent instanceof PsiVariable) {
            if (!tokenType.equals(JavaTokenType.EQ)) {
                return true;
            }
        } else if (parent instanceof PsiAssignmentExpression) {
            final PsiAssignmentExpression nestedAssignment = (PsiAssignmentExpression) parent;
            final IElementType nestedTokenType = nestedAssignment.getOperationTokenType();
            if (!tokenType.equals(nestedTokenType)) {
                return true;
            }
        }
        return isUnclearExpression(assignmentExpression.getRExpression(), assignmentExpression);
    } else if (expression instanceof PsiConditionalExpression) {
        final PsiConditionalExpression conditionalExpression = (PsiConditionalExpression) expression;
        if (PsiUtilCore.hasErrorElementChild(expression)) {
            return false;
        }
        return (parent instanceof PsiConditionalExpression) || isUnclearExpression(conditionalExpression.getCondition(), conditionalExpression) || isUnclearExpression(conditionalExpression.getThenExpression(), conditionalExpression) || isUnclearExpression(conditionalExpression.getElseExpression(), conditionalExpression);
    } else if (expression instanceof PsiPolyadicExpression) {
        if ((parent instanceof PsiConditionalExpression) || (parent instanceof PsiPolyadicExpression) || (parent instanceof PsiInstanceOfExpression)) {
            return true;
        }
        final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression) expression;
        for (PsiExpression operand : polyadicExpression.getOperands()) {
            final PsiType type = operand.getType();
            if ((type == null) || type.equalsToText(CommonClassNames.JAVA_LANG_STRING)) {
                return false;
            }
            if (isUnclearExpression(operand, polyadicExpression)) {
                return true;
            }
        }
    } else if (expression instanceof PsiInstanceOfExpression) {
        if ((parent instanceof PsiPolyadicExpression) || (parent instanceof PsiConditionalExpression)) {
            return true;
        }
        final PsiInstanceOfExpression instanceOfExpression = (PsiInstanceOfExpression) expression;
        return isUnclearExpression(instanceOfExpression.getOperand(), instanceOfExpression);
    } else if (expression instanceof PsiParenthesizedExpression) {
        final PsiParenthesizedExpression parenthesizedExpression = (PsiParenthesizedExpression) expression;
        final PsiExpression nestedExpression = parenthesizedExpression.getExpression();
        return isUnclearExpression(nestedExpression, parenthesizedExpression);
    }
    return false;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Contract(org.jetbrains.annotations.Contract)

Example 37 with Contract

use of org.jetbrains.annotations.Contract in project intellij-community by JetBrains.

the class IdeFrameFixture method findFileByRelativePath.

@Nullable
@Contract("_, true -> !null")
public VirtualFile findFileByRelativePath(@NotNull String relativePath, boolean requireExists) {
    //noinspection Contract
    assertFalse("Should use '/' in test relative paths, not File.separator", relativePath.contains("\\"));
    Project project = getProject();
    VirtualFile file = project.getBaseDir().findFileByRelativePath(relativePath);
    if (requireExists) {
        //noinspection Contract
        assertNotNull("Unable to find file with relative path " + quote(relativePath), file);
    }
    return file;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) Contract(org.jetbrains.annotations.Contract) Nullable(org.jetbrains.annotations.Nullable)

Example 38 with Contract

use of org.jetbrains.annotations.Contract in project intellij-community by JetBrains.

the class StringUtil method getWordIndicesIn.

@NotNull
@Contract(pure = true)
public static List<TextRange> getWordIndicesIn(@NotNull String text) {
    List<TextRange> result = new SmartList<TextRange>();
    int start = -1;
    for (int i = 0; i < text.length(); i++) {
        char c = text.charAt(i);
        boolean isIdentifierPart = Character.isJavaIdentifierPart(c);
        if (isIdentifierPart && start == -1) {
            start = i;
        }
        if (isIdentifierPart && i == text.length() - 1 && start != -1) {
            result.add(new TextRange(start, i + 1));
        } else if (!isIdentifierPart && start != -1) {
            result.add(new TextRange(start, i));
            start = -1;
        }
    }
    return result;
}
Also used : TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull) Contract(org.jetbrains.annotations.Contract)

Example 39 with Contract

use of org.jetbrains.annotations.Contract in project intellij-community by JetBrains.

the class ScalaGradleProjectResolverExtension method create.

@Nullable
@Contract("null -> null")
private static ScalaCompileOptionsData create(@Nullable ScalaCompileOptions options) {
    if (options == null)
        return null;
    ScalaCompileOptionsData result = new ScalaCompileOptionsData();
    result.setAdditionalParameters(options.getAdditionalParameters());
    result.setDaemonServer(options.getDaemonServer());
    result.setDebugLevel(options.getDebugLevel());
    result.setDeprecation(options.isDeprecation());
    result.setEncoding(options.getEncoding());
    result.setFailOnError(options.isFailOnError());
    result.setForce(options.getForce());
    result.setFork(options.isFork());
    result.setListFiles(options.isListFiles());
    result.setLoggingLevel(options.getLoggingLevel());
    result.setDebugLevel(options.getDebugLevel());
    result.setLoggingPhases(options.getLoggingPhases());
    result.setOptimize(options.isOptimize());
    result.setUnchecked(options.isUnchecked());
    result.setUseAnt(options.isUseAnt());
    result.setUseCompileDaemon(options.isUseCompileDaemon());
    result.setForkOptions(create(options.getForkOptions()));
    return result;
}
Also used : ScalaCompileOptionsData(org.jetbrains.plugins.gradle.model.data.ScalaCompileOptionsData) Contract(org.jetbrains.annotations.Contract) Nullable(org.jetbrains.annotations.Nullable)

Example 40 with Contract

use of org.jetbrains.annotations.Contract in project intellij-community by JetBrains.

the class WritesCounterDFAInstance method getVariable.

@Contract("null -> null")
@Nullable
private static GrVariable getVariable(@Nullable PsiElement instructionElement) {
    final GrVariable variable;
    if (instructionElement instanceof GrReferenceExpression) {
        final PsiElement resolved = ((GrReferenceExpression) instructionElement).resolve();
        variable = resolved instanceof GrVariable ? (GrVariable) resolved : null;
    } else if (instructionElement instanceof GrVariable) {
        variable = (GrVariable) instructionElement;
    } else {
        variable = null;
    }
    return variable != null && PsiUtil.isLocalOrParameter(variable) ? variable : null;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Contract(org.jetbrains.annotations.Contract) Nullable(org.jetbrains.annotations.Nullable)

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