static void chooseAmbiguousTargetAndPerform(
     @NotNull final Project project,
     final Editor editor,
     @NotNull PsiElementProcessor<PsiElement> processor) {
   if (editor == null) {
     Messages.showMessageDialog(
         project,
         FindBundle.message("find.no.usages.at.cursor.error"),
         CommonBundle.getErrorTitle(),
         Messages.getErrorIcon());
   } else {
     int offset = editor.getCaretModel().getOffset();
     boolean chosen =
         GotoDeclarationAction.chooseAmbiguousTarget(
             editor,
             offset,
             processor,
             FindBundle.message("find.usages.ambiguous.title", "crap"),
             null);
     if (!chosen) {
       ApplicationManager.getApplication()
           .invokeLater(
               new Runnable() {
                 @Override
                 public void run() {
                   if (editor.isDisposed() || !editor.getComponent().isShowing()) return;
                   HintManager.getInstance()
                       .showErrorHint(
                           editor, FindBundle.message("find.no.usages.at.cursor.error"));
                 }
               },
               project.getDisposed());
     }
   }
 }
Ejemplo n.º 2
0
  @Nullable
  private Info getInfoAt(@NotNull Editor editor, PsiFile file, int offset, BrowseMode browseMode) {
    PsiElement targetElement = null;

    if (browseMode == BrowseMode.TypeDeclaration) {
      try {
        targetElement = GotoTypeDeclarationAction.findSymbolType(editor, offset);
      } catch (IndexNotReadyException e) {
        showDumbModeNotification(myProject);
      }
    } else if (browseMode == BrowseMode.Declaration) {
      final PsiReference ref = TargetElementUtilBase.findReference(editor, offset);
      final List<PsiElement> resolvedElements =
          ref != null ? resolve(ref) : Collections.<PsiElement>emptyList();
      final PsiElement resolvedElement =
          resolvedElements.size() == 1 ? resolvedElements.get(0) : null;

      final PsiElement[] targetElements =
          GotoDeclarationAction.findTargetElementsNoVS(myProject, editor, offset, false);
      final PsiElement elementAtPointer =
          file.findElementAt(
              TargetElementUtilBase.adjustOffset(file, editor.getDocument(), offset));

      if (targetElements != null) {
        if (targetElements.length == 0) {
          return null;
        } else if (targetElements.length == 1) {
          if (targetElements[0] != resolvedElement
              && elementAtPointer != null
              && targetElements[0].isPhysical()) {
            return ref != null
                ? new InfoSingle(ref, targetElements[0])
                : new InfoSingle(elementAtPointer, targetElements[0]);
          }
        } else {
          return elementAtPointer != null ? new InfoMultiple(elementAtPointer) : null;
        }
      }

      if (resolvedElements.size() == 1) {
        return new InfoSingle(ref, resolvedElements.get(0));
      } else if (resolvedElements.size() > 1) {
        return elementAtPointer != null ? new InfoMultiple(elementAtPointer, ref) : null;
      }
    } else if (browseMode == BrowseMode.Implementation) {
      final PsiElement element =
          TargetElementUtilBase.getInstance()
              .findTargetElement(editor, ImplementationSearcher.getFlags(), offset);
      PsiElement[] targetElements =
          new ImplementationSearcher() {
            @Override
            @NotNull
            protected PsiElement[] searchDefinitions(final PsiElement element, Editor editor) {
              final List<PsiElement> found = new ArrayList<PsiElement>(2);
              DefinitionsScopedSearch.search(element, getSearchScope(element, editor))
                  .forEach(
                      new Processor<PsiElement>() {
                        @Override
                        public boolean process(final PsiElement psiElement) {
                          found.add(psiElement);
                          return found.size() != 2;
                        }
                      });
              return PsiUtilCore.toPsiElementArray(found);
            }
          }.searchImplementations(editor, element, offset);
      if (targetElements.length > 1) {
        PsiElement elementAtPointer = file.findElementAt(offset);
        if (elementAtPointer != null) {
          return new InfoMultiple(elementAtPointer);
        }
        return null;
      }
      if (targetElements.length == 1) {
        Navigatable descriptor = EditSourceUtil.getDescriptor(targetElements[0]);
        if (descriptor == null || !descriptor.canNavigate()) {
          return null;
        }
        targetElement = targetElements[0];
      }
    }

    if (targetElement != null && targetElement.isPhysical()) {
      PsiElement elementAtPointer = file.findElementAt(offset);
      if (elementAtPointer != null) {
        return new InfoSingle(elementAtPointer, targetElement);
      }
    }

    return null;
  }