Exemplo n.º 1
0
    private void searchUsages() {
      UsageSearcher usageSearcher = mySearcherFactory.create();
      usageSearcher.generate(
          new Processor<Usage>() {
            public boolean process(final Usage usage) {
              checkSearchCanceled();

              boolean incrementCounter = true;
              final int i = myUsageCountWithoutDefinition.get();

              // Handle self reference (under definition we are invoked on) just to skip it
              if (mySearchFor.length == 1
                  && usage instanceof PsiElementUsage
                  && mySearchFor[0] instanceof PsiElementUsageTarget) {
                final PsiElement element = ((PsiElementUsage) usage).getElement();
                // TODO: self usage might be configurable
                if (element != null
                    && element.getParent()
                        == ((PsiElementUsageTarget) mySearchFor[0]).getElement()) {
                  incrementCounter = false;
                }
              }

              int usageCount =
                  incrementCounter ? myUsageCountWithoutDefinition.incrementAndGet() : i;
              if (usageCount == 1 && !myProcessPresentation.isShowPanelIfOnlyOneUsage()) {
                myFirstUsage.compareAndSet(null, usage);
              }
              UsageViewImpl usageView = getUsageView();
              if (usageView != null) {
                usageView.appendUsageLater(usage);
              }
              final ProgressIndicator indicator =
                  ProgressManager.getInstance().getProgressIndicator();
              return indicator == null || !indicator.isCanceled();
            }
          });
      if (getUsageView() != null) {
        ApplicationManager.getApplication()
            .invokeLater(
                new Runnable() {
                  public void run() {
                    showToolWindow(true);
                  }
                },
                myProject.getDisposed());
      }
    }
 public boolean isUsed(@NotNull PsiElement element, @NotNull FindUsagesOptions findUsagesOptions) {
   FindUsagesHandler handler = getFindUsagesHandler(element, true);
   if (handler == null) return false;
   UsageInfoToUsageConverter.TargetElementsDescriptor descriptor =
       new UsageInfoToUsageConverter.TargetElementsDescriptor(element);
   UsageSearcher usageSearcher = createUsageSearcher(descriptor, handler, findUsagesOptions, null);
   final AtomicBoolean used = new AtomicBoolean();
   usageSearcher.generate(
       new Processor<Usage>() {
         @Override
         public boolean process(final Usage usage) {
           used.set(true);
           return false;
         }
       });
   return used.get();
 }
  private static Usage findSiblingUsage(
      @NotNull final UsageSearcher usageSearcher,
      FileSearchScope dir,
      final FileEditorLocation currentLocation,
      @NotNull final boolean[] usagesWereFound,
      @NotNull FileEditor fileEditor) {
    if (fileEditor.getUserData(KEY_START_USAGE_AGAIN) != null) {
      dir =
          dir == FileSearchScope.AFTER_CARET
              ? FileSearchScope.FROM_START
              : FileSearchScope.FROM_END;
    }

    final FileSearchScope direction = dir;

    final Usage[] foundUsage = {null};
    usageSearcher.generate(
        new Processor<Usage>() {
          @Override
          public boolean process(Usage usage) {

            usagesWereFound[0] = true;

            if (direction == FileSearchScope.FROM_START) {
              foundUsage[0] = usage;
              return false;
            }
            if (direction == FileSearchScope.FROM_END) {
              foundUsage[0] = usage;
            } else if (direction == FileSearchScope.AFTER_CARET) {
              if (Comparing.compare(usage.getLocation(), currentLocation) > 0) {
                foundUsage[0] = usage;
                return false;
              }
            } else if (direction == FileSearchScope.BEFORE_CARET) {
              if (Comparing.compare(usage.getLocation(), currentLocation) < 0) {
                if (foundUsage[0] != null) {
                  if (foundUsage[0].getLocation().compareTo(usage.getLocation()) < 0) {
                    foundUsage[0] = usage;
                  }
                } else {
                  foundUsage[0] = usage;
                }
              } else {
                return false;
              }
            }

            return true;
          }
        });

    fileEditor.putUserData(KEY_START_USAGE_AGAIN, null);

    return foundUsage[0];
  }
  private static Usage findSiblingUsage(
      @NotNull final UsageSearcher usageSearcher,
      @NotNull FileSearchScope dir,
      final FileEditorLocation currentLocation,
      @NotNull final AtomicBoolean usagesWereFound,
      @NotNull FileEditor fileEditor) {
    if (fileEditor.getUserData(KEY_START_USAGE_AGAIN) != null) {
      dir =
          dir == FileSearchScope.AFTER_CARET
              ? FileSearchScope.FROM_START
              : FileSearchScope.FROM_END;
    }

    final FileSearchScope direction = dir;

    final AtomicReference<Usage> foundUsage = new AtomicReference<Usage>();
    usageSearcher.generate(
        new Processor<Usage>() {
          @Override
          public boolean process(Usage usage) {
            usagesWereFound.set(true);
            if (direction == FileSearchScope.FROM_START) {
              foundUsage.compareAndSet(null, usage);
              return false;
            }
            if (direction == FileSearchScope.FROM_END) {
              foundUsage.set(usage);
            } else if (direction == FileSearchScope.AFTER_CARET) {
              if (Comparing.compare(usage.getLocation(), currentLocation) > 0) {
                foundUsage.set(usage);
                return false;
              }
            } else if (direction == FileSearchScope.BEFORE_CARET) {
              if (Comparing.compare(usage.getLocation(), currentLocation) >= 0) {
                return false;
              }
              while (true) {
                Usage found = foundUsage.get();
                if (found == null) {
                  if (foundUsage.compareAndSet(null, usage)) break;
                } else {
                  if (Comparing.compare(found.getLocation(), usage.getLocation()) < 0
                      && foundUsage.compareAndSet(found, usage)) break;
                }
              }
            }

            return true;
          }
        });

    fileEditor.putUserData(KEY_START_USAGE_AGAIN, null);

    return foundUsage.get();
  }