com.intellij.usages.UsageTarget Java Examples

The following examples show how to use com.intellij.usages.UsageTarget. 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: FindUsagesInFileAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static boolean isEnabled(DataContext dataContext) {
  Project project = dataContext.getData(CommonDataKeys.PROJECT);
  if (project == null) {
    return false;
  }

  Editor editor = dataContext.getData(PlatformDataKeys.EDITOR);
  if (editor == null) {
    UsageTarget[] target = dataContext.getData(UsageView.USAGE_TARGETS_KEY);
    return target != null && target.length > 0;
  }
  else {
    PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
    if (file == null) {
      return false;
    }

    Language language = PsiUtilBase.getLanguageInEditor(editor, project);
    if (language == null) {
      language = file.getLanguage();
    }
    return !(LanguageFindUsages.INSTANCE.forLanguage(language) instanceof EmptyFindUsagesProvider);
  }
}
 
Example #2
Source File: Decider.java    From dagger-intellij-plugin with Apache License 2.0 6 votes vote down vote up
@Override public boolean shouldShow(UsageTarget target, Usage usage) {
  PsiElement element = ((UsageInfo2UsageAdapter) usage).getElement();

  PsiField field = PsiConsultantImpl.findField(element);
  if (field != null //
      && PsiConsultantImpl.hasAnnotation(field, CLASS_INJECT) //
      && PsiConsultantImpl.hasQuailifierAnnotations(field, qualifierAnnotations)
      && PsiConsultantImpl.hasTypeParameters(field, typeParameters)) {
    return true;
  }

  PsiMethod method = PsiConsultantImpl.findMethod(element);
  if (method != null && (PsiConsultantImpl.hasAnnotation(method, CLASS_INJECT)
          || PsiConsultantImpl.hasAnnotation(method, CLASS_PROVIDES))) {
    for (PsiParameter parameter : method.getParameterList().getParameters()) {
      PsiClass parameterClass = PsiConsultantImpl.checkForLazyOrProvider(parameter);
      if (parameterClass.equals(returnType) && PsiConsultantImpl.hasQuailifierAnnotations(
          parameter, qualifierAnnotations)
          && PsiConsultantImpl.hasTypeParameters(parameter, typeParameters)) {
        return true;
      }
    }
  }

  return false;
}
 
Example #3
Source File: Decider.java    From dagger-intellij-plugin with Apache License 2.0 6 votes vote down vote up
@Override public boolean shouldShow(UsageTarget target, Usage usage) {
  PsiElement element = ((UsageInfo2UsageAdapter) usage).getElement();
  PsiMethod psimethod = PsiConsultantImpl.findMethod(element);

  PsiAnnotationMemberValue attribValue = PsiConsultantImpl
      .findTypeAttributeOfProvidesAnnotation(psimethod);

  // Is it a @Provides method?
  return psimethod != null
      // Ensure it has an @Provides.
      && PsiConsultantImpl.hasAnnotation(psimethod, CLASS_PROVIDES)
      // Check for Qualifier annotations.
      && PsiConsultantImpl.hasQuailifierAnnotations(psimethod, qualifierAnnotations)
      // Right return type.
      && PsiConsultantImpl.getReturnClassFromMethod(psimethod, false)
      .getName()
      .equals(target.getName())
      // Right type parameters.
      && PsiConsultantImpl.hasTypeParameters(psimethod, typeParameters)
      // @Provides(type=SET)
      && attribValue != null
      && attribValue.textMatches(SET_TYPE);
}
 
Example #4
Source File: Decider.java    From dagger-intellij-plugin with Apache License 2.0 6 votes vote down vote up
@Override public boolean shouldShow(UsageTarget target, Usage usage) {
  PsiElement element = ((UsageInfo2UsageAdapter) usage).getElement();

  PsiMethod psimethod = PsiConsultantImpl.findMethod(element);

  // For constructors annotated w/ @Inject, this is searched first before committing to the usage search.

  // Is it a @Provides method?
  return psimethod != null
      // Ensure it has an @Provides.
      && PsiConsultantImpl.hasAnnotation(psimethod, CLASS_PROVIDES)

      // Check for Qualifier annotations.
      && PsiConsultantImpl.hasQuailifierAnnotations(psimethod, qualifierAnnotations)

      // Right return type.
      && PsiConsultantImpl.getReturnClassFromMethod(psimethod, false)
      .getName()
      .equals(target.getName())

      // Right type parameters.
      && PsiConsultantImpl.hasTypeParameters(psimethod, typeParameters);
}
 
