Search in sources :

Example 1 with PerlCompletionProcessor

use of com.perl5.lang.perl.idea.completion.providers.processors.PerlCompletionProcessor in project Perl5-IDEA by Camelcade.

the class PerlPackageCompletionUtil method processAllNamespacesNames.

public static boolean processAllNamespacesNames(@NotNull PerlCompletionProcessor completionProcessor, boolean appendNamespaceSeparator, boolean addPackageTag) {
    PsiElement element = completionProcessor.getLeafElement();
    final Project project = element.getProject();
    Processor<PerlNamespaceDefinitionElement> namespaceProcessor = namespace -> processNamespaceLookupElement(namespace, completionProcessor, appendNamespaceSeparator);
    PerlBuiltInNamespacesService.getInstance(project).processNamespaces(namespaceProcessor);
    if (addPackageTag && !PerlPackageCompletionUtil.processPackageLookupElement(null, __PACKAGE__, PACKAGE_GUTTER_ICON, completionProcessor, false)) {
        return false;
    }
    return processFirstNamespaceForEachName(completionProcessor, project, element.getResolveScope(), namespaceProcessor);
}
Also used : InsertHandler(com.intellij.codeInsight.completion.InsertHandler) PerlFileImpl(com.perl5.lang.perl.psi.impl.PerlFileImpl) Lookup(com.intellij.codeInsight.lookup.Lookup) PerlFeaturesTable(com.perl5.lang.perl.internals.PerlFeaturesTable) PerlPackageUtil(com.perl5.lang.perl.util.PerlPackageUtil) VirtualFile(com.intellij.openapi.vfs.VirtualFile) AutoPopupController(com.intellij.codeInsight.AutoPopupController) PerlIcons(com.perl5.PerlIcons) Map(java.util.Map) PsiElement(com.intellij.psi.PsiElement) PerlVersion(com.perl5.lang.perl.internals.PerlVersion) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) PerlBuiltInNamespacesService(com.perl5.lang.perl.psi.references.PerlBuiltInNamespacesService) Logger(com.intellij.openapi.diagnostic.Logger) PACKAGE_GUTTER_ICON(com.perl5.PerlIcons.PACKAGE_GUTTER_ICON) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) LookupElement(com.intellij.codeInsight.lookup.LookupElement) StringUtil(com.intellij.openapi.util.text.StringUtil) Collection(java.util.Collection) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) PerlPackageUtil.__PACKAGE__(com.perl5.lang.perl.util.PerlPackageUtil.__PACKAGE__) Processor(com.intellij.util.Processor) ObjectUtils(com.intellij.util.ObjectUtils) PerlFileTypePackage(com.perl5.lang.perl.fileTypes.PerlFileTypePackage) PerlSimpleDelegatingCompletionProcessor(com.perl5.lang.perl.idea.completion.providers.processors.PerlSimpleDelegatingCompletionProcessor) PerlNamespaceDefinitionElement(com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement) NotNull(org.jetbrains.annotations.NotNull) PerlCompletionProcessor(com.perl5.lang.perl.idea.completion.providers.processors.PerlCompletionProcessor) PerlTimeLogger(com.perl5.lang.perl.util.PerlTimeLogger) javax.swing(javax.swing) Project(com.intellij.openapi.project.Project) PerlNamespaceDefinitionElement(com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement) PsiElement(com.intellij.psi.PsiElement)

Example 2 with PerlCompletionProcessor

use of com.perl5.lang.perl.idea.completion.providers.processors.PerlCompletionProcessor in project Perl5-IDEA by Camelcade.

the class PerlStringCompletionUtil method fillWithHashIndexes.

