下面列出了怎么用com.intellij.psi.search.PsiSearchHelper的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* @param project project
* @param key environment variable key
* @return All key usages, like getenv('KEY')
*/
@NotNull
public static PsiElement[] getKeyUsages(Project project, String key) {
List<PsiElement> targets = new ArrayList<>();
PsiSearchHelper searchHelper = PsiSearchHelper.getInstance(project);
Processor<PsiFile> psiFileProcessor = psiFile -> {
for (EnvironmentVariablesUsagesProvider provider : EnvironmentVariablesProviderUtil.USAGES_PROVIDERS) {
targets.addAll(EnvironmentVariablesUtil.getUsagesElementsByKey(key, provider.getUsages(psiFile)));
}
return true;
};
searchHelper.processAllFilesWithWord(key, GlobalSearchScope.allScope(project), psiFileProcessor, true);
searchHelper.processAllFilesWithWordInLiterals(key, GlobalSearchScope.allScope(project), psiFileProcessor);
return targets.toArray(PsiElement.EMPTY_ARRAY);
}
@RequiredReadAction
public static boolean mayRenameInplace(@Nonnull 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.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));
}
});
}
@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;
}
/**
* Search scope taken from PsiSearchHelper::getUseScope, which incorporates UseScopeEnlarger /
* UseScopeOptimizer EPs.
*/
public static PsiReference[] findReferencesInElementScope(PsiElement element) {
return findReferencesInScope(
element, PsiSearchHelper.SERVICE.getInstance(element.getProject()).getUseScope(element));
}
public static SearchScope getSearchScope(Editor editor, PsiElement element) {
return PsiSearchHelper.SERVICE.getInstance(element.getProject()).getUseScope(element);
}
@Override
protected boolean isInFileOnly() {
return super.isInFileOnly() ||
PsiSearchHelper.SERVICE.getInstance(myPsiElement.getProject()).getUseScope(myPsiElement) instanceof LocalSearchScope;
}