Example #5
Source File: BuildFileGroupingRule.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
public UsageGroup getParentGroupFor(Usage usage, UsageTarget[] targets) {
  // give the delegate a chance to refuse the usage
  UsageGroup base = delegate.getParentGroupFor(usage, targets);
  if (base == null || !(usage instanceof UsageInFile)) {
    return null;
  }
  VirtualFile vf = ((UsageInFile) usage).getFile();
  if (vf == null || vf.getFileType() != BuildFileType.INSTANCE) {
    return base;
  }
  return new FileUsageGroup(project, vf) {
    String name;

    @Override
    public void update() {
      super.update();
      if (isValid()) {
        name = BuildFile.getBuildFileString(project, vf.getPath());
      }
    }

    @Override
    public String getPresentableName() {
      return name;
    }

    @Override
    public String getText(UsageView view) {
      return name;
    }
  };
}
 
Example #6
Source File: DefaultUsageTargetProvider.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public UsageTarget[] getTargets(PsiElement psiElement) {
  if (psiElement instanceof NavigationItem) {
    if (FindManager.getInstance(psiElement.getProject()).canFindUsages(psiElement)) {
      return new UsageTarget[]{new PsiElement2UsageTargetAdapter(psiElement)};
    }
  }
  return null;
}
 
Example #7
Source File: FindUsagesInFileAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(AnActionEvent e) {
  DataContext dataContext = e.getDataContext();
  final Project project = dataContext.getData(CommonDataKeys.PROJECT);
  if (project == null) return;
  PsiDocumentManager.getInstance(project).commitAllDocuments();
  Editor editor = dataContext.getData(PlatformDataKeys.EDITOR);

  UsageTarget[] usageTargets = dataContext.getData(UsageView.USAGE_TARGETS_KEY);
  if (usageTargets != null) {
    FileEditor fileEditor = dataContext.getData(PlatformDataKeys.FILE_EDITOR);
    if (fileEditor != null) {
      usageTargets[0].findUsagesInEditor(fileEditor);
    }
  }
  else if (editor == null) {
    Messages.showMessageDialog(
      project,
      FindBundle.message("find.no.usages.at.cursor.error"),
      CommonBundle.getErrorTitle(),
      Messages.getErrorIcon()
    );
  }
  else {
    HintManager.getInstance().showErrorHint(editor, FindBundle.message("find.no.usages.at.cursor.error"));
  }
}
 
Example #8
Source File: HighlightUsagesHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private static UsageTarget[] getUsageTargets(@Nonnull Editor editor, PsiFile file) {
  UsageTarget[] usageTargets = UsageTargetUtil.findUsageTargets(editor, file);

  if (usageTargets == null) {
    PsiElement targetElement = getTargetElement(editor, file);
    if (targetElement != null && targetElement != file) {
      if (!(targetElement instanceof NavigationItem)) {
        targetElement = targetElement.getNavigationElement();
      }
      if (targetElement instanceof NavigationItem) {
        usageTargets = new UsageTarget[]{new PsiElement2UsageTargetAdapter(targetElement)};
      }
    }
  }

  if (usageTargets == null) {
    PsiReference ref = TargetElementUtil.findReference(editor);

    if (ref instanceof PsiPolyVariantReference) {
      ResolveResult[] results = ((PsiPolyVariantReference)ref).multiResolve(false);

      if (results.length > 0) {
        usageTargets = ContainerUtil.mapNotNull(results, result -> {
          PsiElement element = result.getElement();
          return element == null ? null : new PsiElement2UsageTargetAdapter(element);
        }, UsageTarget.EMPTY_ARRAY);
      }
    }
  }
  return usageTargets;
}
 
Example #9
Source File: HighlightUsagesHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void invoke(@Nonnull final Project project, @Nonnull final Editor editor, final PsiFile file) {
  PsiDocumentManager.getInstance(project).commitAllDocuments();

  final SelectionModel selectionModel = editor.getSelectionModel();
  if (file == null && !selectionModel.hasSelection()) {
    selectionModel.selectWordAtCaret(false);
  }
  if (file == null || selectionModel.hasSelection()) {
    doRangeHighlighting(editor, project);
    return;
  }

  final HighlightUsagesHandlerBase handler = createCustomHandler(editor, file);
  if (handler != null) {
    final String featureId = handler.getFeatureId();

    if (featureId != null) {
      FeatureUsageTracker.getInstance().triggerFeatureUsed(featureId);
    }

    handler.highlightUsages();
    return;
  }

  DumbService.getInstance(project).withAlternativeResolveEnabled(() -> {
    UsageTarget[] usageTargets = getUsageTargets(editor, file);
    if (usageTargets == null) {
      handleNoUsageTargets(file, editor, selectionModel, project);
      return;
    }

    boolean clearHighlights = isClearHighlights(editor);
    for (UsageTarget target : usageTargets) {
      target.highlightUsages(file, editor, clearHighlights);
    }
  });
}
 
