Search in sources :

Example 1 with PodSubReference

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

the class PodIdentifierImpl method computeReferences.

@Override
public PsiReference[] computeReferences() {
    final PodIdentifierImpl element = PodIdentifierImpl.this;
    List<PsiReference> references = new ArrayList<>();
    if (element.getParent() instanceof PodSectionTitle && element.getPrevSibling() == null) {
        references.add(new PodSubReference(element));
    }
    references.addAll(Arrays.asList(ReferenceProvidersRegistry.getReferencesFromProviders(element)));
    return references.toArray(new PsiReference[references.size()]);
}
Also used : PodSectionTitle(com.perl5.lang.pod.parser.psi.PodSectionTitle) PodSubReference(com.perl5.lang.pod.parser.psi.references.PodSubReference) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference)

Example 2 with PodSubReference

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

Aggregations

PodSectionTitle (com.perl5.lang.pod.parser.psi.PodSectionTitle)2 PodSubReference (com.perl5.lang.pod.parser.psi.references.PodSubReference)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 StringUtil (com.intellij.openapi.util.text.StringUtil)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 PsiReference (com.intellij.psi.PsiReference)1 ResolveResult (com.intellij.psi.ResolveResult)1 IElementType (com.intellij.psi.tree.IElementType)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 PodElementPatterns (com.perl5.lang.pod.parser.PodElementPatterns)1 PodRecursiveVisitor (com.perl5.lang.pod.parser.psi.PodRecursiveVisitor)1 PodTitledSection (com.perl5.lang.pod.parser.psi.PodTitledSection)1