Search in sources :

Example 1 with PodTitledSection

use of com.perl5.lang.pod.parser.psi.PodTitledSection 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 PodTitledSection

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

the class PodReferencesSearch method processQuery.

@Override
public void processQuery(@NotNull ReferencesSearch.SearchParameters queryParameters, @NotNull Processor<? super PsiReference> consumer) {
    final PsiElement element = queryParameters.getElementToSearch();
    if (!(element instanceof PodTitledSection)) {
        return;
    }
    final String textTitle = ((PodTitledSection) element).getTitleText();
    if (!StringUtil.isNotEmpty(textTitle)) {
        return;
    }
    String longestWord = "";
    for (String chunk : textTitle.split("[^\\w_]+")) {
        if (chunk.length() > longestWord.length()) {
            longestWord = chunk;
        }
    }
    queryParameters.getOptimizer().searchWord(longestWord, queryParameters.getEffectiveSearchScope(), true, element);
    String escapedTitle = PodLinkCompletionProvider.escapeTitle(textTitle);
    if (!StringUtil.equals(escapedTitle, textTitle)) {
        queryParameters.getOptimizer().searchWord(escapedTitle, queryParameters.getEffectiveSearchScope(), true, element);
    }
}
Also used : PodTitledSection(com.perl5.lang.pod.parser.psi.PodTitledSection) PsiElement(com.intellij.psi.PsiElement)

Example 3 with PodTitledSection

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

the class PerlDocUtil method renderElement.

@Contract("null->null")
@Nullable
public static String renderElement(@Nullable PodSection podSection) {
    if (podSection == null) {
        return null;
    }
    PsiElement run = podSection;
    // detecting first section
    while (true) {
        PsiElement prevSibling = run.getPrevSibling();
        if (prevSibling == null) {
            break;
        }
        if (prevSibling instanceof PodSection && ((PodSection) prevSibling).hasContent()) {
            break;
        }
        if (prevSibling instanceof PodTitledSection) {
            podSection = (PodTitledSection) prevSibling;
        }
        run = prevSibling;
    }
    boolean isTitledSection = podSection instanceof PodTitledSection;
    boolean hasContent = podSection.hasContent();
    PsiElement lastSection = podSection;
    // detecting last section
    while (true) {
        PsiElement nextSibling = lastSection.getNextSibling();
        boolean isNextTitledSection = nextSibling instanceof PodTitledSection && !(nextSibling instanceof PodFormatterX);
        if (nextSibling == null || (isTitledSection && isNextTitledSection && hasContent)) {
            break;
        }
        hasContent = hasContent || nextSibling instanceof PodSection && ((PodSection) nextSibling).hasContent();
        lastSection = nextSibling;
    }
    StringBuilder builder = new StringBuilder();
    // appending breadcrumbs
    List<String> breadCrumbs = new ArrayList<>();
    run = podSection.getParent();
    while (true) {
        if (run instanceof PodLinkTarget) {
            String bcLink = ((PodLinkTarget) run).getPodLink();
            if (StringUtil.isNotEmpty(bcLink)) {
                breadCrumbs.add(0, PodRenderUtil.getHTMLPsiLink(bcLink, ((PodLinkTarget) run).getPodLinkText()));
            }
        }
        if (run instanceof PsiFile) {
            break;
        }
        run = run.getParent();
    }
    if (!breadCrumbs.isEmpty()) {
        builder.append("<p>");
        builder.append(StringUtil.join(breadCrumbs, ": "));
        builder.append("</p>");
    }
    String closeTag = "";
    if (podSection instanceof PodSectionItem) {
        if (((PodSectionItem) podSection).isBulleted()) {
            builder.append("<ul style=\"fon-size:200%;\">");
            closeTag = "</ul>";
        } else {
            builder.append("<dl>");
            closeTag = "</dl>";
        }
    }
    builder.append(PodRenderUtil.renderPsiRangeAsHTML(podSection, lastSection));
    builder.append(closeTag);
    return builder.toString();
}
Also used : PodFormatterX(com.perl5.lang.pod.parser.psi.mixin.PodFormatterX) ArrayList(java.util.ArrayList) PodSectionItem(com.perl5.lang.pod.parser.psi.mixin.PodSectionItem) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) Contract(org.jetbrains.annotations.Contract) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with PodTitledSection

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

the class UpdatePerlPackageFileCopyright method getCopyrightRangesInPod.

/**
 * @return text ranges to remove block and text range to replace block
 */
