WEB-36042 Angular: process only equivalent references for directive selectors usage.

GitOrigin-RevId: 05e21560a331a6da48b2a68f30cf99b595381be0
This commit is contained in:
Piotr Tomiak
2019-05-20 13:08:19 +03:00
committed by intellij-monorepo-bot
parent c4868611a7
commit cf77e8a06f
3 changed files with 23 additions and 7 deletions
@@ -193,7 +193,7 @@ public class JavaFindUsagesHelper {
if (!isSearchable && options.isSearchForTextOccurrences && options.searchScope instanceof GlobalSearchScope) {
Collection<String> stringsToSearch = ReadAction.compute(() -> getElementNames(element));
// todo add to fastTrack
if (!FindUsagesHelper.processUsagesInText(element, stringsToSearch, (GlobalSearchScope)options.searchScope, processor)) return false;
if (!FindUsagesHelper.processUsagesInText(element, stringsToSearch, false, (GlobalSearchScope)options.searchScope, processor)) return false;
}
return true;
}
@@ -31,13 +31,27 @@ import org.jetbrains.annotations.NotNull;
import java.util.Collection;
public class FindUsagesHelper {
/**
* @deprecated use {@code processUsagesInText(PsiElement, Collection<String>, GlobalSearchScope, boolean, Processor<? super UsageInfo>} instead.
*/
@Deprecated
public static boolean processUsagesInText(@NotNull final PsiElement element,
@NotNull Collection<String> stringToSearch,
@NotNull GlobalSearchScope searchScope,
@NotNull Processor<? super UsageInfo> processor) {
final TextRange elementTextRange = ReadAction.compute(() -> !element.isValid() || element instanceof PsiCompiledElement ? null : element.getTextRange());
return processUsagesInText(element, stringToSearch, false, searchScope, processor);
}
public static boolean processUsagesInText(@NotNull final PsiElement element,
@NotNull Collection<String> stringToSearch,
boolean equivalentReferencesOnly,
@NotNull GlobalSearchScope searchScope,
@NotNull Processor<? super UsageInfo> processor) {
final TextRange elementTextRange = ReadAction.compute(
() -> !element.isValid() || element instanceof PsiCompiledElement ? null
: element.getTextRange());
UsageInfoFactory factory = (usage, startOffset, endOffset) -> {
if (!element.isValid()) return new UsageInfo(usage, startOffset, endOffset, true);
if (!element.isValid()) return equivalentReferencesOnly ? null : new UsageInfo(usage, startOffset, endOffset, true);
if (elementTextRange != null
&& usage.getContainingFile() == element.getContainingFile()
&& elementTextRange.contains(startOffset)
@@ -48,15 +62,17 @@ public class FindUsagesHelper {
PsiReference someReference = usage.findReferenceAt(startOffset);
if (someReference != null) {
PsiElement refElement = someReference.getElement();
for (PsiReference ref : PsiReferenceService.getService().getReferences(refElement, new PsiReferenceService.Hints(element, null))) {
for (PsiReference ref : PsiReferenceService.getService()
.getReferences(refElement, new PsiReferenceService.Hints(element, null))) {
if (element.getManager().areElementsEquivalent(ref.resolve(), element)) {
TextRange range = ref.getRangeInElement().shiftRight(refElement.getTextRange().getStartOffset() - usage.getTextRange().getStartOffset());
TextRange range = ref.getRangeInElement()
.shiftRight(refElement.getTextRange().getStartOffset() - usage.getTextRange().getStartOffset());
return new UsageInfo(usage, range.getStartOffset(), range.getEndOffset(), true);
}
}
}
return new UsageInfo(usage, startOffset, endOffset, true);
return equivalentReferencesOnly ? null : new UsageInfo(usage, startOffset, endOffset, true);
};
for (String s : stringToSearch) {
if (!PsiSearchHelperImpl.processTextOccurrences(element, s, searchScope, processor, factory)) return false;
@@ -142,7 +142,7 @@ public abstract class FindUsagesHandler {
@NotNull GlobalSearchScope searchScope) {
Collection<String> stringToSearch = ReadAction.compute(() -> getStringsToSearch(element));
if (stringToSearch == null) return true;
return FindUsagesHelper.processUsagesInText(element, stringToSearch, searchScope, processor);
return FindUsagesHelper.processUsagesInText(element, stringToSearch, false, searchScope, processor);
}
@Nullable