use of com.intellij.psi.search.SearchScope in project intellij-community by JetBrains.
the class JavaFxMethodSearcher method searchMethod.
private static void searchMethod(@NotNull PsiMethod psiMethod, @NotNull ReferencesSearch.SearchParameters queryParameters, @NotNull Processor<PsiReference> consumer) {
final Project project = PsiUtilCore.getProjectInReadAction(psiMethod);
final SearchScope scope = ReadAction.compute(queryParameters::getEffectiveSearchScope);
if (scope instanceof LocalSearchScope) {
final VirtualFile[] vFiles = ((LocalSearchScope) scope).getVirtualFiles();
for (VirtualFile vFile : vFiles) {
if (JavaFxFileTypeFactory.isFxml(vFile)) {
final PsiFile psiFile = PsiManager.getInstance(project).findFile(vFile);
if (psiFile != null) {
final Boolean goOn = ReadAction.compute(() -> searchMethodInFile(psiMethod, psiFile, consumer));
if (!goOn)
break;
}
}
}
} else if (scope instanceof GlobalSearchScope) {
final String propertyName = ReadAction.compute(() -> PropertyUtil.getPropertyName(psiMethod.getName()));
if (propertyName == null)
return;
final String className = ReadAction.compute(() -> {
final PsiClass psiClass = psiMethod.getContainingClass();
return psiClass != null ? psiClass.getName() : null;
});
if (className == null)
return;
final GlobalSearchScope fxmlScope = new JavaFxScopeEnlarger.GlobalFxmlSearchScope((GlobalSearchScope) scope);
final VirtualFile[] filteredFiles = ReadAction.compute(() -> CacheManager.SERVICE.getInstance(project).getVirtualFilesWithWord(className, UsageSearchContext.IN_PLAIN_TEXT, fxmlScope, true));
if (ArrayUtil.isEmpty(filteredFiles))
return;
final GlobalSearchScope filteredScope = GlobalSearchScope.filesScope(project, ContainerUtil.newHashSet(filteredFiles));
ApplicationManager.getApplication().runReadAction((Runnable) () -> CacheManager.SERVICE.getInstance(project).processFilesWithWord(file -> searchMethodInFile(psiMethod, file, consumer), propertyName, UsageSearchContext.IN_PLAIN_TEXT, filteredScope, true));
}
}
use of com.intellij.psi.search.SearchScope in project kotlin by JetBrains.
the class KtNamedDeclarationStub method getUseScope.
@NotNull
@Override
public SearchScope getUseScope() {
KtElement enclosingBlock = KtPsiUtil.getEnclosingElementForLocalDeclaration(this, false);
if (enclosingBlock != null) {
PsiElement enclosingParent = enclosingBlock.getParent();
if (enclosingParent instanceof KtContainerNode) {
enclosingParent = enclosingParent.getParent();
}
if (enclosingBlock instanceof KtBlockExpression && enclosingParent instanceof KtDoWhileExpression) {
KtExpression condition = ((KtDoWhileExpression) enclosingParent).getCondition();
if (condition != null)
return new LocalSearchScope(new PsiElement[] { enclosingBlock, condition });
}
return new LocalSearchScope(enclosingBlock);
}
if (hasModifier(KtTokens.PRIVATE_KEYWORD)) {
KtElement containingClass = PsiTreeUtil.getParentOfType(this, KtClassOrObject.class);
if (containingClass instanceof KtObjectDeclaration && ((KtObjectDeclaration) containingClass).isCompanion()) {
KtElement companionObjectClass = PsiTreeUtil.getParentOfType(containingClass, KtClassOrObject.class);
if (companionObjectClass != null) {
containingClass = companionObjectClass;
}
}
if (containingClass != null) {
return new LocalSearchScope(containingClass);
}
}
SearchScope scope = super.getUseScope();
KtClassOrObject classOrObject = KtPsiUtilKt.getContainingClassOrObject(this);
if (classOrObject != null) {
scope = scope.intersectWith(classOrObject.getUseScope());
}
return scope;
}
use of com.intellij.psi.search.SearchScope in project kotlin by JetBrains.
the class KotlinCallerMethodsTreeStructure method buildChildren.
@NotNull
@Override
protected Object[] buildChildren(@NotNull HierarchyNodeDescriptor descriptor) {
final PsiElement element = getTargetElement(descriptor);
KtElement codeBlockForLocalDeclaration = getEnclosingElementForLocalDeclaration(element);
if (codeBlockForLocalDeclaration != null) {
BindingContext bindingContext = ResolutionUtils.analyze((KtElement) element, BodyResolveMode.FULL);
final Map<PsiReference, PsiElement> referencesToElements = new HashMap<PsiReference, PsiElement>();
codeBlockForLocalDeclaration.accept(new CalleeReferenceVisitorBase(bindingContext, true) {
@Override
protected void processDeclaration(KtSimpleNameExpression reference, PsiElement declaration) {
if (!declaration.equals(element))
return;
//noinspection unchecked
PsiElement container = PsiTreeUtil.getParentOfType(reference, KtNamedFunction.class, KtPropertyAccessor.class, KtClassOrObject.class);
if (container instanceof KtPropertyAccessor) {
container = PsiTreeUtil.getParentOfType(container, KtProperty.class);
}
if (container != null) {
referencesToElements.put(ReferenceUtilKt.getMainReference(reference), container);
}
}
});
return collectNodeDescriptors(descriptor, referencesToElements, null);
}
SearchScope searchScope = getSearchScope(scopeType, basePsiClass);
Map<PsiElement, HierarchyNodeDescriptor> methodToDescriptorMap = Maps.newHashMap();
Object[] javaCallers = null;
if (element instanceof PsiMethod) {
javaCallers = javaTreeStructure.getChildElements(getJavaNodeDescriptor(descriptor));
processPsiMethodCallers(Collections.singleton((PsiMethod) element), descriptor, methodToDescriptorMap, searchScope, true);
}
if (element instanceof KtNamedFunction || element instanceof KtSecondaryConstructor) {
Collection<PsiMethod> lightMethods = LightClassUtil.INSTANCE.getLightClassMethods((KtFunction) element);
processPsiMethodCallers(lightMethods, descriptor, methodToDescriptorMap, searchScope, false);
}
if (element instanceof KtProperty) {
LightClassUtil.PropertyAccessorsPsiMethods propertyMethods = LightClassUtil.INSTANCE.getLightClassPropertyMethods((KtProperty) element);
processPsiMethodCallers(propertyMethods, descriptor, methodToDescriptorMap, searchScope, false);
}
if (element instanceof KtClassOrObject) {
KtPrimaryConstructor constructor = ((KtClassOrObject) element).getPrimaryConstructor();
if (constructor != null) {
PsiMethod lightMethod = LightClassUtil.INSTANCE.getLightClassMethod(constructor);
processPsiMethodCallers(Collections.singleton(lightMethod), descriptor, methodToDescriptorMap, searchScope, false);
} else {
processKtClassOrObjectCallers((KtClassOrObject) element, descriptor, methodToDescriptorMap, searchScope);
}
}
Object[] callers = methodToDescriptorMap.values().toArray(new Object[methodToDescriptorMap.size()]);
return (javaCallers != null) ? ArrayUtil.mergeArrays(javaCallers, callers) : callers;
}
use of com.intellij.psi.search.SearchScope in project intellij-community by JetBrains.
the class PyTargetExpressionImpl method getUseScope.
@NotNull
@Override
public SearchScope getUseScope() {
if (isQualified()) {
return super.getUseScope();
}
final ScopeOwner owner = ScopeUtil.getScopeOwner(this);
if (owner != null) {
final Scope scope = ControlFlowCache.getScope(owner);
if (scope.isGlobal(getName())) {
return GlobalSearchScope.projectScope(getProject());
}
if (scope.isNonlocal(getName())) {
return new LocalSearchScope(getContainingFile());
}
}
// find highest level function containing our var
PyElement container = this;
while (true) {
PyElement parentContainer = PsiTreeUtil.getParentOfType(container, PyFunction.class, PyClass.class);
if (parentContainer instanceof PyClass) {
if (isQualified()) {
return super.getUseScope();
}
break;
}
if (parentContainer == null) {
break;
}
container = parentContainer;
}
if (container instanceof PyFunction) {
return new LocalSearchScope(container);
}
return super.getUseScope();
}
use of com.intellij.psi.search.SearchScope in project intellij-plugins by JetBrains.
the class CucumberUtil method findPossibleGherkinElementUsages.
/**
* Passes to {@link com.intellij.psi.search.TextOccurenceProcessor} all elements in gherkin files that <em>may</em> have reference to
* provided argument. I.e: calling this function for string literal "(.+)foo" would find step "Given I am foo".
* To extract search text, {@link #getTheBiggestWordToSearchByIndex(String)} is used.
*
* @param stepDefinitionElement step defining element to search refs for.
* @param regexp regexp step should match
* @param processor each text occurence would be reported here
* @param effectiveSearchScope search scope
* @return whether reference was found and passed to processor
* @see #findGherkinReferencesToElement(com.intellij.psi.PsiElement, String, com.intellij.util.Processor, com.intellij.psi.search.SearchScope)
*/
public static boolean findPossibleGherkinElementUsages(@NotNull final PsiElement stepDefinitionElement, @NotNull final String regexp, @NotNull final TextOccurenceProcessor processor, @NotNull final SearchScope effectiveSearchScope) {
final String word = getTheBiggestWordToSearchByIndex(regexp);
if (StringUtil.isEmptyOrSpaces(word)) {
return true;
}
final SearchScope searchScope = CucumberStepSearchUtil.restrictScopeToGherkinFiles(() -> effectiveSearchScope);
final short context = (short) (UsageSearchContext.IN_STRINGS | UsageSearchContext.IN_CODE);
final PsiSearchHelper instance = SERVICE.getInstance(stepDefinitionElement.getProject());
return instance.processElementsWithWord(processor, searchScope, word, context, true);
}
Aggregations