Java Code Examples for com.intellij.psi.search.searches.ReferencesSearch#SearchParameters

The following examples show how to use com.intellij.psi.search.searches.ReferencesSearch#SearchParameters . 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: SpecReferenceSearch.java    From intellij-swagger with MIT License 6 votes vote down vote up
private void process(
    final ReferencesSearch.SearchParameters queryParameters,
    final PsiElement elementToSearch,
    final Project project) {
  Optional.ofNullable(((PsiNamedElement) elementToSearch).getName())
      .ifPresent(
          word -> {
            final String escaped = PathExpressionUtil.escapeJsonPointer(word);

            if (!escaped.equals(word)) {
              queryParameters
                  .getOptimizer()
                  .searchWord(
                      escaped,
                      GlobalSearchScope.allScope(project),
                      CASE_SENSITIVE,
                      elementToSearch);
            }
          });
}
 
Example 2
Source File: AdditionalReferenceSearch.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Override
public void processQuery(@Nonnull ReferencesSearch.SearchParameters queryParameters, @Nonnull Processor<? super PsiReference> consumer)
{
	PsiElement elementToSearch = queryParameters.getElementToSearch();

	PsiElement declaration = elementToSearch.getUserData(CSharpResolveUtil.EXTENSION_METHOD_WRAPPER);
	if(declaration == null)
	{
		declaration = elementToSearch.getUserData(CSharpResolveUtil.ACCESSOR_VALUE_VARIABLE_OWNER);
	}

	if(declaration == null)
	{
		return;
	}

	ReferencesSearch.search(declaration, queryParameters.getEffectiveSearchScope(), queryParameters.isIgnoreAccessScope()).forEach(consumer);
}
 
Example 3
Source File: CSharpImplementedReferenceSearch.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Override
public void processQuery(@Nonnull ReferencesSearch.SearchParameters queryParameters, @Nonnull Processor<? super PsiReference> consumer)
{
	final PsiElement elementToSearch = queryParameters.getElementToSearch();
	if(elementToSearch instanceof CSharpMethodDeclaration)
	{
		Collection<DotNetVirtualImplementOwner> targets = ApplicationManager.getApplication().runReadAction(new Computable<Collection<DotNetVirtualImplementOwner>>()
		{
			@Override
			public Collection<DotNetVirtualImplementOwner> compute()
			{
				if(((CSharpMethodDeclaration) elementToSearch).hasModifier(DotNetModifier.ABSTRACT))
				{
					return OverrideUtil.collectOverridenMembers((DotNetVirtualImplementOwner) elementToSearch);
				}
				return Collections.emptyList();
			}
		});

		for(DotNetVirtualImplementOwner target : targets)
		{
			if(!ReferencesSearch.search(target, queryParameters.getEffectiveSearchScope()).forEach(consumer))
			{
				return;
			}
		}
	}
}
 
Example 4
Source File: DotEnvReferencesSearcher.java    From idea-php-dotenv-plugin with MIT License 5 votes vote down vote up
@Override
public void processQuery(@NotNull ReferencesSearch.SearchParameters queryParameters, @NotNull Processor<? super PsiReference> consumer) {
    PsiElement refElement = queryParameters.getElementToSearch();
    if (!(refElement instanceof DotEnvProperty)) return;

    addPropertyUsages((DotEnvProperty)refElement, queryParameters.getEffectiveSearchScope(), queryParameters.getOptimizer());
}
 
Example 5
Source File: CSharpConstructorPlusTypeReferenceSearch.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Override
public void processQuery(@Nonnull ReferencesSearch.SearchParameters queryParameters, @Nonnull Processor<? super PsiReference> consumer)
{
	PsiElement elementToSearch = queryParameters.getElementToSearch();

	if(elementToSearch instanceof CSharpTypeDeclaration)
	{
		String name = AccessRule.read(((CSharpTypeDeclaration) elementToSearch)::getName);
		if(name == null)
		{
			return;
		}

		for(DotNetNamedElement member : AccessRule.read(((CSharpTypeDeclaration) elementToSearch)::getMembers))
		{
			if(member instanceof CSharpConstructorDeclaration)
			{
				queryParameters.getOptimizer().searchWord(name, queryParameters.getEffectiveSearchScope(), true, member);
			}
		}

		CSharpLightConstructorDeclarationBuilder constructor = AccessRule.read(() -> StructOrGenericParameterConstructorProvider.buildDefaultConstructor((DotNetNamedElement) elementToSearch, name));

		queryParameters.getOptimizer().searchWord(name, queryParameters.getEffectiveSearchScope(), true, constructor);
	}   /*
	else if(elementToSearch instanceof CSharpConstructorDeclaration)
	{
		PsiElement parent = elementToSearch.getParent();
		if(parent instanceof CSharpTypeDeclaration)
		{
			ReferencesSearch.search(parent, queryParameters.getEffectiveSearchScope(), queryParameters.isIgnoreAccessScope()).forEach
					(consumer);
		}
	} */

}
 
