Search in sources :

Example 31 with ProperTextRange

use of com.intellij.openapi.util.ProperTextRange in project intellij-community by JetBrains.

the class IconsReferencesContributor method execute.

@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, usage -> {
                ApplicationManager.getApplication().runReadAction(() -> {
                    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;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) UsageViewPresentation(com.intellij.usages.UsageViewPresentation) FindModel(com.intellij.find.FindModel) FindUsagesProcessPresentation(com.intellij.usages.FindUsagesProcessPresentation) ProperTextRange(com.intellij.openapi.util.ProperTextRange) Project(com.intellij.openapi.project.Project) PsiFileReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.PsiFileReference) Module(com.intellij.openapi.module.Module)

Aggregations

ProperTextRange (com.intellij.openapi.util.ProperTextRange)31 TextRange (com.intellij.openapi.util.TextRange)14 com.intellij.codeInsight.hint (com.intellij.codeInsight.hint)4 PsiElement (com.intellij.psi.PsiElement)4 RelativePoint (com.intellij.ui.awt.RelativePoint)4 NotNull (org.jetbrains.annotations.NotNull)4 Nullable (org.jetbrains.annotations.Nullable)4 Document (com.intellij.openapi.editor.Document)2 Segment (com.intellij.openapi.util.Segment)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 List (java.util.List)2 BackgroundEditorHighlighter (com.intellij.codeHighlighting.BackgroundEditorHighlighter)1 HighlightingPass (com.intellij.codeHighlighting.HighlightingPass)1 EditorInfo (com.intellij.codeInsight.EditorInfo)1 DefaultHighlightInfoProcessor (com.intellij.codeInsight.daemon.impl.DefaultHighlightInfoProcessor)1 GeneralHighlightingPass (com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass)1 LocalInspectionsPass (com.intellij.codeInsight.daemon.impl.LocalInspectionsPass)1 EmptyPass (com.intellij.codeInsight.daemon.impl.ProgressableTextEditorHighlightingPass.EmptyPass)1 GenericElementSignatureProvider (com.intellij.codeInsight.folding.impl.GenericElementSignatureProvider)1 InspectionManager (com.intellij.codeInspection.InspectionManager)1