Search in sources :

Example 76 with HighlightInfo

use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.

the class HighlightUtil method checkValidArrayAccessExpression.

@Nullable
static HighlightInfo checkValidArrayAccessExpression(@NotNull PsiArrayAccessExpression arrayAccessExpression) {
    final PsiExpression arrayExpression = arrayAccessExpression.getArrayExpression();
    final PsiType arrayExpressionType = arrayExpression.getType();
    if (arrayExpressionType != null && !(arrayExpressionType instanceof PsiArrayType)) {
        final String description = JavaErrorMessages.message("array.type.expected", JavaHighlightUtil.formatType(arrayExpressionType));
        final HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(arrayExpression).descriptionAndTooltip(description).create();
        QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createReplaceWithListAccessFix(arrayAccessExpression));
        return info;
    }
    final PsiExpression indexExpression = arrayAccessExpression.getIndexExpression();
    return indexExpression != null ? checkAssignability(PsiType.INT, indexExpression.getType(), indexExpression, indexExpression) : null;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Example 77 with HighlightInfo

use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.

the class HighlightUtil method checkUnaryOperatorApplicable.

@Nullable
static HighlightInfo checkUnaryOperatorApplicable(@Nullable PsiJavaToken token, @Nullable PsiExpression expression) {
    if (token != null && expression != null && !TypeConversionUtil.isUnaryOperatorApplicable(token, expression)) {
        PsiType type = expression.getType();
        if (type == null)
            return null;
        String message = JavaErrorMessages.message("unary.operator.not.applicable", token.getText(), JavaHighlightUtil.formatType(type));
        PsiElement parentExpr = token.getParent();
        HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(parentExpr).descriptionAndTooltip(message).create();
        if (parentExpr instanceof PsiPrefixExpression && token.getTokenType() == JavaTokenType.EXCL) {
            QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createNegationBroadScopeFix((PsiPrefixExpression) parentExpr));
        }
        return highlightInfo;
    }
    return null;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Example 78 with HighlightInfo

use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.

the class HighlightUtil method checkCaseStatement.