Example 6
Source File: LatteReferenceSearch.java    From intellij-latte with MIT License 5 votes vote down vote up
@Override
public void processQuery(ReferencesSearch.SearchParameters searchParameters, @NotNull Processor<? super PsiReference> processor) {
    if (searchParameters.getElementToSearch() instanceof Field) {
        processField((Field) searchParameters.getElementToSearch(), searchParameters.getScopeDeterminedByUser(), processor);

    } else if (searchParameters.getElementToSearch() instanceof PhpClass) {
        processClass((PhpClass) searchParameters.getElementToSearch(), searchParameters.getScopeDeterminedByUser(), processor);
    }
}
 
Example 7
Source File: ReferenceSearchTest.java    From Intellij-Plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testShouldFindReferencesOfGaugeElement() throws Exception {
    when(element.getProject()).thenReturn(project);
    ReferencesSearch.SearchParameters searchParameters = new ReferencesSearch.SearchParameters(element, GlobalSearchScope.allScope(project), true);

    when(helper.shouldFindReferences(searchParameters, searchParameters.getElementToSearch())).thenReturn(true);
    when(helper.getStepCollector(element)).thenReturn(collector);

    new ReferenceSearch(helper).processQuery(searchParameters, psiReference -> false);

    verify(helper, times(1)).getPsiElements(collector, element);
}
 
Example 8
Source File: ReferenceSearchTest.java    From Intellij-Plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testShouldNotFindReferencesOfNonGaugeElement() throws Exception {
    when(element.getProject()).thenReturn(project);
    ReferencesSearch.SearchParameters searchParameters = new ReferencesSearch.SearchParameters(element, GlobalSearchScope.allScope(project), true);
    when(helper.shouldFindReferences(searchParameters, searchParameters.getElementToSearch())).thenReturn(false);

    new ReferenceSearch(helper).processQuery(searchParameters, psiReference -> false);

    verify(helper, never()).getPsiElements(any(StepCollector.class), any(PsiElement.class));
}
 
Example 9
Source File: ReferenceSearchHelperTest.java    From Intellij-Plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testShouldFindReferencesIfGaugeModule() throws Exception {
    SpecStepImpl element = mock(SpecStepImpl.class);
    when(element.getProject()).thenReturn(project);
    ReferencesSearch.SearchParameters searchParameters = new ReferencesSearch.SearchParameters(element, scope, true);

    when(moduleHelper.isGaugeModule(element)).thenReturn(true);
    when(scope.getDisplayName()).thenReturn("Other Scope");

    ReferenceSearchHelper referenceSearchHelper = new ReferenceSearchHelper(moduleHelper);
    referenceSearchHelper.shouldFindReferences(searchParameters, searchParameters.getElementToSearch());

    verify(scope, times(1)).getDisplayName();
}
 
Example 10
Source File: ReferenceSearchHelperTest.java    From Intellij-Plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testShouldNotFindReferencesIfNotGaugeModule() throws Exception {
    SpecStepImpl element = mock(SpecStepImpl.class);
    when(element.getProject()).thenReturn(project);
    ReferencesSearch.SearchParameters searchParameters = new ReferencesSearch.SearchParameters(element, scope, true);

    when(moduleHelper.isGaugeModule(element)).thenReturn(false);

    ReferenceSearchHelper referenceSearchHelper = new ReferenceSearchHelper(moduleHelper);
    boolean shouldFindReferences = referenceSearchHelper.shouldFindReferences(searchParameters, searchParameters.getElementToSearch());

    assertFalse("Should find reference for non Gauge Module(Expected: false, Actual: true)", shouldFindReferences);

    verify(scope, never()).getDisplayName();
}
 
Example 11
Source File: ReferenceSearchHelperTest.java    From Intellij-Plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetReferenceElementsForConceptStep() throws Exception {
    ConceptStepImpl element = mock(ConceptStepImpl.class);
    when(element.getProject()).thenReturn(project);
    StepValue stepValue = new StepValue("# hello", "", new ArrayList<>());
    ReferencesSearch.SearchParameters searchParameters = new ReferencesSearch.SearchParameters(element, scope, true);

    when(element.getStepValue()).thenReturn(stepValue);

    ReferenceSearchHelper referenceSearchHelper = new ReferenceSearchHelper();
    referenceSearchHelper.getPsiElements(collector, searchParameters.getElementToSearch());

    verify(collector, times(1)).get("hello");
}
 
