Search in sources :

Example 1 with ControlFlow

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

the class PyControlFlowBuilderTest method doTest.

private void doTest() {
    final String testName = getTestName(false).toLowerCase();
    configureByFile(testName + ".py");
    final ControlFlow flow = ControlFlowCache.getControlFlow((PyFile) myFile);
    final String fullPath = getTestDataPath() + testName + ".txt";
    check(fullPath, flow);
}
Also used : ControlFlow(com.intellij.codeInsight.controlflow.ControlFlow)

Example 2 with ControlFlow

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

the class PyControlFlowBuilderTest method doTestFirstStatement.

private void doTestFirstStatement() {
    final String testName = getTestName(false).toLowerCase();
    configureByFile(testName + ".py");
    final String fullPath = getTestDataPath() + testName + ".txt";
    final ControlFlow flow = ControlFlowCache.getControlFlow((ScopeOwner) ((PyFile) myFile).getStatements().get(0));
    check(fullPath, flow);
}
Also used : ControlFlow(com.intellij.codeInsight.controlflow.ControlFlow)

Example 3 with ControlFlow

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

the class PyControlFlowBuilderTest method testSelf.

public void testSelf() {
    final String testName = getTestName(false).toLowerCase();
    configureByFile(testName + ".py");
    final String fullPath = getTestDataPath() + testName + ".txt";
    final PyClass pyClass = ((PyFile) myFile).getTopLevelClasses().get(0);
    final ControlFlow flow = ControlFlowCache.getControlFlow(pyClass.getMethods()[0]);
    check(fullPath, flow);
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) ControlFlow(com.intellij.codeInsight.controlflow.ControlFlow)

Example 4 with ControlFlow

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

the class ControlFlowCache method getControlFlow.

public static ControlFlow getControlFlow(@NotNull ScopeOwner element) {
    SoftReference<ControlFlow> ref = element.getUserData(CONTROL_FLOW_KEY);
    ControlFlow flow = SoftReference.dereference(ref);
    if (flow == null) {
        flow = new PyControlFlowBuilder().buildControlFlow(element);
        element.putUserData(CONTROL_FLOW_KEY, new SoftReference<>(flow));
    }
    return flow;
}
Also used : ControlFlow(com.intellij.codeInsight.controlflow.ControlFlow)

Example 5 with ControlFlow

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

the class PyBaseMakeFunctionTopLevelProcessor method analyseScope.

@NotNull
protected AnalysisResult analyseScope(@NotNull ScopeOwner owner) {
    final ControlFlow controlFlow = ControlFlowCache.getControlFlow(owner);
    final AnalysisResult result = new AnalysisResult();
    for (Instruction instruction : controlFlow.getInstructions()) {
        if (instruction instanceof ReadWriteInstruction) {
            final ReadWriteInstruction readWriteInstruction = (ReadWriteInstruction) instruction;
            final PsiElement element = readWriteInstruction.getElement();
            if (element == null) {
                continue;
            }
            if (readWriteInstruction.getAccess().isReadAccess()) {
                for (PsiElement resolved : PyUtil.multiResolveTopPriority(element, myResolveContext)) {
                    if (resolved != null) {
                        if (isInitOrNewMethod(resolved)) {
                            resolved = ((PyFunction) resolved).getContainingClass();
                        }
                        if (isFromEnclosingScope(resolved)) {
                            result.readsFromEnclosingScope.add(element);
                        } else if (!belongsToFunction(resolved)) {
                            myExternalReads.add(resolved);
                        }
                        if (resolved instanceof PyParameter && ((PyParameter) resolved).isSelf()) {
                            if (PsiTreeUtil.getParentOfType(resolved, PyFunction.class) == myFunction) {
                                result.readsOfSelfParameter.add(element);
                            } else if (!PsiTreeUtil.isAncestor(myFunction, resolved, true)) {
                                result.readsOfSelfParametersFromEnclosingScope.add(element);
                            }
                        }
                    }
                }
            }
            if (readWriteInstruction.getAccess().isWriteAccess() && element instanceof PyTargetExpression) {
                for (PsiElement resolved : PyUtil.multiResolveTopPriority(element, myResolveContext)) {
                    if (resolved != null) {
                        if (element.getParent() instanceof PyNonlocalStatement && isFromEnclosingScope(resolved)) {
                            result.nonlocalWritesToEnclosingScope.add((PyTargetExpression) element);
                        }
                        if (resolved instanceof PyParameter && ((PyParameter) resolved).isSelf() && PsiTreeUtil.getParentOfType(resolved, PyFunction.class) == myFunction) {
                            result.writesToSelfParameter.add((PyTargetExpression) element);
                        }
                    }
                }
            }
        }
    }
    return result;
}
Also used : ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) Instruction(com.intellij.codeInsight.controlflow.Instruction) ControlFlow(com.intellij.codeInsight.controlflow.ControlFlow) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ControlFlow (com.intellij.codeInsight.controlflow.ControlFlow)14 Instruction (com.intellij.codeInsight.controlflow.Instruction)7 ReadWriteInstruction (com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction)5 NotNull (org.jetbrains.annotations.NotNull)5 PsiElement (com.intellij.psi.PsiElement)4 PyClass (com.jetbrains.python.psi.PyClass)2 CannotCreateCodeFragmentException (com.intellij.codeInsight.codeFragment.CannotCreateCodeFragmentException)1 StubBasedPsiElement (com.intellij.psi.StubBasedPsiElement)1 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)1 ArrayList (java.util.ArrayList)1