Search in sources :

Example 11 with PerlSimpleCompletionProcessor

use of com.perl5.lang.perl.idea.completion.providers.processors.PerlSimpleCompletionProcessor 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

PerlSimpleCompletionProcessor (com.perl5.lang.perl.idea.completion.providers.processors.PerlSimpleCompletionProcessor)11 PsiElement (com.intellij.psi.PsiElement)9 PerlTimeLogger (com.perl5.lang.perl.util.PerlTimeLogger)8 CompletionParameters (com.intellij.codeInsight.completion.CompletionParameters)2 CompletionResultSet (com.intellij.codeInsight.completion.CompletionResultSet)2 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)2 StringUtil (com.intellij.openapi.util.text.StringUtil)2 IElementType (com.intellij.psi.tree.IElementType)2 ProcessingContext (com.intellij.util.ProcessingContext)2 PerlVariableCompletionProcessorImpl (com.perl5.lang.perl.idea.completion.providers.processors.PerlVariableCompletionProcessorImpl)2 PerlSubElement (com.perl5.lang.perl.psi.PerlSubElement)2 PsiPerlMethod (com.perl5.lang.perl.psi.PsiPerlMethod)2 PerlPsiUtil (com.perl5.lang.perl.psi.utils.PerlPsiUtil)2 NotNull (org.jetbrains.annotations.NotNull)2 CompletionProvider (com.intellij.codeInsight.completion.CompletionProvider)1 PsiFile (com.intellij.psi.PsiFile)1 PsiLanguageInjectionHost (com.intellij.psi.PsiLanguageInjectionHost)1 ResolveResult (com.intellij.psi.ResolveResult)1 PsiUtilCore (com.intellij.psi.util.PsiUtilCore)1 PerlElementPatterns (com.perl5.lang.perl.idea.PerlElementPatterns)1