Search in sources :

Example 26 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.

the class VariableInIncompleteCodeSearcher method processQuery.

@Override
public void processQuery(@NotNull final ReferencesSearch.SearchParameters p, @NotNull final Processor<PsiReference> consumer) {
    final PsiElement refElement = p.getElementToSearch();
    if (!refElement.isValid() || !(refElement instanceof PsiVariable))
        return;
    final String name = ((PsiVariable) refElement).getName();
    if (StringUtil.isEmptyOrSpaces(name))
        return;
    SearchScope scope = p.getEffectiveSearchScope();
    if (!(scope instanceof LocalSearchScope)) {
        final PsiFile file = refElement.getContainingFile();
        if (file == null || file instanceof PsiCompiledElement)
            return;
        //process incomplete references to the 'field' in the same file only
        scope = new LocalSearchScope(new PsiElement[] { file }, null, !PsiSearchHelperImpl.shouldProcessInjectedPsi(p.getScopeDeterminedByUser()));
    } else {
        PsiElement[] elements = ((LocalSearchScope) scope).getScope();
        PsiElement[] sourceElements = ContainerUtil.findAllAsArray(elements, e -> !(e instanceof PsiCompiledElement));
        if (sourceElements.length != elements.length) {
            if (sourceElements.length == 0)
                return;
            scope = new LocalSearchScope(sourceElements);
        }
    }
    PsiElement[] elements = ((LocalSearchScope) scope).getScope();
    if (elements.length == 0)
        return;
    PsiSearchHelper.SERVICE.getInstance(p.getProject()).processElementsWithWord((element, offsetInElement) -> {
        for (PsiElement child = element.findElementAt(offsetInElement); child != null; child = child.getParent()) {
            if (!child.textMatches(name)) {
                break;
            }
            if (child instanceof PsiJavaCodeReferenceElement) {
                final PsiJavaCodeReferenceElement ref = (PsiJavaCodeReferenceElement) child;
                if (!ref.isQualified() && !(ref.getParent() instanceof PsiMethodCallExpression) && ref.resolve() == null && ref.advancedResolve(true).getElement() == refElement) {
                    consumer.process(ref);
                }
            }
        }
        return true;
    }, scope, name, UsageSearchContext.ANY, true);
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope)

Example 27 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.

the class ResolveVariable15Test method testNavigateToEnumFunction.

public void testNavigateToEnumFunction() throws Exception {
    PsiElement element = resolveTarget();
    assertTrue(element instanceof PsiMethod);
    PsiClass aClass = ((PsiMethod) element).getContainingClass();
    assertTrue(aClass instanceof PsiEnumConstantInitializer);
    SearchScope scope = element.getUseScope();
    assertFalse(scope instanceof LocalSearchScope);
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope)

Example 28 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.

the class MethodReturnTypeFix method changeClassTypeArgument.

private static boolean changeClassTypeArgument(PsiMethod myMethod, Project project, PsiType superReturnType, PsiClass superClass, Editor editor, PsiType returnType) {
    if (superClass == null || !superClass.hasTypeParameters())
        return true;
    final PsiClass superReturnTypeClass = PsiUtil.resolveClassInType(superReturnType);
    if (superReturnTypeClass == null || !(superReturnTypeClass instanceof PsiTypeParameter || superReturnTypeClass.hasTypeParameters()))
        return true;
    final PsiClass derivedClass = myMethod.getContainingClass();
    if (derivedClass == null)
        return true;
    final PsiReferenceParameterList referenceParameterList = findTypeArgumentsList(superClass, derivedClass);
    if (referenceParameterList == null)
        return true;
    final PsiElement resolve = ((PsiJavaCodeReferenceElement) referenceParameterList.getParent()).resolve();
    if (!(resolve instanceof PsiClass))
        return true;
    final PsiClass baseClass = (PsiClass) resolve;
    if (returnType instanceof PsiPrimitiveType) {
        returnType = ((PsiPrimitiveType) returnType).getBoxedType(derivedClass);
    }
    final PsiSubstitutor superClassSubstitutor = TypeConversionUtil.getSuperClassSubstitutor(superClass, baseClass, PsiSubstitutor.EMPTY);
    final PsiType superReturnTypeInBaseClassType = superClassSubstitutor.substitute(superReturnType);
    final PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(project).getResolveHelper();
    final PsiSubstitutor psiSubstitutor = resolveHelper.inferTypeArguments(PsiTypesUtil.filterUnusedTypeParameters(superReturnTypeInBaseClassType, baseClass.getTypeParameters()), new PsiType[] { superReturnTypeInBaseClassType }, new PsiType[] { returnType }, PsiUtil.getLanguageLevel(superClass));
    final TypeMigrationRules rules = new TypeMigrationRules();
    final PsiSubstitutor compoundSubstitutor = TypeConversionUtil.getSuperClassSubstitutor(superClass, derivedClass, PsiSubstitutor.EMPTY).putAll(psiSubstitutor);
    rules.setBoundScope(new LocalSearchScope(derivedClass));
    TypeMigrationProcessor.runHighlightingTypeMigration(project, editor, rules, referenceParameterList, JavaPsiFacade.getElementFactory(project).createType(baseClass, compoundSubstitutor));
    return false;
}
Also used : TypeMigrationRules(com.intellij.refactoring.typeMigration.TypeMigrationRules) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) LocalQuickFixAndIntentionActionOnPsiElement(com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)

