com.intellij.find.findUsages.FindUsagesOptions Java Examples

The following examples show how to use com.intellij.find.findUsages.FindUsagesOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LSPReferencesAction.java    From lsp4intellij with Apache License 2.0 6 votes vote down vote up
private UsageViewPresentation createPresentation(PsiElement psiElement, FindUsagesOptions options,
        boolean toOpenInNewTab) {
    UsageViewPresentation presentation = new UsageViewPresentation();
    String scopeString = options.searchScope.getDisplayName();
    presentation.setScopeText(scopeString);
    String usagesString = options.generateUsagesString();
    presentation.setUsagesString(usagesString);
    String title = FindBundle.message("find.usages.of.element.in.scope.panel.title", usagesString,
            UsageViewUtil.getLongName(psiElement), scopeString);
    presentation.setTabText(title);
    presentation.setTabName(FindBundle
            .message("find.usages.of.element.tab.name", usagesString, UsageViewUtil.getShortName(psiElement)));
    presentation.setTargetsNodeText(StringUtil.capitalize(UsageViewUtil.getType(psiElement)));
    presentation.setOpenInNewTab(toOpenInNewTab);
    presentation.setShowCancelButton(true);
    return presentation;
}
 
Example #2
Source File: GeneratedClassFindUsagesHandler.java    From litho with Apache License 2.0 6 votes vote down vote up
@Override
public FindUsagesOptions getFindUsagesOptions(@Nullable DataContext dataContext) {
  FindUsagesOptions findUsagesOptions = super.getFindUsagesOptions(dataContext);
  Optional.of(getPsiElement())
      .filter(PsiClass.class::isInstance)
      .map(PsiClass.class::cast)
      .map(findGeneratedClass)
      .ifPresent(
          generatedCls -> {
            findUsagesOptions.searchScope =
                new ExcludingScope(
                    findUsagesOptions.searchScope,
                    ComponentScope.getVirtualFile(generatedCls.getContainingFile()));
          });
  return findUsagesOptions;
}
 
Example #3
Source File: FindUsagesImpl171.java    From Android-Resource-Usage-Count with MIT License 6 votes vote down vote up
int findUsage(XmlTag element) {
    final FindUsagesHandler handler = FindUsagesImpl171.getFindUsagesHandler(element, element.getProject());
    if (handler != null) {
        final FindUsagesOptions findUsagesOptions = handler.getFindUsagesOptions();
        final PsiElement[] primaryElements = handler.getPrimaryElements();
        final PsiElement[] secondaryElements = handler.getSecondaryElements();
        Factory factory = new Factory() {
            public UsageSearcher create() {
                return FindUsagesImpl171.createUsageSearcher(primaryElements, secondaryElements, handler, findUsagesOptions, (PsiFile) null);
            }
        };
        UsageSearcher usageSearcher = (UsageSearcher)factory.create();
        final AtomicInteger mCount = new AtomicInteger(0);
        usageSearcher.generate(new Processor<Usage>() {
            @Override
            public boolean process(Usage usage) {
                if (ResourceUsageCountUtils.isUsefulUsageToCount(usage)) {
                    mCount.incrementAndGet();
                }
                return true;
            }
        });
        return mCount.get();
    }
    return 0;
}
 
Example #4
Source File: ShowUsagesAction.java    From otto-intellij-plugin with Apache License 2.0 5 votes vote down vote up
private void navigateAndHint(@NotNull Usage usage,
    @Nullable final String hint,
    @NotNull final FindUsagesHandler handler,
    @NotNull final RelativePoint popupPosition,
    final int maxUsages,
    @NotNull final FindUsagesOptions options) {
  usage.navigate(true);
  if (hint == null) return;
  final Editor newEditor = getEditorFor(usage);
  if (newEditor == null) return;
  final Project project = handler.getProject();
  //opening editor is performing in invokeLater
  IdeFocusManager.getInstance(project).doWhenFocusSettlesDown(new Runnable() {
    @Override
    public void run() {
      newEditor.getScrollingModel().runActionOnScrollingFinished(new Runnable() {
        @Override
        public void run() {
          // after new editor created, some editor resizing events are still bubbling. To prevent hiding hint, invokeLater this
          IdeFocusManager.getInstance(project).doWhenFocusSettlesDown(new Runnable() {
            @Override
            public void run() {
              if (newEditor.getComponent().isShowing()) {
                showHint(hint, newEditor, popupPosition, handler, maxUsages, options);
              }
            }
          });
        }
      });
    }
  });
}
 
