use of com.intellij.usageView.UsageInfoFactory in project intellij-community by JetBrains.
the class SafeDeleteProcessor method addNonCodeUsages.
public static void addNonCodeUsages(final PsiElement element, List<UsageInfo> usages, @Nullable final Condition<PsiElement> insideElements, boolean searchNonJava, boolean searchInCommentsAndStrings) {
UsageInfoFactory nonCodeUsageFactory = new UsageInfoFactory() {
@Override
public UsageInfo createUsageInfo(@NotNull PsiElement usage, int startOffset, int endOffset) {
if (insideElements != null && insideElements.value(usage)) {
return null;
}
return new SafeDeleteReferenceSimpleDeleteUsageInfo(usage, element, startOffset, endOffset, true, false);
}
};
if (searchInCommentsAndStrings) {
String stringToSearch = ElementDescriptionUtil.getElementDescription(element, NonCodeSearchDescriptionLocation.STRINGS_AND_COMMENTS);
TextOccurrencesUtil.addUsagesInStringsAndComments(element, stringToSearch, usages, nonCodeUsageFactory);
}
if (searchNonJava) {
String stringToSearch = ElementDescriptionUtil.getElementDescription(element, NonCodeSearchDescriptionLocation.NON_JAVA);
TextOccurrencesUtil.addTextOccurences(element, stringToSearch, GlobalSearchScope.projectScope(element.getProject()), usages, nonCodeUsageFactory);
}
}
use of com.intellij.usageView.UsageInfoFactory in project intellij-community by JetBrains.
the class RenameUtil method findUsages.
@NotNull
public static UsageInfo[] findUsages(@NotNull final PsiElement element, final String newName, boolean searchInStringsAndComments, boolean searchForTextOccurrences, Map<? extends PsiElement, String> allRenames) {
final List<UsageInfo> result = Collections.synchronizedList(new ArrayList<UsageInfo>());
PsiManager manager = element.getManager();
GlobalSearchScope projectScope = GlobalSearchScope.projectScope(manager.getProject());
RenamePsiElementProcessor processor = RenamePsiElementProcessor.forElement(element);
Collection<PsiReference> refs = processor.findReferences(element, searchInStringsAndComments);
for (final PsiReference ref : refs) {
if (ref == null) {
LOG.error("null reference from processor " + processor);
continue;
}
PsiElement referenceElement = ref.getElement();
result.add(new MoveRenameUsageInfo(referenceElement, ref, ref.getRangeInElement().getStartOffset(), ref.getRangeInElement().getEndOffset(), element, ref.resolve() == null && !(ref instanceof PsiPolyVariantReference && ((PsiPolyVariantReference) ref).multiResolve(true).length > 0)));
}
processor.findCollisions(element, newName, allRenames, result);
final PsiElement searchForInComments = processor.getElementToSearchInStringsAndComments(element);
if (searchInStringsAndComments && searchForInComments != null) {
String stringToSearch = ElementDescriptionUtil.getElementDescription(searchForInComments, NonCodeSearchDescriptionLocation.STRINGS_AND_COMMENTS);
if (stringToSearch.length() > 0) {
final String stringToReplace = getStringToReplace(element, newName, false, processor);
UsageInfoFactory factory = new NonCodeUsageInfoFactory(searchForInComments, stringToReplace);
TextOccurrencesUtil.addUsagesInStringsAndComments(searchForInComments, stringToSearch, result, factory);
}
}
if (searchForTextOccurrences && searchForInComments != null) {
String stringToSearch = ElementDescriptionUtil.getElementDescription(searchForInComments, NonCodeSearchDescriptionLocation.NON_JAVA);
if (stringToSearch.length() > 0) {
final String stringToReplace = getStringToReplace(element, newName, true, processor);
addTextOccurrence(searchForInComments, result, projectScope, stringToSearch, stringToReplace);
}
final Pair<String, String> additionalStringToSearch = processor.getTextOccurrenceSearchStrings(searchForInComments, newName);
if (additionalStringToSearch != null && additionalStringToSearch.first.length() > 0) {
addTextOccurrence(searchForInComments, result, projectScope, additionalStringToSearch.first, additionalStringToSearch.second);
}
}
return result.toArray(new UsageInfo[result.size()]);
}
use of com.intellij.usageView.UsageInfoFactory in project intellij-community by JetBrains.
the class TextOccurrencesUtil method findNonCodeUsages.
public static void findNonCodeUsages(PsiElement element, String stringToSearch, boolean searchInStringsAndComments, boolean searchInNonJavaFiles, String newQName, Collection<UsageInfo> results) {
if (searchInStringsAndComments || searchInNonJavaFiles) {
UsageInfoFactory factory = createUsageInfoFactory(element, newQName);
if (searchInStringsAndComments) {
addUsagesInStringsAndComments(element, stringToSearch, results, factory);
}
if (searchInNonJavaFiles) {
GlobalSearchScope projectScope = GlobalSearchScope.projectScope(element.getProject());
addTextOccurences(element, stringToSearch, projectScope, results, factory);
}
}
}
use of com.intellij.usageView.UsageInfoFactory in project intellij-community by JetBrains.
the class FindUsagesHelper method processUsagesInText.
protected static boolean processUsagesInText(@NotNull final PsiElement element, @NotNull Collection<String> stringToSearch, @NotNull GlobalSearchScope searchScope, @NotNull Processor<UsageInfo> processor) {
final TextRange elementTextRange = ApplicationManager.getApplication().runReadAction(new NullableComputable<TextRange>() {
@Override
public TextRange compute() {
if (!element.isValid() || element instanceof PsiCompiledElement)
return null;
return element.getTextRange();
}
});
UsageInfoFactory factory = new UsageInfoFactory() {
@Override
public UsageInfo createUsageInfo(@NotNull PsiElement usage, int startOffset, int endOffset) {
if (!element.isValid())
return new UsageInfo(usage, startOffset, endOffset, true);
if (elementTextRange != null && usage.getContainingFile() == element.getContainingFile() && elementTextRange.contains(startOffset) && elementTextRange.contains(endOffset)) {
return null;
}
PsiReference someReference = usage.findReferenceAt(startOffset);
if (someReference != null) {
PsiElement refElement = someReference.getElement();
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());
return new UsageInfo(usage, range.getStartOffset(), range.getEndOffset(), true);
}
}
}
return new UsageInfo(usage, startOffset, endOffset, true);
}
};
for (String s : stringToSearch) {
if (!PsiSearchHelperImpl.processTextOccurrences(element, s, searchScope, processor, factory))
return false;
}
return true;
}
use of com.intellij.usageView.UsageInfoFactory in project intellij-community by JetBrains.
the class InlineConstantFieldProcessor method findUsages.
@Override
@NotNull
protected UsageInfo[] findUsages() {
if (myInlineThisOnly)
return new UsageInfo[] { new UsageInfo(myRefExpr) };
List<UsageInfo> usages = new ArrayList<>();
for (PsiReference ref : ReferencesSearch.search(myField, GlobalSearchScope.projectScope(myProject), false)) {
PsiElement element = ref.getElement();
UsageInfo info = new UsageInfo(element);
if (!(element instanceof PsiExpression) && PsiTreeUtil.getParentOfType(element, PsiImportStaticStatement.class) == null) {
info = new UsageFromJavaDoc(element);
}
usages.add(info);
}
if (mySearchInCommentsAndStrings || mySearchForTextOccurrences) {
UsageInfoFactory nonCodeUsageFactory = new NonCodeUsageInfoFactory(myField, myField.getName()) {
@Override
public UsageInfo createUsageInfo(@NotNull PsiElement usage, int startOffset, int endOffset) {
if (PsiTreeUtil.isAncestor(myField, usage, false))
return null;
return super.createUsageInfo(usage, startOffset, endOffset);
}
};
if (mySearchInCommentsAndStrings) {
String stringToSearch = ElementDescriptionUtil.getElementDescription(myField, NonCodeSearchDescriptionLocation.STRINGS_AND_COMMENTS);
TextOccurrencesUtil.addUsagesInStringsAndComments(myField, stringToSearch, usages, nonCodeUsageFactory);
}
if (mySearchForTextOccurrences) {
String stringToSearch = ElementDescriptionUtil.getElementDescription(myField, NonCodeSearchDescriptionLocation.NON_JAVA);
TextOccurrencesUtil.addTextOccurences(myField, stringToSearch, GlobalSearchScope.projectScope(myProject), usages, nonCodeUsageFactory);
}
}
return usages.toArray(new UsageInfo[usages.size()]);
}
Aggregations