public static void fillWithHashIndexes(@NotNull PerlCompletionProcessor completionProcessor) {
    Set<String> hashIndexesCache = PerlStringCompletionCache.getInstance(completionProcessor.getProject()).getHashIndexesCache();
    for (String text : hashIndexesCache) {
        if (completionProcessor.matches(text) && !completionProcessor.process(LookupElementBuilder.create(text))) {
            return;
        }
    }
    PsiElement element = completionProcessor.getLeafElement();
    PsiFile file = completionProcessor.getContainingFile();
    file.accept(new PerlCompletionRecursiveVisitor(completionProcessor) {

        @Override
        public void visitStringContentElement(@NotNull PerlStringContentElementImpl o) {
            if (o != element && SIMPLE_HASH_INDEX.accepts(o)) {
                processStringElement(o);
            }
            super.visitStringContentElement(o);
        }

        @Override
        public void visitCommaSequenceExpr(@NotNull PsiPerlCommaSequenceExpr o) {
            if (o.getParent() instanceof PsiPerlAnonHash) {
                PsiElement sequenceElement = o.getFirstChild();
                boolean isKey = true;
                while (sequenceElement != null) {
                    ProgressManager.checkCanceled();
                    IElementType elementType = sequenceElement.getNode().getElementType();
                    if (isKey && sequenceElement instanceof PerlString) {
                        for (PerlStringContentElement stringElement : PerlPsiUtil.collectStringElements(sequenceElement)) {
                            processStringElement(stringElement);
                        }
                    } else if (elementType == COMMA || elementType == FAT_COMMA) {
                        isKey = !isKey;
                    }
                    sequenceElement = PerlPsiUtil.getNextSignificantSibling(sequenceElement);
                }
            }
            super.visitCommaSequenceExpr(o);
        }

        protected void processStringElement(PerlStringContentElement stringContentElement) {
            String text = stringContentElement.getText();
            if (StringUtil.isNotEmpty(text) && hashIndexesCache.add(text) && completionProcessor.matches(text) && isIdentifier(text)) {
                completionProcessor.process(LookupElementBuilder.create(stringContentElement, text));
            }
        }
    });
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) PerlStringContentElementImpl(com.perl5.lang.perl.psi.impl.PerlStringContentElementImpl)

Example 3 with PerlCompletionProcessor

use of com.perl5.lang.perl.idea.completion.providers.processors.PerlCompletionProcessor in project Perl5-IDEA by Camelcade.

the class PerlStringCompletionUtil method fillWithInjectableMarkers.

public static void fillWithInjectableMarkers(@NotNull PerlCompletionProcessor completionProcessor) {
    // injectable markers
    PerlInjectionMarkersService injectionService = PerlInjectionMarkersService.getInstance(completionProcessor.getProject());
    for (String marker : injectionService.getSupportedMarkers()) {
        if (!completionProcessor.matches(marker)) {
            continue;
        }
        Language language = injectionService.getLanguageByMarker(marker);
        if (language == null) {
            continue;
        }
        LookupElementBuilder newItem = LookupElementBuilder.create(marker).withTypeText("inject with " + language.getDisplayName(), true);
        if (language.getAssociatedFileType() != null) {
            newItem = newItem.withIcon(language.getAssociatedFileType().getIcon());
        }
        if (!completionProcessor.process(newItem)) {
            return;
        }
    }
}
Also used : Language(com.intellij.lang.Language) PerlInjectionMarkersService(com.perl5.lang.perl.idea.intellilang.PerlInjectionMarkersService) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder)

Example 4 with PerlCompletionProcessor

use of com.perl5.lang.perl.idea.completion.providers.processors.PerlCompletionProcessor in project Perl5-IDEA by Camelcade.

the class PerlStringCompletionUtil method fillWithUseParameters.

