Search in sources :

Example 1 with PodStubsAwareRecursiveVisitor

use of com.perl5.lang.pod.parser.psi.PodStubsAwareRecursiveVisitor in project Perl5-IDEA by Camelcade.

the class PodLinkToSectionReference method getAllSynonymousSections.

/**
 * @return list of all section synonymous to {@link titledSection}
 */
@NotNull
public static List<PodTitledSection> getAllSynonymousSections(@NotNull PodTitledSection titledSection) {
    List<PodTitledSection> result = new ArrayList<>();
    String titleText = titledSection.getTitleText();
    titledSection.getContainingFile().accept(new PodStubsAwareRecursiveVisitor() {

        @Override
        public void visitTargetableSection(PodTitledSection o) {
            if (StringUtil.equals(titleText, o.getTitleText())) {
                result.add(o);
            }
            super.visitTargetableSection(o);
        }
    });
    return result;
}
Also used : PodTitledSection(com.perl5.lang.pod.parser.psi.PodTitledSection) ArrayList(java.util.ArrayList) PodStubsAwareRecursiveVisitor(com.perl5.lang.pod.parser.psi.PodStubsAwareRecursiveVisitor) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PodStubsAwareRecursiveVisitor

use of com.perl5.lang.pod.parser.psi.PodStubsAwareRecursiveVisitor in project Perl5-IDEA by Camelcade.

the class PodLinkCompletionProvider method addSectionsCompletions.

protected static void addSectionsCompletions(@Nullable PsiFile targetFile, @NotNull PerlCompletionProcessor completionProcessor) {
    if (targetFile == null) {
        return;
    }
    Set<String> distinctString = new HashSet<>();
    targetFile.accept(new PodStubsAwareRecursiveVisitor() {

        @Override
        public void visitElement(@NotNull PsiElement element) {
            if (completionProcessor.result()) {
                super.visitElement(element);
            }
        }

        @Override
        public void visitTargetableSection(PodTitledSection o) {
            String title = cleanItemText(o.getTitleText());
            if (completionProcessor.matches(title) && distinctString.add(title)) {
                completionProcessor.process(LookupElementBuilder.create(o, escapeTitle(title)).withLookupString(title).withPresentableText(title).withIcon(PerlIcons.POD_FILE).withTypeText(UsageViewUtil.getType(o)));
            }
            super.visitTargetableSection(o);
        }

        @Override
        public void visitItemSection(@NotNull PsiItemSection o) {
            if (((PodSectionItem) o).isTargetable()) {
                super.visitItemSection(o);
            } else {
                visitElement(o);
            }
        }

        @Contract("null->null")
        @Nullable
        private String cleanItemText(@Nullable String itemText) {
            if (itemText == null) {
                return null;
            }
            String trimmed = StringUtil.trimLeading(itemText.trim(), '*');
            if (NumberUtils.isNumber(trimmed)) {
                return null;
            }
            return trimItemText(trimmed);
        }

        @Override
        public void visitPodFormatIndex(@NotNull PsiPodFormatIndex o) {
            assert o instanceof PodFormatterX;
            if (!((PodFormatterX) o).isMeaningful()) {
                return;
            }
            String indexTitle = ((PodFormatterX) o).getTitleText();
            if (StringUtil.isEmpty(indexTitle) || !distinctString.add(indexTitle)) {
                return;
            }
            String escapedIndexTitle = escapeTitle(indexTitle);
            if (!completionProcessor.matches(indexTitle) && !completionProcessor.matches(escapedIndexTitle)) {
                return;
            }
            PsiElement indexTarget = ((PodFormatterX) o).getIndexTarget();
            String targetPresentableText;
            if (indexTarget instanceof PodFile) {
                targetPresentableText = cleanItemText(((PodFile) indexTarget).getPodLinkText());
            } else if (indexTarget instanceof PodCompositeElement) {
                targetPresentableText = cleanItemText(((PodCompositeElement) indexTarget).getPresentableText());
            } else {
                LOG.warn("Unhandled index target: " + indexTarget);
                return;
            }
            String tailText = null;
            if (targetPresentableText != null) {
                tailText = "(" + targetPresentableText + ")";
            }
            completionProcessor.process(LookupElementBuilder.create(o, escapedIndexTitle).withLookupString(indexTitle).withPresentableText(indexTitle).withTailText(tailText).withTypeText(UsageViewUtil.getType(o)).withIcon(PerlIcons.POD_FILE));
        }
    });
}
Also used : PsiItemSection(com.perl5.lang.pod.psi.PsiItemSection) PsiPodFormatIndex(com.perl5.lang.pod.psi.PsiPodFormatIndex) PodTitledSection(com.perl5.lang.pod.parser.psi.PodTitledSection) PodFormatterX(com.perl5.lang.pod.parser.psi.mixin.PodFormatterX) PodStubsAwareRecursiveVisitor(com.perl5.lang.pod.parser.psi.PodStubsAwareRecursiveVisitor) PodCompositeElement(com.perl5.lang.pod.parser.psi.PodCompositeElement) Contract(org.jetbrains.annotations.Contract) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable) PodFile(com.perl5.lang.pod.parser.psi.PodFile)

