Search in sources :

Example 36 with IndexNotReadyException

use of com.intellij.openapi.project.IndexNotReadyException in project intellij-community by JetBrains.

the class CompletionData method getReferencePrefix.

@Nullable
public static String getReferencePrefix(@NotNull PsiElement insertedElement, int offsetInFile) {
    try {
        final PsiReference ref = insertedElement.getContainingFile().findReferenceAt(offsetInFile);
        if (ref != null) {
            final List<TextRange> ranges = ReferenceRange.getRanges(ref);
            final PsiElement element = ref.getElement();
            final int elementStart = element.getTextRange().getStartOffset();
            for (TextRange refRange : ranges) {
                if (refRange.contains(offsetInFile - elementStart)) {
                    final int endIndex = offsetInFile - elementStart;
                    final int beginIndex = refRange.getStartOffset();
                    if (beginIndex > endIndex) {
                        LOG.error("Inconsistent reference (found at offset not included in its range): ref=" + ref + " element=" + element + " text=" + element.getText());
                    }
                    if (beginIndex < 0) {
                        LOG.error("Inconsistent reference (begin < 0): ref=" + ref + " element=" + element + "; begin=" + beginIndex + " text=" + element.getText());
                    }
                    LOG.assertTrue(endIndex >= 0);
                    return element.getText().substring(beginIndex, endIndex);
                }
            }
        }
    } catch (IndexNotReadyException ignored) {
    }
    return null;
}
Also used : IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) TextRange(com.intellij.openapi.util.TextRange) LookupValueWithUIHint(com.intellij.codeInsight.lookup.LookupValueWithUIHint) Nullable(org.jetbrains.annotations.Nullable)

Example 37 with IndexNotReadyException

use of com.intellij.openapi.project.IndexNotReadyException in project intellij-community by JetBrains.

the class AbstractLayoutCodeProcessor method runLayoutCodeProcess.

private void runLayoutCodeProcess(final Runnable readAction, final Runnable writeAction) {
    final ProgressWindow progressWindow = new ProgressWindow(true, myProject);
    progressWindow.setTitle(myCommandName);
    progressWindow.setText(myProgressText);
    final ModalityState modalityState = ModalityState.current();
    final Runnable process = () -> ApplicationManager.getApplication().runReadAction(readAction);
    Runnable runnable = () -> {
        try {
            ProgressManager.getInstance().runProcess(process, progressWindow);
        } catch (ProcessCanceledException e) {
            return;
        } catch (IndexNotReadyException e) {
            LOG.warn(e);
            return;
        }
        final Runnable writeRunnable = () -> CommandProcessor.getInstance().executeCommand(myProject, () -> {
            try {
                writeAction.run();
                if (myPostRunnable != null) {
                    ApplicationManager.getApplication().invokeLater(myPostRunnable);
                }
            } catch (IndexNotReadyException e) {
                LOG.warn(e);
            }
        }, myCommandName, null);
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            writeRunnable.run();
        } else {
            ApplicationManager.getApplication().invokeLater(writeRunnable, modalityState, myProject.getDisposed());
        }
    };
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        runnable.run();
    } else {
        ApplicationManager.getApplication().executeOnPooledThread(runnable);
    }
}
Also used : ProgressWindow(com.intellij.openapi.progress.util.ProgressWindow) ModalityState(com.intellij.openapi.application.ModalityState) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 38 with IndexNotReadyException

use of com.intellij.openapi.project.IndexNotReadyException in project intellij-community by JetBrains.

the class HighlightVisitorImpl method visitModifierList.

