Search in sources :

Example 1 with HighlightUsagesHandlerBase

use of com.intellij.codeInsight.highlighting.HighlightUsagesHandlerBase in project Perl5-IDEA by Camelcade.

the class PodUsagesHighlightingFactory method createHighlightUsagesHandler.

@Override
@Nullable
public HighlightUsagesHandlerBase<?> createHighlightUsagesHandler(@NotNull Editor editor, @NotNull PsiFile file, @NotNull PsiElement target) {
    if (!target.getLanguage().isKindOf(PodLanguage.INSTANCE)) {
        return null;
    }
    PsiElement targetElement = TargetElementUtil.findTargetElement(editor, REFERENCED_ELEMENT_ACCEPTED | ELEMENT_NAME_ACCEPTED);
    if (!(targetElement instanceof PodTitledSection)) {
        return null;
    }
    return new HighlightUsagesHandlerBase<>(editor, file) {

        @Override
        @NotNull
        public List<PsiElement> getTargets() {
            return Collections.singletonList(targetElement);
        }

        @Override
        protected void selectTargets(@NotNull List<? extends PsiElement> targets, @NotNull Consumer<? super List<? extends PsiElement>> selectionConsumer) {
            selectionConsumer.consume(targets);
        }

        @Override
        public void computeUsages(@NotNull List<? extends PsiElement> targets) {
            List<PodTitledSection> allTargetSections = PodLinkToSectionReference.getAllSynonymousSections((PodTitledSection) targetElement);
            if (allTargetSections.isEmpty()) {
                return;
            }
            if (Objects.equals(PsiUtilCore.getVirtualFile(targetElement), PsiUtilCore.getVirtualFile(file))) {
                List<TextRange> writeUsages = getWriteUsages();
                for (PodTitledSection titledSection : allTargetSections) {
                    PsiElement titleElement = titledSection.getTitleElement();
                    if (titleElement != null) {
                        writeUsages.add(titleElement.getTextRange());
                    }
                }
            }
            List<TextRange> readUsages = getReadUsages();
            ReferencesSearch.search(allTargetSections.get(0), new LocalSearchScope(file)).forEach((PsiReference it) -> readUsages.add(it.getRangeInElement().shiftRight(it.getElement().getTextRange().getStartOffset())));
        }
    };
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PodTitledSection(com.perl5.lang.pod.parser.psi.PodTitledSection) Consumer(com.intellij.util.Consumer) PsiReference(com.intellij.psi.PsiReference) List(java.util.List) TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) HighlightUsagesHandlerBase(com.intellij.codeInsight.highlighting.HighlightUsagesHandlerBase) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

HighlightUsagesHandlerBase (com.intellij.codeInsight.highlighting.HighlightUsagesHandlerBase)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiElement (com.intellij.psi.PsiElement)1 PsiReference (com.intellij.psi.PsiReference)1 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)1 Consumer (com.intellij.util.Consumer)1 PodTitledSection (com.perl5.lang.pod.parser.psi.PodTitledSection)1 List (java.util.List)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1