Search in sources :

Example 1 with ChainCallExtractor

use of com.intellij.refactoring.chainCall.ChainCallExtractor in project intellij-community by JetBrains.

the class ExtractChainedMapAction method isAvailable.

@Override
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
    PsiLocalVariable variable = PsiTreeUtil.getParentOfType(element, PsiLocalVariable.class, false, PsiStatement.class, PsiLambdaExpression.class);
    if (variable == null || variable.getName() == null)
        return false;
    PsiExpression initializer = variable.getInitializer();
    if (initializer == null)
        return false;
    PsiDeclarationStatement declaration = tryCast(variable.getParent(), PsiDeclarationStatement.class);
    if (declaration == null || declaration.getDeclaredElements().length != 1)
        return false;
    PsiCodeBlock block = tryCast(declaration.getParent(), PsiCodeBlock.class);
    if (block == null)
        return false;
    PsiLambdaExpression lambda = tryCast(block.getParent(), PsiLambdaExpression.class);
    ChainCallExtractor extractor = ChainCallExtractor.findExtractor(lambda, initializer, variable.getType());
    if (extractor == null)
        return false;
    PsiParameter parameter = lambda.getParameterList().getParameters()[0];
    if (!ReferencesSearch.search(parameter).forEach((Processor<PsiReference>) ref -> PsiTreeUtil.isAncestor(initializer, ref.getElement(), false))) {
        return false;
    }
    setText(CodeInsightBundle.message("intention.extract.map.step.text", variable.getName(), extractor.getMethodName(parameter, initializer, variable.getType())));
    return true;
}
Also used : Processor(com.intellij.util.Processor) ChainCallExtractor(com.intellij.refactoring.chainCall.ChainCallExtractor)

Aggregations

ChainCallExtractor (com.intellij.refactoring.chainCall.ChainCallExtractor)1 Processor (com.intellij.util.Processor)1