use of com.perl5.lang.perl.idea.completion.providers.processors.PerlVariableCompletionProcessor 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();
};
}
use of com.perl5.lang.perl.idea.completion.providers.processors.PerlVariableCompletionProcessor in project Perl5-IDEA by Camelcade.
the class PerlVariableCompletionUtil method processVariableLookupElement.
/**
* @param sigilToPrepend '_' means we don't need to prepend
*/
public static boolean processVariableLookupElement(@NotNull PerlVariableDeclarationElement element, char sigilToPrepend, @NotNull PerlVariableCompletionProcessor completionProcessor) {
String variableName = StringUtil.notNullize(element.getName());
if (!completionProcessor.hasBraces()) {
variableName = PerlVariable.braceName(variableName);
}
boolean isBuiltIn = element instanceof PerlBuiltInVariable;
String explicitNamespaceName = completionProcessor.getExplicitNamespaceName();
if (StringUtil.isNotEmpty(explicitNamespaceName) && !isBuiltIn && element.isGlobalDeclaration() && !explicitNamespaceName.equals(element.getNamespaceName())) {
return completionProcessor.result();
}
String lookupString = variableName;
if (!isBuiltIn && element.isGlobalDeclaration() && !completionProcessor.isLexical()) {
lookupString = element.getCanonicalName();
if (StringUtil.isEmpty(lookupString)) {
return completionProcessor.result();
}
}
if (completionProcessor.isFullQualified()) {
if (!completionProcessor.matches(lookupString)) {
return completionProcessor.result();
}
} else {
if (!completionProcessor.matches(variableName)) {
return completionProcessor.result();
}
}
Icon icon;
if (sigilToPrepend != '_') {
lookupString = sigilToPrepend + lookupString;
icon = PerlVariableType.bySigil(sigilToPrepend).getIcon();
} else {
icon = PerlIconProvider.getIcon(element);
}
LookupElementBuilder elementBuilder = LookupElementBuilder.create(element, lookupString).withIcon(icon).withPresentableText(variableName);
if (isBuiltIn) {
elementBuilder = elementBuilder.withTypeText(PerlBundle.message("built.in.type.text"));
} else if (element.isGlobalDeclaration()) {
elementBuilder = elementBuilder.withTypeText(element.getNamespaceName(), true);
}
return completionProcessor.process(sigilToPrepend == '_' ? elementBuilder : elementBuilder.withLookupString(completionProcessor.isFullQualified() ? StringUtil.notNullize(element.getCanonicalName()) : variableName));
}
use of com.perl5.lang.perl.idea.completion.providers.processors.PerlVariableCompletionProcessor in project Perl5-IDEA by Camelcade.
the class PerlVariableCompletionProvider method addCompletions.
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull ProcessingContext context, @NotNull CompletionResultSet resultSet) {
if (!Experiments.getInstance().isFeatureEnabled("perl5.completion.var.without.sigil")) {
return;
}
PsiElement subName = parameters.getPosition();
PsiElement method = subName.getParent();
String namespaceName = null;
if (!(method instanceof PsiPerlMethod) || ((PsiPerlMethod) method).isObjectMethod()) {
return;
}
namespaceName = ((PsiPerlMethod) method).getExplicitNamespaceName();
if (StringUtil.isNotEmpty(namespaceName)) {
resultSet = resultSet.withPrefixMatcher(PerlPackageUtil.join(namespaceName, resultSet.getPrefixMatcher().getPrefix()));
}
PerlVariableCompletionProcessor variableCompletionProcessor = new PerlVariableCompletionProcessorImpl(parameters, resultSet, subName, namespaceName, false, false, false);
PerlTimeLogger logger = PerlTimeLogger.create(LOG);
PerlVariableCompletionUtil.processVariables(variableCompletionProcessor, logger);
variableCompletionProcessor.logStatus(getClass());
}
use of com.perl5.lang.perl.idea.completion.providers.processors.PerlVariableCompletionProcessor in project Perl5-IDEA by Camelcade.
the class PerlVariableCompletionUtil method fillWithImportedVariables.
public static void fillWithImportedVariables(@NotNull PerlVariableCompletionProcessor variableCompletionProcessor) {
PerlNamespaceDefinitionElement namespaceContainer = PerlPackageUtil.getNamespaceContainerForElement(variableCompletionProcessor.getLeafElement());
if (namespaceContainer == null) {
return;
}
namespaceContainer = CompletionUtil.getOriginalOrSelf(namespaceContainer);
String packageName = namespaceContainer.getNamespaceName();
if (StringUtil.isEmpty(packageName)) {
return;
}
PerlNamespaceEntityProcessor<PerlExportDescriptor> processor = null;
PsiElement perlVariable = variableCompletionProcessor.getLeafParentElement();
if (perlVariable instanceof PsiPerlScalarVariable) {
processor = (namespaceName, descriptor) -> {
LookupElementBuilder lookupElement = null;
String entityName = descriptor.getImportedName();
if (!variableCompletionProcessor.matches(entityName)) {
return variableCompletionProcessor.result();
}
if (descriptor.isScalar()) {
lookupElement = processVariableLookupElement(entityName, PerlVariableType.SCALAR);
} else if (descriptor.isArray()) {
lookupElement = processVariableLookupElement(entityName, PerlVariableType.ARRAY);
} else if (descriptor.isHash()) {
lookupElement = processVariableLookupElement(entityName, PerlVariableType.HASH);
}
if (lookupElement != null) {
return variableCompletionProcessor.process(lookupElement.withTypeText(descriptor.getRealPackage(), true));
}
return variableCompletionProcessor.result();
};
} else if (perlVariable instanceof PsiPerlArrayVariable || perlVariable instanceof PsiPerlArrayIndexVariable) {
processor = (namespaceName, descriptor) -> {
LookupElementBuilder lookupElement = null;
String entityName = descriptor.getImportedName();
if (!variableCompletionProcessor.matches(entityName)) {
return variableCompletionProcessor.result();
}
if (descriptor.isArray()) {
lookupElement = createArrayElementLookupElement(entityName, PerlVariableType.ARRAY);
} else if (descriptor.isHash()) {
lookupElement = processHashElementLookupElement(entityName, PerlVariableType.HASH);
}
if (lookupElement != null) {
return variableCompletionProcessor.process(lookupElement.withTypeText(descriptor.getRealPackage(), true));
}
return variableCompletionProcessor.result();
};
} else if (perlVariable instanceof PsiPerlHashVariable) {
processor = (namespaceName, descriptor) -> {
LookupElementBuilder lookupElement = null;
if (descriptor.isHash()) {
String entityName = descriptor.getImportedName();
if (!variableCompletionProcessor.matches(entityName)) {
return variableCompletionProcessor.result();
}
lookupElement = processVariableLookupElement(entityName, PerlVariableType.HASH);
}
if (lookupElement != null) {
return variableCompletionProcessor.process(lookupElement.withTypeText(descriptor.getRealPackage(), true));
}
return variableCompletionProcessor.result();
};
}
if (processor != null) {
namespaceContainer.processExportDescriptors(processor);
}
}
use of com.perl5.lang.perl.idea.completion.providers.processors.PerlVariableCompletionProcessor in project Perl5-IDEA by Camelcade.
the class PerlVariableCompletionUtil method processFullQualifiedVariables.
public static boolean processFullQualifiedVariables(@NotNull PerlVariableCompletionProcessor variableCompletionProcessor) {
if (!variableCompletionProcessor.isFullQualified() && variableCompletionProcessor.getCompletionParameters().getInvocationCount() == 0) {
return variableCompletionProcessor.result();
}
PsiElement variableNameElement = variableCompletionProcessor.getLeafElement();
PsiElement perlVariable = variableCompletionProcessor.getLeafParentElement();
Processor<PerlVariableDeclarationElement> variableProcessor = createVariableLookupProcessor(variableCompletionProcessor);
Processor<PerlVariableDeclarationElement> lookupGenerator = it -> {
if (!variableCompletionProcessor.isFullQualified() && VARIABLES_AVAILABLE_ONLY_WITH_FQN.contains(it.getName())) {
return variableCompletionProcessor.result();
}
String idString = it.getCanonicalNameWithSigil();
if (!variableCompletionProcessor.isRegistered(idString)) {
return variableProcessor.process(it);
}
return variableCompletionProcessor.result();
};
Project project = variableNameElement.getProject();
GlobalSearchScope resolveScope = variableNameElement.getResolveScope();
String namespaceName = variableCompletionProcessor.getExplicitNamespaceName();
if (perlVariable instanceof PsiPerlScalarVariable) {
return PerlScalarUtil.processDefinedGlobalScalars(project, resolveScope, lookupGenerator, false, namespaceName) && PerlArrayUtil.processDefinedGlobalArrays(project, resolveScope, lookupGenerator, false, namespaceName) && PerlHashUtil.processDefinedGlobalHashes(project, resolveScope, lookupGenerator, false, namespaceName);
} else if (perlVariable instanceof PsiPerlArrayVariable) {
return PerlArrayUtil.processDefinedGlobalArrays(project, resolveScope, lookupGenerator, false, namespaceName) && PerlHashUtil.processDefinedGlobalHashes(project, resolveScope, lookupGenerator, false, namespaceName);
} else if (perlVariable instanceof PsiPerlArrayIndexVariable) {
// global arrays
return PerlArrayUtil.processDefinedGlobalArrays(project, resolveScope, lookupGenerator, false, namespaceName);
} else if (perlVariable instanceof PsiPerlHashVariable) {
// global hashes
return PerlHashUtil.processDefinedGlobalHashes(project, resolveScope, lookupGenerator, false, namespaceName) && (!hasHashSlices(perlVariable) || PerlArrayUtil.processDefinedGlobalArrays(project, resolveScope, lookupGenerator, false, namespaceName));
} else {
Processor<PerlGlobVariable> typeGlobProcessor = typeglob -> variableCompletionProcessor.process(processVariableLookupElement(typeglob, perlVariable instanceof PsiPerlMethod, variableCompletionProcessor));
return PerlScalarUtil.processDefinedGlobalScalars(project, resolveScope, lookupGenerator, false, namespaceName) && PerlArrayUtil.processDefinedGlobalArrays(project, resolveScope, lookupGenerator, false, namespaceName) && PerlHashUtil.processDefinedGlobalHashes(project, resolveScope, lookupGenerator, false, namespaceName) && PerlGlobUtil.processDefinedGlobs(project, resolveScope, null, typeGlobProcessor, false, namespaceName);
}
}
Aggregations