private static PsiElement bindElement(final PsiElement element, PsiFileSystemItem item) { if (item != null && item.isValid() && item.getVirtualFile() != null) { PsiReference[] refs = element.getReferences(); for (PsiReference ref : refs) { if (ref instanceof FileReferenceOwner) { final PsiFileReference fileReference = ((FileReferenceOwner)refs[0]).getLastFileReference(); if (fileReference != null) { try { PsiElement newElement = fileReference.bindToElement(item); if (newElement != null) { // assertion for troubles like IDEA-59540 LOG.assertTrue(element.getClass() == newElement.getClass(), "Reference " + ref + " violated contract of bindToElement()"); } return newElement; } catch (IncorrectOperationException e) { LOG.error(e); } } break; } } } return element; }
@Nullable private static FileReference findFileReference(@NotNull final PsiReference original) { if (original instanceof PsiMultiReference) { final PsiMultiReference multiReference = (PsiMultiReference)original; for (PsiReference reference : multiReference.getReferences()) { if (reference instanceof FileReference) { return (FileReference)reference; } } } else if (original instanceof FileReferenceOwner) { final PsiFileReference fileReference = ((FileReferenceOwner)original).getLastFileReference(); if (fileReference instanceof FileReference) { return (FileReference)fileReference; } } return null; }
@Override public PsiFileReference getLastFileReference() { for (PsiReference reference : myReferences) { if (reference instanceof FileReferenceOwner) { return ((FileReferenceOwner)reference).getLastFileReference(); } } return null; }
@Override public boolean execute(@NotNull ReferencesSearch.SearchParameters queryParameters, @NotNull final Processor<PsiReference> consumer) { final PsiElement file = queryParameters.getElementToSearch(); if (file instanceof PsiBinaryFile) { final Module module = ApplicationManager.getApplication().runReadAction(new Computable<Module>() { @Override public Module compute() { return ModuleUtilCore.findModuleForPsiElement(file); } }); final VirtualFile image = ((PsiBinaryFile)file).getVirtualFile(); if (isImage(image) && isIconsModule(module)) { final Project project = file.getProject(); final FindModel model = new FindModel(); final String path = getPathToImage(image, module); if (path == null) return true; model.setStringToFind(path); model.setCaseSensitive(true); model.setFindAll(true); model.setWholeWordsOnly(true); FindInProjectUtil.findUsages(model, project, new Processor<UsageInfo>() { @Override public boolean process(final UsageInfo usage) { ApplicationManager.getApplication().runReadAction(new Runnable() { public void run() { final PsiElement element = usage.getElement(); final ProperTextRange textRange = usage.getRangeInElement(); if (element != null && textRange != null) { final PsiElement start = element.findElementAt(textRange.getStartOffset()); final PsiElement end = element.findElementAt(textRange.getEndOffset()); if (start != null && end != null) { PsiElement value = PsiTreeUtil.findCommonParent(start, end); if (value instanceof PsiJavaToken) { value = value.getParent(); } if (value != null) { final PsiFileReference reference = FileReferenceUtil.findFileReference(value); if (reference != null) { consumer.process(reference); } } } } } }); return true; } }, new FindUsagesProcessPresentation(new UsageViewPresentation())); } } return true; }
@Nullable @Override public PsiFileReference getLastFileReference() { return this; }
@Override public boolean execute(@NotNull ReferencesSearch.SearchParameters queryParameters, @NotNull final Processor<PsiReference> consumer) { final PsiElement file = queryParameters.getElementToSearch(); if (file instanceof PsiBinaryFile) { final Module module = ApplicationManager.getApplication().runReadAction(new Computable<Module>() { @Override public Module compute() { return ModuleUtilCore.findModuleForPsiElement(file); } }); final VirtualFile image = ((PsiBinaryFile)file).getVirtualFile(); if (isImage(image) && isIconsModule(module)) { final Project project = file.getProject(); final FindModel model = new FindModel(); final String path = getPathToImage(image, module); if (path == null) return true; model.setStringToFind(path); model.setCaseSensitive(true); model.setFindAll(true); model.setWholeWordsOnly(true); FindInProjectUtil.findUsages(model, FindInProjectUtil.getPsiDirectory(model, project), project, false, new Processor<UsageInfo>() { @Override public boolean process(final UsageInfo usage) { ApplicationManager.getApplication().runReadAction(new Runnable() { public void run() { final PsiElement element = usage.getElement(); final ProperTextRange textRange = usage.getRangeInElement(); if (element != null && textRange != null) { final PsiElement start = element.findElementAt(textRange.getStartOffset()); final PsiElement end = element.findElementAt(textRange.getEndOffset()); if (start != null && end != null) { PsiElement value = PsiTreeUtil.findCommonParent(start, end); if (value instanceof PsiJavaToken) { value = value.getParent(); } if (value != null) { final PsiFileReference reference = FileReferenceUtil.findFileReference(value); if (reference != null) { consumer.process(reference); } } } } } }); return true; } }, new FindUsagesProcessPresentation()); } } return true; }