@Nullable
private Pair<TextRange, TextRange> getCopyrightRangesInPod() {
    Pattern pattern = getSearchPattern();
    if (pattern == null) {
        return null;
    }
    PsiFile podPsiFile = getPodFile();
    if (podPsiFile == null) {
        return null;
    }
    Ref<PodTitledSection> copyrightSectionRef = Ref.create();
    podPsiFile.accept(new PodRecursiveVisitor() {

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

        @Override
        public void visitTargetableSection(PodTitledSection o) {
            String titleText = o.getTitleText();
            if (StringUtil.isNotEmpty(titleText) && pattern.matcher(titleText).find()) {
                copyrightSectionRef.set(o);
                return;
            }
            super.visitTargetableSection(o);
        }
    });
    PodTitledSection copyrightSection = copyrightSectionRef.get();
    if (copyrightSection == null) {
        return null;
    }
    int[] offsetAdjustments = new int[] { -1, -1 };
    copyrightSection.accept(new PodRecursiveVisitor() {

        @Override
        public void visitElement(@NotNull PsiElement element) {
            if (offsetAdjustments[1] > -1) {
                return;
            }
            IElementType elementType = PsiUtilCore.getElementType(element);
            if (elementType == CUT_SECTION) {
                offsetAdjustments[1] = element.getTextOffset() - 1;
                if (PsiUtilCore.getElementType(PsiTreeUtil.nextLeaf(element)) == POD_OUTER) {
                    offsetAdjustments[0] = element.getTextRange().getEndOffset() + 1;
                } else {
                    offsetAdjustments[0] = offsetAdjustments[1];
                }
                return;
            }
            super.visitElement(element);
        }
    });
    PsiElement sectionNextSibling = copyrightSection.getNextSibling();
    while (sectionNextSibling instanceof PsiWhiteSpace) {
        sectionNextSibling = sectionNextSibling.getNextSibling();
    }
    TextRange copyrightSectionTextRange = copyrightSection.getTextRange();
    TextRange copyrightSectionRange = sectionNextSibling == null ? copyrightSectionTextRange : TextRange.create(copyrightSectionTextRange.getStartOffset(), sectionNextSibling.getTextRange().getStartOffset());
    PsiElement copyrightBlock = copyrightSection.getContentBlock();
    TextRange copyrightBlockRange = copyrightBlock == null ? TextRange.EMPTY_RANGE : copyrightBlock.getTextRange();
    if (offsetAdjustments[0] < 0) {
        return Pair.create(copyrightSectionRange, copyrightBlockRange);
    }
    return Pair.create(TextRange.create(copyrightSectionRange.getStartOffset(), offsetAdjustments[0]), TextRange.create(copyrightBlockRange.getStartOffset(), offsetAdjustments[1]));
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Pattern(java.util.regex.Pattern) PodTitledSection(com.perl5.lang.pod.parser.psi.PodTitledSection) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) PodRecursiveVisitor(com.perl5.lang.pod.parser.psi.PodRecursiveVisitor) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with PodTitledSection

use of com.perl5.lang.pod.parser.psi.PodTitledSection 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)

Aggregations

PodTitledSection (com.perl5.lang.pod.parser.psi.PodTitledSection)11 PsiElement (com.intellij.psi.PsiElement)9 Nullable (org.jetbrains.annotations.Nullable)7 NotNull (org.jetbrains.annotations.NotNull)4 PsiFile (com.intellij.psi.PsiFile)3 PodStubsAwareRecursiveVisitor (com.perl5.lang.pod.parser.psi.PodStubsAwareRecursiveVisitor)3 ArrayList (java.util.ArrayList)3 TextRange (com.intellij.openapi.util.TextRange)2 IElementType (com.intellij.psi.tree.IElementType)2 PodRecursiveVisitor (com.perl5.lang.pod.parser.psi.PodRecursiveVisitor)2 PodFormatterX (com.perl5.lang.pod.parser.psi.mixin.PodFormatterX)2 Contract (org.jetbrains.annotations.Contract)2 CompletionParameters (com.intellij.codeInsight.completion.CompletionParameters)1 CompletionProvider (com.intellij.codeInsight.completion.CompletionProvider)1 CompletionResultSet (com.intellij.codeInsight.completion.CompletionResultSet)1 HighlightUsagesHandlerBase (com.intellij.codeInsight.highlighting.HighlightUsagesHandlerBase)1 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)1 StringUtil (com.intellij.openapi.util.text.StringUtil)1 PsiReference (com.intellij.psi.PsiReference)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1