Search in sources :

Example 1 with PerlDelegatingVariableCompletionProcessor

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

the class PerlVariableCompletionUtil method createVariableLookupProcessor.

/**
 * @return processor of variable declarations, generating lookup elements for them and feeding to the {@code lookupConsumer}
 */
@NotNull
private static Processor<PerlVariableDeclarationElement> createVariableLookupProcessor(@NotNull PerlVariableCompletionProcessor completionProcessor) {
    PsiElement perlVariable = completionProcessor.getLeafParentElement();
    boolean addHashSlices = hasHashSlices(perlVariable);
    PerlDelegatingVariableCompletionProcessor hashElementCompletionProcessor = new PerlDelegatingVariableCompletionProcessor(completionProcessor) {

        @Override
        public void addElement(@NotNull LookupElementBuilder lookupElement) {
            super.addElement(lookupElement.withInsertHandler(PerlInsertHandlers.HASH_ELEMENT_INSERT_HANDLER).withTailText("{}"));
        }
    };
    PerlDelegatingVariableCompletionProcessor arrayElementCompletionProcessor = new PerlDelegatingVariableCompletionProcessor(completionProcessor) {

        @Override
        public void addElement(@NotNull LookupElementBuilder lookupElement) {
            super.addElement(lookupElement.withInsertHandler(PerlInsertHandlers.ARRAY_ELEMENT_INSERT_HANDLER).withTailText("[]"));
        }
    };
    if (perlVariable instanceof PsiPerlScalarVariable) {
        return variable -> {
            if (variable.getActualType() == PerlVariableType.SCALAR) {
                return processVariableLookupElement(variable, '_', completionProcessor);
            } else if (variable.getActualType() == PerlVariableType.ARRAY) {
                return processVariableLookupElement(variable, '_', arrayElementCompletionProcessor);
            } else if (variable.getActualType() == PerlVariableType.HASH) {
                return processVariableLookupElement(variable, '_', hashElementCompletionProcessor);
            }
            return completionProcessor.result();
        };
    } else if (perlVariable instanceof PsiPerlArrayVariable) {
        return variable -> {
            if (variable.getActualType() == PerlVariableType.ARRAY) {
                return processVariableLookupElement(variable, '_', completionProcessor) && processVariableLookupElement(variable, '_', arrayElementCompletionProcessor);
            } else if (variable.getActualType() == PerlVariableType.HASH) {
                return processVariableLookupElement(variable, '_', hashElementCompletionProcessor);
            }
            return completionProcessor.result();
        };
    } else if (perlVariable instanceof PsiPerlArrayIndexVariable) {
        return variable -> {
            if (variable.getActualType() == PerlVariableType.ARRAY) {
                return processVariableLookupElement(variable, '_', completionProcessor);
            }
            return completionProcessor.result();
        };
    } else if (perlVariable instanceof PsiPerlHashVariable) {
        return variable -> {
            PerlVariableType variableType = variable.getActualType();
            if (variableType == PerlVariableType.HASH) {
                return processVariableLookupElement(variable, '_', completionProcessor) && (!addHashSlices || processVariableLookupElement(variable, '_', hashElementCompletionProcessor));
            } else if (addHashSlices && variableType == PerlVariableType.ARRAY) {
                return processVariableLookupElement(variable, '_', arrayElementCompletionProcessor);
            }
            return completionProcessor.result();
        };
    } else if (perlVariable instanceof PerlGlobVariable) {
        return variable -> processVariableLookupElement(variable, '_', completionProcessor);
    }
    return variable -> {
        PerlVariableType variableType = variable.getActualType();
        if (variableType == PerlVariableType.SCALAR) {
            return processVariableLookupElement(variable, PerlVariableType.SCALAR.getSigil(), completionProcessor);
        } else if (variableType == PerlVariableType.ARRAY) {
            return processVariableLookupElement(variable, PerlVariableType.ARRAY.getSigil(), completionProcessor) && processVariableLookupElement(variable, PerlVariableType.SCALAR.getSigil(), arrayElementCompletionProcessor) && processVariableLookupElement(variable, PerlVariableType.ARRAY.getSigil(), arrayElementCompletionProcessor);
        } else if (variable.getActualType() == PerlVariableType.HASH) {
            return processVariableLookupElement(variable, PerlVariableType.HASH.getSigil(), completionProcessor) && processVariableLookupElement(variable, PerlVariableType.SCALAR.getSigil(), hashElementCompletionProcessor) && processVariableLookupElement(variable, PerlVariableType.ARRAY.getSigil(), hashElementCompletionProcessor);
        }
        return completionProcessor.result();
    };
}
Also used : PerlBundle(com.perl5.PerlBundle) GLOB_GUTTER_ICON(com.perl5.PerlIcons.GLOB_GUTTER_ICON) PerlDelegatingVariableCompletionProcessor(com.perl5.lang.perl.idea.completion.providers.processors.PerlDelegatingVariableCompletionProcessor) THashSet(gnu.trove.THashSet) ContainerUtil(com.intellij.util.containers.ContainerUtil) PerlInsertHandlers(com.perl5.lang.perl.idea.completion.PerlInsertHandlers) PerlLexicalScope(com.perl5.lang.perl.psi.properties.PerlLexicalScope) PerlVariableCompletionProcessorImpl(com.perl5.lang.perl.idea.completion.providers.processors.PerlVariableCompletionProcessorImpl) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) PerlExportDescriptor(com.perl5.lang.perl.extensions.packageprocessor.PerlExportDescriptor) PerlSharedSettings(com.perl5.lang.perl.idea.configuration.settings.PerlSharedSettings) PsiElement(com.intellij.psi.PsiElement) PerlVersion(com.perl5.lang.perl.internals.PerlVersion) Project(com.intellij.openapi.project.Project) com.perl5.lang.perl.psi(com.perl5.lang.perl.psi) PerlIconProvider(com.perl5.lang.perl.idea.ui.PerlIconProvider) PerlBuiltInVariablesService(com.perl5.lang.perl.psi.references.PerlBuiltInVariablesService) CompletionUtil(com.intellij.codeInsight.completion.CompletionUtil) com.perl5.lang.perl.util(com.perl5.lang.perl.util) PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) PerlVariableType(com.perl5.lang.perl.psi.utils.PerlVariableType) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) LookupElement(com.intellij.codeInsight.lookup.LookupElement) StringUtil(com.intellij.openapi.util.text.StringUtil) Predicate(java.util.function.Predicate) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Set(java.util.Set) Objects(java.util.Objects) Nullable(org.jetbrains.annotations.Nullable) PerlBuiltInVariable(com.perl5.lang.perl.psi.impl.PerlBuiltInVariable) PerlCompletionWeighter(com.perl5.lang.perl.idea.PerlCompletionWeighter) Processor(com.intellij.util.Processor) PerlResolveUtil(com.perl5.lang.perl.psi.utils.PerlResolveUtil) NotNull(org.jetbrains.annotations.NotNull) PerlVariableCompletionProcessor(com.perl5.lang.perl.idea.completion.providers.processors.PerlVariableCompletionProcessor) PerlNamespaceEntityProcessor(com.perl5.lang.perl.util.processors.PerlNamespaceEntityProcessor) javax.swing(javax.swing) PerlVariableType(com.perl5.lang.perl.psi.utils.PerlVariableType) PerlDelegatingVariableCompletionProcessor(com.perl5.lang.perl.idea.completion.providers.processors.PerlDelegatingVariableCompletionProcessor) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PerlDelegatingVariableCompletionProcessor

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