@Override
public void visitModifierList(PsiModifierList list) {
    super.visitModifierList(list);
    PsiElement parent = list.getParent();
    if (parent instanceof PsiMethod) {
        PsiMethod method = (PsiMethod) parent;
        if (!myHolder.hasErrorResults())
            myHolder.add(HighlightMethodUtil.checkMethodCanHaveBody(method, myLanguageLevel));
        MethodSignatureBackedByPsiMethod methodSignature = MethodSignatureBackedByPsiMethod.create(method, PsiSubstitutor.EMPTY);
        if (!method.isConstructor()) {
            try {
                List<HierarchicalMethodSignature> superMethodSignatures = method.getHierarchicalMethodSignature().getSuperSignatures();
                if (!superMethodSignatures.isEmpty()) {
                    if (!myHolder.hasErrorResults())
                        myHolder.add(HighlightMethodUtil.checkMethodIncompatibleReturnType(methodSignature, superMethodSignatures, true));
                    if (!myHolder.hasErrorResults())
                        myHolder.add(HighlightMethodUtil.checkMethodIncompatibleThrows(methodSignature, superMethodSignatures, true, method.getContainingClass()));
                    if (!method.hasModifierProperty(PsiModifier.STATIC)) {
                        if (!myHolder.hasErrorResults())
                            myHolder.add(HighlightMethodUtil.checkMethodWeakerPrivileges(methodSignature, superMethodSignatures, true, myFile));
                        if (!myHolder.hasErrorResults())
                            myHolder.add(HighlightMethodUtil.checkMethodOverridesFinal(methodSignature, superMethodSignatures));
                    }
                }
            } catch (IndexNotReadyException ignored) {
            }
        }
        PsiClass aClass = method.getContainingClass();
        if (!myHolder.hasErrorResults())
            myHolder.add(HighlightMethodUtil.checkMethodMustHaveBody(method, aClass));
        if (!myHolder.hasErrorResults())
            myHolder.add(HighlightMethodUtil.checkConstructorCallsBaseClassConstructor(method, myRefCountHolder, myResolveHelper));
        if (!myHolder.hasErrorResults())
            myHolder.add(HighlightMethodUtil.checkStaticMethodOverride(method, myFile));
        if (!myHolder.hasErrorResults() && aClass != null && myOverrideEquivalentMethodsVisitedClasses.add(aClass)) {
            myHolder.addAll(GenericsHighlightUtil.checkOverrideEquivalentMethods(aClass));
        }
    } else if (parent instanceof PsiClass) {
        PsiClass aClass = (PsiClass) parent;
        try {
            if (!myHolder.hasErrorResults())
                myHolder.add(HighlightClassUtil.checkDuplicateNestedClass(aClass));
            if (!myHolder.hasErrorResults()) {
                TextRange textRange = HighlightNamesUtil.getClassDeclarationTextRange(aClass);
                myHolder.add(HighlightClassUtil.checkClassMustBeAbstract(aClass, textRange));
            }
            if (!myHolder.hasErrorResults()) {
                myHolder.add(HighlightClassUtil.checkClassDoesNotCallSuperConstructorOrHandleExceptions(aClass, myRefCountHolder, myResolveHelper));
            }
            if (!myHolder.hasErrorResults())
                myHolder.add(HighlightMethodUtil.checkOverrideEquivalentInheritedMethods(aClass, myFile, myLanguageLevel));
            if (!myHolder.hasErrorResults() && myOverrideEquivalentMethodsVisitedClasses.add(aClass)) {
                myHolder.addAll(GenericsHighlightUtil.checkOverrideEquivalentMethods(aClass));
            }
            if (!myHolder.hasErrorResults())
                myHolder.add(HighlightClassUtil.checkCyclicInheritance(aClass));
        } catch (IndexNotReadyException ignored) {
        }
    } else if (parent instanceof PsiEnumConstant) {
        if (!myHolder.hasErrorResults())
            myHolder.addAll(GenericsHighlightUtil.checkEnumConstantModifierList(list));
    }
}
Also used : IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) TextRange(com.intellij.openapi.util.TextRange) LocalQuickFixAndIntentionActionOnPsiElement(com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)

Example 39 with IndexNotReadyException

use of com.intellij.openapi.project.IndexNotReadyException in project intellij-community by JetBrains.

the class HighlightVisitorImpl method visitReferenceExpression.