Example #10
Source File: ReadAccessFilteringRule.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isVisible(@Nonnull Usage usage, @Nonnull UsageTarget[] targets) {
  if (usage instanceof ReadWriteAccessUsage) {
    final ReadWriteAccessUsage readWriteAccessUsage = (ReadWriteAccessUsage)usage;
    final boolean isForReadingOnly = readWriteAccessUsage.isAccessedForReading() && !readWriteAccessUsage.isAccessedForWriting();
    return !isForReadingOnly;
  }
  return true;
}
 
Example #11
Source File: WriteAccessFilteringRule.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isVisible(@Nonnull Usage usage, @Nonnull UsageTarget[] targets) {
  if (usage instanceof ReadWriteAccessUsage) {
    final ReadWriteAccessUsage readWriteAccessUsage = (ReadWriteAccessUsage)usage;
    final boolean isForWritingOnly = readWriteAccessUsage.isAccessedForWriting() && !readWriteAccessUsage.isAccessedForReading();
    return !isForWritingOnly;
  }
  return true;
}
 
Example #12
Source File: UsageViewTreeModelBuilder.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void addTargetNodes() {
  ApplicationManager.getApplication().assertIsDispatchThread();
  if (myTargets.length == 0) return;
  myTargetNodes = new UsageTargetNode[myTargets.length];
  myTargetsNode.removeAllChildren();
  for (int i = 0; i < myTargets.length; i++) {
    UsageTarget target = myTargets[i];
    UsageTargetNode targetNode = new UsageTargetNode(target);
    myTargetsNode.add(targetNode);
    myTargetNodes[i] = targetNode;
  }
  myRootNode.addTargetsNode(myTargetsNode, this);
  reload(myTargetsNode);
}
 
Example #13
Source File: UsageViewTreeModelBuilder.java    From consulo with Apache License 2.0 5 votes vote down vote up
public UsageViewTreeModelBuilder(@Nonnull UsageViewPresentation presentation, @Nonnull UsageTarget[] targets) {
  super(GroupNode.createRoot());
  myRootNode = (GroupNode.Root)root;
  myTargetsNodeText = presentation.getTargetsNodeText();
  myTargets = targets;
  myTargetsNode = myTargetsNodeText == null ? null : new TargetsRootNode(myTargetsNodeText);

  UIUtil.invokeLaterIfNeeded(() -> {
    if (myTargetsNodeText != null) {
      addTargetNodes();
    }
    setRoot(myRootNode);
  });
}
 
Example #14
Source File: UsageNodeTreeBuilder.java    From consulo with Apache License 2.0 5 votes vote down vote up
UsageNodeTreeBuilder(@Nonnull UsageTarget[] targets, @Nonnull UsageGroupingRule[] groupingRules, @Nonnull UsageFilteringRule[] filteringRules, @Nonnull GroupNode root, @Nonnull Project project) {
  myTargets = targets;
  myGroupingRules = groupingRules;
  myFilteringRules = filteringRules;
  myRoot = root;
  myProject = project;
}
 
Example #15
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 #16
Source File: BuildUsageTypeProvider.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
public UsageType getUsageType(PsiElement element, @NotNull UsageTarget[] targets) {
  if (!(element instanceof BuildElement)) {
    return null;
  }
  if (PsiTreeUtil.getParentOfType(element, LoadStatement.class) != null) {
    return IN_LOAD;
  }
  if (PsiTreeUtil.getParentOfType(element, GlobExpression.class, false) != null) {
    return IN_GLOB;
  }
  return GENERIC;
}
 
Example #17
Source File: Unity3dAssetUsageFilteringRuleProvider.java    From consulo-unity3d with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isVisible(@Nonnull Usage usage, @Nonnull UsageTarget[] targets)
{
	if(usage instanceof PsiElementUsage)
	{
		PsiFile containingFile = ((PsiElementUsage) usage).getElement().getContainingFile();
		if(containingFile != null && containingFile.getFileType() == Unity3dYMLAssetFileType.INSTANCE)
		{
			return false;
		}
	}
	return true;
}
 
Example #18
Source File: UsageTypeProviderEx.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
UsageType
getUsageType(PsiElement element, @Nonnull UsageTarget[] targets);
 
Example #19
Source File: ShowUsagesAction.java    From otto-intellij-plugin with Apache License 2.0 4 votes vote down vote up
public StringNode(Object string) {
  super(NullUsage.INSTANCE, new UsageViewTreeModelBuilder(new UsageViewPresentation(), UsageTarget.EMPTY_ARRAY));
  myString = string;
}
 