@Nullable
static HighlightInfo checkCaseStatement(@NotNull PsiSwitchLabelStatement statement) {
    PsiSwitchStatement switchStatement = statement.getEnclosingSwitchStatement();
    if (switchStatement == null) {
        String description = JavaErrorMessages.message("case.statement.outside.switch");
        return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(statement).descriptionAndTooltip(description).create();
    }
    if (switchStatement.getBody() == null)
        return null;
    PsiExpression switchExpression = switchStatement.getExpression();
    PsiType switchType = switchExpression == null ? PsiType.INT : switchExpression.getType();
    // check constant expression
    PsiExpression caseValue = statement.getCaseValue();
    // Every case constant expression associated with a switch statement must be assignable ($5.2) to the type of the switch Expression.
    if (caseValue != null && switchExpression != null) {
        HighlightInfo highlightInfo = checkAssignability(switchType, caseValue.getType(), caseValue, caseValue);
        if (highlightInfo != null)
            return highlightInfo;
    }
    Object value = null;
    boolean isEnumSwitch = false;
    if (!statement.isDefaultCase() && caseValue != null) {
        if (caseValue instanceof PsiReferenceExpression) {
            PsiElement element = ((PsiReferenceExpression) caseValue).resolve();
            if (element instanceof PsiEnumConstant) {
                isEnumSwitch = true;
                value = ((PsiEnumConstant) element).getName();
                if (((PsiReferenceExpression) caseValue).getQualifier() != null) {
                    String message = JavaErrorMessages.message("qualified.enum.constant.in.switch");
                    return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(caseValue).descriptionAndTooltip(message).create();
                }
            }
        }
        if (!isEnumSwitch) {
            value = ConstantExpressionUtil.computeCastTo(caseValue, switchType);
        }
        if (value == null) {
            String description = JavaErrorMessages.message("constant.expression.required");
            return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(caseValue).descriptionAndTooltip(description).create();
        }
    }
    // check duplicate
    PsiStatement[] statements = switchStatement.getBody().getStatements();
    for (PsiStatement st : statements) {
        if (st == statement)
            continue;
        if (!(st instanceof PsiSwitchLabelStatement))
            continue;
        PsiSwitchLabelStatement labelStatement = (PsiSwitchLabelStatement) st;
        if (labelStatement.isDefaultCase() != statement.isDefaultCase())
            continue;
        PsiExpression caseExpr = labelStatement.getCaseValue();
        if (isEnumSwitch && caseExpr instanceof PsiReferenceExpression) {
            PsiElement element = ((PsiReferenceExpression) caseExpr).resolve();
            if (!(element instanceof PsiEnumConstant && Comparing.equal(((PsiEnumConstant) element).getName(), value)))
                continue;
        } else {
            // not assignable error already caught
            if (!TypeConversionUtil.areTypesAssignmentCompatible(switchType, caseExpr))
                continue;
            if (!Comparing.equal(ConstantExpressionUtil.computeCastTo(caseExpr, switchType), value))
                continue;
        }
        String description = statement.isDefaultCase() ? JavaErrorMessages.message("duplicate.default.switch.label") : JavaErrorMessages.message("duplicate.switch.label", value);
        PsiElement element = value == null ? statement : caseValue;
        return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(description).create();
    }
    // must be followed with colon
    PsiElement lastChild = statement.getLastChild();
    while (lastChild instanceof PsiComment || lastChild instanceof PsiWhiteSpace) {
        lastChild = lastChild.getPrevSibling();
    }
    if (!(lastChild instanceof PsiJavaToken && ((PsiJavaToken) lastChild).getTokenType() == JavaTokenType.COLON)) {
        int start = statement.getTextRange().getEndOffset();
        int end = statement.getTextRange().getEndOffset() + 1;
        String description = JavaErrorMessages.message("switch.colon.expected.after.case.label");
        CharSequence chars = statement.getContainingFile().getViewProvider().getContents();
        boolean isAfterEndOfLine = end >= chars.length() || chars.charAt(start) == '\n' || chars.charAt(start) == '\r';
        HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(start, end).descriptionAndTooltip(description);
        if (isAfterEndOfLine) {
            builder.endOfLine();
        }
        return builder.create();
    }
    return null;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Example 79 with HighlightInfo

use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.

the class HighlightUtil method checkWithImprovedCatchAnalysis.

@Nullable
static Collection<HighlightInfo> checkWithImprovedCatchAnalysis(@NotNull PsiParameter parameter, @NotNull Collection<PsiClassType> thrownInTryStatement, @NotNull PsiFile containingFile) {
    final PsiElement scope = parameter.getDeclarationScope();
    if (!(scope instanceof PsiCatchSection))
        return null;
    final PsiCatchSection catchSection = (PsiCatchSection) scope;
    final PsiCatchSection[] allCatchSections = catchSection.getTryStatement().getCatchSections();
    final int idx = ArrayUtilRt.find(allCatchSections, catchSection);
    if (idx <= 0)
        return null;
    final Collection<PsiClassType> thrownTypes = ContainerUtil.newHashSet(thrownInTryStatement);
    final PsiManager manager = containingFile.getManager();
    final GlobalSearchScope parameterResolveScope = parameter.getResolveScope();
    thrownTypes.add(PsiType.getJavaLangError(manager, parameterResolveScope));
    thrownTypes.add(PsiType.getJavaLangRuntimeException(manager, parameterResolveScope));
    final Collection<HighlightInfo> result = ContainerUtil.newArrayList();
    final List<PsiTypeElement> parameterTypeElements = PsiUtil.getParameterTypeElements(parameter);
    final boolean isMultiCatch = parameterTypeElements.size() > 1;
    for (PsiTypeElement catchTypeElement : parameterTypeElements) {
        final PsiType catchType = catchTypeElement.getType();
        if (ExceptionUtil.isGeneralExceptionType(catchType))
            continue;
        // collect exceptions which are caught by this type
        Collection<PsiClassType> caught = ContainerUtil.findAll(thrownTypes, catchType::isAssignableFrom);
        if (caught.isEmpty())
            continue;
        final Collection<PsiClassType> caughtCopy = ContainerUtil.newHashSet(caught);
        // exclude all which are caught by previous catch sections
        for (int i = 0; i < idx; i++) {
            final PsiParameter prevCatchParameter = allCatchSections[i].getParameter();
            if (prevCatchParameter == null)
                continue;
            for (PsiTypeElement prevCatchTypeElement : PsiUtil.getParameterTypeElements(prevCatchParameter)) {
                final PsiType prevCatchType = prevCatchTypeElement.getType();
                caught.removeIf(prevCatchType::isAssignableFrom);
                if (caught.isEmpty())
                    break;
            }
        }
        // check & warn
        if (caught.isEmpty()) {
            final String message = JavaErrorMessages.message("exception.already.caught.warn", formatTypes(caughtCopy), caughtCopy.size());
            final HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.WARNING).range(catchSection).descriptionAndTooltip(message).create();
            if (isMultiCatch) {
                QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createDeleteMultiCatchFix(catchTypeElement));
            } else {
                QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createDeleteCatchFix(parameter));
            }
            result.add(highlightInfo);
        }
    }
    return result;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope)

Example 80 with HighlightInfo

use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.

the class HighlightUtil method checkFeature.

@Nullable
static HighlightInfo checkFeature(@NotNull PsiElement element, @NotNull Feature feature, @NotNull LanguageLevel level, @NotNull PsiFile file) {
    if (file.getManager().isInProject(file) && !level.isAtLeast(feature.level)) {
        String message = getUnsupportedFeatureMessage(element, feature, level, file);
        HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(message).create();
        QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createIncreaseLanguageLevelFix(feature.level));
        QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createShowModulePropertiesFix(element));
        return info;
    }
    return null;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Aggregations

HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)221 Nullable (org.jetbrains.annotations.Nullable)51 TextRange (com.intellij.openapi.util.TextRange)33 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)30 VirtualFile (com.intellij.openapi.vfs.VirtualFile)28 NotNull (org.jetbrains.annotations.NotNull)17 HighlightInfoType (com.intellij.codeInsight.daemon.impl.HighlightInfoType)16 Document (com.intellij.openapi.editor.Document)12 ArrayList (java.util.ArrayList)11 PsiElement (com.intellij.psi.PsiElement)10 File (java.io.File)8 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)7 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)6 Pair (com.intellij.openapi.util.Pair)6 PsiFile (com.intellij.psi.PsiFile)6 Editor (com.intellij.openapi.editor.Editor)5 NonNls (org.jetbrains.annotations.NonNls)5 StringUtil (com.intellij.openapi.util.text.StringUtil)4 IElementType (com.intellij.psi.tree.IElementType)4 ContainerUtil (com.intellij.util.containers.ContainerUtil)4