@Override
public void visitReferenceExpression(PsiReferenceExpression expression) {
    JavaResolveResult resultForIncompleteCode = doVisitReferenceElement(expression);
    if (!myHolder.hasErrorResults()) {
        visitExpression(expression);
        if (myHolder.hasErrorResults())
            return;
    }
    JavaResolveResult[] results = resolveOptimised(expression);
    if (results == null)
        return;
    JavaResolveResult result = results.length == 1 ? results[0] : JavaResolveResult.EMPTY;
    PsiElement resolved = result.getElement();
    if (resolved instanceof PsiVariable && resolved.getContainingFile() == expression.getContainingFile()) {
        if (!myHolder.hasErrorResults()) {
            try {
                myHolder.add(HighlightControlFlowUtil.checkVariableInitializedBeforeUsage(expression, (PsiVariable) resolved, myUninitializedVarProblems, myFile));
            } catch (IndexNotReadyException ignored) {
            }
        }
        PsiVariable variable = (PsiVariable) resolved;
        boolean isFinal = variable.hasModifierProperty(PsiModifier.FINAL);
        if (isFinal && !variable.hasInitializer()) {
            if (!myHolder.hasErrorResults()) {
                myHolder.add(HighlightControlFlowUtil.checkFinalVariableMightAlreadyHaveBeenAssignedTo(variable, expression, myFinalVarProblems));
            }
            if (!myHolder.hasErrorResults())
                myHolder.add(HighlightControlFlowUtil.checkFinalVariableInitializedInLoop(expression, resolved));
        }
    }
    PsiElement parent = expression.getParent();
    if (parent instanceof PsiMethodCallExpression && ((PsiMethodCallExpression) parent).getMethodExpression() == expression && (!result.isAccessible() || !result.isStaticsScopeCorrect())) {
        PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) parent;
        PsiExpressionList list = methodCallExpression.getArgumentList();
        if (!HighlightMethodUtil.isDummyConstructorCall(methodCallExpression, myResolveHelper, list, expression)) {
            try {
                myHolder.add(HighlightMethodUtil.checkAmbiguousMethodCallIdentifier(expression, results, list, resolved, result, methodCallExpression, myResolveHelper, myLanguageLevel, myFile));
                if (!PsiTreeUtil.findChildrenOfType(methodCallExpression.getArgumentList(), PsiLambdaExpression.class).isEmpty()) {
                    PsiElement nameElement = expression.getReferenceNameElement();
                    if (nameElement != null) {
                        myHolder.add(HighlightMethodUtil.checkAmbiguousMethodCallArguments(expression, results, list, resolved, result, methodCallExpression, myResolveHelper, nameElement));
                    }
                }
            } catch (IndexNotReadyException ignored) {
            }
        }
    }
    if (!myHolder.hasErrorResults() && resultForIncompleteCode != null) {
        myHolder.add(HighlightUtil.checkExpressionRequired(expression, resultForIncompleteCode));
    }
    if (!myHolder.hasErrorResults() && resolved instanceof PsiField) {
        try {
            myHolder.add(HighlightUtil.checkIllegalForwardReferenceToField(expression, (PsiField) resolved));
        } catch (IndexNotReadyException ignored) {
        }
    }
    if (!myHolder.hasErrorResults())
        myHolder.add(GenericsHighlightUtil.checkAccessStaticFieldFromEnumConstructor(expression, result));
    if (!myHolder.hasErrorResults())
        myHolder.add(HighlightUtil.checkClassReferenceAfterQualifier(expression, resolved));
    final PsiExpression qualifierExpression = expression.getQualifierExpression();
    myHolder.add(HighlightUtil.checkUnqualifiedSuperInDefaultMethod(myLanguageLevel, expression, qualifierExpression));
    if (!myHolder.hasErrorResults() && qualifierExpression != null) {
        PsiType type = qualifierExpression.getType();
        if (type instanceof PsiCapturedWildcardType) {
            type = ((PsiCapturedWildcardType) type).getUpperBound();
        }
        final PsiClass psiClass = PsiUtil.resolveClassInType(type);
        if (psiClass != null) {
            myHolder.add(GenericsHighlightUtil.areSupersAccessible(psiClass, expression));
        }
    }
    if (!myHolder.hasErrorResults() && resolved != null && myJavaModule != null) {
        myHolder.add(ModuleHighlightUtil.checkPackageAccessibility(expression, resolved, myJavaModule));
    }
}
Also used : IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) LocalQuickFixAndIntentionActionOnPsiElement(com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)

