Search in sources :

Example 1 with PodRecursiveVisitor

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

the class PodTitledSection method getTitleTextWithoutIndexes.

/**
 * @return a text of the section title with all formatting codes but indexes, or null if there is no title section
 */
@Nullable
default String getTitleTextWithoutIndexes() {
    PsiElement titleElement = getTitleElement();
    if (titleElement == null) {
        return null;
    }
    StringBuilder sb = new StringBuilder();
    titleElement.accept(new PodRecursiveVisitor() {

        @Override
        public void visitElement(@NotNull PsiElement element) {
            if (element.getChildren().length > 0) {
                super.visitElement(element);
            } else {
                sb.append(element.getNode().getChars());
            }
        }

        @Override
        public void visitPodFormatIndex(@NotNull PsiPodFormatIndex o) {
        }
    });
    return sb.toString().trim();
}
Also used : PsiPodFormatIndex(com.perl5.lang.pod.psi.PsiPodFormatIndex) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with PodRecursiveVisitor

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

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

the class PodTitleCompletionProvider method addCompletions.

@Override
protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull ProcessingContext context, @NotNull CompletionResultSet result) {
    PsiElement element = parameters.getPosition();
    PsiElement elementParent = element.getParent();
    if (PsiUtilCore.getElementType(element) != POD_IDENTIFIER || element.getPrevSibling() != null || !(elementParent instanceof PodSectionTitle)) {
        return;
    }
    IElementType grandparentElementType = PsiUtilCore.getElementType(elementParent.getParent());
    PerlSimpleCompletionProcessor completionProcessor = new PerlSimpleCompletionProcessor(parameters, result, element);
    if (grandparentElementType == HEAD_1_SECTION) {
        for (String sectionTitle : DEFAULT_POD_SECTIONS) {
            if (completionProcessor.matches(sectionTitle) && !completionProcessor.process(LookupElementBuilder.create(sectionTitle))) {
                break;
            }
        }
    }
    final PsiFile elementFile = element.getContainingFile().getOriginalFile();
    final PsiFile perlFile = PodFileUtil.getTargetPerlFile(elementFile);
    if (perlFile == null) {
        return;
    }
    Set<PerlSubElement> possibleTargets = new HashSet<>();
    PerlPsiUtil.processSubElements(perlFile, possibleTargets::add);
    elementFile.accept(new PodRecursiveVisitor() {

        @Override
        public void visitTargetableSection(PodTitledSection o) {
            processSection(o);
            super.visitTargetableSection(o);
        }

        private void processSection(@NotNull PodTitledSection o) {
            PsiElement titleBlock = o.getTitleElement();
            if (titleBlock == null) {
                return;
            }
            PsiElement firstChild = titleBlock.getFirstChild();
            if (firstChild == null) {
                return;
            }
            // noinspection SuspiciousMethodCalls
            Arrays.stream(firstChild.getReferences()).filter(it -> it instanceof PodSubReference).flatMap(it -> Arrays.stream(((PodSubReference) it).multiResolve(false))).map(ResolveResult::getElement).forEach(possibleTargets::remove);
        }
    });
    for (PerlSubElement subElement : possibleTargets) {
        String lookupString = StringUtil.notNullize(subElement.getPresentableName());
        if (completionProcessor.matches(lookupString) && !completionProcessor.process(LookupElementBuilder.create(subElement, lookupString).withIcon(subElement.getIcon(0)))) {
            break;
        }
    }
    completionProcessor.logStatus(getClass());
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) PodFileUtil(com.perl5.lang.pod.parser.psi.util.PodFileUtil) java.util(java.util) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) IElementType(com.intellij.psi.tree.IElementType) PodRecursiveVisitor(com.perl5.lang.pod.parser.psi.PodRecursiveVisitor) PodSectionTitle(com.perl5.lang.pod.parser.psi.PodSectionTitle) StringUtil(com.intellij.openapi.util.text.StringUtil) CompletionParameters(com.intellij.codeInsight.completion.CompletionParameters) PerlSimpleCompletionProcessor(com.perl5.lang.perl.idea.completion.providers.processors.PerlSimpleCompletionProcessor) PerlPsiUtil(com.perl5.lang.perl.psi.utils.PerlPsiUtil) PodTitledSection(com.perl5.lang.pod.parser.psi.PodTitledSection) PodSubReference(com.perl5.lang.pod.parser.psi.references.PodSubReference) PodElementPatterns(com.perl5.lang.pod.parser.PodElementPatterns) CompletionResultSet(com.intellij.codeInsight.completion.CompletionResultSet) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) ResolveResult(com.intellij.psi.ResolveResult) CompletionProvider(com.intellij.codeInsight.completion.CompletionProvider) PsiElement(com.intellij.psi.PsiElement) PsiFile(com.intellij.psi.PsiFile) NotNull(org.jetbrains.annotations.NotNull) PerlSubElement(com.perl5.lang.perl.psi.PerlSubElement) PodSectionTitle(com.perl5.lang.pod.parser.psi.PodSectionTitle) PodSubReference(com.perl5.lang.pod.parser.psi.references.PodSubReference) PodTitledSection(com.perl5.lang.pod.parser.psi.PodTitledSection) PerlSimpleCompletionProcessor(com.perl5.lang.perl.idea.completion.providers.processors.PerlSimpleCompletionProcessor) PodRecursiveVisitor(com.perl5.lang.pod.parser.psi.PodRecursiveVisitor) IElementType(com.intellij.psi.tree.IElementType) PerlSubElement(com.perl5.lang.perl.psi.PerlSubElement) PsiFile(com.intellij.psi.PsiFile) ResolveResult(com.intellij.psi.ResolveResult) PsiElement(com.intellij.psi.PsiElement)

