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);
}
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);
}
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;
}
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;
}
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);
}
Aggregations