Search in sources :

Example 1 with PodVisitor

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

the class PodLegacySectionLinkInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new PodVisitor() {

        @Override
        public void visitPodFormatterL(@NotNull PodFormatterL o) {
            PodLinkDescriptor descriptor = o.getLinkDescriptor();
            if (descriptor != null && !descriptor.isUrl() && descriptor.getSection() != null) {
                PsiElement contentBlock = o.getContentBlock();
                TextRange sectionRange = descriptor.getSectionTextRangeInLink();
                if (contentBlock != null && sectionRange != null) {
                    if (isSectionLegacy(contentBlock.getText(), sectionRange)) {
                        holder.registerProblem(o, "Section \"" + descriptor.getSection() + "\" should have a slash before it", ProblemHighlightType.LIKE_DEPRECATED, sectionRange.shiftRight(contentBlock.getStartOffsetInParent()));
                    }
                }
            }
            super.visitPodFormatterL(o);
        }

        private boolean isSectionLegacy(String text, TextRange sectionRange) {
            if (text == null) {
                return false;
            }
            int sectionStartOffset = sectionRange.getStartOffset();
            if (sectionStartOffset == 0) {
                return true;
            }
            char prevChar = text.charAt(sectionStartOffset - 1);
            if (sectionStartOffset == 1) {
                return prevChar != '/';
            }
            char prevPrevChar = text.charAt(sectionStartOffset - 2);
            return !(prevChar == '/' || prevChar == '"' && prevPrevChar == '/');
        }
    };
}
Also used : PodVisitor(com.perl5.lang.pod.parser.psi.PodVisitor) TextRange(com.intellij.openapi.util.TextRange) PodFormatterL(com.perl5.lang.pod.parser.psi.PodFormatterL) PodLinkDescriptor(com.perl5.lang.pod.parser.psi.PodLinkDescriptor) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PodVisitor

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

the class PodUnresolvableLinkInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new PodVisitor() {

        @Override
        public void visitPodFormatLink(@NotNull PsiPodFormatLink o) {
            for (PsiReference reference : o.getReferences()) {
                if (reference instanceof PsiPolyVariantReference && ((PsiPolyVariantReference) reference).multiResolve(false).length == 0) {
                    String error;
                    if (reference instanceof PodLinkToFileReference) {
                        String fileName = "UNKNONW";
                        PodLinkDescriptor descriptor = o.getLinkDescriptor();
                        if (descriptor != null && descriptor.getFileId() != null) {
                            fileName = descriptor.getFileId();
                        }
                        error = "Can't find POD or PM file by: " + fileName;
                    } else if (reference instanceof PodLinkToSectionReference) {
                        String fileName = "UNKNONW";
                        PodLinkDescriptor descriptor = o.getLinkDescriptor();
                        if (descriptor != null && descriptor.getSection() != null) {
                            fileName = descriptor.getSection();
                        }
                        error = "Can't find POD section: " + fileName;
                    } else {
                        error = "Can't find reference target";
                    }
                    holder.registerProblem(reference, error, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
                }
            }
            super.visitPodFormatLink(o);
        }
    };
}
Also used : PodLinkToFileReference(com.perl5.lang.pod.parser.psi.references.PodLinkToFileReference) PodVisitor(com.perl5.lang.pod.parser.psi.PodVisitor) PsiPodFormatLink(com.perl5.lang.pod.psi.PsiPodFormatLink) PsiReference(com.intellij.psi.PsiReference) PodLinkDescriptor(com.perl5.lang.pod.parser.psi.PodLinkDescriptor) NotNull(org.jetbrains.annotations.NotNull) PodLinkToSectionReference(com.perl5.lang.pod.parser.psi.references.PodLinkToSectionReference) PsiPolyVariantReference(com.intellij.psi.PsiPolyVariantReference) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PodLinkDescriptor (com.perl5.lang.pod.parser.psi.PodLinkDescriptor)2 PodVisitor (com.perl5.lang.pod.parser.psi.PodVisitor)2 NotNull (org.jetbrains.annotations.NotNull)2 TextRange (com.intellij.openapi.util.TextRange)1 PsiElement (com.intellij.psi.PsiElement)1 PsiPolyVariantReference (com.intellij.psi.PsiPolyVariantReference)1 PsiReference (com.intellij.psi.PsiReference)1 PodFormatterL (com.perl5.lang.pod.parser.psi.PodFormatterL)1 PodLinkToFileReference (com.perl5.lang.pod.parser.psi.references.PodLinkToFileReference)1 PodLinkToSectionReference (com.perl5.lang.pod.parser.psi.references.PodLinkToSectionReference)1 PsiPodFormatLink (com.perl5.lang.pod.psi.PsiPodFormatLink)1