Example 40 with IndexNotReadyException

use of com.intellij.openapi.project.IndexNotReadyException in project intellij-community by JetBrains.

the class HighlightVisitorImpl method visitMethodReferenceExpression.

@Override
public void visitMethodReferenceExpression(PsiMethodReferenceExpression expression) {
    myHolder.add(checkFeature(expression, Feature.METHOD_REFERENCES));
    final PsiElement parent = PsiUtil.skipParenthesizedExprUp(expression.getParent());
    if (parent instanceof PsiExpressionStatement)
        return;
    final JavaResolveResult result;
    final JavaResolveResult[] results;
    try {
        results = expression.multiResolve(true);
        result = results.length == 1 ? results[0] : JavaResolveResult.EMPTY;
    } catch (IndexNotReadyException e) {
        return;
    }
    if (myRefCountHolder != null) {
        myRefCountHolder.registerReference(expression, result);
    }
    final PsiElement method = result.getElement();
    if (method != null && !result.isAccessible()) {
        final String accessProblem = HighlightUtil.buildProblemWithAccessDescription(expression, result);
        HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(accessProblem).create();
        HighlightUtil.registerAccessQuickFixAction((PsiMember) method, expression, info, result.getCurrentFileResolveScope());
        myHolder.add(info);
    } else {
        final TextAttributesScheme colorsScheme = myHolder.getColorsScheme();
        if (method instanceof PsiMethod && !expression.isConstructor()) {
            PsiElement methodNameElement = expression.getReferenceNameElement();
            if (methodNameElement != null) {
                myHolder.add(HighlightNamesUtil.highlightMethodName((PsiMethod) method, methodNameElement, false, colorsScheme));
            }
        }
        myHolder.add(HighlightNamesUtil.highlightClassNameInQualifier(expression, colorsScheme));
    }
    if (!LambdaUtil.isValidLambdaContext(parent)) {
        String description = "Method reference expression is not expected here";
        myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create());
    }
    final PsiType functionalInterfaceType = expression.getFunctionalInterfaceType();
    if (!myHolder.hasErrorResults()) {
        if (functionalInterfaceType != null) {
            final boolean notFunctional = !LambdaUtil.isFunctionalType(functionalInterfaceType);
            if (notFunctional) {
                String description = functionalInterfaceType.getPresentableText() + " is not a functional interface";
                myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create());
            }
        }
    }
    if (!myHolder.hasErrorResults()) {
        final PsiElement referenceNameElement = expression.getReferenceNameElement();
        if (referenceNameElement instanceof PsiKeyword) {
            if (!PsiMethodReferenceUtil.isValidQualifier(expression)) {
                PsiElement qualifier = expression.getQualifier();
                if (qualifier != null) {
                    String description = "Cannot find class " + qualifier.getText();
                    myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(qualifier).descriptionAndTooltip(description).create());
                }
            }
        }
    }
    if (!myHolder.hasErrorResults()) {
        checkFunctionalInterfaceTypeAccessible(expression, functionalInterfaceType);
    }
    if (!myHolder.hasErrorResults() && functionalInterfaceType != null) {
        final String errorMessage = PsiMethodReferenceUtil.checkMethodReferenceContext(expression);
        if (errorMessage != null) {
            final HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(errorMessage).create();
            if (method instanceof PsiMethod && !((PsiMethod) method).isConstructor() && !((PsiMethod) method).hasModifierProperty(PsiModifier.ABSTRACT)) {
                final boolean shouldHave = !((PsiMethod) method).hasModifierProperty(PsiModifier.STATIC);
                final LocalQuickFixAndIntentionActionOnPsiElement fixStaticModifier = QuickFixFactory.getInstance().createModifierListFix((PsiModifierListOwner) method, PsiModifier.STATIC, shouldHave, false);
                QuickFixAction.registerQuickFixAction(info, fixStaticModifier);
            }
            myHolder.add(info);
        }
    }
    if (!myHolder.hasErrorResults()) {
        PsiElement qualifier = expression.getQualifier();
        if (qualifier instanceof PsiTypeElement) {
            final PsiType psiType = ((PsiTypeElement) qualifier).getType();
            final HighlightInfo genericArrayCreationInfo = GenericsHighlightUtil.checkGenericArrayCreation(qualifier, psiType);
            if (genericArrayCreationInfo != null) {
                myHolder.add(genericArrayCreationInfo);
            } else {
                final String wildcardMessage = PsiMethodReferenceUtil.checkTypeArguments((PsiTypeElement) qualifier, psiType);
                if (wildcardMessage != null) {
                    myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(qualifier).descriptionAndTooltip(wildcardMessage).create());
                }
            }
        }
    }
    if (!myHolder.hasErrorResults()) {
        myHolder.add(PsiMethodReferenceHighlightingUtil.checkRawConstructorReference(expression));
    }
    if (!myHolder.hasErrorResults()) {
        myHolder.add(HighlightUtil.checkUnhandledExceptions(expression, expression.getTextRange()));
    }
    if (!myHolder.hasErrorResults()) {
        final String badReturnTypeMessage = PsiMethodReferenceUtil.checkReturnType(expression, result, functionalInterfaceType);
        if (badReturnTypeMessage != null) {
            myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(badReturnTypeMessage).create());
        }
    }
    if (!myHolder.hasErrorResults()) {
        if (results.length == 0 || results[0] instanceof MethodCandidateInfo && !((MethodCandidateInfo) results[0]).isApplicable() && functionalInterfaceType != null) {
            String description = null;
            if (results.length == 1) {
                description = ((MethodCandidateInfo) results[0]).getInferenceErrorMessage();
            }
            if (expression.isConstructor()) {
                final PsiClass containingClass = PsiMethodReferenceUtil.getQualifierResolveResult(expression).getContainingClass();
                if (containingClass != null) {
                    if (!myHolder.add(HighlightClassUtil.checkInstantiationOfAbstractClass(containingClass, expression)) && !myHolder.add(GenericsHighlightUtil.checkEnumInstantiation(expression, containingClass)) && containingClass.isPhysical() && description == null) {
                        description = JavaErrorMessages.message("cannot.resolve.constructor", containingClass.getName());
                    }
                }
            } else if (description == null) {
                description = JavaErrorMessages.message("cannot.resolve.method", expression.getReferenceName());
            }
            if (description != null) {
                final PsiElement referenceNameElement = expression.getReferenceNameElement();
                final HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(results.length == 0 ? HighlightInfoType.WRONG_REF : HighlightInfoType.ERROR).descriptionAndTooltip(description).range(referenceNameElement).create();
                myHolder.add(highlightInfo);
                final TextRange fixRange = HighlightMethodUtil.getFixRange(referenceNameElement);
                QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QuickFixFactory.getInstance().createCreateMethodFromUsageFix(expression));
            }
        }
    }
}
Also used : TextAttributesScheme(com.intellij.openapi.editor.colors.TextAttributesScheme) TextRange(com.intellij.openapi.util.TextRange) LocalQuickFixAndIntentionActionOnPsiElement(com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement) MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) LocalQuickFixAndIntentionActionOnPsiElement(com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)

Aggregations

IndexNotReadyException (com.intellij.openapi.project.IndexNotReadyException)70 Nullable (org.jetbrains.annotations.Nullable)14 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)12 PsiElement (com.intellij.psi.PsiElement)11 Project (com.intellij.openapi.project.Project)10 PsiFile (com.intellij.psi.PsiFile)10 TextRange (com.intellij.openapi.util.TextRange)9 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 NotNull (org.jetbrains.annotations.NotNull)8 Document (com.intellij.openapi.editor.Document)6 Editor (com.intellij.openapi.editor.Editor)6 LocalQuickFixAndIntentionActionOnPsiElement (com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)5 Ref (com.intellij.openapi.util.Ref)4 LightweightHint (com.intellij.ui.LightweightHint)4 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)3 Module (com.intellij.openapi.module.Module)3 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)3 StringUtil (com.intellij.openapi.util.text.StringUtil)3 PsiClass (com.intellij.psi.PsiClass)3 javax.swing (javax.swing)3