Example #5
Source File: LSPReferencesAction.java    From lsp4intellij with Apache License 2.0 5 votes vote down vote up
private void showReferences(Editor editor, List<PsiElement2UsageTargetAdapter> targets, LogicalPosition position) {
    if (targets.isEmpty()) {
        short constraint = HintManager.ABOVE;
        int flags = HintManager.HIDE_BY_ANY_KEY | HintManager.HIDE_BY_TEXT_CHANGE | HintManager.HIDE_BY_SCROLLING;
        JLabel label = new JLabel("No references found");
        label.setBackground(new JBColor(new Color(150, 0, 0), new Color(150, 0, 0)));
        LightweightHint hint = new LightweightHint(label);
        Point p = HintManagerImpl.getHintPosition(hint, editor, position, constraint);
        HintManagerImpl.getInstanceImpl().showEditorHint(hint, editor, p, flags, 0, false,
                HintManagerImpl.createHintHint(editor, p, hint, constraint).setContentActive(false));
    } else {
        List<Usage> usages = new ArrayList<>();
        targets.forEach(ut -> {
            PsiElement elem = ut.getElement();
            usages.add(new UsageInfo2UsageAdapter(new UsageInfo(elem, -1, -1, false)));
        });

        if (editor == null) {
            return;
        }
        Project project = editor.getProject();
        if (project == null) {
            return;
        }
        UsageViewPresentation presentation = createPresentation(targets.get(0).getElement(),
                new FindUsagesOptions(editor.getProject()), false);
        UsageViewManager.getInstance(project)
                .showUsages(new UsageTarget[] { targets.get(0) }, usages.toArray(new Usage[usages.size()]),
                        presentation);
    }
}
 
Example #6
Source File: ShowUsagesAction.java    From otto-intellij-plugin with Apache License 2.0 5 votes vote down vote up
private void searchEverywhere(@NotNull FindUsagesOptions options,
    @NotNull FindUsagesHandler handler,
    Editor editor,
    @NotNull RelativePoint popupPosition,
    int maxUsages) {
  FindUsagesOptions cloned = options.clone();
  cloned.searchScope = FindUsagesManager.getMaximalScope(handler);
  showElementUsages(handler, editor, popupPosition, maxUsages, cloned);
}
 
Example #7
Source File: ShowUsagesAction.java    From otto-intellij-plugin with Apache License 2.0 5 votes vote down vote up
@Nullable
private static String getSecondInvocationTitle(@NotNull FindUsagesOptions options, @NotNull FindUsagesHandler handler) {
  if (getShowUsagesShortcut() != null) {
    GlobalSearchScope maximalScope = FindUsagesManager.getMaximalScope(handler);
    if (!notNullizeScope(options, handler.getProject()).equals(maximalScope)) {
      return "Press " + KeymapUtil.getShortcutText(getShowUsagesShortcut()) + " again to search in " + maximalScope.getDisplayName();
    }
  }
  return null;
}
 
Example #8
Source File: ShowUsagesAction.java    From otto-intellij-plugin with Apache License 2.0 5 votes vote down vote up
@NotNull
private static String suggestSecondInvocation(@NotNull FindUsagesOptions options, @NotNull FindUsagesHandler handler, @NotNull String text) {
  final String title = getSecondInvocationTitle(options, handler);

  if (title != null) {
    text += "<br><small>Press " + title + "</small>";
  }
  return "<html><body>" + text + "</body></html>";
}
 
Example #9
Source File: ShowUsagesAction.java    From otto-intellij-plugin with Apache License 2.0 5 votes vote down vote up
@NotNull
private JComponent createHintComponent(@NotNull String text,
    @NotNull final FindUsagesHandler handler,
    @NotNull final RelativePoint popupPosition,
    final Editor editor,
    @NotNull final Runnable cancelAction,
    final int maxUsages,
    @NotNull final FindUsagesOptions options) {
  JComponent label = HintUtil.createInformationLabel(suggestSecondInvocation(options, handler, text + "&nbsp;"));
  InplaceButton button = createSettingsButton(handler, popupPosition, editor, maxUsages, cancelAction);

  JPanel panel = new JPanel(new BorderLayout()) {
    @Override
    public void addNotify() {
      mySearchEverywhereRunnable = new Runnable() {
        @Override
        public void run() {
          searchEverywhere(options, handler, editor, popupPosition, maxUsages);
        }
      };
      super.addNotify();
    }

    @Override
    public void removeNotify() {
      mySearchEverywhereRunnable = null;
      super.removeNotify();
    }
  };
  button.setBackground(label.getBackground());
  panel.setBackground(label.getBackground());
  label.setOpaque(false);
  label.setBorder(null);
  panel.setBorder(HintUtil.createHintBorder());
  panel.add(label, BorderLayout.CENTER);
  panel.add(button, BorderLayout.EAST);
  return panel;
}
 