the class PerlVariableCompletionUtil method fillWithLexicalVariables.

public static void fillWithLexicalVariables(@NotNull PerlVariableCompletionProcessor variableCompletionProcessor) {
    PsiElement perlVariable = variableCompletionProcessor.getLeafParentElement();
    Processor<PerlVariableDeclarationElement> lookupProcessor = createLexicalLookupProcessor(new PerlDelegatingVariableCompletionProcessor(variableCompletionProcessor) {

        @Override
        public boolean isLexical() {
            return true;
        }
    });
    PsiScopeProcessor processor = (element, __) -> {
        if (!(element instanceof PerlVariableDeclarationElement)) {
            return true;
        }
        PerlVariableDeclarationElement variable = (PerlVariableDeclarationElement) element;
        PsiElement declarationStatement = PsiTreeUtil.getParentOfType(variable, PerlStatement.class);
        if (PsiTreeUtil.isAncestor(declarationStatement, perlVariable, false)) {
            return true;
        }
        if (StringUtil.isNotEmpty(variable.getName())) {
            boolean processResult = lookupProcessor.process(variable);
            if (processResult && variable.isGlobalDeclaration()) {
                variableCompletionProcessor.register(variable.getCanonicalNameWithSigil());
            }
            return processResult;
        }
        return true;
    };
    PerlResolveUtil.treeWalkUp(variableCompletionProcessor.getLeafElement(), processor);
}
Also used : PerlBundle(com.perl5.PerlBundle) GLOB_GUTTER_ICON(com.perl5.PerlIcons.GLOB_GUTTER_ICON) PerlDelegatingVariableCompletionProcessor(com.perl5.lang.perl.idea.completion.providers.processors.PerlDelegatingVariableCompletionProcessor) THashSet(gnu.trove.THashSet) ContainerUtil(com.intellij.util.containers.ContainerUtil) PerlInsertHandlers(com.perl5.lang.perl.idea.completion.PerlInsertHandlers) PerlLexicalScope(com.perl5.lang.perl.psi.properties.PerlLexicalScope) PerlVariableCompletionProcessorImpl(com.perl5.lang.perl.idea.completion.providers.processors.PerlVariableCompletionProcessorImpl) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) PerlExportDescriptor(com.perl5.lang.perl.extensions.packageprocessor.PerlExportDescriptor) PerlSharedSettings(com.perl5.lang.perl.idea.configuration.settings.PerlSharedSettings) PsiElement(com.intellij.psi.PsiElement) PerlVersion(com.perl5.lang.perl.internals.PerlVersion) Project(com.intellij.openapi.project.Project) com.perl5.lang.perl.psi(com.perl5.lang.perl.psi) PerlIconProvider(com.perl5.lang.perl.idea.ui.PerlIconProvider) PerlBuiltInVariablesService(com.perl5.lang.perl.psi.references.PerlBuiltInVariablesService) CompletionUtil(com.intellij.codeInsight.completion.CompletionUtil) com.perl5.lang.perl.util(com.perl5.lang.perl.util) PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) PerlVariableType(com.perl5.lang.perl.psi.utils.PerlVariableType) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) LookupElement(com.intellij.codeInsight.lookup.LookupElement) StringUtil(com.intellij.openapi.util.text.StringUtil) Predicate(java.util.function.Predicate) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Set(java.util.Set) Objects(java.util.Objects) Nullable(org.jetbrains.annotations.Nullable) PerlBuiltInVariable(com.perl5.lang.perl.psi.impl.PerlBuiltInVariable) PerlCompletionWeighter(com.perl5.lang.perl.idea.PerlCompletionWeighter) Processor(com.intellij.util.Processor) PerlResolveUtil(com.perl5.lang.perl.psi.utils.PerlResolveUtil) NotNull(org.jetbrains.annotations.NotNull) PerlVariableCompletionProcessor(com.perl5.lang.perl.idea.completion.providers.processors.PerlVariableCompletionProcessor) PerlNamespaceEntityProcessor(com.perl5.lang.perl.util.processors.PerlNamespaceEntityProcessor) javax.swing(javax.swing) PerlDelegatingVariableCompletionProcessor(com.perl5.lang.perl.idea.completion.providers.processors.PerlDelegatingVariableCompletionProcessor) PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) PsiElement(com.intellij.psi.PsiElement)

