Long running reference search in PsiReference multiResolve method

Hi,
We are developing a plugin that tries to to register new PsiReferences for certain PsiElements [1]. For these PsiReferences we want to implement a custom multiResolve method that handles the find Usages functionality on these references[2]. In order to do that we utilize ReferenceSearch to search for all usages of this PsiReference in the project[3]. The search takes a considerable amount of time, causing the UI to freeze. We tried to run the search in a backgroundTask[4], but the problem is that the multiResolve method is called so often by the IDE that the accumulating workload freezes the IDE.

IDE target version: 2024.2.4

[1] se.isselab.HAnS.referencing.FeatureReferenceContributor
[2] se.isselab.HAnS.referencing.FeatureReference#multiResolve (specifically the part for FileAnnotations)
[3] se.isselab.HAnS.featureLocation.FeatureLocationManager#getFeatureFileMapping
[4] se.isselab.HAnS.pluginExtensions.ProjectMetricsService#getFeatureFileMappingBackground

Just from quick look so far and AFAIU:

  1. (EASY) FeatureLocationManager#getFeatureFileMapping you can restrict the references search to files of matching type, that is, se.isselab.HAnS.codeAnnotation.CodeAnnotationFileType#INSTANCE and the other feature-file types by using com.intellij.psi.search.GlobalSearchScope#getScopeRestrictedByFileTypes. Possibly, rather huge projectScope() could be also more restricted, e.g. current Module (eventually with its dependencies).
  2. (COMPLEX) Introduce Stubs and corresponding indexes for your feature files, so locating feature by name becomes essentially a simple index lookup. See Stub Indexes | IntelliJ Platform Plugin SDK. This is probably the “proper” solution and will scale very well, regardless of project size.

Please remove all “background search/resolve” functionality, it will not behave properly.