package com.intellij.psi.search;

import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.PsiReferenceService;
import com.intellij.psi.ReferenceRange;
import com.intellij.util.Processor;
import org.jetbrains.annotations.NotNull;

/**
 * @author peter
 */
public final class SingleTargetRequestResultProcessor extends RequestResultProcessor {
  private static final PsiReferenceService ourReferenceService = PsiReferenceService.getService();
  private final PsiElement myTarget;

  public SingleTargetRequestResultProcessor(@NotNull PsiElement target) {
    super(target);
    myTarget = target;
  }

  public boolean processTextOccurrence(PsiElement element, int offsetInElement, final Processor<PsiReference> consumer) {
    for (PsiReference ref : ourReferenceService.getReferences(element, new PsiReferenceService.Hints(myTarget, offsetInElement))) {
      if (ReferenceRange.containsOffsetInElement(ref, offsetInElement)) {
        if (ref.isReferenceTo(myTarget)) {
          if (!consumer.process(ref)) {
            return false;
          }
        }
      }
    }
    return true;
  }

  @Override
  public String toString() {
    return "SingleTarget: " + myTarget;
  }
}

@@ -265,16 +265,40 @@
 tNull;%0A%0A
+import java.util.List;%0A%0A
 /**%0A * @
@@ -790,12 +790,18 @@
    f
-or (
+inal List%3C
 PsiR
@@ -812,14 +812,22 @@
 ence
+%3E
  ref
- :
+erences =
  our
@@ -865,16 +865,92 @@
 element,
+%0A                                                                           
  new Psi
@@ -999,16 +999,56 @@
 lement))
+;%0A    for (PsiReference ref : references
 ) %7B%0A    

use QueryExecutorBase