Example 29 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.

the class MatcherImpl method findMatches.

private boolean findMatches(MatchOptions options, CompiledPattern compiledPattern) {
    SearchScope searchScope = compiledPattern.getScope();
    final boolean ourOptimizedScope = searchScope != null;
    if (!ourOptimizedScope)
        searchScope = options.getScope();
    if (searchScope instanceof GlobalSearchScope) {
        final GlobalSearchScope scope = (GlobalSearchScope) searchScope;
        final ContentIterator ci = new ContentIterator() {

            @Override
            public boolean processFile(final VirtualFile fileOrDir) {
                if (!fileOrDir.isDirectory() && scope.contains(fileOrDir) && fileOrDir.getFileType() != FileTypes.UNKNOWN) {
                    ++totalFilesToScan;
                    scheduler.addOneTask(new MatchOneVirtualFile(fileOrDir));
                }
                return true;
            }
        };
        ApplicationManager.getApplication().runReadAction(() -> FileBasedIndex.getInstance().iterateIndexableFiles(ci, project, progress));
        progress.setText2("");
    } else {
        final PsiElement[] elementsToScan = ((LocalSearchScope) searchScope).getScope();
        totalFilesToScan = elementsToScan.length;
        for (int i = 0; i < elementsToScan.length; ++i) {
            final PsiElement psiElement = elementsToScan[i];
            if (psiElement == null)
                continue;
            scheduler.addOneTask(new MatchOnePsiFile(psiElement));
            // to prevent long PsiElement reference
            if (ourOptimizedScope)
                elementsToScan[i] = null;
        }
    }
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) ContentIterator(com.intellij.openapi.roots.ContentIterator) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope)

Example 30 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.

the class MatcherImpl method configureOptions.

private void configureOptions(MatchContext context, final Configuration configuration, PsiElement psiFile, final PairProcessor<MatchResult, Configuration> processor) {
    if (psiFile == null)
        return;
    LocalSearchScope scope = new LocalSearchScope(psiFile);
    matchContext.clear();
    matchContext.setMatcher(visitor);
    MatchOptions options = context.getOptions();
    matchContext.setOptions(options);
    matchContext.setPattern(context.getPattern());
    matchContext.setShouldRecursivelyMatch(context.shouldRecursivelyMatch());
    visitor.setMatchContext(matchContext);
    matchContext.setSink(new DuplicateFilteringResultSink(new DefaultMatchResultSink() {

        @Override
        public void newMatch(MatchResult result) {
            processor.process(result, configuration);
        }
    }));
    options.setScope(scope);
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) DuplicateFilteringResultSink(com.intellij.structuralsearch.plugin.util.DuplicateFilteringResultSink)

Aggregations

LocalSearchScope (com.intellij.psi.search.LocalSearchScope)113 SearchScope (com.intellij.psi.search.SearchScope)31 NotNull (org.jetbrains.annotations.NotNull)22 PsiElement (com.intellij.psi.PsiElement)19 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)19 Project (com.intellij.openapi.project.Project)18 Nullable (org.jetbrains.annotations.Nullable)13 ArrayList (java.util.ArrayList)12 VirtualFile (com.intellij.openapi.vfs.VirtualFile)11 UsageInfo (com.intellij.usageView.UsageInfo)11 TextRange (com.intellij.openapi.util.TextRange)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)9 com.intellij.psi (com.intellij.psi)8 PsiReference (com.intellij.psi.PsiReference)8 PsiFile (com.intellij.psi.PsiFile)7 ReferencesSearch (com.intellij.psi.search.searches.ReferencesSearch)7 AnalysisScope (com.intellij.analysis.AnalysisScope)6 Processor (com.intellij.util.Processor)6 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)5 Ref (com.intellij.openapi.util.Ref)4