Example #10
Source File: ShowUsagesAction.java    From otto-intellij-plugin with Apache License 2.0 5 votes vote down vote up
private void showHint(@NotNull String text,
    @Nullable final Editor editor,
    @NotNull final RelativePoint popupPosition,
    @NotNull FindUsagesHandler handler,
    int maxUsages,
    @NotNull FindUsagesOptions options) {
  JComponent label = createHintComponent(text, handler, popupPosition, editor, HIDE_HINTS_ACTION, maxUsages, options);
  if (editor == null || editor.isDisposed()) {
    HintManager.getInstance().showHint(label, popupPosition, HintManager.HIDE_BY_ANY_KEY |
        HintManager.HIDE_BY_TEXT_CHANGE | HintManager.HIDE_BY_SCROLLING, 0);
  }
  else {
    HintManager.getInstance().showInformationHint(editor, label);
  }
}
 
Example #11
Source File: ShowUsagesAction.java    From otto-intellij-plugin with Apache License 2.0 5 votes vote down vote up
@NotNull
private static FindUsagesOptions getDefaultOptions(@NotNull FindUsagesHandler handler) {
  FindUsagesOptions options = handler.getFindUsagesOptions(DataManager.getInstance().getDataContext());
  // by default, scope in FindUsagesOptions is copied from the FindSettings, but we need a default one
  options.searchScope = FindUsagesManager.getMaximalScope(handler);
  return options;
}
 
Example #12
Source File: ShowUsagesAction.java    From dagger-intellij-plugin with Apache License 2.0 5 votes vote down vote up
private void navigateAndHint(@NotNull Usage usage, @Nullable final String hint,
    @NotNull final FindUsagesHandler handler, @NotNull final RelativePoint popupPosition,
    final int maxUsages, @NotNull final FindUsagesOptions options) {
  usage.navigate(true);
  if (hint == null) return;
  final Editor newEditor = getEditorFor(usage);
  if (newEditor == null) return;
  final Project project = handler.getProject();
  //opening editor is performing in invokeLater
  IdeFocusManager.getInstance(project).doWhenFocusSettlesDown(new Runnable() {
    @Override
    public void run() {
      newEditor.getScrollingModel().runActionOnScrollingFinished(new Runnable() {
        @Override
        public void run() {
          // after new editor created, some editor resizing events are still bubbling. To prevent hiding hint, invokeLater this
          IdeFocusManager.getInstance(project).doWhenFocusSettlesDown(new Runnable() {
            @Override
            public void run() {
              if (newEditor.getComponent().isShowing()) {
                showHint(hint, newEditor, popupPosition, handler, maxUsages, options);
              }
            }
          });
        }
      });
    }
  });
}
 
Example #13
Source File: ShowUsagesAction.java    From dagger-intellij-plugin with Apache License 2.0 5 votes vote down vote up
private void searchEverywhere(@NotNull FindUsagesOptions options,
    @NotNull FindUsagesHandler handler, Editor editor, @NotNull RelativePoint popupPosition,
    int maxUsages) {
  FindUsagesOptions cloned = options.clone();
  cloned.searchScope = FindUsagesManager.getMaximalScope(handler);
  showElementUsages(handler, editor, popupPosition, maxUsages, cloned);
}
 
Example #14
Source File: ShowUsagesAction.java    From dagger-intellij-plugin with Apache License 2.0 5 votes vote down vote up
@Nullable
private static String getSecondInvocationTitle(@NotNull FindUsagesOptions options,
    @NotNull FindUsagesHandler handler) {
  if (getShowUsagesShortcut() != null) {
    GlobalSearchScope maximalScope = FindUsagesManager.getMaximalScope(handler);
    if (!notNullizeScope(options, handler.getProject()).equals(maximalScope)) {
      return "Press "
          + KeymapUtil.getShortcutText(getShowUsagesShortcut())
          + " again to search in "
          + maximalScope.getDisplayName();
    }
  }
  return null;
}
 
