Search in sources :

Example 61 with HighlightInfo

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

the class LightAdvHighlightingTest method testUnusedInspectionNonPrivateMembersReferencedFromText.

public void testUnusedInspectionNonPrivateMembersReferencedFromText() {
    doTest(true);
    WriteCommandAction.runWriteCommandAction(null, () -> {
        PsiDirectory directory = myFile.getParent();
        assertNotNull(myFile.toString(), directory);
        PsiFile txt = directory.createFile("x.txt");
        VirtualFile vFile = txt.getVirtualFile();
        assertNotNull(txt.toString(), vFile);
        try {
            VfsUtil.saveText(vFile, "XXX");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
    List<HighlightInfo> infos = doHighlighting(HighlightSeverity.WARNING);
    assertEmpty(infos);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) IOException(java.io.IOException)

Example 62 with HighlightInfo

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

the class ImportHelperTest method testAutoImportSkipsClassReferenceInMethodPosition.

public void testAutoImportSkipsClassReferenceInMethodPosition() throws Throwable {
    @NonNls String text = "package x; import java.util.HashMap; class S { HashMap<String,String> f(){ return  Hash<caret>Map <String, String >();} }  ";
    configureByText(StdFileTypes.JAVA, text);
    boolean old = CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY;
    CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = true;
    DaemonCodeAnalyzerSettings.getInstance().setImportHintEnabled(true);
    try {
        List<HighlightInfo> errs = highlightErrors();
        assertTrue(errs.size() > 1);
        PsiJavaFile javaFile = (PsiJavaFile) getFile();
        assertEquals(1, javaFile.getImportList().getAllImportStatements().length);
        PsiReference ref = javaFile.findReferenceAt(getEditor().getCaretModel().getOffset());
        ImportClassFix fix = new ImportClassFix((PsiJavaCodeReferenceElement) ref);
        assertFalse(fix.isAvailable(getProject(), getEditor(), getFile()));
    } finally {
        CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = old;
    }
}
Also used : NonNls(org.jetbrains.annotations.NonNls) ImportClassFix(com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFix) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Example 63 with HighlightInfo

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

the class HighlightUtil method checkUnhandledExceptions.

@Nullable
static HighlightInfo checkUnhandledExceptions(@NotNull final PsiElement element, @Nullable TextRange textRange) {
    final List<PsiClassType> unhandledExceptions = ExceptionUtil.getUnhandledExceptions(element);
    if (unhandledExceptions.isEmpty())
        return null;
    final HighlightInfoType highlightType = getUnhandledExceptionHighlightType(element);
    if (highlightType == null)
        return null;
    if (textRange == null)
        textRange = element.getTextRange();
    final String description = getUnhandledExceptionsDescriptor(unhandledExceptions);
    HighlightInfo errorResult = HighlightInfo.newHighlightInfo(highlightType).range(textRange).descriptionAndTooltip(description).create();
    registerUnhandledExceptionFixes(element, errorResult, unhandledExceptions);
    return errorResult;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType)

Example 64 with HighlightInfo

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

the class HighlightUtil method checkVariableAlreadyDefined.

@Nullable
static HighlightInfo checkVariableAlreadyDefined(@NotNull PsiVariable variable) {
    if (variable instanceof ExternallyDefinedPsiElement)
        return null;
    boolean isIncorrect = false;
    PsiElement declarationScope = null;
    if (variable instanceof PsiLocalVariable || variable instanceof PsiParameter && ((declarationScope = ((PsiParameter) variable).getDeclarationScope()) instanceof PsiCatchSection || declarationScope instanceof PsiForeachStatement || declarationScope instanceof PsiLambdaExpression)) {
        @SuppressWarnings("unchecked") PsiElement scope = PsiTreeUtil.getParentOfType(variable, PsiFile.class, PsiMethod.class, PsiClassInitializer.class, PsiResourceList.class);
        VariablesNotProcessor proc = new VariablesNotProcessor(variable, false) {

            @Override
            protected boolean check(final PsiVariable var, final ResolveState state) {
                return (var instanceof PsiLocalVariable || var instanceof PsiParameter) && super.check(var, state);
            }
        };
        PsiIdentifier identifier = variable.getNameIdentifier();
        assert identifier != null : variable;
        PsiScopesUtil.treeWalkUp(proc, identifier, scope);
        if (scope instanceof PsiResourceList && proc.size() == 0) {
            scope = PsiTreeUtil.getParentOfType(variable, PsiFile.class, PsiMethod.class, PsiClassInitializer.class);
            PsiScopesUtil.treeWalkUp(proc, identifier, scope);
        }
        if (proc.size() > 0) {
            isIncorrect = true;
        } else if (declarationScope instanceof PsiLambdaExpression) {
            isIncorrect = checkSameNames(variable);
        }
    } else if (variable instanceof PsiField) {
        PsiField field = (PsiField) variable;
        PsiClass aClass = field.getContainingClass();
        if (aClass == null)
            return null;
        PsiField fieldByName = aClass.findFieldByName(variable.getName(), false);
        if (fieldByName != null && fieldByName != field) {
            isIncorrect = true;
        }
    } else {
        isIncorrect = checkSameNames(variable);
    }
    if (isIncorrect) {
        String description = JavaErrorMessages.message("variable.already.defined", variable.getName());
        PsiIdentifier identifier = variable.getNameIdentifier();
        assert identifier != null : variable;
        HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(identifier).descriptionAndTooltip(description).create();
        if (variable instanceof PsiLocalVariable) {
            QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createReuseVariableDeclarationFix((PsiLocalVariable) variable));
        }
        return highlightInfo;
    }
    return null;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) VariablesNotProcessor(com.intellij.psi.scope.processor.VariablesNotProcessor)

Example 65 with HighlightInfo

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

the class HighlightUtil method checkAssignmentOperatorApplicable.

@Nullable
static HighlightInfo checkAssignmentOperatorApplicable(@NotNull PsiAssignmentExpression assignment) {
    PsiJavaToken operationSign = assignment.getOperationSign();
    IElementType eqOpSign = operationSign.getTokenType();
    IElementType opSign = TypeConversionUtil.convertEQtoOperation(eqOpSign);
    if (opSign == null)
        return null;
    final PsiType lType = assignment.getLExpression().getType();
    final PsiExpression rExpression = assignment.getRExpression();
    if (rExpression == null)
        return null;
    final PsiType rType = rExpression.getType();
    HighlightInfo errorResult = null;
    if (!TypeConversionUtil.isBinaryOperatorApplicable(opSign, lType, rType, true)) {
        String operatorText = operationSign.getText().substring(0, operationSign.getText().length() - 1);
        String message = JavaErrorMessages.message("binary.operator.not.applicable", operatorText, JavaHighlightUtil.formatType(lType), JavaHighlightUtil.formatType(rType));
        errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(assignment).descriptionAndTooltip(message).create();
    }
    return errorResult;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) 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