Aggregations

CompletionUtil (com.intellij.codeInsight.completion.CompletionUtil)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)2 Project (com.intellij.openapi.project.Project)2 StringUtil (com.intellij.openapi.util.text.StringUtil)2 PsiElement (com.intellij.psi.PsiElement)2 PsiScopeProcessor (com.intellij.psi.scope.PsiScopeProcessor)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)2 Processor (com.intellij.util.Processor)2 ContainerUtil (com.intellij.util.containers.ContainerUtil)2 PerlBundle (com.perl5.PerlBundle)2 GLOB_GUTTER_ICON (com.perl5.PerlIcons.GLOB_GUTTER_ICON)2 PerlExportDescriptor (com.perl5.lang.perl.extensions.packageprocessor.PerlExportDescriptor)2 PerlCompletionWeighter (com.perl5.lang.perl.idea.PerlCompletionWeighter)2 PerlInsertHandlers (com.perl5.lang.perl.idea.completion.PerlInsertHandlers)2 PerlDelegatingVariableCompletionProcessor (com.perl5.lang.perl.idea.completion.providers.processors.PerlDelegatingVariableCompletionProcessor)2 PerlVariableCompletionProcessor (com.perl5.lang.perl.idea.completion.providers.processors.PerlVariableCompletionProcessor)2 PerlVariableCompletionProcessorImpl (com.perl5.lang.perl.idea.completion.providers.processors.PerlVariableCompletionProcessorImpl)2 PerlSharedSettings (com.perl5.lang.perl.idea.configuration.settings.PerlSharedSettings)2