Example #15
Source File: ShowUsagesAction.java    From dagger-intellij-plugin with Apache License 2.0 5 votes vote down vote up
@NotNull
private static String suggestSecondInvocation(@NotNull FindUsagesOptions options,
    @NotNull FindUsagesHandler handler, @NotNull String text) {
  final String title = getSecondInvocationTitle(options, handler);

  if (title != null) {
    text += "<br><small>Press " + title + "</small>";
  }
  return "<html><body>" + text + "</body></html>";
}
 
Example #16
Source File: ShowUsagesAction.java    From dagger-intellij-plugin with Apache License 2.0 5 votes vote down vote up
@NotNull
private static SearchScope notNullizeScope(@NotNull FindUsagesOptions options,
    @NotNull Project project) {
  SearchScope scope = options.searchScope;
  if (scope == null) return ProjectScope.getAllScope(project);
  return scope;
}
 
Example #17
Source File: ShowUsagesAction.java    From dagger-intellij-plugin with Apache License 2.0 5 votes vote down vote up
@NotNull
private JComponent createHintComponent(@NotNull String text,
    @NotNull final FindUsagesHandler handler, @NotNull final RelativePoint popupPosition,
    final Editor editor, @NotNull final Runnable cancelAction, final int maxUsages,
    @NotNull final FindUsagesOptions options) {
  JComponent label =
      HintUtil.createInformationLabel(suggestSecondInvocation(options, handler, text + "&nbsp;"));
  InplaceButton button =
      createSettingsButton(handler, popupPosition, editor, maxUsages, cancelAction);

  JPanel panel = new JPanel(new BorderLayout()) {
    @Override
    public void addNotify() {
      mySearchEverywhereRunnable = new Runnable() {
        @Override
        public void run() {
          searchEverywhere(options, handler, editor, popupPosition, maxUsages);
        }
      };
      super.addNotify();
    }

    @Override
    public void removeNotify() {
      mySearchEverywhereRunnable = null;
      super.removeNotify();
    }
  };
  button.setBackground(label.getBackground());
  panel.setBackground(label.getBackground());
  label.setOpaque(false);
  label.setBorder(null);
  panel.setBorder(HintUtil.createHintBorder());
  panel.add(label, BorderLayout.CENTER);
  panel.add(button, BorderLayout.EAST);
  return panel;
}
 
Example #18
Source File: ShowUsagesAction.java    From dagger-intellij-plugin with Apache License 2.0 5 votes vote down vote up
private void showHint(@NotNull String text, @Nullable final Editor editor,
    @NotNull final RelativePoint popupPosition, @NotNull FindUsagesHandler handler, int maxUsages,
    @NotNull FindUsagesOptions options) {
  JComponent label =
      createHintComponent(text, handler, popupPosition, editor, HIDE_HINTS_ACTION, maxUsages,
          options);

  HintManager.getInstance().showHint(label, popupPosition, HintManager.HIDE_BY_ANY_KEY |
      HintManager.HIDE_BY_TEXT_CHANGE | HintManager.HIDE_BY_SCROLLING, 0);
}
 
Example #19
Source File: ShowUsagesAction.java    From dagger-intellij-plugin with Apache License 2.0 5 votes vote down vote up
@NotNull
private static FindUsagesOptions getDefaultOptions(@NotNull FindUsagesHandler handler) {
  FindUsagesOptions options =
      handler.getFindUsagesOptions(DataManager.getInstance().getDataContext());
  // by default, scope in FindUsagesOptions is copied from the FindSettings, but we need a default one
  options.searchScope = FindUsagesManager.getMaximalScope(handler);
  return options;
}
 
Example #20
Source File: StepFindUsagesHandler.java    From Intellij-Plugin with Apache License 2.0 5 votes vote down vote up
private void runFindUsageReadAction(final PsiElement psiElement, final Processor<? super UsageInfo> processor, final FindUsagesOptions findUsagesOptions) {
    ApplicationManager.getApplication().runReadAction(() -> {
        if (psiElement instanceof PsiMethod) {
            PsiMethod[] psiMethods = SuperMethodWarningUtil.checkSuperMethods((PsiMethod) psiElement, CustomFUH.getActionString());
            if (psiMethods.length < 1) return;
            for (PsiElement method : psiMethods)
                StepFindUsagesHandler.this.processUsages(method, processor, findUsagesOptions);
        }
        StepFindUsagesHandler.this.processUsages(psiElement, processor, findUsagesOptions);
    });
}
 
Example #21
Source File: ShowUsagesAction.java    From dagger-intellij-plugin with Apache License 2.0 4 votes vote down vote up
private static String searchScopePresentableName(@NotNull FindUsagesOptions options,
    @NotNull Project project) {
  return notNullizeScope(options, project).getDisplayName();
}
 
