下面列出了怎么用com.intellij.psi.PsiReferenceService的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public boolean processTextOccurrence(@Nonnull PsiElement element, int offsetInElement, @Nonnull final Processor<? super PsiReference> consumer) {
if (!myTarget.isValid()) {
return false;
}
final List<PsiReference> references = ourReferenceService.getReferences(element, new PsiReferenceService.Hints(myTarget, offsetInElement));
//noinspection ForLoopReplaceableByForEach
for (int i = 0; i < references.size(); i++) {
PsiReference ref = references.get(i);
ProgressManager.checkCanceled();
if (ReferenceRange.containsOffsetInElement(ref, offsetInElement) && ref.isReferenceTo(myTarget) && !consumer.process(ref)) {
return false;
}
}
return true;
}
@Override
public void addAcceptableReferenceProviders(@Nonnull PsiElement position,
@Nonnull List<ProviderInfo<Provider, ProcessingContext>> list,
@Nonnull PsiReferenceService.Hints hints) {
for (ProviderInfo<Provider, ElementPattern> trinity : myProviderPairs) {
if (hints != PsiReferenceService.Hints.NO_HINTS && !((PsiReferenceProvider)trinity.provider).acceptsHints(position, hints)) {
continue;
}
final ProcessingContext context = new ProcessingContext();
if (hints != PsiReferenceService.Hints.NO_HINTS) {
context.put(PsiReferenceService.HINTS, hints);
}
boolean suitable = false;
try {
suitable = trinity.processingContext.accepts(position, context);
}
catch (IndexNotReadyException ignored) {
}
if (suitable) {
list.add(new ProviderInfo<Provider, ProcessingContext>(trinity.provider, context, trinity.priority));
}
}
}
@Nonnull
List<ProviderBinding.ProviderInfo<PsiReferenceProvider,ProcessingContext>> getPairsByElement(@Nonnull PsiElement element,
@Nonnull PsiReferenceService.Hints hints) {
final Class<? extends PsiElement> clazz = element.getClass();
List<ProviderBinding.ProviderInfo<PsiReferenceProvider, ProcessingContext>> ret = null;
for (Class aClass : myKnownSupers.get(clazz)) {
SimpleProviderBinding<PsiReferenceProvider> simpleBinding = myBindingsMap.get(aClass);
NamedObjectProviderBinding<PsiReferenceProvider> namedBinding = myNamedBindingsMap.get(aClass);
if (simpleBinding == null && namedBinding == null) continue;
if (ret == null) ret = new SmartList<ProviderBinding.ProviderInfo<PsiReferenceProvider, ProcessingContext>>();
if (simpleBinding != null) {
simpleBinding.addAcceptableReferenceProviders(element, ret, hints);
}
if (namedBinding != null) {
namedBinding.addAcceptableReferenceProviders(element, ret, hints);
}
}
return ret == null ? Collections.<ProviderBinding.ProviderInfo<PsiReferenceProvider, ProcessingContext>>emptyList() : ret;
}
private void addMatchingProviders(final PsiElement position,
@Nullable final List<ProviderInfo<Provider, ElementPattern>> providerList,
@Nonnull List<ProviderInfo<Provider, ProcessingContext>> ret,
PsiReferenceService.Hints hints) {
if (providerList == null) return;
for (ProviderInfo<Provider, ElementPattern> trinity : providerList) {
if (hints != PsiReferenceService.Hints.NO_HINTS && !((PsiReferenceProvider)trinity.provider).acceptsHints(position, hints)) {
continue;
}
final ProcessingContext context = new ProcessingContext();
if (hints != PsiReferenceService.Hints.NO_HINTS) {
context.put(PsiReferenceService.HINTS, hints);
}
boolean suitable = false;
try {
suitable = trinity.processingContext.accepts(position, context);
}
catch (IndexNotReadyException ignored) {
}
if (suitable) {
ret.add(new ProviderInfo<Provider, ProcessingContext>(trinity.provider, context, trinity.priority));
}
}
}
@Nonnull
@Override
@RequiredReadAction
public PsiReference[] getReferences()
{
return PsiReferenceService.getService().getContributedReferences(this);
}
public static boolean processUsagesInText(@Nonnull final PsiElement element,
@Nonnull Collection<String> stringToSearch,
@Nonnull GlobalSearchScope searchScope,
@Nonnull Processor<UsageInfo> processor) {
final TextRange elementTextRange = ApplicationManager.getApplication().runReadAction(new NullableComputable<TextRange>() {
@Override
public TextRange compute() {
if (!element.isValid() || element instanceof PsiCompiledElement) return null;
return element.getTextRange();
}
});
UsageInfoFactory factory = new UsageInfoFactory() {
@Override
public UsageInfo createUsageInfo(@Nonnull PsiElement usage, int startOffset, int endOffset) {
if (elementTextRange != null
&& usage.getContainingFile() == element.getContainingFile()
&& elementTextRange.contains(startOffset)
&& elementTextRange.contains(endOffset)) {
return null;
}
PsiReference someReference = usage.findReferenceAt(startOffset);
if (someReference != null) {
PsiElement refElement = someReference.getElement();
for (PsiReference ref : PsiReferenceService.getService().getReferences(refElement, new PsiReferenceService.Hints(element, null))) {
if (element.getManager().areElementsEquivalent(ref.resolve(), element)) {
TextRange range = ref.getRangeInElement().shiftRight(refElement.getTextRange().getStartOffset() - usage.getTextRange().getStartOffset());
return new UsageInfo(usage, range.getStartOffset(), range.getEndOffset(), true);
}
}
}
return new UsageInfo(usage, startOffset, endOffset, true);
}
};
for (String s : stringToSearch) {
if (!PsiSearchHelperImpl.processTextOccurrences(element, s, searchScope, processor, factory)) return false;
}
return true;
}
public CoreApplicationEnvironment(@Nonnull Disposable parentDisposable) {
myParentDisposable = parentDisposable;
myFileTypeRegistry = new CoreFileTypeRegistry();
myApplication = createApplication(myParentDisposable);
ApplicationManager.setApplication(myApplication, myParentDisposable);
myLocalFileSystem = createLocalFileSystem();
myJarFileSystem = createJarFileSystem();
final InjectingContainer appContainer = myApplication.getInjectingContainer();
registerComponentInstance(appContainer, FileDocumentManager.class, new MockFileDocumentManagerImpl(DocumentImpl::new, null));
VirtualFileSystem[] fs = {myLocalFileSystem, myJarFileSystem};
VirtualFileManagerImpl virtualFileManager = new VirtualFileManagerImpl(myApplication, fs);
registerComponentInstance(appContainer, VirtualFileManager.class, virtualFileManager);
registerApplicationExtensionPoint(ASTLazyFactory.EP.getExtensionPointName(), ASTLazyFactory.class);
registerApplicationExtensionPoint(ASTLeafFactory.EP.getExtensionPointName(), ASTLeafFactory.class);
registerApplicationExtensionPoint(ASTCompositeFactory.EP.getExtensionPointName(), ASTCompositeFactory.class);
addExtension(ASTLazyFactory.EP.getExtensionPointName(), new DefaultASTLazyFactory());
addExtension(ASTLeafFactory.EP.getExtensionPointName(), new DefaultASTLeafFactory());
addExtension(ASTCompositeFactory.EP.getExtensionPointName(), new DefaultASTCompositeFactory());
registerApplicationService(EncodingManager.class, new CoreEncodingRegistry());
registerApplicationService(VirtualFilePointerManager.class, createVirtualFilePointerManager());
registerApplicationService(PsiBuilderFactory.class, new PsiBuilderFactoryImpl());
registerApplicationService(ReferenceProvidersRegistry.class, new MockReferenceProvidersRegistry());
registerApplicationService(StubTreeLoader.class, new CoreStubTreeLoader());
registerApplicationService(PsiReferenceService.class, new PsiReferenceServiceImpl());
registerApplicationService(MetaDataRegistrar.class, new MetaRegistry());
registerApplicationService(ProgressManager.class, createProgressIndicatorProvider());
registerApplicationService(JobLauncher.class, createJobLauncher());
registerApplicationService(CodeFoldingSettings.class, new CodeFoldingSettings());
registerApplicationService(CommandProcessor.class, new CoreCommandProcessor());
}
@Override
public void addAcceptableReferenceProviders(@Nonnull PsiElement position, @Nonnull List<ProviderInfo<Provider, ProcessingContext>> list, @Nonnull PsiReferenceService.Hints hints) {
String name = getName(position);
if (name != null) {
addMatchingProviders(position, myNamesToProvidersMap.get(name), list, hints);
addMatchingProviders(position, myNamesToProvidersMapInsensitive.get(name.toLowerCase()), list, hints);
}
}
@NotNull
public static PsiReference[] getReferences(CypherNamedElement element) {
return PsiReferenceService.getService().getContributedReferences(element);
}
@Nonnull
@Override
public PsiReference[] getReferences()
{
return PsiReferenceService.getService().getContributedReferences(this);
}
@Nonnull
@Override
public PsiReference[] getReferences()
{
return PsiReferenceService.getService().getContributedReferences(this);
}
@Override
protected PsiReference[] doGetReferencesFromProviders(PsiElement context, PsiReferenceService.Hints hints) {
return PsiReference.EMPTY_ARRAY;
}
void addAcceptableReferenceProviders(@Nonnull PsiElement position,
@Nonnull List<ProviderInfo<T, ProcessingContext>> list,
@Nonnull PsiReferenceService.Hints hints);