Search in sources :

Example 1 with PodLinkDescriptor

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

the class PodLinkToFileReference method resolveInner.

@NotNull
@Override
protected ResolveResult[] resolveInner(boolean incompleteCode) {
    PodFormatterL podLink = getElement();
    PodLinkDescriptor descriptor = podLink.getLinkDescriptor();
    if (descriptor != null && !descriptor.isUrl() && descriptor.getFileId() != null) {
        PsiFile targetFile = PodFileUtil.getPodOrPackagePsiByDescriptor(podLink.getProject(), descriptor);
        if (targetFile != null) {
            return new ResolveResult[] { new PsiElementResolveResult(targetFile) };
        }
    }
    return ResolveResult.EMPTY_ARRAY;
}
Also used : PsiFile(com.intellij.psi.PsiFile) PsiElementResolveResult(com.intellij.psi.PsiElementResolveResult) PodFormatterL(com.perl5.lang.pod.parser.psi.PodFormatterL) PodLinkDescriptor(com.perl5.lang.pod.parser.psi.PodLinkDescriptor) PsiElementResolveResult(com.intellij.psi.PsiElementResolveResult) ResolveResult(com.intellij.psi.ResolveResult) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PodLinkDescriptor

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

the class PodLinkToFileReference method handleElementRename.

@Override
public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
    PodLinkDescriptor descriptor = myElement.getLinkDescriptor();
    if (descriptor != null) {
        String currentName = descriptor.getFileId();
        if (StringUtil.isNotEmpty(currentName) && newElementName.endsWith("." + PerlFileTypePackage.EXTENSION) || newElementName.endsWith("." + PodFileType.EXTENSION)) {
            String[] nameChunks = currentName.split(PerlPackageUtil.PACKAGE_SEPARATOR);
            nameChunks[nameChunks.length - 1] = newElementName.replaceFirst(PodFileUtil.PM_OR_POD_EXTENSION_PATTERN, "");
            newElementName = StringUtils.join(nameChunks, PerlPackageUtil.PACKAGE_SEPARATOR);
            return super.handleElementRename(newElementName);
        }
    // throw new IncorrectOperationException("Can't bind package use/require to a non-pm file: " + newElementName);
    }
    return myElement;
}
Also used : PodLinkDescriptor(com.perl5.lang.pod.parser.psi.PodLinkDescriptor)

Example 3 with PodLinkDescriptor

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

the class PodFormatterLMixin method getLinkDescriptor.

@Nullable
@Override
public PodLinkDescriptor getLinkDescriptor() {
    return CachedValuesManager.getCachedValue(this, () -> {
        PsiElement contentBlock = getContentBlock();
        PodLinkDescriptor result = null;
        if (contentBlock != null) {
            String contentText = contentBlock.getText();
            if (StringUtil.isNotEmpty(contentText)) {
                result = PodLinkDescriptor.getDescriptor(contentText);
            }
        }
        return CachedValueProvider.Result.create(result, PodFormatterLMixin.this);
    });
}
Also used : PodLinkDescriptor(com.perl5.lang.pod.parser.psi.PodLinkDescriptor) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with PodLinkDescriptor

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

the class PodFormatterLMixin method computeReferences.

@Override
public PsiReference[] computeReferences() {
    List<PsiReference> references = new ArrayList<>();
    final PodLinkDescriptor descriptor = getLinkDescriptor();
    if (descriptor != null && !descriptor.isUrl()) {
        PsiElement contentBlock = getContentBlock();
        if (contentBlock != null) {
            int rangeOffset = contentBlock.getStartOffsetInParent();
            // file reference
            TextRange fileRange = descriptor.getFileIdTextRangeInLink();
            if (fileRange != null && !fileRange.isEmpty()) {
                references.add(new PodLinkToFileReference(this, fileRange.shiftRight(rangeOffset)));
            }
            // section reference
            TextRange sectionRange = descriptor.getSectionTextRangeInLink();
            if (sectionRange != null && !sectionRange.isEmpty()) {
                references.add(new PodLinkToSectionReference(this, sectionRange.shiftRight(rangeOffset)));
            }
        }
    }
    references.addAll(Arrays.asList(ReferenceProvidersRegistry.getReferencesFromProviders(this)));
    return references.toArray(new PsiReference[references.size()]);
}
Also used : PodLinkToFileReference(com.perl5.lang.pod.parser.psi.references.PodLinkToFileReference) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) PodLinkDescriptor(com.perl5.lang.pod.parser.psi.PodLinkDescriptor) PodLinkToSectionReference(com.perl5.lang.pod.parser.psi.references.PodLinkToSectionReference) PsiElement(com.intellij.psi.PsiElement)

Example 5 with PodLinkDescriptor

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

Aggregations

PodLinkDescriptor (com.perl5.lang.pod.parser.psi.PodLinkDescriptor)6 PsiElement (com.intellij.psi.PsiElement)3 NotNull (org.jetbrains.annotations.NotNull)3 TextRange (com.intellij.openapi.util.TextRange)2 PsiReference (com.intellij.psi.PsiReference)2 PodFormatterL (com.perl5.lang.pod.parser.psi.PodFormatterL)2 PodVisitor (com.perl5.lang.pod.parser.psi.PodVisitor)2 PodLinkToFileReference (com.perl5.lang.pod.parser.psi.references.PodLinkToFileReference)2 PodLinkToSectionReference (com.perl5.lang.pod.parser.psi.references.PodLinkToSectionReference)2 PsiElementResolveResult (com.intellij.psi.PsiElementResolveResult)1 PsiFile (com.intellij.psi.PsiFile)1 PsiPolyVariantReference (com.intellij.psi.PsiPolyVariantReference)1 ResolveResult (com.intellij.psi.ResolveResult)1 PsiPodFormatLink (com.perl5.lang.pod.psi.PsiPodFormatLink)1 ArrayList (java.util.ArrayList)1 Nullable (org.jetbrains.annotations.Nullable)1