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();
};
}
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);
}
Aggregations