private Boolean doExecute(ReferencesSearch.SearchParameters queryParameters, final Processor<PsiReference> consumer) { final PsiElement element = queryParameters.getElementToSearch(); //was selector_identifier->redefined in DictionaryComponent dictionaryComponent = null; if (element instanceof DictionaryComponent) { dictionaryComponent = (DictionaryComponent) element; } if (dictionaryComponent == null) return true; final List<String> parts = dictionaryComponent.getNameIdentifiers(); if (parts.isEmpty()) return true; final String componentName = dictionaryComponent.getName(); //or just getName()... final PsiSearchHelper helper = PsiSearchHelper.SERVICE.getInstance(element.getProject()); String searchWord = parts.get(0); return searchWord.isEmpty() || helper.processElementsWithWord(new MyOccurrenceProcessor(dictionaryComponent, componentName, consumer), queryParameters.getScopeDeterminedByUser(), searchWord, UsageSearchContext.IN_CODE, true); }
public static boolean mayRenameInplace(PsiElement elementToRename, final PsiElement nameSuggestionContext) { if (nameSuggestionContext != null && nameSuggestionContext.getContainingFile() != elementToRename.getContainingFile()) return false; if (!(elementToRename instanceof PsiLocalVariable) && !(elementToRename instanceof PsiParameter) && !(elementToRename instanceof PsiLabeledStatement)) { return false; } SearchScope useScope = PsiSearchHelper.SERVICE.getInstance(elementToRename.getProject()).getUseScope(elementToRename); if (!(useScope instanceof LocalSearchScope)) return false; PsiElement[] scopeElements = ((LocalSearchScope)useScope).getScope(); if (scopeElements.length > 1 && // assume there are no elements with use scopes with holes in them !isElementWithComment(scopeElements) && // ... except a case of element and it's doc comment !isResourceVariable(scopeElements)) { return false; // ... and badly scoped resource variables } PsiFile containingFile = elementToRename.getContainingFile(); return PsiTreeUtil.isAncestor(containingFile, scopeElements[0], false); }
/** * @return the class the specified method is used from, or null if it is * used from 0 or more than 1 other classes. */ @Nullable public PsiClass getUsageClass(final PsiMethod method) { final ProgressManager progressManager = ProgressManager.getInstance(); final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(method.getProject()); final String name = method.getName(); final GlobalSearchScope scope = GlobalSearchScope.allScope(method.getProject()); if (searchHelper.isCheapEnoughToSearch(name, scope, null, progressManager.getProgressIndicator()) == PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES) { return null; } progressManager.runProcess(new Runnable() { @Override public void run() { final Query<PsiReference> query = MethodReferencesSearch.search(method); if (!query.forEach(UsageProcessor.this)) { foundClass.set(null); } } }, null); return foundClass.get(); }
private static void processTextUsages(final Map<String, Set<PsiFile>> processedTextToFiles, final String text, final Map<String, Set<PsiFile>> processedFoundTextToFiles, final PsiSearchHelper searchHelper, final GlobalSearchScope scope) { if (!processedTextToFiles.containsKey(text)) { if (processedFoundTextToFiles.containsKey(text)) { final Set<PsiFile> filesWithValue = processedFoundTextToFiles.get(text); processedTextToFiles.put(text, filesWithValue); } else { final Set<PsiFile> resultFiles = new HashSet<PsiFile>(); findFilesWithText(text, searchHelper, scope, resultFiles); if (resultFiles.isEmpty()) return; processedTextToFiles.put(text, resultFiles); } } }
private static void findFilesWithText(String stringToFind, PsiSearchHelper searchHelper, GlobalSearchScope scope, final Set<PsiFile> resultFiles) { final List<String> words = StringUtil.getWordsIn(stringToFind); if (words.isEmpty()) return; Collections.sort(words, new Comparator<String>() { @Override public int compare(final String o1, final String o2) { return o2.length() - o1.length(); } }); for (String word : words) { final Set<PsiFile> files = new THashSet<PsiFile>(); searchHelper.processAllFilesWithWord(word, scope, new CommonProcessors.CollectProcessor<PsiFile>(files), true); if (resultFiles.isEmpty()) { resultFiles.addAll(files); } else { resultFiles.retainAll(files); } if (resultFiles.isEmpty()) return; } }
private static void findExtensionPointCandidates(PsiClass psiClass, final List<ExtensionPointCandidate> list) { String name = psiClass.getQualifiedName(); if (name == null) { return; } final Project project = psiClass.getProject(); final Collection<VirtualFile> candidates = DomService.getInstance().getDomFileCandidates(IdeaPlugin.class, project, GlobalSearchScope.allScope(project)); GlobalSearchScope scope = GlobalSearchScope.filesScope(project, candidates); PsiSearchHelper.SERVICE.getInstance(project).processUsagesInNonJavaFiles(name, new PsiNonJavaFileReferenceProcessor() { @Override public boolean process(PsiFile file, int startOffset, int endOffset) { PsiElement element = file.findElementAt(startOffset); processExtensionPointCandidate(element, list); return true; } }, scope); }
@Override public boolean isInplaceRenameAvailable(@NotNull PsiElement elementToRename, PsiElement nameSuggestionContext) { //local vars & params renames GrVariableInplaceRenameHandler if (nameSuggestionContext != null && nameSuggestionContext.getContainingFile() != elementToRename.getContainingFile()) return false; if (!(elementToRename instanceof GrLabeledStatement)) { return false; } SearchScope useScope = PsiSearchHelper.SERVICE.getInstance(elementToRename.getProject()).getUseScope(elementToRename); if (!(useScope instanceof LocalSearchScope)) return false; PsiElement[] scopeElements = ((LocalSearchScope)useScope).getScope(); if (scopeElements.length > 1) { return false; } PsiFile containingFile = elementToRename.getContainingFile(); return PsiTreeUtil.isAncestor(containingFile, scopeElements[0], false); }
/** * Finds all fragment definition across files in the project * * @return a list of known fragment definitions, or an empty list if the index is not yet ready */ public List<JSGraphQLFragmentDefinitionPsiElement> getKnownFragmentDefinitions() { try { final List<JSGraphQLFragmentDefinitionPsiElement> fragmentDefinitions = Lists.newArrayList(); PsiSearchHelper.SERVICE.getInstance(myProject).processElementsWithWord((psiElement, offsetInElement) -> { if (psiElement.getNode().getElementType() == JSGraphQLTokenTypes.KEYWORD && psiElement.getParent() instanceof JSGraphQLFragmentDefinitionPsiElement) { final JSGraphQLFragmentDefinitionPsiElement fragmentDefinition = (JSGraphQLFragmentDefinitionPsiElement) psiElement.getParent(); final String fragmentName = fragmentDefinition.getName(); if (fragmentName != null) { fragmentDefinitions.add(fragmentDefinition); } } return true; }, searchScope, "fragment", UsageSearchContext.IN_CODE, true, true); return fragmentDefinitions; } catch (IndexNotReadyException e) { // can't search yet (e.g. during project startup) } return Collections.emptyList(); }
@Override public boolean isInplaceRenameAvailable(PsiElement elementToRename, PsiElement nameSuggestionContext) { //local vars & params renames GrVariableInplaceRenameHandler if (nameSuggestionContext != null && nameSuggestionContext.getContainingFile() != elementToRename.getContainingFile()) return false; if (!(elementToRename instanceof GrLabeledStatement)) { return false; } SearchScope useScope = PsiSearchHelper.SERVICE.getInstance(elementToRename.getProject()).getUseScope(elementToRename); if (!(useScope instanceof LocalSearchScope)) return false; PsiElement[] scopeElements = ((LocalSearchScope)useScope).getScope(); if (scopeElements.length > 1) { return false; } PsiFile containingFile = elementToRename.getContainingFile(); return PsiTreeUtil.isAncestor(containingFile, scopeElements[0], false); }
public static boolean isTooExpensiveToSearch(PsiNamedElement element, boolean zeroResult) { final String name = element.getName(); if(name == null) { return true; } final ProgressManager progressManager = ProgressManager.getInstance(); final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(element.getProject()); final SearchScope useScope = element.getUseScope(); if(!(useScope instanceof GlobalSearchScope)) { return zeroResult; } final PsiSearchHelper.SearchCostResult cost = searchHelper.isCheapEnoughToSearch(name, (GlobalSearchScope) useScope, null, progressManager.getProgressIndicator()); if(cost == PsiSearchHelper.SearchCostResult.ZERO_OCCURRENCES) { return zeroResult; } return cost == PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES; }
@Override public void invoke(@NotNull final Project project, final Editor editor, PsiFile file) throws IncorrectOperationException { final TypeMigrationRules rules = new TypeMigrationRules(TypeMigrationLabeler.getElementType(myVar)); rules.setMigrationRootType(myExpressionType); rules.setBoundScope(PsiSearchHelper.SERVICE.getInstance(project).getUseScope(myVar)); TypeMigrationProcessor.runHighlightingTypeMigration(project, editor, rules, myVar); }
protected TurnRefsToSuperProcessorBase(Project project, boolean replaceInstanceOf, String superClassName) { super(project); mySuperClassName = superClassName; myManager = PsiManager.getInstance(project); mySearchHelper = PsiSearchHelper.SERVICE.getInstance(myManager.getProject()); myManager = PsiManager.getInstance(myProject); myReplaceInstanceOf = replaceInstanceOf; }
private static SearchScope getScope(final PsiSearchHelper helper, final PsiElement element) { SearchScope scope = helper.getUseScope(element); if (scope instanceof GlobalSearchScope) { scope = GlobalSearchScope.getScopeRestrictedByFileTypes((GlobalSearchScope)scope, StdFileTypes.JAVA, StdFileTypes.JSP, StdFileTypes.JSPX); } return scope; }
@NotNull public SearchScope getScope() { return ApplicationManager.getApplication().runReadAction(new Computable<SearchScope>() { @Override public SearchScope compute() { return myScope.intersectWith(PsiSearchHelper.SERVICE.getInstance(myElement.getProject()).getUseScope(myElement)); } }); }
@Override @NotNull public SearchScope getSearchScope(Editor editor, @NotNull PsiElement element) { TargetElementEvaluatorEx2 evaluator = getElementEvaluatorsEx2(element.getLanguage()); SearchScope result = evaluator != null ? evaluator.getSearchScope(editor, element) : null; return result != null ? result : PsiSearchHelper.SERVICE.getInstance(element.getProject()).getUseScope(element); }
@Nullable protected PsiElement checkLocalScope() { final SearchScope searchScope = PsiSearchHelper.SERVICE.getInstance(myElementToRename.getProject()).getUseScope(myElementToRename); if (searchScope instanceof LocalSearchScope) { final PsiElement[] elements = ((LocalSearchScope)searchScope).getScope(); return PsiTreeUtil.findCommonParent(elements); } return null; }
protected static int initOccurrencesNumber(PsiNameIdentifierOwner nameIdentifierOwner) { final ProgressManager progressManager = ProgressManager.getInstance(); final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(nameIdentifierOwner.getProject()); final GlobalSearchScope scope = GlobalSearchScope.projectScope(nameIdentifierOwner.getProject()); final String name = nameIdentifierOwner.getName(); final boolean isCheapToSearch = name != null && searchHelper.isCheapEnoughToSearch(name, scope, null, progressManager.getProgressIndicator()) != PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES; return isCheapToSearch ? ReferencesSearch.search(nameIdentifierOwner).findAll().size() : - 1; }
@Nullable protected PsiElement checkLocalScope(PsiElement element) { final SearchScope searchScope = PsiSearchHelper.SERVICE.getInstance(element.getProject()).getUseScope(element); if (searchScope instanceof LocalSearchScope) { final PsiElement[] elements = ((LocalSearchScope)searchScope).getScope(); return PsiTreeUtil.findCommonParent(elements); } return null; }
public boolean isOnlyAccessedFromInnerClass() { final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(method.getProject()); final ProgressManager progressManager = ProgressManager.getInstance(); final ProgressIndicator progressIndicator = progressManager.getProgressIndicator(); final PsiSearchHelper.SearchCostResult searchCost = searchHelper.isCheapEnoughToSearch(method.getName(), method.getResolveScope(), null, progressIndicator); if (searchCost == PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES || searchCost == PsiSearchHelper.SearchCostResult.ZERO_OCCURRENCES) { return onlyAccessedFromInnerClass; } final Query<PsiReference> query = ReferencesSearch.search(method); query.forEach(this); return onlyAccessedFromInnerClass; }
private boolean isCheapEnoughToSearch(PsiNamedElement element) { final String name = element.getName(); if (name == null) { return false; } final ProgressManager progressManager = ProgressManager.getInstance(); final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(element.getProject()); final SearchScope useScope = element.getUseScope(); if (useScope instanceof GlobalSearchScope) { return searchHelper.isCheapEnoughToSearch(name, (GlobalSearchScope)useScope, null, progressManager.getProgressIndicator()) != PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES; } return true; }
@Override public void processQuery(ReferencesSearch.SearchParameters searchParameters, final Processor<PsiReference> processor) { if (!(searchParameters.getElementToSearch() instanceof PhpDocProperty)) { return; } final PhpDocProperty property = (PhpDocProperty) searchParameters.getElementToSearch(); PsiReferenceProvider[] providers = new PsiReferenceProvider[]{ new CollectionPropertyReferenceProvider(), new SetValueReferenceProvider(), }; PsiSearchHelper.SERVICE.getInstance(property.getProject()) .processElementsWithWord((psiElement, i) -> { if (!(psiElement instanceof StringLiteralExpression)) { return true; } ProcessingContext processingContext = new ProcessingContext(); processingContext.put("field", property.getName()); for (PsiReferenceProvider provider : providers) { for (PsiReference reference : provider.getReferencesByElement(psiElement, processingContext)) { if (reference.isReferenceTo(searchParameters.getElementToSearch())) { processor.process(reference); } } } return true; }, searchParameters.getScopeDeterminedByUser(), property.getName(), UsageSearchContext.IN_STRINGS, true); }
/** * Gets the fragment name element that is the source of a fragment usage by searching across files * * @param fragmentUsage a specific fragment usage, e.g. '...FragmentName' * @return the fragment definition that the usage references, e.g. 'fragment FragmentName' */ public JSGraphQLNamedTypePsiElement resolveFragmentReference(@NotNull JSGraphQLNamedTypePsiElement fragmentUsage) { final String fragmentName = fragmentUsage.getName(); if (fragmentName != null) { final JSGraphQLNamedTypePsiElement cachedResult = fragmentDefinitionsByName.get(fragmentName); if (cachedResult != null) { return cachedResult; } final Ref<JSGraphQLNamedTypePsiElement> fragmentDefinitionRef = new Ref<>(); try { PsiSearchHelper.SERVICE.getInstance(myProject).processElementsWithWord((element, offsetInElement) -> { if (element instanceof JSGraphQLNamedTypePsiElement && element.getParent() instanceof JSGraphQLFragmentDefinitionPsiElement) { if (!element.equals(fragmentUsage)) { // only consider as a reference if the element is not the usage element final JSGraphQLNamedTypePsiElement fragmentDefinition = (JSGraphQLNamedTypePsiElement) element; fragmentDefinitionsByName.put(fragmentName, fragmentDefinition); fragmentDefinitionRef.set(fragmentDefinition); } return false; } return true; }, searchScope, fragmentName, UsageSearchContext.IN_CODE, true, true); } catch (IndexNotReadyException e) { // can't search yet (e.g. during project startup) } return fragmentDefinitionRef.get(); } return null; }
public void process() { GlobalSearchScope robotFilesInProject = GlobalSearchScope.getScopeRestrictedByFileTypes(GlobalSearchScope.projectScope(project), RobotFileType.INSTANCE); PsiSearchHelper.SERVICE.getInstance(project).processAllFilesWithWordInLiterals(TAGS_SETTING, robotFilesInProject, this); PsiSearchHelper.SERVICE.getInstance(project).processAllFilesWithWordInLiterals(FORCE_TAGS_SETTING, robotFilesInProject, this); PsiSearchHelper.SERVICE.getInstance(project).processAllFilesWithWordInLiterals(FORCE_TAGS_SETTING2, robotFilesInProject, this); }
private boolean isCheapEnoughToSearch(PsiNamedElement element) { final String name = element.getName(); if (name == null) { return false; } final ProgressManager progressManager = ProgressManager.getInstance(); final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(element.getProject()); final GlobalSearchScope scope = GlobalSearchScope.projectScope(element.getProject()); return searchHelper.isCheapEnoughToSearch(name, scope, null, progressManager.getProgressIndicator()) != PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES; }
@NotNull public ProcessingItem[] getProcessingItems(final CompileContext context) { if (!isEnabled()) { return ProcessingItem.EMPTY_ARRAY; } final Project project = context.getProject(); final Set<InstrumentationItem> result = new HashSet<InstrumentationItem>(); final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(context.getProject()); DumbService.getInstance(project).runReadActionInSmartMode(new Runnable() { public void run() { final String[] names = getAnnotationNames(project); for (String name : names) { final GlobalSearchScope scope = GlobalSearchScope.projectScope(project); final PsiClass psiClass = JavaPsiFacade.getInstance(project).findClass(name, GlobalSearchScope.allScope(project)); if (psiClass == null) { context.addMessage(CompilerMessageCategory.ERROR, "Cannot find class " + name, null, -1, -1); continue; } // wow, this is a sweet trick... ;) searchHelper.processAllFilesWithWord(StringUtil.getShortName(name), scope, new Processor<PsiFile>() { public boolean process(PsiFile psifile) { if (StdLanguages.JAVA == psifile.getLanguage() && psifile.getVirtualFile() != null && psifile instanceof PsiJavaFile) { addClassFiles((PsiJavaFile)psifile, result, project); } return true; } }, true); } } }); return result.toArray(new ProcessingItem[result.size()]); }
private static void findExtensionPointCandidates(PsiClass aClass, final List<ExtensionPointCandidate> list) { String name = aClass.getQualifiedName(); if (name == null) { return; } GlobalSearchScope scope = GlobalSearchScope.getScopeRestrictedByFileTypes(ProjectScope.getAllScope(aClass.getProject()), XmlFileType.INSTANCE); PsiSearchHelper.SERVICE.getInstance(aClass.getProject()).processUsagesInNonJavaFiles(name, new PsiNonJavaFileReferenceProcessor() { @Override public boolean process(PsiFile file, int startOffset, int endOffset) { PsiElement element = file.findElementAt(startOffset); processExtensionPointCandidate(element, list); return true; } }, scope); }
@RequiredReadAction public static boolean mayRenameInplace(@NotNull PsiElement elementToRename, @Nullable final PsiElement nameSuggestionContext) { if(nameSuggestionContext != null && nameSuggestionContext.getContainingFile() != elementToRename.getContainingFile()) { return false; } if(elementToRename instanceof CSharpTupleVariable || elementToRename instanceof CSharpTupleElementImpl) { return true; } if(elementToRename instanceof DotNetNamespaceAsElement) { return true; } if(!(elementToRename instanceof CSharpLocalVariable) && !(elementToRename instanceof DotNetParameter) && !(elementToRename instanceof CSharpLambdaParameter)) { return false; } SearchScope useScope = PsiSearchHelper.SERVICE.getInstance(elementToRename.getProject()).getUseScope(elementToRename); if(!(useScope instanceof LocalSearchScope)) { return false; } PsiElement[] scopeElements = ((LocalSearchScope) useScope).getScope(); if(scopeElements.length > 1) { return false; // ... and badly scoped resource variables } PsiFile containingFile = elementToRename.getContainingFile(); return PsiTreeUtil.isAncestor(containingFile, scopeElements[0], false); }
@Nonnull public SearchScope getScope() { return ApplicationManager.getApplication().runReadAction(new Computable<SearchScope>() { @Override public SearchScope compute() { return myScope.intersectWith(PsiSearchHelper.SERVICE.getInstance(myElement.getProject()).getUseScope(myElement)); } }); }