Example 4 with PodRecursiveVisitor

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

the class PodTitledSectionMixin method setName.

@Override
public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
    PsiElement nameIdentifier = getNameIdentifier();
    if (nameIdentifier == null) {
        return this;
    }
    StringBuilder indexes = new StringBuilder();
    nameIdentifier.accept(new PodRecursiveVisitor() {

        @Override
        public void visitPodFormatIndex(@NotNull PsiPodFormatIndex o) {
            indexes.append(o.getText());
        }
    });
    if (indexes.length() > 0) {
        name = name + "\n" + indexes;
    }
    ElementManipulators.getManipulator(nameIdentifier).handleContentChange(nameIdentifier, name);
    return this;
}
Also used : PsiPodFormatIndex(com.perl5.lang.pod.psi.PsiPodFormatIndex) PsiElement(com.intellij.psi.PsiElement) PodRecursiveVisitor(com.perl5.lang.pod.parser.psi.PodRecursiveVisitor)

Aggregations

PsiElement (com.intellij.psi.PsiElement)4 PodRecursiveVisitor (com.perl5.lang.pod.parser.psi.PodRecursiveVisitor)3 PsiFile (com.intellij.psi.PsiFile)2 IElementType (com.intellij.psi.tree.IElementType)2 PodTitledSection (com.perl5.lang.pod.parser.psi.PodTitledSection)2 PsiPodFormatIndex (com.perl5.lang.pod.psi.PsiPodFormatIndex)2 Nullable (org.jetbrains.annotations.Nullable)2 CompletionParameters (com.intellij.codeInsight.completion.CompletionParameters)1 CompletionProvider (com.intellij.codeInsight.completion.CompletionProvider)1 CompletionResultSet (com.intellij.codeInsight.completion.CompletionResultSet)1 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)1 TextRange (com.intellij.openapi.util.TextRange)1 StringUtil (com.intellij.openapi.util.text.StringUtil)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 ResolveResult (com.intellij.psi.ResolveResult)1 PsiUtilCore (com.intellij.psi.util.PsiUtilCore)1 ProcessingContext (com.intellij.util.ProcessingContext)1 PerlSimpleCompletionProcessor (com.perl5.lang.perl.idea.completion.providers.processors.PerlSimpleCompletionProcessor)1 PerlSubElement (com.perl5.lang.perl.psi.PerlSubElement)1 PerlPsiUtil (com.perl5.lang.perl.psi.utils.PerlPsiUtil)1