public static void fillWithUseParameters(@NotNull PerlCompletionProcessor completionProcessor) {
    PsiElement baseElement = completionProcessor.getLeafElement();
    PerlUseStatementElement useStatement = PsiTreeUtil.getParentOfType(baseElement, PerlUseStatementElement.class, true, PsiPerlStatement.class);
    if (useStatement == null) {
        return;
    }
    List<String> typedParameters = useStatement.getImportParameters();
    Set<String> typedStringsSet = typedParameters == null ? Collections.emptySet() : new THashSet<>(typedParameters);
    PerlPackageProcessor packageProcessor = useStatement.getPackageProcessor();
    // fixme we should allow lookup elements customization by package processor
    if (packageProcessor instanceof PerlPackageOptionsProvider) {
        Map<String, String> options = ((PerlPackageOptionsProvider) packageProcessor).getOptions();
        for (Map.Entry<String, String> option : options.entrySet()) {
            String lookupString = option.getKey();
            if (!typedStringsSet.contains(lookupString) && completionProcessor.matches(lookupString) && !completionProcessor.process(LookupElementBuilder.create(lookupString).withTypeText(option.getValue(), true).withIcon(PerlIcons.PERL_OPTION))) {
                return;
            }
        }
        options = ((PerlPackageOptionsProvider) packageProcessor).getOptionsBundles();
        for (Map.Entry<String, String> option : options.entrySet()) {
            String lookupString = option.getKey();
            if (!typedStringsSet.contains(lookupString) && completionProcessor.matches(lookupString) && !completionProcessor.process(LookupElementBuilder.create(lookupString).withTypeText(option.getValue(), true).withIcon(PerlIcons.PERL_OPTIONS))) {
                return;
            }
        }
    }
    if (packageProcessor instanceof PerlPackageParentsProvider && ((PerlPackageParentsProvider) packageProcessor).hasPackageFilesOptions() && !PerlPackageUtil.processPackageFilesForPsiElement(baseElement, (packageName, file) -> typedStringsSet.contains(packageName) || PerlPackageCompletionUtil.processPackageLookupElement(file, packageName, null, completionProcessor, false))) {
        return;
    }
    Set<String> export = new HashSet<>();
    Set<String> exportOk = new HashSet<>();
    packageProcessor.addExports(useStatement, export, exportOk);
    exportOk.removeAll(export);
    for (String subName : export) {
        if (!typedStringsSet.contains(subName) && completionProcessor.matches(subName) && !completionProcessor.process(LookupElementBuilder.create(subName).withIcon(PerlIcons.SUB_GUTTER_ICON).withTypeText("default", true))) {
            return;
        }
    }
    for (String subName : exportOk) {
        if (!typedStringsSet.contains(subName) && completionProcessor.matches(subName) && !completionProcessor.process(LookupElementBuilder.create(subName).withIcon(PerlIcons.SUB_GUTTER_ICON).withTypeText("optional", true))) {
            return;
        }
    }
}
Also used : PerlPackageParentsProvider(com.perl5.lang.perl.extensions.packageprocessor.PerlPackageParentsProvider) PerlUseStatementElement(com.perl5.lang.perl.psi.impl.PerlUseStatementElement) PerlPackageOptionsProvider(com.perl5.lang.perl.extensions.packageprocessor.PerlPackageOptionsProvider) PerlPackageProcessor(com.perl5.lang.perl.extensions.packageprocessor.PerlPackageProcessor) PsiElement(com.intellij.psi.PsiElement) THashSet(gnu.trove.THashSet)

Example 5 with PerlCompletionProcessor

use of com.perl5.lang.perl.idea.completion.providers.processors.PerlCompletionProcessor in project Perl5-IDEA by Camelcade.

the class PodLinkCompletionProvider method addSectionsCompletions.