Example #22
Source File: HaxeFindUsagesHandler.java    From intellij-haxe with Apache License 2.0 4 votes vote down vote up
@Override
public boolean processElementUsages(@NotNull PsiElement element,
                                    @NotNull Processor<UsageInfo> processor,
                                    @NotNull FindUsagesOptions options) {
  final SearchScope scope = options.searchScope;

  final boolean searchText = options.isSearchForTextOccurrences && scope instanceof GlobalSearchScope;

  if (options.isUsages) {
    ReadActionProcessor<PsiReference> searchProcessor = null;
    PsiElement searchElement = element;
    boolean fastTrack = true;

    if (element instanceof HaxeMethodDeclaration) {
      final HaxeMethodDeclaration method = (HaxeMethodDeclaration)element;
      final HaxeMethodModel methodModel = method.getModel();
      final HaxeClassModel declaringClass = methodModel.getDeclaringClass();

      if (method.isConstructor()) {
        searchElement = declaringClass.haxeClass;
        searchProcessor = getConstructorSearchProcessor(processor);
        fastTrack = false;
      }
    }

    if (searchProcessor == null) {
      searchProcessor = getSimpleSearchProcessor(processor);
    }

    final ReferencesSearch.SearchParameters parameters =
      new ReferencesSearch.SearchParameters(searchElement, scope, false, fastTrack ? options.fastTrack : null);
    final boolean success = ReferencesSearch.search(parameters).forEach(searchProcessor);

    if (!success) return false;
  }

  if (searchText) {
    if (options.fastTrack != null) {
      options.fastTrack.searchCustom(consumer -> processUsagesInText(element, processor, (GlobalSearchScope)scope));
    } else {
      return processUsagesInText(element, processor, (GlobalSearchScope)scope);
    }
  }
  return true;
}
 
Example #23
Source File: StepFindUsagesHandler.java    From Intellij-Plugin with Apache License 2.0 4 votes vote down vote up
public boolean processUsages(final PsiElement psiElement, final Processor<? super UsageInfo> processor, final FindUsagesOptions findUsagesOptions) {
    return super.processElementUsages(psiElement, processor, findUsagesOptions);
}
 
Example #24
Source File: StepFindUsagesHandler.java    From Intellij-Plugin with Apache License 2.0 4 votes vote down vote up
@Override
public boolean processElementUsages(final PsiElement psiElement, final Processor<? super UsageInfo> processor, final FindUsagesOptions findUsagesOptions) {
    ApplicationManager.getApplication().invokeLater(() -> runFindUsageReadAction(psiElement, processor, findUsagesOptions));
    return true;
}
 
Example #25
Source File: BaseSdkCompat.java    From intellij with Apache License 2.0 4 votes vote down vote up
protected abstract void doProcessElementUsages(
PsiElement element, Processor<? super Usage> processor, FindUsagesOptions options);
 
Example #26
Source File: ShowUsagesAction.java    From otto-intellij-plugin with Apache License 2.0 4 votes vote down vote up
private static String searchScopePresentableName(@NotNull FindUsagesOptions options, @NotNull Project project) {
  return notNullizeScope(options, project).getDisplayName();
}
 
Example #27
Source File: ShowUsagesAction.java    From otto-intellij-plugin with Apache License 2.0 4 votes vote down vote up
@NotNull
private static SearchScope notNullizeScope(@NotNull FindUsagesOptions options, @NotNull Project project) {
  SearchScope scope = options.searchScope;
  if (scope == null) return ProjectScope.getAllScope(project);
  return scope;
}
 
Example #28
Source File: BaseSdkCompat.java    From intellij with Apache License 2.0 4 votes vote down vote up
/** #api193: wildcard generics added in 2020.1. */
@Override
public void processElementUsages(
    PsiElement element, Processor<? super Usage> processor, FindUsagesOptions options) {
  doProcessElementUsages(element, processor, options);
}
 
Example #29
Source File: BaseSdkCompat.java    From intellij with Apache License 2.0 4 votes vote down vote up
protected abstract void doProcessElementUsages(
PsiElement element, Processor<? super Usage> processor, FindUsagesOptions options);
 
Example #30
Source File: BaseSdkCompat.java    From intellij with Apache License 2.0 4 votes vote down vote up
/** #api193: wildcard generics added in 2020.1. */
@Override
public void processElementUsages(
    PsiElement element, Processor<Usage> processor, FindUsagesOptions options) {
  doProcessElementUsages(element, processor, options);
}