Example 3 with PodStubsAwareRecursiveVisitor

use of com.perl5.lang.pod.parser.psi.PodStubsAwareRecursiveVisitor in project Perl5-IDEA by Camelcade.

the class PodLinkToSectionReference method resolveInner.

@Override
@NotNull
protected ResolveResult[] resolveInner(boolean incompleteCode) {
    PodFormatterL podLink = getElement();
    PodLinkDescriptor descriptor = podLink.getLinkDescriptor();
    String sectionTitle = descriptor == null ? null : descriptor.getSection();
    if (sectionTitle == null || descriptor.isUrl()) {
        return ResolveResult.EMPTY_ARRAY;
    }
    List<PsiFile> targetFiles = new ArrayList<>();
    if (descriptor.getName() != null && !descriptor.isSameFile()) {
        for (PsiReference reference : podLink.getReferences()) {
            if (reference instanceof PodLinkToFileReference) {
                for (ResolveResult resolveResult : ((PodLinkToFileReference) reference).multiResolve(false)) {
                    targetFiles.add((PsiFile) resolveResult.getElement());
                }
            }
        }
    } else {
        targetFiles.add(podLink.getContainingFile());
    }
    if (targetFiles.isEmpty()) {
        return ResolveResult.EMPTY_ARRAY;
    }
    List<PsiElement> results = new ArrayList<>();
    for (PsiFile file : targetFiles) {
        file.accept(new PodStubsAwareRecursiveVisitor() {

            @Override
            public void visitTargetableSection(PodTitledSection o) {
                if (StringUtil.equals(sectionTitle, o.getTitleText())) {
                    results.add(o);
                }
                super.visitTargetableSection(o);
            }
        });
        if (!results.isEmpty()) {
            break;
        }
    }
    return PsiElementResolveResult.createResults(results);
}
Also used : PodTitledSection(com.perl5.lang.pod.parser.psi.PodTitledSection) ArrayList(java.util.ArrayList) PodStubsAwareRecursiveVisitor(com.perl5.lang.pod.parser.psi.PodStubsAwareRecursiveVisitor) PodLinkDescriptor(com.perl5.lang.pod.parser.psi.PodLinkDescriptor) PodFormatterL(com.perl5.lang.pod.parser.psi.mixin.PodFormatterL) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PodStubsAwareRecursiveVisitor (com.perl5.lang.pod.parser.psi.PodStubsAwareRecursiveVisitor)3 PodTitledSection (com.perl5.lang.pod.parser.psi.PodTitledSection)3 ArrayList (java.util.ArrayList)2 NotNull (org.jetbrains.annotations.NotNull)2 PsiElement (com.intellij.psi.PsiElement)1 PodCompositeElement (com.perl5.lang.pod.parser.psi.PodCompositeElement)1 PodFile (com.perl5.lang.pod.parser.psi.PodFile)1 PodLinkDescriptor (com.perl5.lang.pod.parser.psi.PodLinkDescriptor)1 PodFormatterL (com.perl5.lang.pod.parser.psi.mixin.PodFormatterL)1 PodFormatterX (com.perl5.lang.pod.parser.psi.mixin.PodFormatterX)1 PsiItemSection (com.perl5.lang.pod.psi.PsiItemSection)1 PsiPodFormatIndex (com.perl5.lang.pod.psi.PsiPodFormatIndex)1 Contract (org.jetbrains.annotations.Contract)1 Nullable (org.jetbrains.annotations.Nullable)1