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); } }
private static void addTextOccurrence( final PsiElement element, final List<UsageInfo> result, final GlobalSearchScope projectScope, final String stringToSearch, final String stringToReplace) { TextOccurrencesUtil.UsageInfoFactory factory = new TextOccurrencesUtil.UsageInfoFactory() { public UsageInfo createUsageInfo( @NotNull PsiElement usage, int startOffset, int endOffset) { TextRange textRange = usage.getTextRange(); int start = textRange == null ? 0 : textRange.getStartOffset(); return NonCodeUsageInfo.create( usage.getContainingFile(), start + startOffset, start + endOffset, element, stringToReplace); } }; TextOccurrencesUtil.addTextOccurences(element, stringToSearch, projectScope, result, factory); }
@NotNull public static UsageInfo[] findUsages( 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)); } 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); TextOccurrencesUtil.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()]); }