Example 12
Source File: ReferenceSearchHelperTest.java    From Intellij-Plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testShouldNotFindReferencesWhenUnknownScope() throws Exception {
    PsiClass element = mock(PsiClass.class);
    when(element.getProject()).thenReturn(project);
    ReferencesSearch.SearchParameters searchParameters = new ReferencesSearch.SearchParameters(element, scope, true);

    when(moduleHelper.isGaugeModule(element)).thenReturn(true);
    when(scope.getDisplayName()).thenReturn(ReferenceSearchHelper.UNKNOWN_SCOPE);

    ReferenceSearchHelper referenceSearchHelper = new ReferenceSearchHelper(moduleHelper);
    boolean shouldFindReferences = referenceSearchHelper.shouldFindReferences(searchParameters, searchParameters.getElementToSearch());

    assertFalse("Should find reference When unknown scope(Expected: false, Actual: true)", shouldFindReferences);
}
 
Example 13
Source File: ReferenceSearchHelperTest.java    From Intellij-Plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testShouldNotFindReferencesOfNonGaugeElement() throws Exception {
    PsiClass element = mock(PsiClass.class);
    when(element.getProject()).thenReturn(project);
    ReferencesSearch.SearchParameters searchParameters = new ReferencesSearch.SearchParameters(element, GlobalSearchScope.allScope(project), true);

    when(moduleHelper.isGaugeModule(element)).thenReturn(true);

    ReferenceSearchHelper referenceSearchHelper = new ReferenceSearchHelper(moduleHelper);
    boolean shouldFindReferences = referenceSearchHelper.shouldFindReferences(searchParameters, searchParameters.getElementToSearch());

    assertFalse("Should find reference for PsiClass(Expected: false, Actual: true)", shouldFindReferences);
}
 
Example 14
Source File: ReferenceSearch.java    From Intellij-Plugin with Apache License 2.0 5 votes vote down vote up
private void processElements(final ReferencesSearch.SearchParameters searchParameters, final Processor<? super PsiReference> processor) {
    ApplicationManager.getApplication().runReadAction(() -> {
        StepCollector collector = helper.getStepCollector(searchParameters.getElementToSearch());
        collector.collect();
        final List<PsiElement> elements = helper.getPsiElements(collector, searchParameters.getElementToSearch());
        for (PsiElement element : elements)
            processor.process(element.getReference());
    });
}
 
Example 15
Source File: ReferenceSearch.java    From Intellij-Plugin with Apache License 2.0 5 votes vote down vote up
@Override
public void processQuery(@NotNull ReferencesSearch.SearchParameters searchParameters, @NotNull Processor<? super PsiReference> processor) {
    ApplicationManager.getApplication().runReadAction(() -> {
        if (!helper.shouldFindReferences(searchParameters, searchParameters.getElementToSearch())) return;
        if (EventQueue.isDispatchThread())
            ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> processElements(searchParameters, processor), "Find Usages", true, searchParameters.getElementToSearch().getProject());
        else
            processElements(searchParameters, processor);
    });
}
 
Example 16
Source File: Unity3dSceneReferenceSearcher.java    From consulo-unity3d with Apache License 2.0 5 votes vote down vote up
@Override
public void processQuery(@Nonnull ReferencesSearch.SearchParameters searchParameters, @Nonnull Processor<? super PsiReference> processor)
{
	SearchScope scope = ReadAction.compute(searchParameters::getEffectiveSearchScope);
	if(!(scope instanceof GlobalSearchScope))
	{
		return;
	}

	Project project = searchParameters.getProject();

	PsiElement element = searchParameters.getElementToSearch();
	if(ReadAction.compute(() -> Unity3dModuleExtensionUtil.getRootModule(searchParameters.getProject()) != null))
	{
		if(element instanceof CSharpFieldDeclaration)
		{
			String name = ReadAction.compute(((CSharpFieldDeclaration) element)::getName);
			MultiMap<VirtualFile, Unity3dYMLAsset> map = ReadAction.compute(() -> Unity3dYMLAsset.findAssetAsAttach(project, PsiUtilCore.getVirtualFile(element)));

			for(VirtualFile virtualFile : map.keySet())
			{
				ProgressManager.checkCanceled();

				searchParameters.getOptimizer().searchWord(name + ":", GlobalSearchScope.fileScope(project, virtualFile), true, element);
			}
		}
		else if(element instanceof YAMLFile)
		{
			String guid = ReadAction.compute(() -> Unity3dMetaManager.getInstance(project).getGUID(PsiUtilCore.getVirtualFile(element)));
			if(guid != null)
			{
				searchParameters.getOptimizer().searchWord(guid, GlobalSearchScope.allScope(project), true, element);
			}
		}
	}
}
 