protected static void addSectionsCompletions(@Nullable PsiFile targetFile, @NotNull PerlCompletionProcessor completionProcessor) {
    if (targetFile == null) {
        return;
    }
    Set<String> distinctString = new HashSet<>();
    targetFile.accept(new PodStubsAwareRecursiveVisitor() {

        @Override
        public void visitElement(@NotNull PsiElement element) {
            if (completionProcessor.result()) {
                super.visitElement(element);
            }
        }

        @Override
        public void visitTargetableSection(PodTitledSection o) {
            String title = cleanItemText(o.getTitleText());
            if (completionProcessor.matches(title) && distinctString.add(title)) {
                completionProcessor.process(LookupElementBuilder.create(o, escapeTitle(title)).withLookupString(title).withPresentableText(title).withIcon(PerlIcons.POD_FILE).withTypeText(UsageViewUtil.getType(o)));
            }
            super.visitTargetableSection(o);
        }

        @Override
        public void visitItemSection(@NotNull PsiItemSection o) {
            if (((PodSectionItem) o).isTargetable()) {
                super.visitItemSection(o);
            } else {
                visitElement(o);
            }
        }

        @Contract("null->null")
        @Nullable
        private String cleanItemText(@Nullable String itemText) {
            if (itemText == null) {
                return null;
            }
            String trimmed = StringUtil.trimLeading(itemText.trim(), '*');
            if (NumberUtils.isNumber(trimmed)) {
                return null;
            }
            return trimItemText(trimmed);
        }

        @Override
        public void visitPodFormatIndex(@NotNull PsiPodFormatIndex o) {
            assert o instanceof PodFormatterX;
            if (!((PodFormatterX) o).isMeaningful()) {
                return;
            }
            String indexTitle = ((PodFormatterX) o).getTitleText();
            if (StringUtil.isEmpty(indexTitle) || !distinctString.add(indexTitle)) {
                return;
            }
            String escapedIndexTitle = escapeTitle(indexTitle);
            if (!completionProcessor.matches(indexTitle) && !completionProcessor.matches(escapedIndexTitle)) {
                return;
            }
            PsiElement indexTarget = ((PodFormatterX) o).getIndexTarget();
            String targetPresentableText;
            if (indexTarget instanceof PodFile) {
                targetPresentableText = cleanItemText(((PodFile) indexTarget).getPodLinkText());
            } else if (indexTarget instanceof PodCompositeElement) {
                targetPresentableText = cleanItemText(((PodCompositeElement) indexTarget).getPresentableText());
            } else {
                LOG.warn("Unhandled index target: " + indexTarget);
                return;
            }
            String tailText = null;
            if (targetPresentableText != null) {
                tailText = "(" + targetPresentableText + ")";
            }
            completionProcessor.process(LookupElementBuilder.create(o, escapedIndexTitle).withLookupString(indexTitle).withPresentableText(indexTitle).withTailText(tailText).withTypeText(UsageViewUtil.getType(o)).withIcon(PerlIcons.POD_FILE));
        }
    });
}
Also used : PsiItemSection(com.perl5.lang.pod.psi.PsiItemSection) PsiPodFormatIndex(com.perl5.lang.pod.psi.PsiPodFormatIndex) PodTitledSection(com.perl5.lang.pod.parser.psi.PodTitledSection) PodFormatterX(com.perl5.lang.pod.parser.psi.mixin.PodFormatterX) PodStubsAwareRecursiveVisitor(com.perl5.lang.pod.parser.psi.PodStubsAwareRecursiveVisitor) PodCompositeElement(com.perl5.lang.pod.parser.psi.PodCompositeElement) Contract(org.jetbrains.annotations.Contract) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable) PodFile(com.perl5.lang.pod.parser.psi.PodFile)

Aggregations

PsiElement (com.intellij.psi.PsiElement)5 PsiFile (com.intellij.psi.PsiFile)4 PerlTimeLogger (com.perl5.lang.perl.util.PerlTimeLogger)3 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)2 Project (com.intellij.openapi.project.Project)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PerlCompletionProcessor (com.perl5.lang.perl.idea.completion.providers.processors.PerlCompletionProcessor)2 PerlInjectionMarkersService (com.perl5.lang.perl.idea.intellilang.PerlInjectionMarkersService)2 PerlFileImpl (com.perl5.lang.perl.psi.impl.PerlFileImpl)2 Nullable (org.jetbrains.annotations.Nullable)2 AutoPopupController (com.intellij.codeInsight.AutoPopupController)1 InsertHandler (com.intellij.codeInsight.completion.InsertHandler)1 Lookup (com.intellij.codeInsight.lookup.Lookup)1 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 Language (com.intellij.lang.Language)1 Logger (com.intellij.openapi.diagnostic.Logger)1 StringUtil (com.intellij.openapi.util.text.StringUtil)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 IElementType (com.intellij.psi.tree.IElementType)1 ObjectUtils (com.intellij.util.ObjectUtils)1