Example #20
Source File: UsageTargetsRule.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
@Nullable
public UsageTarget[] getData(@Nonnull DataProvider dataProvider) {
  return UsageTargetUtil.findUsageTargets(dataProvider);
}
 
Example #21
Source File: UsageTargetsRule.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public Key<UsageTarget[]> getKey() {
  return UsageView.USAGE_TARGETS_KEY;
}
 
Example #22
Source File: VirtualFileArrayRule.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public VirtualFile[] getData(@Nonnull final DataProvider dataProvider) {
  // Try to detect multiselection.

  Project project = dataProvider.getDataUnchecked(PlatformDataKeys.PROJECT_CONTEXT);
  if (project != null && !project.isDisposed()) {
    return ProjectRootManager.getInstance(project).getContentRoots();
  }

  Module[] selectedModules = dataProvider.getDataUnchecked(LangDataKeys.MODULE_CONTEXT_ARRAY);
  if (selectedModules != null && selectedModules.length > 0) {
    return getFilesFromModules(selectedModules);
  }

  Module selectedModule = dataProvider.getDataUnchecked(LangDataKeys.MODULE_CONTEXT);
  if (selectedModule != null && !selectedModule.isDisposed()) {
    return ModuleRootManager.getInstance(selectedModule).getContentRoots();
  }

  PsiElement[] psiElements = dataProvider.getDataUnchecked(LangDataKeys.PSI_ELEMENT_ARRAY);
  if (psiElements != null && psiElements.length != 0) {
    return getFilesFromPsiElements(psiElements);
  }

  // VirtualFile -> VirtualFile[]
  VirtualFile vFile = dataProvider.getDataUnchecked(PlatformDataKeys.VIRTUAL_FILE);
  if (vFile != null) {
    return new VirtualFile[]{vFile};
  }

  //

  PsiFile psiFile = dataProvider.getDataUnchecked(LangDataKeys.PSI_FILE);
  if (psiFile != null && psiFile.getVirtualFile() != null) {
    return new VirtualFile[]{psiFile.getVirtualFile()};
  }

  PsiElement elem = dataProvider.getDataUnchecked(LangDataKeys.PSI_ELEMENT);
  if (elem != null) {
    return getFilesFromPsiElement(elem);
  }

  Usage[] usages = dataProvider.getDataUnchecked(UsageView.USAGES_KEY);
  UsageTarget[] usageTargets = dataProvider.getDataUnchecked(UsageView.USAGE_TARGETS_KEY);
  if (usages != null || usageTargets != null) {
    return UsageDataUtil.provideVirtualFileArray(usages, usageTargets);
  }

  return null;
}
 
Example #23
Source File: DefaultUsageTargetProvider.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public UsageTarget[] getTargets(Editor editor, PsiFile file) {
  return null;
}
 
Example #24
Source File: BuildUsageTypeProvider.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Override
@Nullable
public UsageType getUsageType(PsiElement element) {
  return getUsageType(element, UsageTarget.EMPTY_ARRAY);
}
 
Example #25
Source File: ShowUsagesAction.java    From dagger-intellij-plugin with Apache License 2.0 4 votes vote down vote up
public StringNode(Object string) {
  super(NullUsage.INSTANCE,
      new UsageViewTreeModelBuilder(new UsageViewPresentation(), UsageTarget.EMPTY_ARRAY));
  myString = string;
}
 
Example #26
Source File: UsageGroupingRuleEx.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
default List<UsageGroup> getParentGroupsFor(@Nonnull Usage usage, @Nonnull UsageTarget[] targets) {
  return ContainerUtil.createMaybeSingletonList(groupUsage(usage, targets));
}
 
Example #27
Source File: UsageFilteringRule.java    From consulo with Apache License 2.0 4 votes vote down vote up
default boolean isVisible(@Nonnull Usage usage, @Nonnull UsageTarget[] targets) {
  return isVisible(usage);
}
 
Example #28
Source File: UsageTargetNode.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
public UsageTarget getTarget() {
  return (UsageTarget)getUserObject();
}
 
Example #29
Source File: UsageTargetNode.java    From consulo with Apache License 2.0 4 votes vote down vote up
UsageTargetNode(@Nonnull UsageTarget target) {
  setUserObject(target);
}
 
Example #30
Source File: UsageGroupingRuleEx.java    From consulo with Apache License 2.0 4 votes vote down vote up
@javax.annotation.Nullable
UsageGroup groupUsage(@Nonnull Usage usage, @Nonnull UsageTarget[] targets);