Search in sources :

Example 21 with Instruction

use of com.intellij.codeInsight.controlflow.Instruction in project intellij-community by JetBrains.

the class PyDefUseUtil method getLatestDefs.

private static Collection<Instruction> getLatestDefs(final String varName, final Instruction[] instructions, final int instr, final boolean acceptTypeAssertions, final boolean acceptImplicitImports) {
    final Collection<Instruction> result = new LinkedHashSet<>();
    ControlFlowUtil.iteratePrev(instr, instructions, instruction -> {
        final PsiElement element = instruction.getElement();
        final PyImplicitImportNameDefiner implicit = PyUtil.as(element, PyImplicitImportNameDefiner.class);
        if (instruction instanceof ReadWriteInstruction) {
            final ReadWriteInstruction rwInstruction = (ReadWriteInstruction) instruction;
            final ReadWriteInstruction.ACCESS access = rwInstruction.getAccess();
            if (access.isWriteAccess() || acceptTypeAssertions && access.isAssertTypeAccess()) {
                final String name = elementName(element);
                if (Comparing.strEqual(name, varName)) {
                    result.add(rwInstruction);
                    return ControlFlowUtil.Operation.CONTINUE;
                }
            }
        } else if (acceptImplicitImports && implicit != null) {
            if (!implicit.multiResolveName(varName).isEmpty()) {
                result.add(instruction);
                return ControlFlowUtil.Operation.CONTINUE;
            }
        }
        return ControlFlowUtil.Operation.NEXT;
    });
    return result;
}
Also used : ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) Instruction(com.intellij.codeInsight.controlflow.Instruction) PsiElement(com.intellij.psi.PsiElement)

Example 22 with Instruction

use of com.intellij.codeInsight.controlflow.Instruction in project intellij-community by JetBrains.

the class PyConvertLambdaToFunctionIntention method isAvailable.

public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
    if (!(file instanceof PyFile)) {
        return false;
    }
    PyLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyLambdaExpression.class);
    if (lambdaExpression != null) {
        if (lambdaExpression.getBody() != null) {
            final ControlFlow flow = ControlFlowCache.getControlFlow(lambdaExpression);
            final List<Instruction> graph = Arrays.asList(flow.getInstructions());
            final List<PsiElement> elements = PyCodeFragmentUtil.getInputElements(graph, graph);
            if (elements.size() > 0)
                return false;
            return true;
        }
    }
    return false;
}
Also used : Instruction(com.intellij.codeInsight.controlflow.Instruction) ControlFlow(com.intellij.codeInsight.controlflow.ControlFlow) PsiElement(com.intellij.psi.PsiElement)

Example 23 with Instruction

use of com.intellij.codeInsight.controlflow.Instruction in project intellij-community by JetBrains.

the class PyHighlightExitPointsHandler method findExitPointsAndStatements.

private static Collection<PsiElement> findExitPointsAndStatements(final ControlFlow flow) {
    final List<PsiElement> statements = new ArrayList<>();
    final Instruction[] instructions = flow.getInstructions();
    for (Instruction instruction : instructions[instructions.length - 1].allPred()) {
        final PsiElement element = instruction.getElement();
        if (element == null) {
            continue;
        }
        final PsiElement statement = PyPsiUtils.getStatement(element);
        if (statement != null) {
            statements.add(statement);
        }
    }
    return statements;
}
Also used : ArrayList(java.util.ArrayList) Instruction(com.intellij.codeInsight.controlflow.Instruction) PsiElement(com.intellij.psi.PsiElement)

Example 24 with Instruction

use of com.intellij.codeInsight.controlflow.Instruction in project intellij-community by JetBrains.

the class PyReferenceExpressionImpl method getTypeByControlFlow.

private static PyType getTypeByControlFlow(@NotNull String name, @NotNull TypeEvalContext context, @NotNull PyExpression anchor, @NotNull ScopeOwner scopeOwner) {
    final PyAugAssignmentStatement augAssignment = PsiTreeUtil.getParentOfType(anchor, PyAugAssignmentStatement.class);
    final PyElement element = augAssignment != null ? augAssignment : anchor;
    try {
        final List<Instruction> defs = PyDefUseUtil.getLatestDefs(scopeOwner, name, element, true, false);
        if (!defs.isEmpty()) {
            final ReadWriteInstruction firstInstruction = PyUtil.as(defs.get(0), ReadWriteInstruction.class);
            PyType type = firstInstruction != null ? firstInstruction.getType(context, anchor) : null;
            for (int i = 1; i < defs.size(); i++) {
                final ReadWriteInstruction instruction = PyUtil.as(defs.get(i), ReadWriteInstruction.class);
                type = PyUnionType.union(type, instruction != null ? instruction.getType(context, anchor) : null);
            }
            return type;
        }
    } catch (PyDefUseUtil.InstructionNotFoundException ignored) {
    }
    return null;
}
Also used : PyDefUseUtil(com.jetbrains.python.refactoring.PyDefUseUtil) ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) Instruction(com.intellij.codeInsight.controlflow.Instruction)

Example 25 with Instruction

use of com.intellij.codeInsight.controlflow.Instruction in project intellij-community by JetBrains.

the class ScopeImpl method getDeclaredVariable.

public ScopeVariable getDeclaredVariable(@NotNull final PsiElement anchorElement, @NotNull final String name) throws DFALimitExceededException {
    computeScopeVariables();
    for (int i = 0; i < myFlow.length; i++) {
        Instruction instruction = myFlow[i];
        final PsiElement element = instruction.getElement();
        if (element == anchorElement) {
            return myCachedScopeVariables.get(i).get(name);
        }
    }
    return null;
}
Also used : Instruction(com.intellij.codeInsight.controlflow.Instruction) PsiElement(com.intellij.psi.PsiElement)

Aggregations

Instruction (com.intellij.codeInsight.controlflow.Instruction)33 ReadWriteInstruction (com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction)15 NotNull (org.jetbrains.annotations.NotNull)12 PsiElement (com.intellij.psi.PsiElement)10 ControlFlow (com.intellij.codeInsight.controlflow.ControlFlow)7 ArrayList (java.util.ArrayList)5 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)4 Pair (com.intellij.openapi.util.Pair)3 Scope (com.jetbrains.python.codeInsight.dataflow.scope.Scope)3 Project (com.intellij.openapi.project.Project)2 TextRange (com.intellij.openapi.util.TextRange)2 PyDefUseUtil (com.jetbrains.python.refactoring.PyDefUseUtil)2 TargetElementUtil (com.intellij.codeInsight.TargetElementUtil)1 CannotCreateCodeFragmentException (com.intellij.codeInsight.codeFragment.CannotCreateCodeFragmentException)1 HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)1 Language (com.intellij.lang.Language)1 InlineActionHandler (com.intellij.lang.refactoring.InlineActionHandler)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 CommandProcessor (com.intellij.openapi.command.CommandProcessor)1 Logger (com.intellij.openapi.diagnostic.Logger)1