Search in sources :

Example 1 with PerlNamespaceEntityProcessor

use of com.perl5.lang.perl.util.processors.PerlNamespaceEntityProcessor in project Perl5-IDEA by Camelcade.

the class PerlVariableNameCompletionProvider method fillWithImportedVariables.

private void fillWithImportedVariables(@NotNull PsiElement variableNameElement, @NotNull CompletionResultSet resultSet) {
    PerlNamespaceDefinitionElement namespaceContainer = PerlPackageUtil.getNamespaceContainerForElement(variableNameElement);
    if (namespaceContainer == null) {
        return;
    }
    String packageName = namespaceContainer.getPackageName();
    if (// incomplete package definition
    StringUtil.isEmpty(packageName)) {
        return;
    }
    PerlNamespaceEntityProcessor<PerlExportDescriptor> processor = null;
    PsiElement perlVariable = variableNameElement.getParent();
    if (perlVariable instanceof PsiPerlScalarVariable) {
        processor = new PerlNamespaceEntityProcessor<PerlExportDescriptor>() {

            @Override
            public boolean process(String namespaceName, PerlExportDescriptor entity) {
                LookupElementBuilder lookupElement = null;
                if (entity.isScalar()) {
                    lookupElement = PerlVariableCompletionUtil.getScalarLookupElement(entity.getImportedName());
                } else if (entity.isArray()) {
                    lookupElement = PerlVariableCompletionUtil.getArrayElementLookupElement(entity.getImportedName());
                } else if (entity.isHash()) {
                    lookupElement = PerlVariableCompletionUtil.getHashElementLookupElement(entity.getImportedName());
                }
                if (lookupElement != null) {
                    resultSet.addElement(lookupElement.withTypeText(entity.getRealPackage(), true));
                }
                return true;
            }
        };
    } else if (perlVariable instanceof PsiPerlArrayVariable || perlVariable instanceof PsiPerlArrayIndexVariable) {
        processor = new PerlNamespaceEntityProcessor<PerlExportDescriptor>() {

            @Override
            public boolean process(String namespaceName, PerlExportDescriptor entity) {
                LookupElementBuilder lookupElement = null;
                if (entity.isArray()) {
                    lookupElement = PerlVariableCompletionUtil.getArrayLookupElement(entity.getImportedName());
                } else if (entity.isHash()) {
                    lookupElement = PerlVariableCompletionUtil.getHashSliceLookupElement(entity.getImportedName());
                }
                if (lookupElement != null) {
                    resultSet.addElement(lookupElement.withTypeText(entity.getRealPackage(), true));
                }
                return true;
            }
        };
    } else if (perlVariable instanceof PsiPerlHashVariable) {
        processor = new PerlNamespaceEntityProcessor<PerlExportDescriptor>() {

            @Override
            public boolean process(String namespaceName, PerlExportDescriptor entity) {
                LookupElementBuilder lookupElement = null;
                if (entity.isHash()) {
                    lookupElement = PerlVariableCompletionUtil.getHashLookupElement(entity.getImportedName());
                }
                if (lookupElement != null) {
                    resultSet.addElement(lookupElement.withTypeText(entity.getRealPackage(), true));
                }
                return true;
            }
        };
    }
    if (processor != null) {
        PerlUtil.processImportedEntities(namespaceContainer, processor);
    }
}
Also used : PerlExportDescriptor(com.perl5.lang.perl.extensions.packageprocessor.PerlExportDescriptor) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) PerlNamespaceEntityProcessor(com.perl5.lang.perl.util.processors.PerlNamespaceEntityProcessor) PsiElement(com.intellij.psi.PsiElement)

Example 2 with PerlNamespaceEntityProcessor

use of com.perl5.lang.perl.util.processors.PerlNamespaceEntityProcessor in project Perl5-IDEA by Camelcade.

the class PerlUtil method processImportedEntities.

/**
 * Processing use statements in the namespace or file and processing all imports found
 *
 * @param namespace Root element to start searching from
 */
public static void processImportedEntities(@NotNull PerlNamespaceDefinitionElement namespace, @NotNull PerlNamespaceEntityProcessor<PerlExportDescriptor> processor) {
    for (PsiElement element : PerlPsiUtil.collectUseStatements(namespace)) {
        PerlUseStatement useStatement = (PerlUseStatement) element;
        String packageName = useStatement.getPackageName();
        if (packageName != null) {
            for (PerlExportDescriptor entry : useStatement.getPackageProcessor().getImports(useStatement)) {
                ProgressManager.checkCanceled();
                processor.process(packageName, entry);
            }
        }
    }
    PerlImportsProvider.processProviders(namespace, provider -> {
        for (PerlExportDescriptor descriptor : provider.getImports()) {
            if (!processor.process(namespace.getPackageName(), descriptor)) {
                return false;
            }
        }
        return true;
    });
}
Also used : PerlExportDescriptor(com.perl5.lang.perl.extensions.packageprocessor.PerlExportDescriptor) PerlUseStatement(com.perl5.lang.perl.psi.PerlUseStatement) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiElement (com.intellij.psi.PsiElement)2 PerlExportDescriptor (com.perl5.lang.perl.extensions.packageprocessor.PerlExportDescriptor)2 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)1 PerlUseStatement (com.perl5.lang.perl.psi.PerlUseStatement)1 PerlNamespaceEntityProcessor (com.perl5.lang.perl.util.processors.PerlNamespaceEntityProcessor)1