Example 17
Source File: SpecReferenceSearch.java    From intellij-swagger with MIT License 5 votes vote down vote up
@Override
public void processQuery(
    @NotNull final ReferencesSearch.SearchParameters queryParameters,
    @NotNull final Processor<? super PsiReference> consumer) {
  final PsiElement elementToSearch = queryParameters.getElementToSearch();

  final Project project = queryParameters.getProject();

  if (indexFacade.isIndexReady(project)) {
    if (isSpec(elementToSearch, project)) {
      process(queryParameters, elementToSearch, project);
    }
  }
}
 
Example 18
Source File: CppReferencesSearcher.java    From CppTools with Apache License 2.0 4 votes vote down vote up
private boolean doFindRefsInCppCode(final PsiFile psiFile, PsiElement target, ReferencesSearch.SearchParameters params,
                                    GlobalSearchScope globalScope, final Processor<PsiReference> processor) {
  final Project project = psiFile.getProject();
  final String commandName = project.getUserData(ourKey);
  final int offset;
  VirtualFile file;

  if (target instanceof CppFile) {
    offset = FindUsagesCommand.MAGIC_FILE_OFFSET;
    file = ((CppFile)target).getVirtualFile();
  } else {
    VirtualFile actionStartFile = null;
    int actionStartOffset = -1;
    
    final PsiFile actionStartPsiFile = project.getUserData(ourFileKey);
    if (actionStartPsiFile != null) {
      actionStartFile = actionStartPsiFile.getVirtualFile();
      final Integer integer = project.getUserData(ourOffsetKey);
      if (integer != null) actionStartOffset = integer.intValue();
    }
    
    if (actionStartOffset != -1) {
      offset = actionStartOffset;
      file = actionStartFile;
    } else {
      file = psiFile.getVirtualFile();
      offset = target.getTextOffset();
    }
  }

  final FindUsagesCommand findUsagesCommand = new FindUsagesCommand(
    file.getPath(),
    offset,
    RENAME_ACTION_TEXT.equals(commandName)
  );
  findUsagesCommand.setDoInfiniteBlockingWithCancelledCheck(true);

  findUsagesCommand.post(psiFile.getProject());

  if (!findUsagesCommand.hasReadyResult()) return true;

  final int count = findUsagesCommand.getUsageCount();
  if (count == 0) return true;

  final boolean scopeIsLocal = params.getScope() instanceof LocalSearchScope;
  final Set<VirtualFile> localScope = scopeIsLocal ? new HashSet<VirtualFile>() : null;

  if (scopeIsLocal) {
    for(PsiElement e: ((LocalSearchScope)params.getScope()).getScope()) {
      localScope.add(e.getContainingFile().getVirtualFile());
    }
  }

  for(final FileUsage fu:findUsagesCommand.getUsagesList().files) {
    final VirtualFile usagefile = fu.findVirtualFile();
    if ((globalScope != null && !globalScope.contains(usagefile)) ||
        localScope != null && !localScope.contains(usagefile)
      ) {
      continue;
    }


    Runnable runnable = new Runnable() {
      public void run() {
        final PsiFile usageFile = psiFile.getManager().findFile( usagefile );

        for(final OurUsage u:fu.usageList) {
          final PsiElement psiElement = usageFile.findElementAt(u.getStart());

          if (psiElement != null) {
            final PsiElement parentElement = psiElement.getParent();
            if (parentElement instanceof CppElement || parentElement instanceof CppKeyword /*operator*/) {
              final PsiReference reference = parentElement.getReference();
              if (reference != null) processor.process( reference );
            }
          }
        }
      }
    };

    ApplicationManager.getApplication().runReadAction(runnable);
  }
  return false;
}
 
Example 19
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 20
Source File: ReferenceSearchHelper.java    From Intellij-Plugin with Apache License 2.0 4 votes vote down vote up
public boolean shouldFindReferences(@NotNull ReferencesSearch.SearchParameters searchParameters, PsiElement element) {
    return helper.isGaugeModule(element) &&
            !searchParameters.getScopeDeterminedByUser().getDisplayName().equals(UNKNOWN_SCOPE) &&
            GaugeUtil.isGaugeElement(element);
}